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
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
This commit is contained in:
parent
4f9ed6039b
commit
0748256ce8
@ -25,19 +25,10 @@ filamentSchema.virtual('id').get(function () {
|
|||||||
filamentSchema.set('toJSON', { virtuals: true });
|
filamentSchema.set('toJSON', { virtuals: true });
|
||||||
|
|
||||||
filamentSchema.statics.recalculate = async function (filament, user) {
|
filamentSchema.statics.recalculate = async function (filament, user) {
|
||||||
const orderItemModel = mongoose.model('orderItem');
|
const filamentSkuModel = mongoose.model('filamentSku');
|
||||||
const itemId = filament._id;
|
const skus = await filamentSkuModel.find({ filament: filament._id }).select('_id').lean();
|
||||||
const draftOrderItems = await orderItemModel
|
for (const sku of skus) {
|
||||||
.find({
|
await filamentSkuModel.recalculate(sku, user);
|
||||||
'state.type': 'draft',
|
|
||||||
itemType: 'filament',
|
|
||||||
item: itemId,
|
|
||||||
})
|
|
||||||
.populate('order')
|
|
||||||
.lean();
|
|
||||||
|
|
||||||
for (const orderItem of draftOrderItems) {
|
|
||||||
await orderItemModel.recalculate(orderItem, user);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user