fix: align stock and refill semantics

Squash merge PR #474
This commit is contained in:
Daniel Volz
2026-03-25 06:49:34 +01:00
committed by GitHub
parent 37fc2b8e66
commit 7059c25f1c
18 changed files with 1063 additions and 463 deletions
+8 -4
View File
@@ -464,7 +464,7 @@ const stockAdjustmentBodySchema = {
looseTablets: { type: "integer", minimum: 0 },
totalPills: { type: "integer", minimum: 0 },
packageAmountValue: { type: "integer", minimum: 0 },
packCount: { type: "integer", minimum: 1 },
packCount: { type: "integer", minimum: 0 },
},
example: {
stockAdjustment: -2,
@@ -1238,8 +1238,8 @@ export async function medicationRoutes(app: FastifyInstance) {
) {
return reply.badRequest("packageAmountValue must be a non-negative integer");
}
if (packCount !== undefined && (typeof packCount !== "number" || !Number.isInteger(packCount) || packCount < 1)) {
return reply.badRequest("packCount must be an integer >= 1");
if (packCount !== undefined && (typeof packCount !== "number" || !Number.isInteger(packCount) || packCount < 0)) {
return reply.badRequest("packCount must be a non-negative integer");
}
const updateFields: {
@@ -1258,12 +1258,16 @@ export async function medicationRoutes(app: FastifyInstance) {
const packageType = normalizePackageType(existing.packageType);
const allowsAmountBaseUpdate = isTubePackageType(packageType) || isLiquidContainerPackageType(packageType);
const allowsBottleCapacityUpdate = packageType === "bottle";
if (allowsAmountBaseUpdate) {
if (totalPills !== undefined) updateFields.totalPills = totalPills;
if (looseTablets !== undefined) updateFields.looseTablets = looseTablets;
if (packageAmountValue !== undefined) updateFields.packageAmountValue = packageAmountValue;
if (packCount !== undefined) updateFields.packCount = packCount;
}
if (allowsBottleCapacityUpdate && totalPills !== undefined) {
updateFields.totalPills = totalPills;
}
if (packCount !== undefined) updateFields.packCount = packCount;
if (looseTablets !== undefined) {
updateFields.looseTablets = looseTablets;
}
+4 -1
View File
@@ -197,18 +197,21 @@ export async function refillRoutes(app: FastifyInstance) {
? Math.max(0, remainingPrescriptionRefills - consumedRefills)
: (med.prescriptionRemainingRefills ?? null);
const refillBaselineAt = new Date();
const updatePayload: {
packCount: number;
looseTablets: number;
totalPills?: number;
packageAmountValue?: number;
prescriptionRemainingRefills: number | null;
lastStockCorrectionAt: Date;
updatedAt: Date;
} = {
packCount: newPackCount,
looseTablets: newLooseTablets,
prescriptionRemainingRefills: newRemainingRefills,
updatedAt: new Date(),
lastStockCorrectionAt: refillBaselineAt,
updatedAt: refillBaselineAt,
};
if (isCountBasedAmountPackage) {