feat: platform backbone and field capture complete (checkpoint)

Platform backbone (plan 2026-10-13-plattform-rueckgrat), all tasks reviewed:
- webhooks with delivery table, retries and stable delivery id
- calendar handlers; a finished task removes its Outlook event
- tasks, automations, inspections, varroa, treatments, feedings,
  harvests, colonies, wintering, stock and bottling record domain events
  in the same transaction; legacy post-commit calls removed
- row locks through one helper (lockRowForUpdate, lockRowsForShare,
  lockRowsForUpdate)
- job framework with JobRun, cli exit codes, cleanup, task templates,
  daily evaluation, hourly app-backup and the rekey job
- system API and page "Verwaltung > System" with retry
- @nestjs/schedule removed, guard tests against in-process schedulers

Field capture T3 (plan 2026-10-13-t3-erfassung-am-stand), all tasks reviewed:
- inspection photo offline without metadata
- bulk capture for treatments, feedings and varroa (backend + frontend)
- offline stand mode with service worker, offline pages and packs
- offline session, cold start and logout on shared devices

Docs: ARCHITECTURE.md assumptions and sections updated, TASKS.md.
Gates: backend lint, typecheck, 1770 unit, 420 e2e, build, no schema
drift; frontend lint, typecheck, 913 tests, build. Not rolled out.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-10-14 11:36:38 +02:00
1 parent c01a38835a
commit cdd30cf3ee
288 files changed
+39165 -2853

No files matched your search

