'use strict'; const Currencies = require('../helpers/currencies'); const env = require('../helpers/env'); const { InvoiceStatusAction } = require('../helpers/constants'); const mongoose = require('mongoose'), Schema = mongoose.Schema; const schema = new Schema( { userId: { type: Schema.Types.ObjectId, ref: 'User', index: true }, byPuid: { type: Schema.Types.ObjectId, ref: 'User', index: true, required: true }, companyName: { type: String, default: '' }, address: { type: String, default: '' }, taxValue: { type: Number, min: 0 }, discount: { type: Number, min: 0 }, paymentTerm: { type: Number, min: 0, require: true }, termOpts: { type: [Number] }, currency: { type: String, enum: Object.keys(Currencies) }, note: { type: String, default: '' }, logo: { type: String, require: true, get: (logo) => logo ? `${env.INV_IMG_VIR_DIR}/${logo}` : undefined }, // Days to mark overdue invoice to uncollectible status dueToUncollectibleDays: { type: Number, min: 0, max: 9999, default: env.INV_MAX_OVERDUE_DAYS }, dueToUncollectibleOp: { type: String, enum: Object.values(InvoiceStatusAction), message: "{VALUE} for 'dueToUncollectibleOp' is not supported", default: InvoiceStatusAction.None, }, createdBy: { type: Schema.Types.ObjectId, ref: 'User', require: true }, updatedBy: { type: Schema.Types.ObjectId, ref: 'User', require: true }, }, { timestamps: true, toJSON: { getters: true }, toObject: { getters: true } } ); schema.index({ userId: 1, byPuid: 1 }, { unique: true }); // Notes: Add index only when not exists or there will always be error module.exports = mongoose.model('invoice_settings', schema);