1
10import { resolveRecordId, writeRecordPropertyById } from "./record-write.js";
11import { resolveUserId } from "./helpers.js";
12import { FatalError } from "./classify.js";
13import type { ActionExecutor, ActionResult } from "./types.js";
14
15const executor: ActionExecutor = {
16 type: "ASSIGN_USER",
17 async execute(cfg, ctx, deps): Promise<ActionResult> {
18 const recordId = resolveRecordId(cfg, ctx);
19 const propertyId = cfg.propertyId as string;
20 const userId = resolveUserId(cfg.userId as string, ctx);
21
22
23 const prop = await deps.prisma.databaseProperty.findFirst({
24 where: { id: propertyId, deletedAt: null },
25 select: { id: true, type: true }
26 });
27 if (!prop) throw new FatalError("PROPERTY_UNKNOWN", `property ${propertyId} not found`);
28 if (prop.type !== "PERSON") {
29 throw new FatalError("PROPERTY_TYPE_MISMATCH", `property ${propertyId} is ${prop.type}, not PERSON`);
30 }
31
32 const result = await writeRecordPropertyById(deps, ctx, recordId, propertyId, [userId]);
33 return {
34 output: { recordId: result.recordId, propertyId, userId, eventEmitted: result.eventEmitted },
35 emittedEvents: result.eventEmitted ? ["record.updated"] : [],
36 audit: { action: "automation.user_assigned", resourceType: "record", resourceId: result.recordId, metadata: { userId, propertyId } }
37 };
38 }
39};
40
41export default executor;
42