From d65969591271402e735dca035d2b49874fdf8b8c Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 6 Sep 2026 22:58:31 +0100 Subject: [PATCH] Add scheduled properties and event handling for invoice schema - Introduced `scheduledProperties` to define properties for invoice scheduling, specifically for `dueAt`. - Implemented `onSchedulerEvent` method to update invoice state to 'overdue' when the due date has passed. - Enhanced the invoice schema with new static methods to improve scheduling functionality and state management. --- .../schemas/finance/invoice.schema.js | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/database/schemas/finance/invoice.schema.js b/src/database/schemas/finance/invoice.schema.js index 954d09a..c09bbb7 100644 --- a/src/database/schemas/finance/invoice.schema.js +++ b/src/database/schemas/finance/invoice.schema.js @@ -1,7 +1,12 @@ import mongoose from 'mongoose'; import { generateId } from '../../utils.js'; const { Schema } = mongoose; -import { aggregateRollups, aggregateRollupsHistory, editObject, getObject } from '../../database.js'; +import { + aggregateRollups, + aggregateRollupsHistory, + editObject, + getObject, +} from '../../database.js'; import { taxRateModel } from '../management/taxrate.schema.js'; import { amountWithTax, resolveTaxRate } from '../../tax.js'; @@ -136,6 +141,33 @@ invoiceSchema.statics.history = async function (from, to) { return results; }; +invoiceSchema.statics.scheduledProperties = [ + { + name: 'dueAt', + filter: { state: 'due|sent|acknowledged' }, + type: 'dateTime', + }, +]; + +invoiceSchema.statics.onSchedulerEvent = async function (invoice, property) { + const invoiceId = invoice._id || invoice; + if (!invoiceId) { + return; + } + + if (property === 'dueAt' && invoice.dueAt && invoice.dueAt < new Date()) { + await editObject({ + model: this, + id: invoiceId, + updateData: { + state: { type: 'overdue' }, + }, + user: 'system', + recalculate: false, + }); + } +}; + invoiceSchema.statics.recalculate = async function (invoice, user) { const invoiceId = invoice._id || invoice; if (!invoiceId) {