@@ -1,15 +1,15 @@
import * as crypto from 'node:crypto';
import { BadRequestException, Logger, NotFoundException } from '@nestjs/common';
import { WebhooksService } from './webhooks.service';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
import { postWebhook } from './webhook-delivery';
import { openWebhookSecret, sealWebhookSecret } from './webhook-secret';
import { DbClient } from '../prisma/db-client';
import { openWebhookSecret } from './webhook-secret';
jest.mock('./webhook-delivery', () => ({ postWebhook: jest.fn() }));
const postWebhookMock = postWebhook as jest.MockedFunction<typeof postWebhook>;
const flushDeliveries = () => new Promise((resolve) => setImmediate(resolve));
interface DeliveryRowInput {
subscriptionId: string;
event: string;
body: string;
}
describe('WebhooksService', () => {
let service: WebhooksService;
@@ -21,13 +21,16 @@ describe('WebhooksService', () => {
update: jest.Mock;
delete: jest.Mock;
};
webhookDelivery: {
createMany: jest.Mock;
findMany: jest.Mock;
};
$executeRaw: jest.Mock;
};
let auditLog: { record: jest.Mock };
beforeEach(() => {
process.env.JWT_ACCESS_SECRET = 'unit-test-jwt-access-secret-0123456789';
postWebhookMock.mockReset();
postWebhookMock.mockResolvedValue({ status: 200 });
prisma = {
webhookSubscription: {
findMany: jest.fn().mockResolvedValue([]),
@@ -36,6 +39,15 @@ describe('WebhooksService', () => {
update: jest.fn(),
delete: jest.fn(),
},
webhookDelivery: {
createMany: jest
.fn()
.mockImplementation(({ data }: { data: DeliveryRowInput[] }) =>
Promise.resolve({ count: data.length }),
),
findMany: jest.fn().mockResolvedValue([]),
},
$executeRaw: jest.fn().mockResolvedValue(1),
};
auditLog = { record: jest.fn().mockResolvedValue(undefined) };
service = new WebhooksService(
@@ -44,6 +56,10 @@ describe('WebhooksService', () => {
);
});
afterEach(() => jest.restoreAllMocks());
const db = () => prisma as unknown as DbClient;
it('create generates a random secret and stores it', async () => {
prisma.webhookSubscription.create.mockImplementation(
({ data }: { data: Record<string, unknown> }) => ({
@@ -85,108 +101,191 @@ describe('WebhooksService', () => {
);
});
it('dispatch only queries subscriptions matching the event and active flag', async () => {
await service.dispatch('colony.created', { id: 'colony-1' });
describe('enqueueDeliveries', () => {
it('enqueueDeliveries writes one row per active subscription for the event and wakes the worker', async () => {
prisma.webhookSubscription.findMany.mockResolvedValueOnce([
{ id: 'wh-1' },
{ id: 'wh-2' },
]);
const payload = { id: 'colony-1', code: 'V-1' };
expect(prisma.webhookSubscription.findMany).toHaveBeenCalledWith({
where: { active: true, events: { has: 'colony.created' } },
await expect(
service.enqueueDeliveries(db(), 'colony.created', payload),
).resolves.toBe(2);
expect(prisma.webhookSubscription.findMany).toHaveBeenCalledWith({
where: { active: true, events: { has: 'colony.created' } },
select: { id: true },
});
// The body is fixed here, so every retry sends the same bytes.
const body = JSON.stringify({ event: 'colony.created', data: payload });
expect(prisma.webhookDelivery.createMany).toHaveBeenCalledWith({
data: [
{ subscriptionId: 'wh-1', event: 'colony.created', body },
{ subscriptionId: 'wh-2', event: 'colony.created', body },
],
});
expect(prisma.$executeRaw).toHaveBeenCalledTimes(1);
const [[strings, ...values]] = prisma.$executeRaw.mock.calls as [
[TemplateStringsArray, ...unknown[]],
];
expect(strings.join('$')).toBe("SELECT pg_notify($, '')");
expect(values).toEqual(['webhook_delivery']);
});
});
it('dispatch signs timestamp, delivery id and payload with HMAC-SHA256', async () => {
const subscription = {
id: 'wh-1',
url: 'https://example.test/hook',
secret: sealWebhookSecret('topsecret'),
active: true,
};
prisma.webhookSubscription.findMany.mockResolvedValueOnce([subscription]);
it('writes nothing and wakes nobody without a matching subscription', async () => {
await expect(
service.enqueueDeliveries(db(), 'task.created', { id: 't1' }),
).resolves.toBe(0);
await service.dispatch('colony.created', { id: 'colony-1' });
await flushDeliveries();
expect(prisma.webhookDelivery.createMany).not.toHaveBeenCalled();
expect(prisma.$executeRaw).not.toHaveBeenCalled();
});
expect(postWebhookMock).toHaveBeenCalledTimes(1);
const [url, headers, body] = postWebhookMock.mock.calls[0];
expect(url).toBe('https://example.test/hook');
expect(headers['X-ApiCore-Event']).toBe('colony.created');
expect(headers['X-ApiCore-Delivery']).toMatch(/^[0-9a-f-]{36}$/);
expect(Number(headers['X-ApiCore-Timestamp'])).toBeGreaterThan(0);
it('works on the client it is given, e.g. a handler transaction', async () => {
const tx = {
webhookSubscription: {
findMany: jest.fn().mockResolvedValue([{ id: 'wh-1' }]),
},
webhookDelivery: {
createMany: jest.fn().mockResolvedValue({ count: 1 }),
},
$executeRaw: jest.fn().mockResolvedValue(1),
};
const expectedSignature = crypto
.createHmac('sha256', 'topsecret')
.update(
`${headers['X-ApiCore-Timestamp']}.${headers['X-ApiCore-Delivery']}.${body}`,
)
.digest('hex');
expect(headers['X-ApiCore-Signature']).toBe(`sha256=${expectedSignature}`);
});
it('dispatch returns before a slow receiver has answered', async () => {
prisma.webhookSubscription.findMany.mockResolvedValueOnce([
{ id: 'wh-1', url: 'https://example.test/hook', secret: 's' },
]);
postWebhookMock.mockReturnValueOnce(new Promise(() => undefined));
await expect(
service.dispatch('colony.created', { id: 'colony-1' }),
).resolves.toBeUndefined();
});
it('dispatch does not throw when delivery fails', async () => {
prisma.webhookSubscription.findMany.mockResolvedValueOnce([
{
id: 'wh-1',
url: 'https://example.test/hook',
secret: 's',
active: true,
},
]);
postWebhookMock.mockRejectedValueOnce(new Error('network down'));
await expect(
service.dispatch('colony.created', { id: 'colony-1' }),
).resolves.toBeUndefined();
await flushDeliveries();
});
// dispatch() runs after the event's record is committed: a failure must
// not fail that request (the client would retry and store it twice).
describe('dispatch after commit', () => {
let errorSpy: jest.SpyInstance;
beforeEach(() => {
errorSpy = jest
.spyOn(Logger.prototype, 'error')
.mockImplementation(() => undefined);
prisma.webhookSubscription.findMany.mockRejectedValue(
new Error('db down'),
await service.enqueueDeliveries(
tx as unknown as DbClient,
'stock.changed',
{ bottlingBatchIds: ['b1'], productIds: [] },
);
});
afterEach(() => {
errorSpy.mockRestore();
expect(tx.webhookDelivery.createMany).toHaveBeenCalledTimes(1);
expect(tx.$executeRaw).toHaveBeenCalledTimes(1);
expect(prisma.webhookDelivery.createMany).not.toHaveBeenCalled();
expect(prisma.$executeRaw).not.toHaveBeenCalled();
});
});
describe('dispatch (legacy path)', () => {
it('enqueues on the root client after the commit and sends nothing itself', async () => {
prisma.webhookSubscription.findMany.mockResolvedValueOnce([
{ id: 'wh-1' },
]);
it('never throws when the subscriptions cannot be loaded and logs the record', async () => {
await expect(
service.dispatch('colony.created', { id: 'colony-1' }),
).resolves.toBeUndefined();
expect(errorSpy).toHaveBeenCalledTimes(1);
const [message] = errorSpy.mock.calls[0] as [string];
expect(message).toContain('colony.created colony-1');
expect(message).toContain('db down');
expect(prisma.webhookSubscription.findMany).toHaveBeenCalledWith({
where: { active: true, events: { has: 'colony.created' } },
select: { id: true },
});
expect(prisma.webhookDelivery.createMany).toHaveBeenCalledWith({
data: [
{
subscriptionId: 'wh-1',
event: 'colony.created',
body: '{"event":"colony.created","data":{"id":"colony-1"}}',
},
],
});
expect(prisma.$executeRaw).toHaveBeenCalledTimes(1);
});
it('also logs a payload without its own id', async () => {
await service.dispatch('stock.changed', {
bottlingBatchIds: ['b-1'],
productIds: [],
// dispatch() runs after the event's record is committed: a failure must
// not fail that request (the client would retry and store it twice).
describe('when enqueuing fails', () => {
let errorSpy: jest.SpyInstance;
beforeEach(() => {
errorSpy = jest
.spyOn(Logger.prototype, 'error')
.mockImplementation(() => undefined);
});
const [message] = errorSpy.mock.calls[0] as [string];
expect(message).toContain('stock.changed');
expect(message).toContain('db down');
it('dispatch never throws when enqueuing fails (legacy path)', async () => {
prisma.webhookSubscription.findMany.mockResolvedValueOnce([
{ id: 'wh-1' },
]);
prisma.webhookDelivery.createMany.mockRejectedValueOnce(
new Error('db down'),
);
await expect(
service.dispatch('colony.created', { id: 'colony-1' }),
).resolves.toBeUndefined();
expect(errorSpy).toHaveBeenCalledTimes(1);
const [message] = errorSpy.mock.calls[0] as [string];
expect(message).toContain('colony.created colony-1');
expect(message).toContain('db down');
});
it('never throws when the subscriptions cannot be loaded and logs the record', async () => {
prisma.webhookSubscription.findMany.mockRejectedValueOnce(
new Error('db down'),
);
await expect(
service.dispatch('colony.created', { id: 'colony-1' }),
).resolves.toBeUndefined();
const [message] = errorSpy.mock.calls[0] as [string];
expect(message).toContain('colony.created colony-1');
expect(message).toContain('db down');
});
it('also logs a payload without its own id', async () => {
prisma.webhookSubscription.findMany.mockRejectedValueOnce(
new Error('db down'),
);
await service.dispatch('stock.changed', {
bottlingBatchIds: ['b-1'],
productIds: [],
});
const [message] = errorSpy.mock.calls[0] as [string];
expect(message).toContain('stock.changed');
expect(message).toContain('db down');
});
});
});
describe('findDeliveries', () => {
it('lists the newest deliveries of a subscription without their body', async () => {
prisma.webhookSubscription.findUnique.mockResolvedValueOnce({
id: 'wh-1',
});
const rows = [{ id: 'd2' }, { id: 'd1' }];
prisma.webhookDelivery.findMany.mockResolvedValueOnce(rows);
await expect(service.findDeliveries('wh-1', 20)).resolves.toBe(rows);
expect(prisma.webhookDelivery.findMany).toHaveBeenCalledWith({
where: { subscriptionId: 'wh-1' },
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
take: 20,
select: {
id: true,
event: true,
status: true,
attempts: true,
lastStatusCode: true,
lastError: true,
createdAt: true,
deliveredAt: true,
},
});
});
it('throws NotFoundException for an unknown subscription', async () => {
prisma.webhookSubscription.findUnique.mockResolvedValueOnce(null);
await expect(
service.findDeliveries('missing', 50),
).rejects.toBeInstanceOf(NotFoundException);
expect(prisma.webhookDelivery.findMany).not.toHaveBeenCalled();
});
});