refactor(server): packing templates, road trip, day and booking-import services refuse with DomainError

This commit is contained in:
Maurice
2026-10-10 16:32:26 +02:00
parent 6cff4802bf
commit c353302e3f
14 changed files with 127 additions and 147 deletions
@@ -4,6 +4,7 @@
* envelope, same create-201 split, same audit actions — these cases came over from
* admin.controller.test.ts with the routes.
*/
import { DomainError } from '../../../src/nest/common/domain-error';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { HttpException } from '@nestjs/common';
import { AdminPackingTemplatesController } from '../../../src/nest/packing/admin-packing-templates.controller';
@@ -35,16 +36,6 @@ function controller(over: Partial<PackingService> = {}) {
return { c: new AdminPackingTemplatesController(packing, { writeAudit } as unknown as AuditService), packing };
}
/**
* An override whose return value the real method signature cannot produce. Every error
* branch in PackingService sets a status, so an error envelope without one is not part
* of the service's return type, yet the controller still has a fallback for it and
* PACKTPL-003 is the case that covers that fallback.
*/
function offContract<K extends keyof PackingService>(name: K, impl: () => unknown) {
return { [name]: vi.fn(impl) } as unknown as Partial<PackingService>;
}
const thrown = async (run: () => unknown) => {
try {
await run();
@@ -63,14 +54,14 @@ describe('AdminPackingTemplatesController', () => {
expect(await controller().c.list()).toEqual({ templates: [{ id: 1 }] });
});
it('PACKTPL-002 a service {error,status} becomes that HTTP status, not a 200 body', async () => {
const { c } = controller({ getPackingTemplate: vi.fn(async () => ({ error: 'not found', status: 404 })) });
it('PACKTPL-002 a service refusal becomes that HTTP status, not a 200 body', async () => {
const { c } = controller({ getPackingTemplate: vi.fn(async () => { throw new DomainError(404, 'not found'); }) });
expect(await thrown(() => c.get('9'))).toEqual({ status: 404, body: { error: 'not found' } });
});
it('PACKTPL-003 an error without a status defaults to 400', async () => {
const { c } = controller(offContract('createTemplateCategory', async () => ({ error: 'name required' })));
expect(await thrown(() => c.createCategory('1', { name: '' }))).toEqual({ status: 400, body: { error: 'name required' } });
it('PACKTPL-003 a 400 refusal keeps its status and text', async () => {
const { c } = controller({ createTemplateCategory: vi.fn(async () => { throw new DomainError(400, 'Category name is required'); }) });
expect(await thrown(() => c.createCategory('1', { name: '' }))).toEqual({ status: 400, body: { error: 'Category name is required' } });
});
it('PACKTPL-004 create audits with the new template id', async () => {
+1 -1
View File
@@ -59,7 +59,7 @@ describe('Google route import', () => {
it('rejects unrelated URLs and oversized routes without fetching or truncating', async () => {
const { service, maps } = await setup();
for (const url of ['https://example.com/maps/dir/A/B', 'http://google.com/maps/dir/A/B', 'https://user:pass@google.com/maps/dir/A/B', 'https://google.com/search?q=A']) {
await expect(service.preview(url)).rejects.toThrow('Http Exception');
await expect(service.preview(url)).rejects.toThrow('Use a Google Maps directions link.');
}
await expect(service.preview(`https://google.com/maps/dir/${Array.from({ length: 31 }, (_, i) => `A${i}`).join('/')}`)).rejects.toThrow();
expect(maps.geocodeQuery).not.toHaveBeenCalled();
+56 -55
View File
@@ -14,6 +14,7 @@
* 2026-08 admin fold, since this service already owned all three template
* tables.
*/
import { asLegacyResult } from '../../helpers/domain-error';
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
// ── DB setup ──────────────────────────────────────────────────────────────────
@@ -1049,44 +1050,44 @@ describe('PackingService — the surface the deleted bridge exposed', () => {
describe('Packing templates', () => {
it('ADMIN-SVC-031 — createPackingTemplate returns template', async () => {
const { user: admin } = createAdmin(testDb);
const result = await svc.createPackingTemplate('Beach Trip', admin.id) as any;
const result = await asLegacyResult(svc.createPackingTemplate('Beach Trip', admin.id)) as any;
expect(result.template.name).toBe('Beach Trip');
});
it('ADMIN-SVC-032 — createPackingTemplate returns 400 for empty name', async () => {
const { user: admin } = createAdmin(testDb);
const result = await svc.createPackingTemplate('', admin.id) as any;
const result = await asLegacyResult(svc.createPackingTemplate('', admin.id)) as any;
expect(result.status).toBe(400);
});
it('ADMIN-SVC-033 — listPackingTemplates returns array', async () => {
const { user: admin } = createAdmin(testDb);
await svc.createPackingTemplate('Template A', admin.id);
await asLegacyResult(svc.createPackingTemplate('Template A', admin.id));
const templates = await svc.listPackingTemplates() as any[];
expect(templates.length).toBeGreaterThanOrEqual(1);
});
it('ADMIN-SVC-034 — updatePackingTemplate updates name', async () => {
const { user: admin } = createAdmin(testDb);
const created = await svc.createPackingTemplate('Old Name', admin.id) as any;
const result = await svc.updatePackingTemplate(String(created.template.id), { name: 'New Name' }) as any;
const created = await asLegacyResult(svc.createPackingTemplate('Old Name', admin.id)) as any;
const result = await asLegacyResult(svc.updatePackingTemplate(String(created.template.id), { name: 'New Name' })) as any;
expect(result.template.name).toBe('New Name');
});
it('ADMIN-SVC-035 — updatePackingTemplate returns 404 for non-existent', async () => {
const result = await svc.updatePackingTemplate('99999', { name: 'Ghost' }) as any;
const result = await asLegacyResult(svc.updatePackingTemplate('99999', { name: 'Ghost' })) as any;
expect(result.status).toBe(404);
});
it('ADMIN-SVC-036 — deletePackingTemplate removes template', async () => {
const { user: admin } = createAdmin(testDb);
const created = await svc.createPackingTemplate('To Delete', admin.id) as any;
const result = await svc.deletePackingTemplate(String(created.template.id)) as any;
const created = await asLegacyResult(svc.createPackingTemplate('To Delete', admin.id)) as any;
const result = await asLegacyResult(svc.deletePackingTemplate(String(created.template.id))) as any;
expect(result.name).toBe('To Delete');
});
it('ADMIN-SVC-037 — deletePackingTemplate returns 404 for non-existent', async () => {
const result = await svc.deletePackingTemplate('99999') as any;
const result = await asLegacyResult(svc.deletePackingTemplate('99999')) as any;
expect(result.status).toBe(404);
});
});
@@ -1094,50 +1095,50 @@ describe('Packing templates', () => {
describe('Template categories', () => {
it('ADMIN-SVC-038 — createTemplateCategory creates a category', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const result = await svc.createTemplateCategory(String(tpl.template.id), 'Clothing') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const result = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Clothing')) as any;
expect(result.category.name).toBe('Clothing');
});
it('ADMIN-SVC-039 — createTemplateCategory returns 400 for empty name', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const result = await svc.createTemplateCategory(String(tpl.template.id), '') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const result = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), '')) as any;
expect(result.status).toBe(400);
});
it('ADMIN-SVC-040 — createTemplateCategory returns 404 for missing template', async () => {
const result = await svc.createTemplateCategory('99999', 'Clothing') as any;
const result = await asLegacyResult(svc.createTemplateCategory('99999', 'Clothing')) as any;
expect(result.status).toBe(404);
});
it('ADMIN-SVC-041 — updateTemplateCategory updates name', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Old') as any;
const result = await svc.updateTemplateCategory(String(tpl.template.id), String(cat.category.id), { name: 'New' }) as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Old')) as any;
const result = await asLegacyResult(svc.updateTemplateCategory(String(tpl.template.id), String(cat.category.id), { name: 'New' })) as any;
expect(result.category.name).toBe('New');
});
it('ADMIN-SVC-042 — updateTemplateCategory returns 404 for missing category', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const result = await svc.updateTemplateCategory(String(tpl.template.id), '99999', { name: 'X' }) as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const result = await asLegacyResult(svc.updateTemplateCategory(String(tpl.template.id), '99999', { name: 'X' })) as any;
expect(result.status).toBe(404);
});
it('ADMIN-SVC-043 — deleteTemplateCategory removes category', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Remove Me') as any;
const result = await svc.deleteTemplateCategory(String(tpl.template.id), String(cat.category.id)) as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Remove Me')) as any;
const result = await asLegacyResult(svc.deleteTemplateCategory(String(tpl.template.id), String(cat.category.id))) as any;
expect(result.error).toBeUndefined();
});
it('ADMIN-SVC-044 — deleteTemplateCategory returns 404 for missing', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const result = await svc.deleteTemplateCategory(String(tpl.template.id), '99999') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const result = await asLegacyResult(svc.deleteTemplateCategory(String(tpl.template.id), '99999')) as any;
expect(result.status).toBe(404);
});
});
@@ -1145,11 +1146,11 @@ describe('Template categories', () => {
describe('getPackingTemplate', () => {
it('ADMIN-SVC-056 — returns template with categories and items when template exists', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Full Template', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Clothing') as any;
await svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'T-Shirt');
const tpl = await asLegacyResult(svc.createPackingTemplate('Full Template', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Clothing')) as any;
await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'T-Shirt'));
const result = await svc.getPackingTemplate(String(tpl.template.id)) as any;
const result = await asLegacyResult(svc.getPackingTemplate(String(tpl.template.id))) as any;
expect(result.template).toBeDefined();
expect(result.template.name).toBe('Full Template');
expect(Array.isArray(result.categories)).toBe(true);
@@ -1160,7 +1161,7 @@ describe('getPackingTemplate', () => {
});
it('ADMIN-SVC-057 — returns 404 for non-existent template', async () => {
const result = await svc.getPackingTemplate('99999') as any;
const result = await asLegacyResult(svc.getPackingTemplate('99999')) as any;
expect(result.status).toBe(404);
expect(result.error).toBeDefined();
});
@@ -1169,54 +1170,54 @@ describe('getPackingTemplate', () => {
describe('Template items', () => {
it('ADMIN-SVC-058 — createTemplateItem returns item with name', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Gear') as any;
const result = await svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'Backpack') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Gear')) as any;
const result = await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'Backpack')) as any;
expect(result.item).toBeDefined();
expect(result.item.name).toBe('Backpack');
});
it('ADMIN-SVC-059 — createTemplateItem returns 400 for empty name', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Gear') as any;
const result = await svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), '') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Gear')) as any;
const result = await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), '')) as any;
expect(result.status).toBe(400);
});
it('ADMIN-SVC-060 — createTemplateItem returns 404 for non-existent category', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const result = await svc.createTemplateItem(String(tpl.template.id), '99999', 'Item') as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const result = await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), '99999', 'Item')) as any;
expect(result.status).toBe(404);
});
it('ADMIN-SVC-061 — updateTemplateItem updates name', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Gear') as any;
const item = await svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'Old Item') as any;
const result = await svc.updateTemplateItem(String(tpl.template.id), String(item.item.id), { name: 'New Item' }) as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Gear')) as any;
const item = await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'Old Item')) as any;
const result = await asLegacyResult(svc.updateTemplateItem(String(tpl.template.id), String(item.item.id), { name: 'New Item' })) as any;
expect(result.item.name).toBe('New Item');
});
it('ADMIN-SVC-062 — updateTemplateItem returns 404 for non-existent item', async () => {
const result = await svc.updateTemplateItem('1', '99999', { name: 'Ghost' }) as any;
const result = await asLegacyResult(svc.updateTemplateItem('1', '99999', { name: 'Ghost' })) as any;
expect(result.status).toBe(404);
});
it('ADMIN-SVC-063 — deleteTemplateItem removes item', async () => {
const { user: admin } = createAdmin(testDb);
const tpl = await svc.createPackingTemplate('Tpl', admin.id) as any;
const cat = await svc.createTemplateCategory(String(tpl.template.id), 'Gear') as any;
const item = await svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'To Delete') as any;
const result = await svc.deleteTemplateItem(String(tpl.template.id), String(item.item.id)) as any;
const tpl = await asLegacyResult(svc.createPackingTemplate('Tpl', admin.id)) as any;
const cat = await asLegacyResult(svc.createTemplateCategory(String(tpl.template.id), 'Gear')) as any;
const item = await asLegacyResult(svc.createTemplateItem(String(tpl.template.id), String(cat.category.id), 'To Delete')) as any;
const result = await asLegacyResult(svc.deleteTemplateItem(String(tpl.template.id), String(item.item.id))) as any;
expect(result.error).toBeUndefined();
expect(await findRow(await orm(), PackingTemplateItems, { id: item.item.id })).toBeNull();
});
it('ADMIN-SVC-064 — deleteTemplateItem returns 404 for non-existent item', async () => {
const result = await svc.deleteTemplateItem('1', '99999') as any;
const result = await asLegacyResult(svc.deleteTemplateItem('1', '99999')) as any;
expect(result.status).toBe(404);
});
});
@@ -1226,23 +1227,23 @@ describe('Template items', () => {
describe('Template item scoping (post-fold quirk fix)', () => {
it('ADMIN-SVC-076 — update/deleteTemplateItem honour :templateId instead of ignoring it', async () => {
const { user: admin } = createAdmin(testDb);
const tplA = await svc.createPackingTemplate('A', admin.id) as any;
const tplB = await svc.createPackingTemplate('B', admin.id) as any;
const catA = await svc.createTemplateCategory(String(tplA.template.id), 'Gear') as any;
const item = await svc.createTemplateItem(String(tplA.template.id), String(catA.category.id), 'Tent') as any;
const tplA = await asLegacyResult(svc.createPackingTemplate('A', admin.id)) as any;
const tplB = await asLegacyResult(svc.createPackingTemplate('B', admin.id)) as any;
const catA = await asLegacyResult(svc.createTemplateCategory(String(tplA.template.id), 'Gear')) as any;
const item = await asLegacyResult(svc.createTemplateItem(String(tplA.template.id), String(catA.category.id), 'Tent')) as any;
// Template B does not own the item — both routes must 404 rather than act.
expect(await svc.updateTemplateItem(String(tplB.template.id), String(item.item.id), { name: 'Hijacked' }) as any)
expect(await asLegacyResult(svc.updateTemplateItem(String(tplB.template.id), String(item.item.id), { name: 'Hijacked' })) as any)
.toMatchObject({ status: 404 });
expect(await svc.deleteTemplateItem(String(tplB.template.id), String(item.item.id)) as any)
expect(await asLegacyResult(svc.deleteTemplateItem(String(tplB.template.id), String(item.item.id))) as any)
.toMatchObject({ status: 404 });
expect((await findRow(await orm(), PackingTemplateItems, { id: item.item.id }))!.name)
.toBe('Tent');
// The owning template still works.
expect((await svc.updateTemplateItem(String(tplA.template.id), String(item.item.id), { name: 'Tarp' }) as any).item.name)
expect((await asLegacyResult(svc.updateTemplateItem(String(tplA.template.id), String(item.item.id), { name: 'Tarp' })) as any).item.name)
.toBe('Tarp');
expect(await svc.deleteTemplateItem(String(tplA.template.id), String(item.item.id)) as any).toEqual({});
expect(await asLegacyResult(svc.deleteTemplateItem(String(tplA.template.id), String(item.item.id))) as any).toEqual({});
});
});