Refactored filament recalculation logic to utilize filament SKUs instead of order items, improving efficiency. Updated allowed filters in the filament SKUs route to include 'costWithTax' and modified population logic in the service for better data retrieval.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-06-21 23:59:33 +01:00
parent 74a9139002
commit efb3fdbcb9
3 changed files with 6 additions and 15 deletions

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);
}
};

View File

@ -16,7 +16,7 @@ import {
router.get('/', isAuthenticated, (req, res) => {
const { page, limit, property, search, sort, order } = req.query;
const allowedFilters = ['_id', 'barcode', 'filament', 'filament._id', 'name', 'color', 'cost'];
const allowedFilters = ['_id', 'barcode', 'filament', 'filament._id', 'name', 'color', 'cost', 'costWithTax'];
const filter = getFilter(req.query, allowedFilters);
listFilamentSkusRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

View File

@ -35,7 +35,7 @@ export const listFilamentSkusRouteHandler = async (
search,
sort,
order,
populate: ['costTaxRate'],
populate: [{ path: 'filament', populate: 'costTaxRate' }, 'costTaxRate'],
});
if (result?.error) {