fix: batch contract database writes
This commit is contained in:
@@ -213,32 +213,37 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const contractId = crypto.randomUUID();
|
const contractId = crypto.randomUUID();
|
||||||
await db.transaction(async (tx) => {
|
const now = new Date();
|
||||||
await tx.insert(contracts).values({
|
const createContract = db.insert(contracts).values({
|
||||||
id: contractId,
|
id: contractId,
|
||||||
organizationId: activeOrganizationId,
|
organizationId: activeOrganizationId,
|
||||||
clientId: form.data.clientId,
|
clientId: form.data.clientId,
|
||||||
serviceId: form.data.serviceId || null,
|
serviceId: form.data.serviceId || null,
|
||||||
licenseFeeGbp: form.data.licenseFeeGbp,
|
licenseFeeGbp: form.data.licenseFeeGbp,
|
||||||
depositGbp: form.data.depositGbp,
|
depositGbp: form.data.depositGbp,
|
||||||
startDate: form.data.startDate,
|
startDate: form.data.startDate,
|
||||||
endDate: form.data.endDate,
|
endDate: form.data.endDate,
|
||||||
status: 'draft',
|
status: 'draft',
|
||||||
updatedAt: new Date(),
|
updatedAt: now,
|
||||||
createdAt: new Date()
|
createdAt: now
|
||||||
});
|
});
|
||||||
|
|
||||||
const uniqueRoomIds = [...new Set(form.data.roomIds)];
|
const uniqueRoomIds = [...new Set(form.data.roomIds)];
|
||||||
if (uniqueRoomIds.length > 0) {
|
if (uniqueRoomIds.length > 0) {
|
||||||
await tx.insert(contractRooms).values(
|
await db.batch([
|
||||||
|
createContract,
|
||||||
|
db.insert(contractRooms).values(
|
||||||
uniqueRoomIds.map((roomId) => ({
|
uniqueRoomIds.map((roomId) => ({
|
||||||
contractId,
|
contractId,
|
||||||
roomId
|
roomId
|
||||||
}))
|
}))
|
||||||
);
|
)
|
||||||
}
|
]);
|
||||||
});
|
} else {
|
||||||
} catch {
|
await createContract;
|
||||||
|
}
|
||||||
|
} catch (cause) {
|
||||||
|
console.error('Unable to create contract', cause);
|
||||||
return message(form, 'Unable to create contract.', { status: 400 });
|
return message(form, 'Unable to create contract.', { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,39 +300,47 @@ export const actions: Actions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.transaction(async (tx) => {
|
const now = new Date();
|
||||||
await tx
|
const updateContract = db
|
||||||
.update(contracts)
|
.update(contracts)
|
||||||
.set({
|
.set({
|
||||||
clientId: form.data.clientId,
|
clientId: form.data.clientId,
|
||||||
serviceId: form.data.serviceId || null,
|
serviceId: form.data.serviceId || null,
|
||||||
licenseFeeGbp: form.data.licenseFeeGbp,
|
licenseFeeGbp: form.data.licenseFeeGbp,
|
||||||
depositGbp: form.data.depositGbp,
|
depositGbp: form.data.depositGbp,
|
||||||
startDate: form.data.startDate,
|
startDate: form.data.startDate,
|
||||||
endDate: form.data.endDate,
|
endDate: form.data.endDate,
|
||||||
updatedAt: new Date()
|
updatedAt: now
|
||||||
})
|
})
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(contracts.id, form.data.id),
|
eq(contracts.id, form.data.id),
|
||||||
eq(contracts.clientId, params.id),
|
eq(contracts.clientId, params.id),
|
||||||
eq(contracts.organizationId, activeOrganizationId),
|
eq(contracts.organizationId, activeOrganizationId),
|
||||||
isNull(contracts.archivedAt)
|
isNull(contracts.archivedAt)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
const deleteRoomLinks = db
|
||||||
|
.delete(contractRooms)
|
||||||
|
.where(eq(contractRooms.contractId, form.data.id));
|
||||||
|
const uniqueRoomIds = [...new Set(form.data.roomIds)];
|
||||||
|
|
||||||
await tx.delete(contractRooms).where(eq(contractRooms.contractId, form.data.id));
|
if (uniqueRoomIds.length > 0) {
|
||||||
const uniqueRoomIds = [...new Set(form.data.roomIds)];
|
await db.batch([
|
||||||
if (uniqueRoomIds.length > 0) {
|
updateContract,
|
||||||
await tx.insert(contractRooms).values(
|
deleteRoomLinks,
|
||||||
|
db.insert(contractRooms).values(
|
||||||
uniqueRoomIds.map((roomId) => ({
|
uniqueRoomIds.map((roomId) => ({
|
||||||
contractId: form.data.id,
|
contractId: form.data.id,
|
||||||
roomId
|
roomId
|
||||||
}))
|
}))
|
||||||
);
|
)
|
||||||
}
|
]);
|
||||||
});
|
} else {
|
||||||
} catch {
|
await db.batch([updateContract, deleteRoomLinks]);
|
||||||
|
}
|
||||||
|
} catch (cause) {
|
||||||
|
console.error('Unable to update contract', cause);
|
||||||
return message(form, 'Unable to update contract.', { status: 400 });
|
return message(form, 'Unable to update contract.', { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user