Numerious fixes.

This commit is contained in:
Tom Butcher 2026-05-17 19:11:29 +01:00
parent 57f057e3aa
commit 92e07c97d7
8 changed files with 41 additions and 10 deletions

View File

@ -19,6 +19,7 @@ const filamentStockSchema = new Schema(
net: { type: Number, required: true },
gross: { type: Number, required: true },
},
filament: { type: mongoose.Schema.Types.ObjectId, ref: 'filament', required: true },
filamentSku: { type: mongoose.Schema.Types.ObjectId, ref: 'filamentSku', required: true },
stockLocation: {
type: mongoose.Schema.Types.ObjectId,
@ -29,6 +30,13 @@ const filamentStockSchema = new Schema(
{ timestamps: true }
);
filamentStockSchema.pre('validate', async function () {
if (!this.filament && this.filamentSku) {
const sku = await mongoose.model('filamentSku').findById(this.filamentSku).select('filament').lean();
if (sku?.filament) this.filament = sku.filament;
}
});
const rollupConfigs = [
{
name: 'totalCurrentWeight',

View File

@ -14,6 +14,7 @@ const gcodeFileSchema = new mongoose.Schema(
name: { required: true, type: String },
gcodeFileName: { required: false, type: String },
size: { type: Number, required: false },
filament: { type: Schema.Types.ObjectId, ref: 'filament', required: true },
filamentSku: { type: Schema.Types.ObjectId, ref: 'filamentSku', required: true },
parts: [partSchema],
file: { type: mongoose.SchemaTypes.ObjectId, ref: 'file', required: false },
@ -22,6 +23,13 @@ const gcodeFileSchema = new mongoose.Schema(
{ timestamps: true }
);
gcodeFileSchema.pre('validate', async function () {
if (!this.filament && this.filamentSku) {
const sku = await mongoose.model('filamentSku').findById(this.filamentSku).select('filament').lean();
if (sku?.filament) this.filament = sku.filament;
}
});
gcodeFileSchema.index({ name: 'text', brand: 'text' });
gcodeFileSchema.virtual('id').get(function () {

View File

@ -19,6 +19,8 @@ import {
router.get('/', isAuthenticated, (req, res) => {
const { page, limit, property, search, sort, order } = req.query;
const allowedFilters = [
'filament',
'filament._id',
'filamentSku',
'state',
'startingWeight',
@ -33,7 +35,7 @@ router.get('/', isAuthenticated, (req, res) => {
router.get('/properties', isAuthenticated, (req, res) => {
let properties = convertPropertiesString(req.query.properties);
const allowedFilters = ['filamentSku', 'state.type'];
const allowedFilters = ['filament', 'filament._id', 'filamentSku', 'state.type'];
const filter = getFilter(req.query, allowedFilters, false);
var masterFilter = {};
if (req.query.masterFilter) {

View File

@ -16,14 +16,14 @@ import { convertPropertiesString, getFilter } from '../../utils.js';
// list of gcodeFiles
router.get('/', isAuthenticated, (req, res) => {
const { page, limit, property, search, sort, order } = req.query;
const allowedFilters = ['_id', 'name', 'filament._id'];
const allowedFilters = ['_id', 'name', 'filament', 'filament._id', 'filamentSku', 'filamentSku._id'];
const filter = getFilter(req.query, allowedFilters);
listGCodeFilesRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});
router.get('/properties', isAuthenticated, (req, res) => {
let properties = convertPropertiesString(req.query.properties);
const allowedFilters = ['filament'];
const allowedFilters = ['filament', 'filament._id', 'filamentSku', 'filamentSku._id'];
const filter = getFilter(req.query, allowedFilters, false);
listGCodeFilesByPropertiesRouteHandler(req, res, properties, filter);
});

View File

@ -36,7 +36,11 @@ export const listFilamentStocksRouteHandler = async (
search,
sort,
order,
populate: [{ path: 'filamentSku' }, { path: 'stockLocation' }],
populate: [
{ path: 'filament' },
{ path: 'filamentSku', populate: 'filament' },
{ path: 'stockLocation' },
],
});
if (result?.error) {
@ -60,7 +64,7 @@ export const listFilamentStocksByPropertiesRouteHandler = async (
model: filamentStockModel,
properties,
filter,
populate: ['filamentSku', 'stockLocation'],
populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'stockLocation'],
masterFilter,
});
@ -79,7 +83,11 @@ export const getFilamentStockRouteHandler = async (req, res) => {
const result = await getObject({
model: filamentStockModel,
id,
populate: [{ path: 'filamentSku' }, { path: 'stockLocation' }],
populate: [
{ path: 'filament' },
{ path: 'filamentSku', populate: 'filament' },
{ path: 'stockLocation' },
],
});
if (result?.error) {
logger.warn(`Filament Stock not found with supplied id.`);
@ -147,6 +155,7 @@ export const newFilamentStockRouteHandler = async (req, res) => {
updatedAt: new Date(),
startingWeight: req.body.startingWeight,
currentWeight: req.body.currentWeight,
filament: req.body.filament,
filamentSku: req.body.filamentSku,
state: req.body.state,
stockLocation: req.body.stockLocation,

View File

@ -88,6 +88,7 @@ async function executePostedLine(transferId, line) {
state: src.state,
startingWeight: destWeight,
currentWeight: destWeight,
filament: src.filament,
filamentSku: src.filamentSku,
stockLocation: toLocId,
});

View File

@ -11,7 +11,8 @@ export const EXPORT_FILTER_BY_TYPE = {
printer: ['host'],
job: ['printer', 'gcodeFile'],
subJob: ['job'],
filamentStock: ['filamentSku'],
filamentStock: ['filament', 'filament._id', 'filamentSku'],
gcodeFile: ['filament', 'filament._id', 'filamentSku'],
filament: ['material', 'material._id', 'name', 'diameter', 'cost'],
filamentSku: ['filament', 'vendor', 'costTaxRate'],
material: ['name', 'tags'],

View File

@ -35,7 +35,7 @@ export const listGCodeFilesRouteHandler = async (
search,
sort,
order,
populate: ['filamentSku'],
populate: ['filament', { path: 'filamentSku', populate: 'filament' }],
});
if (result?.error) {
@ -58,7 +58,7 @@ export const listGCodeFilesByPropertiesRouteHandler = async (
model: gcodeFileModel,
properties,
filter,
populate: 'filamentSku',
populate: ['filament', { path: 'filamentSku', populate: 'filament' }],
});
if (result?.error) {
@ -76,7 +76,7 @@ export const getGCodeFileRouteHandler = async (req, res) => {
const result = await getObject({
model: gcodeFileModel,
id,
populate: ['filamentSku', 'parts.partSku'],
populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'],
});
if (result?.error) {
logger.warn(`GCodeFile not found with supplied id.`);
@ -113,6 +113,7 @@ export const editGCodeFileRouteHandler = async (req, res) => {
updatedAt: new Date(),
name: req.body.name,
file: req.body.file,
filament: req.body.filament,
filamentSku: req.body.filamentSku,
parts: req.body.parts,
};
@ -140,6 +141,7 @@ export const newGCodeFileRouteHandler = async (req, res) => {
updatedAt: new Date(),
name: req.body.name,
file: req.body.file,
filament: req.body.filament,
filamentSku: req.body.filamentSku,
parts: req.body.parts,
};