import { getDbClient } from "./client.server.js"; import type { EntityRelationship, Prisma, RowRelationship } from "./prisma-browser.js"; export type LegacyRowRelationshipEntity = { id: string; }; export type LegacyRowRelationshipDetails = Pick & { relationship: Pick; }; export type LegacyEntityRelationshipDetails = Pick; export type LegacyRowRelationshipCreateInput = { relationshipId: string; parentId: string; childId: string; tenantId?: string | null; metadata?: string | null; }; export type LegacyRowRelationshipDeleteByIdInput = Pick & { tenantId: string | null; }; export type LegacyRelationshipClient = { rowRelationship: { findUnique(args: { where: { id: string }; include: typeof legacyRowRelationshipInclude; }): Promise; findFirst(args: { where: Prisma.RowRelationshipWhereInput; include: typeof legacyRowRelationshipInclude; }): Promise; create(args: { data: LegacyRowRelationshipCreateInput; include: typeof legacyRowRelationshipInclude; }): Promise; deleteMany(args: { where: Prisma.RowRelationshipWhereInput }): Promise; }; entityRelationship: { findFirst(args: { where: Prisma.EntityRelationshipWhereInput; select: typeof selectLegacyEntityRelationshipProperties; }): Promise; }; row: { findFirst(args: { where: Prisma.RowWhereInput; select: { id: true }; }): Promise; }; }; export const legacyRowRelationshipInclude = { relationship: { select: { id: true, parentId: true, childId: true } }, } as const; const selectLegacyEntityRelationshipProperties = { id: true, parentId: true, childId: true, } as const; export async function findLegacyRowRelationshipById( id: string, tenantIdOrClient?: string | null | LegacyRelationshipClient, client?: LegacyRelationshipClient, ): Promise { const tenantId = typeof tenantIdOrClient === "string" || tenantIdOrClient === null ? tenantIdOrClient : undefined; const db = tenantIdOrClient && typeof tenantIdOrClient === "object" ? tenantIdOrClient : client ?? (getDbClient() as unknown as LegacyRelationshipClient); if (tenantId !== undefined) { return db.rowRelationship.findFirst({ where: { id, parent: { tenantId, deletedAt: null }, child: { tenantId, deletedAt: null }, }, include: legacyRowRelationshipInclude, }); } return db.rowRelationship.findUnique({ where: { id }, include: legacyRowRelationshipInclude, }); } export async function createLegacyRowRelationship( data: LegacyRowRelationshipCreateInput, client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient, ): Promise { if (data.tenantId !== undefined) { const entityRelationship = await client.entityRelationship.findFirst({ where: { id: data.relationshipId }, select: selectLegacyEntityRelationshipProperties, }); if (!entityRelationship) { throw new Error("Entity relationship not found."); } const [parent, child] = await Promise.all([ client.row.findFirst({ where: { id: data.parentId, tenantId: data.tenantId, entityId: entityRelationship.parentId, deletedAt: null }, select: { id: true }, }), client.row.findFirst({ where: { id: data.childId, tenantId: data.tenantId, entityId: entityRelationship.childId, deletedAt: null }, select: { id: true }, }), ]); if (!parent || !child) { throw new Error("Relationship rows not found."); } } const createData = { relationshipId: data.relationshipId, parentId: data.parentId, childId: data.childId }; return client.rowRelationship.create({ data: { ...createData, metadata: data.metadata ?? null }, include: legacyRowRelationshipInclude, }); } export async function deleteLegacyRowRelationshipById( { id, relationshipId, parentId, childId, tenantId }: LegacyRowRelationshipDeleteByIdInput, client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient, ): Promise { const deleted = await client.rowRelationship.deleteMany({ where: { id, relationshipId, parentId, childId, parent: { tenantId, deletedAt: null }, child: { tenantId, deletedAt: null }, }, }); if (deleted.count !== 1) { throw new Error(`Relationship ${id} not found`); } return deleted; } export async function deleteLegacyRowRelationship( { parentId, childId, relationshipId, tenantId }: { parentId: string; childId: string; relationshipId?: string; tenantId?: string | null }, client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient, ): Promise { if (tenantId === undefined) { throw new Error("Tenant id is required for relationship deletes."); } if (!relationshipId) { throw new Error("Relationship id is required for scoped relationship deletes."); } const relationship = await client.rowRelationship.findFirst({ where: { relationshipId, parentId, childId, parent: { tenantId, deletedAt: null }, child: { tenantId, deletedAt: null }, }, include: legacyRowRelationshipInclude, }); if (!relationship) { throw new Error("Relationship rows not found."); } return deleteLegacyRowRelationshipById({ ...relationship, tenantId }, client); } export async function findLegacyEntityRelationship( { parentId, childId }: { parentId: string; childId: string }, client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient, ): Promise { return client.entityRelationship.findFirst({ where: { parentId, childId }, select: selectLegacyEntityRelationshipProperties, }); }