- Add WarehouseItem model with bilingual names (FA/EN) and bilingual counting units - Add WarehouseItemTransaction model for IN/OUT movements with reason and actor - Add WarehouseItemStock model for denormalized per-(item×warehouse) running balance - Add warehouseItemService with full CRUD, atomic stock updates via $inc, negative stock guard, and aggregation queries for per-item/per-warehouse/total stock - Add warehouseItemController and REST routes at /api/warehouse-items - Add warehouseService, warehouseController, warehouseRoutes for warehouse location CRUD at /api/warehouses - Register both route groups in routerEndpoints.js
40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
const mongoose = require('mongoose')
|
||
const ObjectID = mongoose.Schema.Types.ObjectId
|
||
|
||
// Running balance snapshot per (item × warehouse)
|
||
// Updated atomically on each transaction using $inc
|
||
// Acts as a fast-read cache — transactions are the source of truth
|
||
const warehouseItemStockSchema = new mongoose.Schema(
|
||
{
|
||
item: {
|
||
type: ObjectID,
|
||
ref: 'WarehouseItem',
|
||
required: true,
|
||
},
|
||
warehouse: {
|
||
type: ObjectID,
|
||
ref: 'Warehouse',
|
||
required: true,
|
||
},
|
||
quantity: {
|
||
type: Number,
|
||
default: 0, // current balance
|
||
},
|
||
unit: {
|
||
type: String,
|
||
},
|
||
unitEn: {
|
||
type: String,
|
||
},
|
||
},
|
||
{
|
||
timestamps: true,
|
||
}
|
||
)
|
||
|
||
// Guarantee one document per (item, warehouse) combination
|
||
warehouseItemStockSchema.index({ item: 1, warehouse: 1 }, { unique: true })
|
||
|
||
const WarehouseItemStock = mongoose.model('WarehouseItemStock', warehouseItemStockSchema)
|
||
module.exports = WarehouseItemStock
|