diff --git a/src/backend/db/migrations/2_add-uniqueness-constraint-on-tasks.sql b/src/backend/db/migrations/2_add-uniqueness-constraint-on-tasks.sql index baec14d4..5d5abe35 100644 --- a/src/backend/db/migrations/2_add-uniqueness-constraint-on-tasks.sql +++ b/src/backend/db/migrations/2_add-uniqueness-constraint-on-tasks.sql @@ -1,7 +1,7 @@ -- Each task should have a unique subgoal_id - assignee_id combination -- which corresponds to a unique benchmark / para combo ALTER TABLE task -ADD CONSTRAINT UC_Task UNIQUE (subgoal_id, assignee_id); +ADD CONSTRAINT subgoal_assignee_unique UNIQUE (subgoal_id, assignee_id); -- Add index to allow easy queries of tasks by assignee CREATE INDEX idx_task_assignee ON task(assignee_id); \ No newline at end of file diff --git a/src/backend/db/zapatos/schema.d.ts b/src/backend/db/zapatos/schema.d.ts index 1df65881..a5b4f275 100644 --- a/src/backend/db/zapatos/schema.d.ts +++ b/src/backend/db/zapatos/schema.d.ts @@ -2165,7 +2165,7 @@ declare module 'zapatos/schema' { */ seen?: boolean | db.Parameter | db.DefaultType | db.SQLFragment | db.SQLFragment | db.DefaultType | db.SQLFragment>; } - export type UniqueIndex = 'task_pkey' | 'uc_task'; + export type UniqueIndex = 'subgoal_assignee_unique' | 'task_pkey'; export type Column = keyof Selectable; export type OnlyCols = Pick; export type SQLExpression = Table | db.ColumnNames | db.ColumnValues | Whereable | Column | db.ParentColumn | db.GenericSQLExpression; diff --git a/src/backend/routers/iep.test.ts b/src/backend/routers/iep.test.ts index 7d76fcc1..78a6a661 100644 --- a/src/backend/routers/iep.test.ts +++ b/src/backend/routers/iep.test.ts @@ -155,7 +155,7 @@ test("addTask - no duplicate subgoal_id + assigned_id combo", async (t) => { t.is( error?.message, - 'duplicate key value violates unique constraint "uc_task"' + "Task already exists: This subgoal has already been assigned to the same para" ); }); diff --git a/src/backend/routers/iep.ts b/src/backend/routers/iep.ts index 0a7ae50a..f36fd49c 100644 --- a/src/backend/routers/iep.ts +++ b/src/backend/routers/iep.ts @@ -135,6 +135,20 @@ export const iep = router({ .mutation(async (req) => { const { subgoal_id, assignee_id, due_date, trial_count } = req.input; + // make sure that this goal belongs to this case manager + const existingTask = await req.ctx.db + .selectFrom("task") + .where("subgoal_id", "=", subgoal_id) + .where("assignee_id", "=", assignee_id) + .selectAll() + .executeTakeFirst(); + + if (existingTask) { + throw new Error( + "Task already exists: This subgoal has already been assigned to the same para" + ); + } + const result = await req.ctx.db .insertInto("task") .values({