Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c894931
feat(api): confirm a draft session and publish its share code
aek676 Aug 12, 2026
5bf7d6a
test(api): pin what the totalCents accumulator does today
aek676 Aug 12, 2026
c739bbe
refactor(web): drop the client-side receipt total check
aek676 Aug 12, 2026
f2c3a08
fix(api): keep the receipt total fixed and check it on confirm
aek676 Aug 12, 2026
9831293
chore(api): sort the schema barrel alphabetically
aek676 Aug 12, 2026
9be32a5
refactor(api): type the session view's status from its schema enum
aek676 Aug 12, 2026
3fce47e
feat(api): let the receipt total follow the line items
aek676 Aug 12, 2026
e721fbf
feat(web): add a bare currency formatter
aek676 Aug 12, 2026
e4f5d0a
feat(web): let the client update the session's receipt total
aek676 Aug 12, 2026
1dca840
feat(web): surface receipt/items total mismatches in the review screen
aek676 Aug 12, 2026
db73072
fix(api): scope the integration test run to the test directory
aek676 Aug 12, 2026
c96d19d
test(api): await the write-failure rejection assertion
aek676 Aug 12, 2026
6db5a7a
test(api): pin that only code collisions are retried on confirm
aek676 Aug 12, 2026
e109101
test(api): cover session code collisions against a real unique index
aek676 Aug 12, 2026
3261df4
test(api): type the getRandomValues mock generically
aek676 Aug 12, 2026
a47fbee
feat(web): confirm a draft session from the review screen
aek676 Aug 12, 2026
a280e34
feat(web): redirect a confirmed session to its share link
aek676 Aug 12, 2026
6bc383e
style(web): add the missing trailing newline to the confirm button
aek676 Aug 12, 2026
05119bf
refactor: share the low-confidence threshold between the api and the web
aek676 Aug 12, 2026
d98f959
fix(api): publish a session with a conditional update
aek676 Aug 12, 2026
5205959
fix(web): refetch the session when a confirm is refused
aek676 Aug 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cache": false,
"options": {
"cwd": "apps/api",
"command": "bun test --preload ./test/setup.ts --coverage-dir coverage/integration test"
"command": "bun test --preload ./test/setup.ts --coverage-dir coverage/integration test/"
}
},
"build": {
Expand Down
9 changes: 9 additions & 0 deletions apps/api/src/common/mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function isDuplicateKeyError(error: unknown, key?: string): boolean {
if (typeof error !== 'object' || error === null) return false;
const { code, keyPattern } = error as {
code?: number;
keyPattern?: Record<string, unknown>;
};
if (code !== 11000) return false;
return key === undefined || keyPattern === undefined || key in keyPattern;
}
175 changes: 174 additions & 1 deletion apps/api/src/modules/session/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,87 @@ describe('GET /sessions/:sessionId', () => {
});
});

describe('PATCH /sessions/:sessionId', () => {
beforeEach(() => {
spyOn(console, 'error').mockImplementation(() => {});
spyOn(Session.prototype, 'save').mockImplementation(async function (
this: unknown,
) {
return this;
});
});

afterEach(() => {
mock.restore();
});

it('corrects the receipt total and returns 200 with the updated view', async () => {
const session = draftSession();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}`, {
method: 'PATCH',
body: { totalCents: 1600 },
}),
);

expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({ totalCents: 1600 });
});

it('returns 422 for a negative total', async () => {
const session = draftSession();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}`, {
method: 'PATCH',
body: { totalCents: -1 },
}),
);

expect(res.status).toBe(422);
expect(session.totalCents).toBe(600);
});

it('returns 403 for a guest token and keeps the total', async () => {
const session = sessionWithGuest();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}`, {
method: 'PATCH',
body: { totalCents: 1600 },
token: GUEST_TOKEN,
}),
);

expect(res.status).toBe(403);
expect(session.totalCents).toBe(600);
});

it('returns 409 once the session is published', async () => {
const session = draftSession();
session.status = 'open';
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}`, {
method: 'PATCH',
body: { totalCents: 1600 },
}),
);

expect(res.status).toBe(409);
expect(await res.text()).toBe('Session is not editable');
});
});

describe('DELETE /sessions/:sessionId', () => {
beforeEach(() => {
spyOn(console, 'error').mockImplementation(() => {});
Expand Down Expand Up @@ -496,7 +577,7 @@ describe('DELETE /sessions/:sessionId/line-items/:lineItemId', () => {
expect(res.status).toBe(200);
const body = await res.json();
expect(body.lineItems).toHaveLength(0);
expect(body.totalCents).toBe(0);
expect(body.totalCents).toBe(600);
});

it('returns 404 when the line item is missing', async () => {
Expand Down Expand Up @@ -531,3 +612,95 @@ describe('DELETE /sessions/:sessionId/line-items/:lineItemId', () => {
expect(session.lineItems).toHaveLength(1);
});
});

describe('POST /sessions/:sessionId/confirm', () => {
beforeEach(() => {
spyOn(console, 'error').mockImplementation(() => {});
spyOn(Session.prototype, 'save').mockImplementation(async function (
this: unknown,
) {
return this;
});
});

afterEach(() => {
mock.restore();
});

it('returns 200 with the published session and its code', async () => {
const session = draftSession();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}/confirm`, { method: 'POST' }),
);

expect(res.status).toBe(200);
const body = await res.json();
expect(body.status).toBe('open');
expect(body.code).toMatch(/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{8}$/);
});

it('returns 403 for a guest token and leaves the session in draft', async () => {
const session = sessionWithGuest();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}/confirm`, {
method: 'POST',
token: GUEST_TOKEN,
}),
);

expect(res.status).toBe(403);
expect(session.status).toBe('draft');
expect(session.code).toBeUndefined();
});

it('returns 401 without a token', async () => {
const session = draftSession();
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}/confirm`, {
method: 'POST',
token: null,
}),
);

expect(res.status).toBe(401);
expect(session.status).toBe('draft');
});

it('returns 409 when the items fall short of the receipt total', async () => {
const session = draftSession();
session.totalCents = 1600;
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}/confirm`, { method: 'POST' }),
);

expect(res.status).toBe(409);
expect(await res.text()).toBe('Items do not add up to the receipt total');
expect(session.status).toBe('draft');
});

it('returns 409 when the session is already published', async () => {
const session = draftSession();
session.status = 'open';
mockLookup(session);
const app = moduleWith(mock<ExtractReceipt>(async () => extracted));

const res = await app.handle(
request(`/sessions/${session._id}/confirm`, { method: 'POST' }),
);

expect(res.status).toBe(409);
expect(await res.text()).toBe('Session is not editable');
});
});
Loading