This commit is contained in:
@@ -3,6 +3,8 @@ import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
import { RealtimeService } from '../realtime/realtime.service';
|
||||
import { StockAdjustmentEntryDto } from './dto/create-stock-adjustments.dto';
|
||||
import { VenueLedger } from '../tabs/venue-ledger';
|
||||
import { applyPendingAdjustments } from './stock-adjustments';
|
||||
|
||||
const adjustmentSelect = {
|
||||
id: true,
|
||||
@@ -106,22 +108,36 @@ export class StockService {
|
||||
}
|
||||
}
|
||||
|
||||
// Applied by the server right away, once, also when every terminal is offline; the terminals get the new stock
|
||||
// on their next sync
|
||||
const requestedBy = user.name || user.email;
|
||||
const created = await this.prisma.$transaction(
|
||||
entries.map((entry) => this.prisma.stockAdjustment.create({
|
||||
data: {
|
||||
venueId,
|
||||
productId: entry.productId,
|
||||
productName: productsById.get(entry.productId)!.name,
|
||||
type: entry.type,
|
||||
quantity: entry.quantity,
|
||||
note: entry.note ?? null,
|
||||
requestedBy,
|
||||
requestedById: user.id,
|
||||
},
|
||||
select: adjustmentSelect,
|
||||
})),
|
||||
);
|
||||
const created = await this.prisma.$transaction(async (tx) => {
|
||||
const now = new Date();
|
||||
const ledger = await VenueLedger.open(tx, venueId);
|
||||
const ids: string[] = [];
|
||||
for (const entry of entries) {
|
||||
const { id } = await tx.stockAdjustment.create({
|
||||
data: {
|
||||
venueId,
|
||||
productId: entry.productId,
|
||||
productName: productsById.get(entry.productId)!.name,
|
||||
type: entry.type,
|
||||
quantity: entry.quantity,
|
||||
note: entry.note ?? null,
|
||||
requestedBy,
|
||||
requestedById: user.id,
|
||||
createdAt: now,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
ids.push(id);
|
||||
}
|
||||
await applyPendingAdjustments(tx, ledger, now, ids);
|
||||
await ledger.save(tx, now);
|
||||
|
||||
const adjustments = await tx.stockAdjustment.findMany({ where: { id: { in: ids } }, select: adjustmentSelect });
|
||||
return ids.map((id) => adjustments.find((a) => a.id === id)!);
|
||||
});
|
||||
|
||||
await this.auditService.log(venueId, 'client_user', user.id, 'stock_adjustments_created', {
|
||||
adjustments: created.map((a) => ({ id: a.id, productId: a.productId, type: a.type, quantity: a.quantity })),
|
||||
|
||||
Reference in New Issue
Block a user