Add scheduled properties and event handling for invoice schema
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- 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.
This commit is contained in:
Tom Butcher 2026-09-06 22:58:31 +01:00
parent 6fcaab56d3
commit d659695912

View File

@ -1,7 +1,12 @@
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, editObject, getObject } from '../../database.js'; import {
aggregateRollups,
aggregateRollupsHistory,
editObject,
getObject,
} from '../../database.js';
import { taxRateModel } from '../management/taxrate.schema.js'; import { taxRateModel } from '../management/taxrate.schema.js';
import { amountWithTax, resolveTaxRate } from '../../tax.js'; import { amountWithTax, resolveTaxRate } from '../../tax.js';
@ -136,6 +141,33 @@ invoiceSchema.statics.history = async function (from, to) {
return results; 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) { invoiceSchema.statics.recalculate = async function (invoice, user) {
const invoiceId = invoice._id || invoice; const invoiceId = invoice._id || invoice;
if (!invoiceId) { if (!invoiceId) {