mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +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);
|
||||
|
||||
@@ -130,6 +130,39 @@ describe('MailIngestCandidatesRepository: the entries', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('decides an added entry only while it still holds the trip and ids it was read with', async () => {
|
||||
const { user } = await makeUser(t);
|
||||
const trip = await makeTrip(t, user.id);
|
||||
const other = await makeTrip(t, user.id);
|
||||
const mail = await mailOf(user.id);
|
||||
const id = await entry(mail, user.id);
|
||||
const added = {
|
||||
resolution: 'applied',
|
||||
reason: null,
|
||||
resolvedTripId: trip.id,
|
||||
createdIdsJson: '{"reservation_ids":[1]}',
|
||||
resolvedBy: user.id,
|
||||
resolvedAt: DAY1,
|
||||
undoUntil: at('2026-11-08T10:00:00Z'),
|
||||
};
|
||||
expect(await candidates.resolve(id, ['pending'], added)).toBe(true);
|
||||
const undone = { ...added, resolution: 'undone', undoUntil: null };
|
||||
|
||||
expect(
|
||||
await candidates.resolve(id, ['applied'], undone, { tripId: other.id, createdIdsJson: added.createdIdsJson }),
|
||||
).toBe(false);
|
||||
expect(await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: '{"x":1}' })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: null })).toBe(false);
|
||||
expect(await candidates.findById(id)).toMatchObject({ resolution: 'applied' });
|
||||
|
||||
expect(
|
||||
await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: added.createdIdsJson }),
|
||||
).toBe(true);
|
||||
expect(await candidates.findById(id)).toMatchObject({ resolution: 'undone', resolved_trip_id: trip.id });
|
||||
});
|
||||
|
||||
it('writes an entry decided as it is written with its trip, ids and undo window', async () => {
|
||||
const { user } = await makeUser(t);
|
||||
const trip = await makeTrip(t, user.id);
|
||||
|
||||
@@ -17,6 +17,7 @@ import { MailReadService } from '../../../../src/nest/llm-parse/mail-read.servic
|
||||
import type { MailReading as AiReading } from '../../../../src/nest/llm-parse/mail-read.types';
|
||||
import { walkMail } from '../../../../src/nest/llm-parse/mime-walker';
|
||||
import { MailIngestActionsService } from '../../../../src/nest/mail-ingest/mail-ingest-actions.service';
|
||||
import { MailIngestApplyService } from '../../../../src/nest/mail-ingest/mail-ingest-apply.service';
|
||||
import { MailIngestInboxService } from '../../../../src/nest/mail-ingest/mail-ingest-inbox.service';
|
||||
import { MailIngestIntakeService } from '../../../../src/nest/mail-ingest/mail-ingest-intake.service';
|
||||
import { MailIngestProcessJob } from '../../../../src/nest/mail-ingest/mail-ingest-process.job';
|
||||
@@ -289,6 +290,33 @@ describe('a booking that passes every check', () => {
|
||||
expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(0);
|
||||
});
|
||||
|
||||
it('is not undone when it moves while the undo waits: the booking stays where it went, with its entry', async () => {
|
||||
const user = await person();
|
||||
const first = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' });
|
||||
const messageId = await forward(user.id, eml('flight'));
|
||||
const [entry] = await entriesOf(messageId);
|
||||
const second = await makeTrip(orm(), user.id, { start_date: '2030-02-25', end_date: '2030-03-05' });
|
||||
// The move lands between the undo reading the entry and claiming it, while the undo checks for edits.
|
||||
const edited = vi.spyOn(t.get(MailIngestApplyService), 'editedSince').mockImplementationOnce(async () => {
|
||||
await actions().move(entry.id, user.id, second.id, true, NOW);
|
||||
return false;
|
||||
});
|
||||
try {
|
||||
await expect(actions().undo(entry.id, user.id, false, NOW)).rejects.toThrow(
|
||||
'This booking was moved or undone already',
|
||||
);
|
||||
} finally {
|
||||
edited.mockRestore();
|
||||
}
|
||||
expect(await countRows(orm(), Reservations, { trip: first.id })).toBe(0);
|
||||
expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(1);
|
||||
const [after] = await entriesOf(messageId);
|
||||
expect(after).toMatchObject({ resolution: 'auto_applied', resolved_trip_id: second.id });
|
||||
|
||||
await actions().undo(entry.id, user.id, false, NOW);
|
||||
expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(0);
|
||||
});
|
||||
|
||||
it('asks before moving a booking that was edited since, and refuses a stranger', async () => {
|
||||
const user = await person();
|
||||
const { user: stranger } = await makeUser(orm());
|
||||
|
||||
Reference in New Issue
Block a user