103 lines
3.9 KiB
JavaScript
103 lines
3.9 KiB
JavaScript
'use strict';
|
|
|
|
const { AppParamError, AppError, AppAuthError } = require('../helpers/app_error'),
|
|
{ Errors, UserTypes } = require('../helpers/constants'),
|
|
{ User, InvoiceSetting } = require('../model'),
|
|
{ isNil } = require('lodash'),
|
|
assert = require('assert'),
|
|
path = require('path'),
|
|
{ getPuid: getPuidBase, checkUserClient } = require('../helpers/user_helper');
|
|
|
|
function getPuid(req, isObject) {
|
|
const roles = [UserTypes.APP_ADM, UserTypes.OFFICER, UserTypes.PILOT, UserTypes.CLIENT, UserTypes.INSPECTOR];
|
|
return getPuidBase(req, isObject, roles);
|
|
}
|
|
|
|
async function getInvoiceSettings_get(req, res) {
|
|
const invoiceSettings = await InvoiceSetting.find({ userId: { $ne: null }, ...getPuid(req) });
|
|
res.json(invoiceSettings);
|
|
}
|
|
|
|
async function createInvoiceSetting(req, body, file) {
|
|
if (body.userId) await checkUserClient(body.userId, req);
|
|
const logo = file?.filename ? path.basename(file.filename) : '';
|
|
const newSetting = new InvoiceSetting({ ...body, logo: logo, byPuid: getPuid(req, false), createdBy: req.uid, updatedBy: req.uid });
|
|
const invoiceSetting = await newSetting.save();
|
|
return invoiceSetting;
|
|
}
|
|
|
|
async function createDefaultInvoiceSetting(req, userId) {
|
|
if (userId) await checkUserClient(userId, req);
|
|
|
|
const byPuid = getPuid(req, false);
|
|
const applicator = await User.findOne({ _id: byPuid, kind: UserTypes.APP });
|
|
if (!applicator) AppAuthError.throw(Errors.APPLICATOR_NOT_FOUND);
|
|
|
|
//TODO: the default currency best to be loocked up from the applicator' country ISO 2 code.
|
|
const defaultSetting = new InvoiceSetting({
|
|
userId: userId ? userId : undefined, companyName: applicator.name, address: applicator.address ?? '',
|
|
taxValue: 0, discount: 0, termOpts: [15, 30, 60], paymentTerm: 15, currency: 'USD', note: '', logo: '',
|
|
byPuid, createdBy: req.uid, updatedBy: req.uid
|
|
});
|
|
const newSetting = new InvoiceSetting(defaultSetting);
|
|
const invoiceSetting = await newSetting.save();
|
|
|
|
return invoiceSetting;
|
|
}
|
|
|
|
async function createInvoiceSetting_post(req, res) {
|
|
const invoiceSetting = await createInvoiceSetting(req, req.body, req.file);
|
|
res.json(invoiceSetting);
|
|
}
|
|
|
|
function getBaseQueryInvoiceSettings(req) {
|
|
const query = {};
|
|
if (req.params.invoiceSettingId) query._id = req.params.invoiceSettingId;
|
|
else if (req.params.clientId) query.userId = req.params.clientId;
|
|
|
|
if (!Object.keys(query).length) AppParamError.throw(Errors.PARAMS_NOT_EMPTY);
|
|
|
|
return query;
|
|
}
|
|
|
|
async function getInvoiceSettingDetail_get(req, res) {
|
|
const invoiceSetting = await InvoiceSetting.findOne({ ...getBaseQueryInvoiceSettings(req), ...getPuid(req) });
|
|
|
|
res.json(invoiceSetting);
|
|
}
|
|
|
|
async function getInvoiceSettingDefault_get(req, res) {
|
|
const invoiceSetting = await InvoiceSetting.findOne({ userId: { $eq: null }, ...getPuid(req) });
|
|
if (!invoiceSetting) {
|
|
const invoiceSettingNew = await createDefaultInvoiceSetting(req, null);
|
|
return res.json(invoiceSettingNew);
|
|
}
|
|
res.json(invoiceSetting);
|
|
}
|
|
|
|
async function updateInvoiceSetting_put(req, res) {
|
|
if (req.body.userId) await checkUserClient(req.body.userId, req);
|
|
|
|
const invoiceSetting = await InvoiceSetting.findOne({ ...getBaseQueryInvoiceSettings(req), ...getPuid(req) });
|
|
if (!invoiceSetting) AppError.throw(Errors.INVOICE_SETTING_NOT_FOUND);
|
|
|
|
for (const [key, value] of Object.entries(req.body)) {
|
|
if (!isNil(value)) invoiceSetting[key] = value;
|
|
}
|
|
|
|
if (!isNil(req.file)) invoiceSetting.logo = (req.file?.filename ? path.basename(req.file.filename) : '');
|
|
invoiceSetting.updatedBy = req.uid;
|
|
await invoiceSetting.save();
|
|
|
|
res.json(invoiceSetting);
|
|
}
|
|
|
|
async function deleteInvoiceSetting(req, res) {
|
|
const invoiceSetting = await InvoiceSetting.findOneAndDelete({ ...getBaseQueryInvoiceSettings(req), ...getPuid(req) });
|
|
res.json(invoiceSetting);
|
|
}
|
|
|
|
module.exports = {
|
|
getInvoiceSettings_get, createInvoiceSetting_post, getInvoiceSettingDetail_get, getInvoiceSettingDefault_get, updateInvoiceSetting_put, deleteInvoiceSetting
|
|
};
|