feat: dedicated, versioned shop-sync feed for Wabenhain

GET /api/v1/shop-sync returns exactly the data ApiCore releases to the
shop: harvest facts, article master data and bottlings with their
harvest ids. Every query uses an explicit select, so notes, colonies,
treatments and fields added to the models later never leave ApiCore
unless deliberately added to the allowlist.

- New permission shop-sync:read (granted to ADMIN automatically, to no
  other role): the shop's service account needs nothing else, in
  particular no access to harvest or extraction records with notes.
- contractVersion 1; only breaking changes bump it.
- Always the full set: small volume, and a full comparison heals missed
  runs by itself.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-09-27 20:31:46 +02:00
1 parent 3c7ff1e0b7
commit ede86c062a
7 files changed
+357 -1

No files matched your search

@@ -0,0 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { RequirePermission } from '../auth/decorators/require-permission.decorator';
import { ShopSyncFeed, ShopSyncService } from './shop-sync.service';
@ApiTags('shop-sync')
@ApiBearerAuth()
@Controller('shop-sync')
export class ShopSyncController {
constructor(private readonly shopSync: ShopSyncService) {}
@RequirePermission('shop-sync:read')
@Get()
feed(): Promise<ShopSyncFeed> {
return this.shopSync.feed();
}
}
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ShopSyncController } from './shop-sync.controller';
import { ShopSyncService } from './shop-sync.service';
@Module({
controllers: [ShopSyncController],
providers: [ShopSyncService],
})
export class ShopSyncModule {}
@@ -0,0 +1,140 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { ProductStatus } from '../../generated/prisma';
/**
* Bumped only for breaking changes (a removed or renamed field, a changed
* meaning). Adding a field keeps the version - consumers must ignore fields
* they do not know.
*/
export const SHOP_SYNC_CONTRACT_VERSION = 1;
export interface ShopSyncHarvest {
id: string;
apiaryId: string;
apiaryName: string;
date: Date;
honeyType: string | null;
honeySuperCount: number | null;
grossWeight: number | null;
netWeight: number | null;
}
export interface ShopSyncProduct {
id: string;
name: string;
description: string | null;
sku: string | null;
packagingSize: number | null;
weight: number | null;
salesUnit: string | null;
status: ProductStatus;
}
export interface ShopSyncBottling {
id: string;
lotNumber: string;
date: Date;
bestBeforeDate: Date;
/** Grams per jar. */
jarSize: number;
jarCount: number;
productId: string | null;
/** Harvests behind this bottling, oldest first. */
harvestIds: string[];
}
export interface ShopSyncFeed {
contractVersion: number;
generatedAt: Date;
harvests: ShopSyncHarvest[];
products: ShopSyncProduct[];
bottlings: ShopSyncBottling[];
}
/**
* The data ApiCore releases to the shop (Wabenhain) - and only that.
*
* Every query uses an explicit `select`: a field reaches the shop only when
* it is named here. Notes, colonies, treatments, users and anything added to
* the models later stay inside ApiCore unless someone deliberately adds them
* to this allowlist. ApiCore stays the source of truth for these facts; the
* shop decides prices, texts and visibility on its own side.
*
* Always the full set, no "changed since" filter: the volume is small (a few
* dozen records a year), and a full comparison heals itself - a missed or
* failed run cannot leave a correction behind.
*/
@Injectable()
export class ShopSyncService {
constructor(private readonly prisma: PrismaService) {}
async feed(): Promise<ShopSyncFeed> {
const [harvests, products, bottlings] = await Promise.all([
this.prisma.harvest.findMany({
select: {
id: true,
apiaryId: true,
date: true,
honeyType: true,
honeySuperCount: true,
grossWeight: true,
netWeight: true,
apiary: { select: { name: true } },
},
orderBy: { date: 'asc' },
}),
this.prisma.product.findMany({
select: {
id: true,
name: true,
description: true,
sku: true,
packagingSize: true,
weight: true,
salesUnit: true,
status: true,
},
orderBy: { name: 'asc' },
}),
this.prisma.bottlingBatch.findMany({
select: {
id: true,
lotNumber: true,
date: true,
bestBeforeDate: true,
jarSize: true,
jarCount: true,
productId: true,
honeyBatch: {
select: {
extraction: {
select: {
harvests: {
select: { harvestId: true },
orderBy: { harvest: { date: 'asc' } },
},
},
},
},
},
},
orderBy: { date: 'asc' },
}),
]);
return {
contractVersion: SHOP_SYNC_CONTRACT_VERSION,
generatedAt: new Date(),
harvests: harvests.map(({ apiary, ...harvest }) => ({
...harvest,
apiaryName: apiary.name,
})),
products,
bottlings: bottlings.map(({ honeyBatch, ...bottling }) => ({
...bottling,
harvestIds: honeyBatch.extraction.harvests.map((h) => h.harvestId),
})),
};
}
}