fix(yachts): run owner existence check inside transaction
This commit is contained in:
@@ -20,14 +20,15 @@ interface AuditMeta {
|
|||||||
async function assertOwnerExists(
|
async function assertOwnerExists(
|
||||||
portId: string,
|
portId: string,
|
||||||
owner: { type: 'client' | 'company'; id: string },
|
owner: { type: 'client' | 'company'; id: string },
|
||||||
|
tx: typeof db,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (owner.type === 'client') {
|
if (owner.type === 'client') {
|
||||||
const client = await db.query.clients.findFirst({
|
const client = await tx.query.clients.findFirst({
|
||||||
where: and(eq(clients.id, owner.id), eq(clients.portId, portId)),
|
where: and(eq(clients.id, owner.id), eq(clients.portId, portId)),
|
||||||
});
|
});
|
||||||
if (!client) throw new ValidationError('owner not found');
|
if (!client) throw new ValidationError('owner not found');
|
||||||
} else {
|
} else {
|
||||||
const company = await db.query.companies.findFirst({
|
const company = await tx.query.companies.findFirst({
|
||||||
where: and(eq(companies.id, owner.id), eq(companies.portId, portId)),
|
where: and(eq(companies.id, owner.id), eq(companies.portId, portId)),
|
||||||
});
|
});
|
||||||
if (!company) throw new ValidationError('owner not found');
|
if (!company) throw new ValidationError('owner not found');
|
||||||
@@ -36,7 +37,7 @@ async function assertOwnerExists(
|
|||||||
|
|
||||||
export async function createYacht(portId: string, data: CreateYachtInput, meta: AuditMeta) {
|
export async function createYacht(portId: string, data: CreateYachtInput, meta: AuditMeta) {
|
||||||
return await db.transaction(async (tx) => {
|
return await db.transaction(async (tx) => {
|
||||||
await assertOwnerExists(portId, data.owner);
|
await assertOwnerExists(portId, data.owner, tx as unknown as typeof db);
|
||||||
|
|
||||||
const [yacht] = await tx
|
const [yacht] = await tx
|
||||||
.insert(yachts)
|
.insert(yachts)
|
||||||
|
|||||||
Reference in New Issue
Block a user