Optionally set staff channel associations when creating or updating a channel.

This commit is contained in:
Hendrik Belitz
2026-01-28 17:23:46 +01:00
parent 16eed3d03e
commit c5d8d174aa
4 changed files with 50 additions and 3 deletions
@@ -175,6 +175,7 @@ describe("ChannelService", () => {
duration: 30,
},
],
staffIds: [],
};
const result = await service.createChannel(request);
@@ -188,6 +189,7 @@ describe("ChannelService", () => {
names: { en: "" },
agentIds: [],
slotTemplates: [],
staffIds: [],
};
await expect(service.createChannel(request)).rejects.toThrow(ValidationError);
@@ -205,6 +207,7 @@ describe("ChannelService", () => {
duration: 30,
},
],
staffIds: [],
};
await expect(service.createChannel(request)).rejects.toThrow(ValidationError);
@@ -217,6 +220,7 @@ describe("ChannelService", () => {
names: { en: "Test Channel" },
agentIds: [],
slotTemplates: [],
staffIds: [],
};
await expect(service.createChannel(request)).rejects.toThrow("Transaction failed");
+33 -3
View File
@@ -36,7 +36,8 @@ const channelCreationSchema = z.object({
descriptions: z.partialRecord(z.enum(supportedLocales), z.string().min(1)).optional(),
isPublic: z.boolean().optional(),
requiresConfirmation: z.boolean().optional(),
agentIds: z.array(z.string().uuid()).optional().default([]),
agentIds: z.array(z.uuid()).optional().default([]),
staffIds: z.array(z.uuid()).optional().default([]),
slotTemplates: z.array(slotTemplateSchema).optional().default([]),
});
@@ -47,7 +48,8 @@ const channelUpdateSchema = z.object({
isPublic: z.boolean().optional(),
pause: z.boolean().optional(),
requiresConfirmation: z.boolean().optional(),
agentIds: z.array(z.string().uuid()).optional(),
agentIds: z.array(z.uuid()).optional(),
staffIds: z.array(z.uuid()).optional(),
slotTemplates: z
.array(
slotTemplateSchema.extend({
@@ -198,6 +200,16 @@ export class ChannelService {
agents.push(...existingAgents);
}
// 4. Link staff to the channel
if (request.staffIds && request.staffIds.length > 0) {
for (const staffId of request.staffIds) {
await tx.insert(tenantSchema.channelStaff).values({
channelId: channel.id,
staffId: staffId,
});
}
}
return {
...channel,
agents,
@@ -355,7 +367,25 @@ export class ChannelService {
);
}
// 3. Handle slot template relationships
// 3. Handle staff relationships
if (updateData.staffIds !== undefined) {
// Remove all existing staff assignments
await tx
.delete(tenantSchema.channelStaff)
.where(eq(tenantSchema.channelStaff.channelId, channelId));
// Add new staff assignments
if (updateData.staffIds.length > 0) {
for (const staffId of updateData.staffIds) {
await tx.insert(tenantSchema.channelStaff).values({
channelId: channelId,
staffId: staffId,
});
}
}
}
// 4. Handle slot template relationships
let slotTemplates: SelectSlotTemplate[] = [];
if (updateData.slotTemplates !== undefined) {
// Get existing slot template IDs for this channel
@@ -62,6 +62,12 @@ registerOpenAPIRoute("/tenants/{id}/channels", "POST", {
description: "IDs of agents to assign to this channel",
example: ["01234567-89ab-cdef-0123-456789abcdef"],
},
staffIds: {
type: "array",
items: { type: "string", format: "uuid" },
description: "IDs of staff members to assign to this channel",
example: ["12345678-90ab-cdef-0123-456789abcdef"],
},
slotTemplates: {
type: "array",
items: {
@@ -312,6 +318,7 @@ export const POST: RequestHandler = async ({ params, request, locals }) => {
requestedBy: locals.user?.id,
channelNames: body.names,
languages: body.languages,
staffIds: body.staffIds,
});
const channelService = await ChannelService.forTenant(tenantId);
@@ -188,6 +188,12 @@ registerOpenAPIRoute("/tenants/{id}/channels/{channelId}", "PUT", {
description: "IDs of agents to assign to this channel",
example: ["01234567-89ab-cdef-0123-456789abcdef"],
},
staffIds: {
type: "array",
items: { type: "string", format: "uuid" },
description: "IDs of staff members to assign to this channel",
example: ["12345678-90ab-cdef-0123-456789abcdef"],
},
slotTemplates: {
type: "array",
items: {