import { Schema, model, models, Model } from "mongoose"; export interface IStockLock { _id: string; // `${businessId}:${branchId}:${productId}` owner: string; // random id of the request holding the lock lockedUntil: Date; // lease expiry: a crashed request cannot hold the lock forever } /** * A short lease on one product's stock at one branch, held while a request * reads the current balance and writes a movement that depends on it (see * withStockLock in src/lib/server/stock.ts). Rows are deleted on release; * an expired row is simply taken over by the next request. */ const stockLockSchema = new Schema( { _id: { type: String, required: true }, owner: { type: String, required: true }, lockedUntil: { type: Date, required: true }, }, { versionKey: false } ); export const StockLock = (models.StockLock as Model) || model("StockLock", stockLockSchema);