Files
ApiCore/ApiCore-Backend/src/bottling/bottling.controller.ts
T
Nick SzaunigandClaude Opus 5.5 04f93e954a feat(stock): batch movement ledger, production batches and shop write-back API
One remaining quantity per batch, kept in ApiCore and always derived:
initial quantity (jarCount / produced quantity) plus the signed movements
in the new BatchMovement ledger, which is never deleted.

- Production batches for MANUFACTURED articles (candles, propolis
  products, ...); articles get a kind and a shop category.
- Withdrawals (farm shop, own use, breakage, sample) and inventory
  corrections on bottlings and production batches, never more than the
  remaining stock; every write locks the bottling or the article row.
- Shop bookings via POST /shop-sync/movements (new scope
  shop-sync:write): idempotent per externalId, articles allocated oldest
  best-before first, returns mirrored onto the batches of the sale,
  invalid entries rejected one by one.
- GET /shop-sync/stock and remainingCount in the shop feed; webhook
  stock.changed after every change (ids only, receivers fetch numbers).
- Bottlings and production batches with movements cannot be deleted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-02 06:38:12 +02:00

101 lines
2.9 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { RequirePermission } from '../auth/decorators/require-permission.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import type { AuthenticatedUser } from '../auth/types/authenticated-user';
import { CreateBottlingBatchDto } from './dto/create-bottling-batch.dto';
import { UpdateBottlingBatchDto } from './dto/update-bottling-batch.dto';
import { BottlingService } from './bottling.service';
import { Idempotent } from '../common/idempotency/idempotent.decorator';
import { StockService } from '../stock/stock.service';
import { WithdrawStockDto } from '../stock/dto/withdraw-stock.dto';
import { CorrectStockDto } from '../stock/dto/correct-stock.dto';
@ApiTags('bottling')
@ApiBearerAuth()
@Controller('bottling-batches')
export class BottlingController {
constructor(
private readonly bottlingService: BottlingService,
private readonly stock: StockService,
) {}
@RequirePermission('bottling:read')
@Get()
findAll(@Query('honeyBatchId') honeyBatchId?: string) {
return this.bottlingService.findAll({ honeyBatchId });
}
@RequirePermission('bottling:read')
@Get(':id')
findOne(@Param('id') id: string) {
return this.bottlingService.findById(id);
}
@RequirePermission('bottling:read')
@Get(':id/stock')
async stockOf(@Param('id') id: string) {
await this.bottlingService.findById(id);
return this.stock.stockOf({ kind: 'bottling', id });
}
@RequirePermission('bottling:withdraw')
@Idempotent()
@Post(':id/withdrawals')
async withdraw(
@Param('id') id: string,
@Body() dto: WithdrawStockDto,
@CurrentUser() actor: AuthenticatedUser,
) {
await this.bottlingService.findById(id);
return this.stock.withdraw({ kind: 'bottling', id }, dto, actor.id);
}
@RequirePermission('bottling:withdraw')
@Idempotent()
@Post(':id/corrections')
async correct(
@Param('id') id: string,
@Body() dto: CorrectStockDto,
@CurrentUser() actor: AuthenticatedUser,
) {
await this.bottlingService.findById(id);
return this.stock.correct({ kind: 'bottling', id }, dto, actor.id);
}
@RequirePermission('bottling:create')
@Idempotent()
@Post()
create(
@Body() dto: CreateBottlingBatchDto,
@CurrentUser() actor: AuthenticatedUser,
) {
return this.bottlingService.create(dto, actor.id);
}
@RequirePermission('bottling:update')
@Patch(':id')
update(
@Param('id') id: string,
@Body() dto: UpdateBottlingBatchDto,
@CurrentUser() actor: AuthenticatedUser,
) {
return this.bottlingService.update(id, dto, actor.id);
}
@RequirePermission('bottling:delete')
@Delete(':id')
remove(@Param('id') id: string, @CurrentUser() actor: AuthenticatedUser) {
return this.bottlingService.remove(id, actor.id);
}
}