All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
- Introduced a new script `ensure-references.js` to ensure all documents have a `_reference` field, enhancing data integrity across models. - Updated `package.json` to include the new script in the npm scripts section for easy execution. - Refactored database utility functions to exclude additional models from audit logging, improving data privacy. - Enhanced various schemas with new fields and methods for better tax calculations and inventory management.
71 lines
2.4 KiB
JavaScript
71 lines
2.4 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { generateId } from '../../utils.js';
|
|
import { taxRateModel } from './taxrate.schema.js';
|
|
import { editObject, getObject } from '../../database.js';
|
|
import { amountWithTax, resolveTaxRate } from '../../tax.js';
|
|
const { Schema } = mongoose;
|
|
|
|
const marketplaceMappingSchema = new mongoose.Schema(
|
|
{
|
|
marketplace: { type: Schema.Types.ObjectId, ref: 'marketplace', required: true },
|
|
externalReference: { type: String, required: false },
|
|
},
|
|
{ _id: true }
|
|
);
|
|
|
|
const courierServiceSchema = new mongoose.Schema(
|
|
{
|
|
_reference: { type: String, default: () => generateId()() },
|
|
name: { required: true, type: String },
|
|
courier: { type: Schema.Types.ObjectId, ref: 'courier', required: true },
|
|
active: { required: true, type: Boolean },
|
|
tracked: { required: true, type: Boolean },
|
|
deliveryTime: { required: true, type: Number },
|
|
website: { required: false, type: String },
|
|
cost: { required: true, type: Number, default: 0 },
|
|
costTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
|
costWithTax: { required: false, type: Number },
|
|
additionalCost: { required: false, type: Number },
|
|
additionalCostWithTax: { required: false, type: Number },
|
|
shippingCurrency: { required: true, type: String, default: 'GBP' },
|
|
international: { required: true, type: Boolean, default: false },
|
|
marketplaces: { type: [marketplaceMappingSchema], default: [] },
|
|
},
|
|
{ timestamps: true }
|
|
);
|
|
|
|
courierServiceSchema.index({ name: 'text', website: 'text' });
|
|
|
|
courierServiceSchema.virtual('id').get(function () {
|
|
return this._id;
|
|
});
|
|
|
|
courierServiceSchema.set('toJSON', { virtuals: true });
|
|
|
|
courierServiceSchema.statics.recalculate = async function (courierService, user) {
|
|
const costTaxRate = await resolveTaxRate(courierService.costTaxRate, getObject, taxRateModel);
|
|
const updateData = {};
|
|
|
|
if (courierService.cost != null) {
|
|
updateData.costWithTax = amountWithTax(courierService.cost, costTaxRate);
|
|
}
|
|
if (courierService.additionalCost != null) {
|
|
updateData.additionalCostWithTax = amountWithTax(
|
|
courierService.additionalCost,
|
|
costTaxRate
|
|
);
|
|
}
|
|
|
|
if (Object.keys(updateData).length > 0) {
|
|
await editObject({
|
|
model: this,
|
|
id: courierService._id,
|
|
updateData,
|
|
user,
|
|
recalculate: false,
|
|
});
|
|
}
|
|
};
|
|
|
|
export const courierServiceModel = mongoose.model('courierService', courierServiceSchema);
|