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.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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user