Schema modifications.

This commit is contained in:
Tom Butcher 2025-12-13 21:01:35 +00:00
parent 30295dad3a
commit aa4d2c6873
7 changed files with 183 additions and 17 deletions

View File

@ -1,6 +1,7 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { generateId } from '../../utils.js'; import { generateId } from '../../utils.js';
const { Schema } = mongoose; const { Schema } = mongoose;
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';
// Define the main filamentStock schema // Define the main filamentStock schema
const filamentStockSchema = new Schema( const filamentStockSchema = new Schema(
@ -23,6 +24,35 @@ const filamentStockSchema = new Schema(
{ timestamps: true } { timestamps: true }
); );
const rollupConfigs = [
{
name: 'totalCurrentWeight',
filter: {},
rollups: [{ name: 'totalCurrentWeight', property: 'currentWeight.net', operation: 'sum' }],
},
];
filamentStockSchema.statics.stats = async function () {
const results = await aggregateRollups({
model: this,
rollupConfigs: rollupConfigs,
});
return results;
};
filamentStockSchema.statics.history = async function (from, to) {
const results = await aggregateRollupsHistory({
model: this,
startDate: from,
endDate: to,
rollupConfigs: rollupConfigs,
});
// Return time-series data array
return results;
};
// Add virtual id getter // Add virtual id getter
filamentStockSchema.virtual('id').get(function () { filamentStockSchema.virtual('id').get(function () {
return this._id; return this._id;

View File

@ -1,4 +1,6 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { purchaseOrderModel } from './purchaseorder.schema.js';
import { aggregateRollups, editObject } from '../../database.js';
import { generateId } from '../../utils.js'; import { generateId } from '../../utils.js';
const { Schema } = mongoose; const { Schema } = mongoose;
@ -20,6 +22,52 @@ const orderItemSchema = new Schema(
{ timestamps: true } { timestamps: true }
); );
orderItemSchema.statics.recalculate = async function (orderItem, user) {
// Only purchase orders are supported for now
if (orderItem.orderType !== 'purchaseOrder') {
return;
}
const orderId = orderItem.order?._id || orderItem.order;
if (!orderId) {
return;
}
const rollupResults = await aggregateRollups({
model: this,
baseFilter: {
order: new mongoose.Types.ObjectId(orderId),
orderType: orderItem.orderType,
},
rollupConfigs: [
{
name: 'orderTotals',
rollups: [
{ name: 'totalAmount', property: 'totalAmount', operation: 'sum' },
{
name: 'totalAmountWithTax',
property: 'totalAmountWithTax',
operation: 'sum',
},
],
},
],
});
const totals = rollupResults.orderTotals || {};
const totalAmount = totals.totalAmount.sum?.toFixed(2) || 0;
const totalAmountWithTax = totals.totalAmountWithTax.sum?.toFixed(2) || 0;
await editObject({
model: purchaseOrderModel,
id: orderId,
updateData: {
cost: { net: totalAmount, gross: totalAmountWithTax },
},
user,
});
};
// Add virtual id getter // Add virtual id getter
orderItemSchema.virtual('id').get(function () { orderItemSchema.virtual('id').get(function () {
return this._id; return this._id;

View File

@ -1,6 +1,7 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { generateId } from '../../utils.js'; import { generateId } from '../../utils.js';
const { Schema } = mongoose; const { Schema } = mongoose;
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';
// Define the main partStock schema // Define the main partStock schema
const partStockSchema = new Schema( const partStockSchema = new Schema(
@ -11,12 +12,42 @@ const partStockSchema = new Schema(
progress: { type: Number, required: false }, progress: { type: Number, required: false },
}, },
part: { type: mongoose.Schema.Types.ObjectId, ref: 'part', required: true }, part: { type: mongoose.Schema.Types.ObjectId, ref: 'part', required: true },
startingQuantity: { type: Number, required: true },
currentQuantity: { type: Number, required: true }, currentQuantity: { type: Number, required: true },
sourceType: { type: String, required: true },
source: { type: Schema.Types.ObjectId, refPath: 'sourceType', required: true },
}, },
{ timestamps: true } { timestamps: true }
); );
const rollupConfigs = [
{
name: 'totalCurrentQuantity',
filter: {},
rollups: [{ name: 'totalCurrentQuantity', property: 'currentQuantity', operation: 'sum' }],
},
];
partStockSchema.statics.stats = async function () {
const results = await aggregateRollups({
model: this,
rollupConfigs: rollupConfigs,
});
return results;
};
partStockSchema.statics.history = async function (from, to) {
const results = await aggregateRollupsHistory({
model: this,
startDate: from,
endDate: to,
rollupConfigs: rollupConfigs,
});
// Return time-series data array
return results;
};
// Add virtual id getter // Add virtual id getter
partStockSchema.virtual('id').get(function () { partStockSchema.virtual('id').get(function () {
return this._id; return this._id;

View File

@ -18,7 +18,8 @@ const documentJobSchema = new Schema(
}, },
state: { state: {
type: { type: String, required: true, default: 'queued' }, type: { type: String, required: true, default: 'queued' },
percent: { type: Number, required: false }, progress: { type: Number, required: false },
message: { type: String, required: false },
}, },
documentTemplate: { documentTemplate: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,

View File

@ -12,6 +12,8 @@ const filamentSchema = new mongoose.Schema({
vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true }, vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
type: { required: true, type: String }, type: { required: true, type: String },
cost: { required: true, type: Number }, cost: { required: true, type: Number },
costTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: true },
costWithTax: { required: true, type: Number },
diameter: { required: true, type: Number }, diameter: { required: true, type: Number },
density: { required: true, type: Number }, density: { required: true, type: Number },
createdAt: { required: true, type: Date }, createdAt: { required: true, type: Date },

View File

@ -41,7 +41,8 @@ const deviceInfoSchema = new mongoose.Schema(
{ _id: false } { _id: false }
); );
const hostSchema = new mongoose.Schema({ const hostSchema = new mongoose.Schema(
{
_reference: { type: String, default: () => generateId()() }, _reference: { type: String, default: () => generateId()() },
name: { required: true, type: String }, name: { required: true, type: String },
tags: [{ required: false, type: String }], tags: [{ required: false, type: String }],
@ -56,7 +57,9 @@ const hostSchema = new mongoose.Schema({
authCode: { type: { required: false, type: String } }, authCode: { type: { required: false, type: String } },
deviceInfo: { deviceInfoSchema }, deviceInfo: { deviceInfoSchema },
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'file' }], files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'file' }],
}); },
{ timestamps: true }
);
hostSchema.virtual('id').get(function () { hostSchema.virtual('id').get(function () {
return this._id; return this._id;

View File

@ -1,6 +1,7 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { generateId } from '../../utils.js'; import { generateId } from '../../utils.js';
const { Schema } = mongoose; const { Schema } = mongoose;
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';
const jobSchema = new mongoose.Schema( const jobSchema = new mongoose.Schema(
{ {
@ -31,6 +32,56 @@ const jobSchema = new mongoose.Schema(
{ timestamps: true } { timestamps: true }
); );
const rollupConfigs = [
{
name: 'queued',
filter: { 'state.type': 'queued' },
rollups: [{ name: 'queued', property: 'state.type', operation: 'count' }],
},
{
name: 'printing',
filter: { 'state.type': 'printing' },
rollups: [{ name: 'printing', property: 'state.type', operation: 'count' }],
},
{
name: 'draft',
filter: { 'state.type': 'draft' },
rollups: [{ name: 'draft', property: 'state.type', operation: 'count' }],
},
{
name: 'complete',
filter: { 'state.type': 'complete' },
rollups: [{ name: 'complete', property: 'state.type', operation: 'count' }],
},
{
name: 'failed',
filter: { 'state.type': 'failed' },
rollups: [{ name: 'failed', property: 'state.type', operation: 'count' }],
},
];
jobSchema.statics.stats = async function () {
const results = await aggregateRollups({
model: this,
rollupConfigs: rollupConfigs,
});
// Transform the results to match the expected format
return results;
};
jobSchema.statics.history = async function (from, to) {
const results = await aggregateRollupsHistory({
model: this,
startDate: from,
endDate: to,
rollupConfigs: rollupConfigs,
});
// Return time-series data array
return results;
};
jobSchema.virtual('id').get(function () { jobSchema.virtual('id').get(function () {
return this._id; return this._id;
}); });