mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
fix(mail-ingest): refuse an undo when the booking was moved in between
A move keeps the entry's resolution and changes only its trip and created ids, so an undo that read them before a move landed still claimed the entry, removed nothing and left the moved booking behind. The claim is now bound to the trip and ids it was read with.
This commit is contained in:
@@ -193,9 +193,16 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
|
||||
/**
|
||||
* Decides an entry, if it is still in one of the `from` states: one
|
||||
* conditional update, so of two decisions at once only the first lands.
|
||||
* With `held`, only while the entry still holds that trip and those
|
||||
* created ids: a move keeps the resolution and changes only these.
|
||||
*/
|
||||
async resolve(id: number, from: readonly string[], patch: MailIngestResolutionPatch): Promise<boolean> {
|
||||
const result = await this.db()
|
||||
async resolve(
|
||||
id: number,
|
||||
from: readonly string[],
|
||||
patch: MailIngestResolutionPatch,
|
||||
held?: { tripId: number; createdIdsJson: string | null },
|
||||
): Promise<boolean> {
|
||||
let query = this.db()
|
||||
.updateTable('mail_ingest_candidates')
|
||||
.set({
|
||||
resolution: patch.resolution,
|
||||
@@ -207,8 +214,15 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
|
||||
undo_until: patch.undoUntil,
|
||||
})
|
||||
.where('id', '=', id)
|
||||
.where('resolution', 'in', [...from])
|
||||
.executeTakeFirst();
|
||||
.where('resolution', 'in', [...from]);
|
||||
if (held) {
|
||||
query = query.where('resolved_trip_id', '=', held.tripId);
|
||||
query =
|
||||
held.createdIdsJson === null
|
||||
? query.where('created_ids_json', 'is', null)
|
||||
: query.where('created_ids_json', '=', held.createdIdsJson);
|
||||
}
|
||||
const result = await query.executeTakeFirst();
|
||||
return Number(result.numUpdatedRows) > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,16 +88,23 @@ export class MailIngestActionsService {
|
||||
const { entry, tripId, created } = await this.addedEntry(candidateId, userId, now);
|
||||
await this.requireConfirmed(tripId, created, confirm, 'Undoing this booking changes more than the booking');
|
||||
const removed = await this.uow.transactional(async () => {
|
||||
const claimed = await this.candidates.resolve(entry.id, ADDED_RESOLUTIONS, {
|
||||
resolution: 'undone',
|
||||
reason: null,
|
||||
resolvedTripId: tripId,
|
||||
createdIdsJson: entry.created_ids_json,
|
||||
resolvedBy: userId,
|
||||
resolvedAt: dbNow(now),
|
||||
undoUntil: null,
|
||||
});
|
||||
if (!claimed) throw new DomainError(409, 'This booking was undone already');
|
||||
// A move keeps the resolution, so the claim also asks for the trip and ids read above: after a move
|
||||
// in between, removing those would miss the booking in the new trip and leave it behind.
|
||||
const claimed = await this.candidates.resolve(
|
||||
entry.id,
|
||||
ADDED_RESOLUTIONS,
|
||||
{
|
||||
resolution: 'undone',
|
||||
reason: null,
|
||||
resolvedTripId: tripId,
|
||||
createdIdsJson: entry.created_ids_json,
|
||||
resolvedBy: userId,
|
||||
resolvedAt: dbNow(now),
|
||||
undoUntil: null,
|
||||
},
|
||||
{ tripId, createdIdsJson: entry.created_ids_json },
|
||||
);
|
||||
if (!claimed) throw new DomainError(409, 'This booking was moved or undone already');
|
||||
return this.apply.removeInTx(tripId, created);
|
||||
});
|
||||
await this.apply.announceRemoved(removed, created.file_ids);
|
||||
|
||||
Reference in New Issue
Block a user