Refactor filament schema recalculation logic to use filamentSku model instead of orderItem model. This change improves the efficiency of recalculating SKUs associated with a filament.
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-06-21 23:59:34 +01:00
parent 4f9ed6039b
commit 0748256ce8

View File

@ -25,19 +25,10 @@ filamentSchema.virtual('id').get(function () {
filamentSchema.set('toJSON', { virtuals: true });
filamentSchema.statics.recalculate = async function (filament, user) {
const orderItemModel = mongoose.model('orderItem');
const itemId = filament._id;
const draftOrderItems = await orderItemModel
.find({
'state.type': 'draft',
itemType: 'filament',
item: itemId,
})
.populate('order')
.lean();
for (const orderItem of draftOrderItems) {
await orderItemModel.recalculate(orderItem, user);
const filamentSkuModel = mongoose.model('filamentSku');
const skus = await filamentSkuModel.find({ filament: filament._id }).select('_id').lean();
for (const sku of skus) {
await filamentSkuModel.recalculate(sku, user);
}
};