Skip to content

Commit

Permalink
clean up after feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninarchive committed Dec 5, 2023
1 parent 33cd816 commit 69b4604
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 115 deletions.
26 changes: 13 additions & 13 deletions backend/src/controllers/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Request, Response } from 'express';
import {Request, Response} from 'express';
import fs from 'fs';
import { ObjectID } from 'mongodb';
import {ObjectID} from 'mongodb';
import appConfig from '../config';
import { ICompanyConfig } from '../models/config';
import { getTemplatesPath } from './utils';
import { CollectionNames, updateAudit } from '../models/common';
import { ConfacRequest } from '../models/technical';
import { saveAudit } from './utils/audit-logs';
import {ICompanyConfig} from '../models/config';
import {getTemplatesPath} from './utils';
import {CollectionNames, updateAudit} from '../models/common';
import {ConfacRequest} from '../models/technical';
import {saveAudit} from './utils/audit-logs';

export const getCompanyConfig = async (req: Request, res: Response) => {
const companyConfig: ICompanyConfig | null = await req.db.collection(CollectionNames.CONFIG).findOne({ key: 'conf' });
const companyConfig: ICompanyConfig | null = await req.db.collection(CollectionNames.CONFIG).findOne({key: 'conf'});
if (companyConfig) {
return res.send(companyConfig);
}
Expand All @@ -32,16 +32,16 @@ export const getSecurityConfig = async (req: Request, res: Response) => {


export const saveCompanyConfig = async (req: ConfacRequest, res: Response) => {
const { _id, ...config }: ICompanyConfig = req.body;
const {_id, ...config}: ICompanyConfig = req.body;

if (_id) {
config.audit = updateAudit(config.audit, req.user);

const { value: originalConfig } = await req.db.collection<ICompanyConfig>(CollectionNames.CONFIG)
.findOneAndUpdate({ _id: new ObjectID(_id) }, { $set: config }, { returnOriginal: true });
const {value: originalConfig} = await req.db.collection<ICompanyConfig>(CollectionNames.CONFIG)
.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: config}, {returnOriginal: true});

await saveAudit(req, 'config', originalConfig, config);
return res.send({ _id, ...config });
return res.send({_id, ...config});
}

const inserted = await req.db.collection<ICompanyConfig>(CollectionNames.CONFIG).insertOne(config);
Expand All @@ -60,7 +60,7 @@ export const getTemplates = (req: Request, res: Response) => {
/** Get logs_audit for an entity */
export const getAudit = async (req: Request, res: Response) => {
const logs = await req.db.collection('logs_audit')
.find({ model: req.query.model, modelId: new ObjectID(req.query.modelId) })
.find({model: req.query.model, modelId: new ObjectID(req.query.modelId)})
.toArray();

return res.send(logs);
Expand Down
76 changes: 37 additions & 39 deletions backend/src/controllers/invoices.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import moment from 'moment';
import { Request, Response } from 'express';
import { ObjectID, Db } from 'mongodb';
import { IInvoice, INVOICE_EXCEL_HEADERS } from '../models/invoices';
import { IAttachmentCollection } from '../models/attachments';
import { createPdf, createXml } from './utils';
import { CollectionNames, IAttachment, createAudit, updateAudit } from '../models/common';
import { IProjectMonth } from '../models/projectsMonth';
import { ConfacRequest, Jwt } from '../models/technical';
import { saveAudit } from './utils/audit-logs';
import { ICompanyConfig } from '../models/config';
import {Request, Response} from 'express';
import {ObjectID, Db} from 'mongodb';
import {IInvoice, INVOICE_EXCEL_HEADERS} from '../models/invoices';
import {IAttachmentCollection} from '../models/attachments';
import {createPdf, createXml} from './utils';
import {CollectionNames, IAttachment, createAudit, updateAudit} from '../models/common';
import {IProjectMonth} from '../models/projectsMonth';
import {ConfacRequest, Jwt} from '../models/technical';
import {saveAudit} from './utils/audit-logs';



Expand All @@ -22,8 +21,7 @@ const createInvoice = async (invoice: IInvoice, db: Db, pdfBuffer: Buffer, user:

let xmlBuffer;
if (!invoice.isQuotation) {
const companyConfig: ICompanyConfig = await db.collection(CollectionNames.CONFIG).findOne({ key: 'conf' });
xmlBuffer = Buffer.from(createXml(invoice, companyConfig));
xmlBuffer = Buffer.from(createXml(invoice));
await db.collection<Pick<IAttachmentCollection, '_id' | 'pdf' | 'xml'>>(CollectionNames.ATTACHMENTS).insertOne({
_id: new ObjectID(createdInvoice._id),
pdf: pdfBuffer,
Expand All @@ -44,21 +42,21 @@ const createInvoice = async (invoice: IInvoice, db: Db, pdfBuffer: Buffer, user:

const moveProjectMonthAttachmentsToInvoice = async (invoice: IInvoice, projectMonthId: ObjectID, db: Db) => {
const projectMonthAttachments: IAttachmentCollection | null = await db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH)
.findOne({ _id: projectMonthId }, { projection: { _id: false } });
.findOne({_id: projectMonthId}, {projection: {_id: false}});

if (projectMonthAttachments) {
await db.collection(CollectionNames.ATTACHMENTS).findOneAndUpdate({ _id: invoice._id }, { $set: { ...projectMonthAttachments } });
await db.collection(CollectionNames.ATTACHMENTS).findOneAndUpdate({_id: invoice._id}, {$set: {...projectMonthAttachments}});
}

const projectMonth = await db.collection<IProjectMonth>(CollectionNames.PROJECTS_MONTH).findOne({ _id: projectMonthId });
const projectMonth = await db.collection<IProjectMonth>(CollectionNames.PROJECTS_MONTH).findOne({_id: projectMonthId});
const updatedAttachmentDetails = projectMonth ? [...invoice.attachments, ...projectMonth?.attachments] : invoice.attachments;

const inserted = await db.collection<IInvoice>(CollectionNames.INVOICES)
.findOneAndUpdate({ _id: new ObjectID(invoice._id) }, { $set: { attachments: updatedAttachmentDetails } }, { returnOriginal: false });
.findOneAndUpdate({_id: new ObjectID(invoice._id)}, {$set: {attachments: updatedAttachmentDetails}}, {returnOriginal: false});
const updatedInvoice = inserted.value;

await db.collection<IProjectMonth>(CollectionNames.PROJECTS_MONTH).findOneAndUpdate({ _id: projectMonthId }, { $set: { attachments: [] } });
await db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH).findOneAndDelete({ _id: projectMonthId });
await db.collection<IProjectMonth>(CollectionNames.PROJECTS_MONTH).findOneAndUpdate({_id: projectMonthId}, {$set: {attachments: []}});
await db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH).findOneAndDelete({_id: projectMonthId});

return updatedInvoice;
};
Expand All @@ -67,7 +65,7 @@ const moveProjectMonthAttachmentsToInvoice = async (invoice: IInvoice, projectMo
export const getInvoicesController = async (req: Request, res: Response) => {
const getFrom = moment().subtract(req.query.months, 'months').startOf('month').format('YYYY-MM-DD');
const invoices = await req.db.collection(CollectionNames.INVOICES)
.find({ date: { $gte: getFrom } })
.find({ date: {$gte: getFrom} })
.toArray();
return res.send(invoices);
};
Expand All @@ -78,8 +76,8 @@ export const createInvoiceController = async (req: ConfacRequest, res: Response)
const invoice: IInvoice = req.body;

if (!invoice.isQuotation) {
const [lastInvoice] = await req.db.collection<IInvoice>(CollectionNames.INVOICES).find({ isQuotation: false })
.sort({ number: -1 })
const [lastInvoice] = await req.db.collection<IInvoice>(CollectionNames.INVOICES).find({isQuotation: false})
.sort({number: -1})
.limit(1)
.toArray();

Expand Down Expand Up @@ -130,27 +128,27 @@ export const createInvoiceController = async (req: ConfacRequest, res: Response)

/** Update an existing invoice */
export const updateInvoiceController = async (req: ConfacRequest, res: Response) => {
const { _id, ...invoice }: IInvoice = req.body;
const {_id, ...invoice}: IInvoice = req.body;

invoice.audit = updateAudit(invoice.audit, req.user);
const updatedPdfBuffer = await createPdf({ _id, ...invoice });
const updatedPdfBuffer = await createPdf({_id, ...invoice});

if (!Buffer.isBuffer(updatedPdfBuffer) && updatedPdfBuffer.error) {
return res.status(500).send(updatedPdfBuffer.error);
}

if (Buffer.isBuffer(updatedPdfBuffer)) {
await req.db.collection<IAttachment>(CollectionNames.ATTACHMENTS)
.findOneAndUpdate({ _id: new ObjectID(_id) }, { $set: { pdf: updatedPdfBuffer } });
.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: { pdf: updatedPdfBuffer }});
}

if (!invoice.projectMonth) {
// Makes sure projectMonth is overwritten in the db if already present there
invoice.projectMonth = undefined;
}

const { value: originalInvoice } = await req.db.collection<IInvoice>(CollectionNames.INVOICES)
.findOneAndUpdate({ _id: new ObjectID(_id) }, { $set: invoice }, { returnOriginal: true });
const {value: originalInvoice} = await req.db.collection<IInvoice>(CollectionNames.INVOICES)
.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: invoice}, {returnOriginal: true});

// Fix diff
if (!invoice.projectMonth) {
Expand All @@ -167,12 +165,12 @@ export const updateInvoiceController = async (req: ConfacRequest, res: Response)
// Right now it is always updating the projectMonth.verified but this only changes when the invoice.verified changes
// This is now 'fixed' on the frontend.
projectMonth = await req.db.collection(CollectionNames.PROJECTS_MONTH)
.findOneAndUpdate({ _id: new ObjectID(invoice.projectMonth.projectMonthId) }, { $set: { verified: invoice.verified } });
.findOneAndUpdate({_id: new ObjectID(invoice.projectMonth.projectMonthId)}, {$set: { verified: invoice.verified }});
}

const result: Array<any> = [{
type: 'invoice',
model: { _id, ...invoice },
model: {_id, ...invoice},
}];
if (projectMonth && projectMonth.ok && projectMonth.value) {
result.push({
Expand All @@ -188,34 +186,34 @@ export const updateInvoiceController = async (req: ConfacRequest, res: Response)

/** Hard invoice delete: There is no coming back from this one */
export const deleteInvoiceController = async (req: Request, res: Response) => {
const { id: invoiceId }: { id: string; } = req.body;
const {id: invoiceId}: {id: string;} = req.body;

const invoice = await req.db.collection<IInvoice>(CollectionNames.INVOICES).findOne({ _id: new ObjectID(invoiceId) });
const invoice = await req.db.collection<IInvoice>(CollectionNames.INVOICES).findOne({_id: new ObjectID(invoiceId)});

if (invoice?.projectMonth) {
const invoiceAttachments: IAttachmentCollection | null = await req.db.collection(CollectionNames.ATTACHMENTS)
.findOne({ _id: new ObjectID(invoiceId) as ObjectID }, {
.findOne({_id: new ObjectID(invoiceId) as ObjectID}, {
projection: {
_id: false,
pdf: false,
},
});

if (invoiceAttachments !== null && Object.keys(invoiceAttachments).length > 0) {
await req.db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH).updateOne({ _id: new ObjectID(invoice.projectMonth.projectMonthId) }, {
$set: { ...invoiceAttachments }
await req.db.collection(CollectionNames.ATTACHMENTS_PROJECT_MONTH).updateOne({_id: new ObjectID(invoice.projectMonth.projectMonthId)}, {
$set: { ...invoiceAttachments }
}, {
upsert: true
});
}

const projectMonthCollection = req.db.collection(CollectionNames.PROJECTS_MONTH);
const attachments = invoice.attachments.filter(a => a.type !== 'pdf');
await projectMonthCollection.findOneAndUpdate({ _id: new ObjectID(invoice.projectMonth.projectMonthId) }, { $set: { attachments } });
await projectMonthCollection.findOneAndUpdate({_id: new ObjectID(invoice.projectMonth.projectMonthId)}, {$set: {attachments}});
}

await req.db.collection(CollectionNames.INVOICES).findOneAndDelete({ _id: new ObjectID(invoiceId) });
await req.db.collection(CollectionNames.ATTACHMENTS).findOneAndDelete({ _id: new ObjectID(invoiceId) });
await req.db.collection(CollectionNames.INVOICES).findOneAndDelete({_id: new ObjectID(invoiceId)});
await req.db.collection(CollectionNames.ATTACHMENTS).findOneAndDelete({_id: new ObjectID(invoiceId)});

return res.send(invoiceId);
};
Expand All @@ -241,7 +239,7 @@ export const previewPdfInvoiceController = async (req: Request, res: Response) =
export const generateExcelForInvoicesController = async (req: Request, res: Response) => {
const invoiceIds: ObjectID[] = req.body.map((invoiceId: string) => new ObjectID(invoiceId));

const invoices = await req.db.collection<IInvoice>(CollectionNames.INVOICES).find({ _id: { $in: invoiceIds } })
const invoices = await req.db.collection<IInvoice>(CollectionNames.INVOICES).find({_id: {$in: invoiceIds}})
.toArray();

const separator = ';';
Expand Down Expand Up @@ -269,9 +267,9 @@ export const generateExcelForInvoicesController = async (req: Request, res: Resp


export const getInvoiceXmlController = async (req: Request, res: Response) => {
const { id } = req.params;
const {id} = req.params;
const invoiceAttachments: IAttachmentCollection | null = await req.db.collection(CollectionNames.ATTACHMENTS)
.findOne({ _id: new ObjectID(id) as ObjectID });
.findOne({_id: new ObjectID(id) as ObjectID});
if (invoiceAttachments && invoiceAttachments.xml) {
return res.type('application/xml').send(atob(invoiceAttachments.xml.toString()));
} else {
Expand Down
Loading

0 comments on commit 69b4604

Please sign in to comment.