Enhance filament schema by adding vendor reference and improving formatting
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- Introduced a new optional field `vendor` in the filament schema to reference the vendor associated with the filament.
- Improved code formatting for better readability and maintainability, including consistent indentation and line breaks.
- Ensured that the `recalculate` method remains functional with updated schema structure.
This commit is contained in:
Tom Butcher 2026-07-26 19:07:46 +01:00
parent 766f4722ea
commit 0a1f75d050

View File

@ -3,20 +3,28 @@ import { generateId } from '../../utils.js';
const { Schema } = mongoose;
// Filament base - cost and tax; color and cost override at FilamentSKU
const filamentSchema = new mongoose.Schema({
_reference: { type: String, default: () => generateId()() },
name: { required: true, type: String },
barcode: { required: false, type: String },
url: { required: false, type: String },
image: { required: false, type: Buffer },
material: { type: Schema.Types.ObjectId, ref: 'material', required: true },
diameter: { required: true, type: Number },
density: { required: true, type: Number },
emptySpoolWeight: { required: true, type: Number },
cost: { type: Number, required: false },
costTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
costWithTax: { type: Number, required: false },
}, { timestamps: true });
const filamentSchema = new mongoose.Schema(
{
_reference: { type: String, default: () => generateId()() },
name: { required: true, type: String },
vendor: { required: false, type: Schema.Types.ObjectId, ref: 'vendor' },
barcode: { required: false, type: String },
url: { required: false, type: String },
image: { required: false, type: Buffer },
material: { type: Schema.Types.ObjectId, ref: 'material', required: true },
diameter: { required: true, type: Number },
density: { required: true, type: Number },
emptySpoolWeight: { required: true, type: Number },
cost: { type: Number, required: false },
costTaxRate: {
type: Schema.Types.ObjectId,
ref: 'taxRate',
required: false
},
costWithTax: { type: Number, required: false }
},
{ timestamps: true }
);
filamentSchema.index({ name: 'text', barcode: 'text', url: 'text' });
@ -28,7 +36,10 @@ filamentSchema.set('toJSON', { virtuals: true });
filamentSchema.statics.recalculate = async function (filament, user) {
const filamentSkuModel = mongoose.model('filamentSku');
const skus = await filamentSkuModel.find({ filament: filament._id }).select('_id').lean();
const skus = await filamentSkuModel
.find({ filament: filament._id })
.select('_id')
.lean();
for (const sku of skus) {
await filamentSkuModel.recalculate(sku, user);
}