fix: batch contract database writes
This commit is contained in:
@@ -213,8 +213,8 @@ 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,
|
||||||
@@ -224,21 +224,26 @@ export const actions: Actions = {
|
|||||||
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 {
|
||||||
|
await createContract;
|
||||||
}
|
}
|
||||||
});
|
} catch (cause) {
|
||||||
} catch {
|
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,8 +300,8 @@ 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,
|
||||||
@@ -305,7 +310,7 @@ export const actions: Actions = {
|
|||||||
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(
|
||||||
@@ -315,19 +320,27 @@ export const actions: Actions = {
|
|||||||
isNull(contracts.archivedAt)
|
isNull(contracts.archivedAt)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
const deleteRoomLinks = db
|
||||||
await tx.delete(contractRooms).where(eq(contractRooms.contractId, form.data.id));
|
.delete(contractRooms)
|
||||||
|
.where(eq(contractRooms.contractId, form.data.id));
|
||||||
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([
|
||||||
|
updateContract,
|
||||||
|
deleteRoomLinks,
|
||||||
|
db.insert(contractRooms).values(
|
||||||
uniqueRoomIds.map((roomId) => ({
|
uniqueRoomIds.map((roomId) => ({
|
||||||
contractId: form.data.id,
|
contractId: form.data.id,
|
||||||
roomId
|
roomId
|
||||||
}))
|
}))
|
||||||
);
|
)
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
await db.batch([updateContract, deleteRoomLinks]);
|
||||||
}
|
}
|
||||||
});
|
} catch (cause) {
|
||||||
} catch {
|
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