import { ok, err } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; import { AssignmentNotFoundError } from "../lib/errors.generated"; export interface RemoveItemFromTaxonomyInput { itemId: string; taxonomyNodeId: string; } /** * Function: removeItemFromTaxonomy * * Removes the link between an item and a taxonomy node. */ export async function run(db: Transaction, input: RemoveItemFromTaxonomyInput) { const { itemId, taxonomyNodeId } = input; // 1. Check assignment exists (read-then-delete — lock) const assignment = await db .selectFrom("ItemTaxonomyAssignment") .selectAll() .where("itemId", "=", itemId) .where("taxonomyNodeId", "=", taxonomyNodeId) .forUpdate() .executeTakeFirst(); if (!assignment) { return err(new AssignmentNotFoundError(`${itemId} in ${taxonomyNodeId}`)); } // 2. Delete assignment await db.deleteFrom("ItemTaxonomyAssignment").where("id", "=", assignment.id).execute(); return ok({ success: true as const }); }