auditing
This commit is contained in:
@@ -12,6 +12,7 @@ import type {
|
||||
WorkOrderSummaryDto,
|
||||
} from "@mrp/shared";
|
||||
|
||||
import { logAuditEvent } from "../../lib/audit.js";
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
const workOrderModel = (prisma as any).workOrder;
|
||||
@@ -634,7 +635,7 @@ export async function listManufacturingStations(): Promise<ManufacturingStationD
|
||||
return stations.map(mapStation);
|
||||
}
|
||||
|
||||
export async function createManufacturingStation(payload: ManufacturingStationInput) {
|
||||
export async function createManufacturingStation(payload: ManufacturingStationInput, actorId?: string | null) {
|
||||
const station = await prisma.manufacturingStation.create({
|
||||
data: {
|
||||
code: payload.code.trim(),
|
||||
@@ -645,6 +646,20 @@ export async function createManufacturingStation(payload: ManufacturingStationIn
|
||||
},
|
||||
});
|
||||
|
||||
await logAuditEvent({
|
||||
actorId,
|
||||
entityType: "manufacturing-station",
|
||||
entityId: station.id,
|
||||
action: "created",
|
||||
summary: `Created manufacturing station ${station.code}.`,
|
||||
metadata: {
|
||||
code: station.code,
|
||||
name: station.name,
|
||||
queueDays: station.queueDays,
|
||||
isActive: station.isActive,
|
||||
},
|
||||
});
|
||||
|
||||
return mapStation(station);
|
||||
}
|
||||
|
||||
@@ -715,7 +730,7 @@ export async function getWorkOrderById(workOrderId: string) {
|
||||
return workOrder ? mapDetail(workOrder as WorkOrderRecord) : null;
|
||||
}
|
||||
|
||||
export async function createWorkOrder(payload: WorkOrderInput) {
|
||||
export async function createWorkOrder(payload: WorkOrderInput, actorId?: string | null) {
|
||||
const validated = await validateWorkOrderInput(payload);
|
||||
if (!validated.ok) {
|
||||
return { ok: false as const, reason: validated.reason };
|
||||
@@ -743,10 +758,26 @@ export async function createWorkOrder(payload: WorkOrderInput) {
|
||||
await syncWorkOrderReservations(created.id);
|
||||
|
||||
const workOrder = await getWorkOrderById(created.id);
|
||||
if (workOrder) {
|
||||
await logAuditEvent({
|
||||
actorId,
|
||||
entityType: "work-order",
|
||||
entityId: created.id,
|
||||
action: "created",
|
||||
summary: `Created work order ${workOrder.workOrderNumber}.`,
|
||||
metadata: {
|
||||
workOrderNumber: workOrder.workOrderNumber,
|
||||
itemId: workOrder.itemId,
|
||||
projectId: workOrder.projectId,
|
||||
status: workOrder.status,
|
||||
quantity: workOrder.quantity,
|
||||
},
|
||||
});
|
||||
}
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
|
||||
export async function updateWorkOrder(workOrderId: string, payload: WorkOrderInput) {
|
||||
export async function updateWorkOrder(workOrderId: string, payload: WorkOrderInput, actorId?: string | null) {
|
||||
const existing = await workOrderModel.findUnique({
|
||||
where: { id: workOrderId },
|
||||
select: {
|
||||
@@ -786,10 +817,26 @@ export async function updateWorkOrder(workOrderId: string, payload: WorkOrderInp
|
||||
await syncWorkOrderReservations(workOrderId);
|
||||
|
||||
const workOrder = await getWorkOrderById(workOrderId);
|
||||
if (workOrder) {
|
||||
await logAuditEvent({
|
||||
actorId,
|
||||
entityType: "work-order",
|
||||
entityId: workOrderId,
|
||||
action: "updated",
|
||||
summary: `Updated work order ${workOrder.workOrderNumber}.`,
|
||||
metadata: {
|
||||
workOrderNumber: workOrder.workOrderNumber,
|
||||
itemId: workOrder.itemId,
|
||||
projectId: workOrder.projectId,
|
||||
status: workOrder.status,
|
||||
quantity: workOrder.quantity,
|
||||
},
|
||||
});
|
||||
}
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
|
||||
export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrderStatus) {
|
||||
export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrderStatus, actorId?: string | null) {
|
||||
const existing = await workOrderModel.findUnique({
|
||||
where: { id: workOrderId },
|
||||
select: {
|
||||
@@ -822,6 +869,19 @@ export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrd
|
||||
await syncWorkOrderReservations(workOrderId);
|
||||
|
||||
const workOrder = await getWorkOrderById(workOrderId);
|
||||
if (workOrder) {
|
||||
await logAuditEvent({
|
||||
actorId,
|
||||
entityType: "work-order",
|
||||
entityId: workOrderId,
|
||||
action: "status.updated",
|
||||
summary: `Updated work order ${workOrder.workOrderNumber} to ${status}.`,
|
||||
metadata: {
|
||||
workOrderNumber: workOrder.workOrderNumber,
|
||||
status,
|
||||
},
|
||||
});
|
||||
}
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
|
||||
@@ -910,6 +970,22 @@ export async function issueWorkOrderMaterial(workOrderId: string, payload: WorkO
|
||||
await syncWorkOrderReservations(workOrderId);
|
||||
|
||||
const nextWorkOrder = await getWorkOrderById(workOrderId);
|
||||
if (nextWorkOrder) {
|
||||
await logAuditEvent({
|
||||
actorId: createdById,
|
||||
entityType: "work-order",
|
||||
entityId: workOrderId,
|
||||
action: "material.issued",
|
||||
summary: `Issued material to work order ${nextWorkOrder.workOrderNumber}.`,
|
||||
metadata: {
|
||||
workOrderNumber: nextWorkOrder.workOrderNumber,
|
||||
componentItemId: payload.componentItemId,
|
||||
warehouseId: payload.warehouseId,
|
||||
locationId: payload.locationId,
|
||||
quantity: payload.quantity,
|
||||
},
|
||||
});
|
||||
}
|
||||
return nextWorkOrder ? { ok: true as const, workOrder: nextWorkOrder } : { ok: false as const, reason: "Unable to load updated work order." };
|
||||
}
|
||||
|
||||
@@ -970,5 +1046,19 @@ export async function recordWorkOrderCompletion(workOrderId: string, payload: Wo
|
||||
await syncWorkOrderReservations(workOrderId);
|
||||
|
||||
const nextWorkOrder = await getWorkOrderById(workOrderId);
|
||||
if (nextWorkOrder) {
|
||||
await logAuditEvent({
|
||||
actorId: createdById,
|
||||
entityType: "work-order",
|
||||
entityId: workOrderId,
|
||||
action: "completion.recorded",
|
||||
summary: `Recorded completion against work order ${nextWorkOrder.workOrderNumber}.`,
|
||||
metadata: {
|
||||
workOrderNumber: nextWorkOrder.workOrderNumber,
|
||||
quantity: payload.quantity,
|
||||
status: nextWorkOrder.status,
|
||||
},
|
||||
});
|
||||
}
|
||||
return nextWorkOrder ? { ok: true as const, workOrder: nextWorkOrder } : { ok: false as const, reason: "Unable to load updated work order." };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user