import { BadRequestException, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { ClientSession, Model, Types } from 'mongoose'; import { StockMovement } from '../_entities/stock-movement.entity'; import { StockContent } from '../_entities/stock-content.entity'; import { StockMovementType } from '../_dto/stock-movement.dto'; @Injectable() export class MovementRepository { constructor( @InjectModel(StockMovement.name) private readonly model: Model, ) {} findAll() { return this.model .find({ articleId: { $exists: true } }) .sort({ createdAt: -1 }) .exec(); } remove(_id: string): never { throw new BadRequestException( 'Buchungsprotokolle können nicht gelöscht werden', ); } async log( type: StockMovementType, content: StockContent, quantity: number, session: ClientSession, toStockId?: string, toArea?: string, ) { const [movement] = await this.model.create( [ { type, articleId: content.articleId, stockId: content.stockId, quantity, batchId: content.batchId ?? undefined, assetId: content.assetId ?? undefined, toStockId: toStockId ? new Types.ObjectId(toStockId) : undefined, meta: { fromArea: content.area, ...(toArea !== undefined ? { toArea } : {}), }, }, ], { session }, ); return movement; } }