From 0b3a911611f3392aee2d9a6a5995fa52722a3dae Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 26 Jul 2026 19:40:16 +0100 Subject: [PATCH] Added recalculation logic to printer schema for managing printer states based on activity and online status. Integrated editObject utility for state updates, enhancing printer state management. --- .../schemas/production/printer.schema.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/database/schemas/production/printer.schema.js b/src/database/schemas/production/printer.schema.js index 89c7962..56efd2c 100644 --- a/src/database/schemas/production/printer.schema.js +++ b/src/database/schemas/production/printer.schema.js @@ -1,7 +1,7 @@ import mongoose from 'mongoose'; import { generateId } from '../../utils.js'; const { Schema } = mongoose; -import { aggregateRollups, aggregateRollupsHistory } from '../../database.js'; +import { aggregateRollups, aggregateRollupsHistory, editObject } from '../../database.js'; // Define the moonraker connection schema const moonrakerSchema = new Schema( @@ -123,6 +123,28 @@ printerSchema.statics.history = async function (from, to) { return results; }; +printerSchema.statics.recalculate = async function (printer, user) { + if (printer.active === false && printer.state?.type !== 'inactive') { + await editObject({ + model: this, + id: printer._id, + updateData: { state: { type: 'inactive' } }, + user, + recalculate: false, + }); + return; + } + if (printer.active === true && printer.state?.type === 'inactive' && printer.online === false) { + await editObject({ + model: this, + id: printer._id, + updateData: { state: { type: 'offline' } }, + user, + recalculate: false, + }); + } +}; + // Add virtual id getter printerSchema.virtual('id').get(function () { return this._id;