Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 11x | /**
* Message Router — list, get, respond
*/
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import type { ProcedureBuilder } from '../trpc.js';
import { requireMessageRepository } from './_helpers.js';
export function messageProcedures(publicProcedure: ProcedureBuilder) {
return {
listMessages: publicProcedure
.input(z.object({
agentId: z.string().optional(),
status: z.enum(['pending', 'read', 'responded']).optional(),
}))
.query(async ({ ctx, input }) => {
const messageRepository = requireMessageRepository(ctx);
let messages = await messageRepository.findByRecipient('user');
if (input.agentId) {
messages = messages.filter(m => m.senderId === input.agentId);
}
if (input.status) {
messages = messages.filter(m => m.status === input.status);
}
return messages;
}),
getMessage: publicProcedure
.input(z.object({ id: z.string().min(1) }))
.query(async ({ ctx, input }) => {
const messageRepository = requireMessageRepository(ctx);
const message = await messageRepository.findById(input.id);
if (!message) {
throw new TRPCError({
code: 'NOT_FOUND',
message: `Message '${input.id}' not found`,
});
}
return message;
}),
respondToMessage: publicProcedure
.input(z.object({
id: z.string().min(1),
response: z.string().min(1),
}))
.mutation(async ({ ctx, input }) => {
const messageRepository = requireMessageRepository(ctx);
const existing = await messageRepository.findById(input.id);
if (!existing) {
throw new TRPCError({
code: 'NOT_FOUND',
message: `Message '${input.id}' not found`,
});
}
const responseMessage = await messageRepository.create({
senderType: 'user',
recipientType: 'agent',
recipientId: existing.senderId,
type: 'response',
content: input.response,
parentMessageId: input.id,
});
await messageRepository.update(input.id, { status: 'responded' });
return responseMessage;
}),
};
}
|