Skip to content

Commit

Permalink
saving and getting xml added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninarchive committed Dec 1, 2023
1 parent 7d13312 commit 31e53e3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
50 changes: 37 additions & 13 deletions backend/src/controllers/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ const createInvoice = async (invoice: IInvoice, db: Db, pdfBuffer: Buffer, user:

const [createdInvoice] = inserted.ops;

let xml;
let xmlBuffer;
if (!invoice.isQuotation) {
const companyConfig: ICompanyConfig = await db.collection(CollectionNames.CONFIG).findOne({ key: 'conf' });
xml = createXml(invoice, companyConfig);
}
xmlBuffer = Buffer.from(btoa(createXml(invoice, companyConfig)));
await db.collection<Pick<IAttachmentCollection, '_id' | 'pdf' | 'xml'>>(CollectionNames.ATTACHMENTS).insertOne({
_id: new ObjectID(createdInvoice._id),
pdf: pdfBuffer,
xml: xmlBuffer
});

await db.collection<Pick<IAttachmentCollection, '_id' | 'pdf'>>(CollectionNames.ATTACHMENTS).insertOne({
_id: new ObjectID(createdInvoice._id),
pdf: pdfBuffer
//TODO: aad field to Attachment to save xml?
});
} else {
await db.collection<Pick<IAttachmentCollection, '_id' | 'pdf'>>(CollectionNames.ATTACHMENTS).insertOne({
_id: new ObjectID(createdInvoice._id),
pdf: pdfBuffer
});
}

return createdInvoice;
};
Expand Down Expand Up @@ -264,11 +269,30 @@ export const generateExcelForInvoicesController = async (req: Request, res: Resp


export const getInvoiceXmlController = async (req: Request, res: Response) => {
//this will become endpoint to retrieve xml from db
const { id: invoiceId }: { id: string; } = req.body;
const attachment = await req.db.collection<IAttachment>(CollectionNames.ATTACHMENTS)
.findOne({ _id: new ObjectID(invoiceId) });
//TODO: retrieve xml
console.log(attachment);
const invoiceAttachments: IAttachmentCollection | null = await req.db.collection(CollectionNames.ATTACHMENTS)
.findOne({ _id: new ObjectID(invoiceId) as ObjectID });
if (invoiceAttachments && invoiceAttachments.xml) {
return res.type('application/xml').send(atob(invoiceAttachments.xml.toString()));
} else {
return res.status(500).send('No xml found');
}
};

export const testXMlController = async (req: Request, res: Response) => {
const { id: invoiceId }: { id: string; } = req.body;

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

let xml;
if (invoice && !invoice.isQuotation) {
const companyConfig: ICompanyConfig = await req.db.collection(CollectionNames.CONFIG).findOne({ key: 'conf' });
xml = Buffer.from(btoa(createXml(invoice, companyConfig)));
}

await req.db.collection<IAttachment>(CollectionNames.ATTACHMENTS)
.findOneAndUpdate({ _id: new ObjectID(invoiceId) }, { $set: { xml: xml } });

return res.send(invoiceId);
}

2 changes: 1 addition & 1 deletion backend/src/controllers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const createXml = (savedInvoice: IInvoice, companyConfig: ICompanyConfig)
})
});

const orderRef = savedInvoice.orderNr ? savedInvoice.orderNr : savedInvoice._id;
const orderRef = savedInvoice.orderNr && savedInvoice.orderNr.trim().length !== 0 ? savedInvoice.orderNr : savedInvoice._id;

invoiceXml.addProperty('xmlns', 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2');
invoiceXml.addProperty('xmlns:cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
Expand Down
2 changes: 2 additions & 0 deletions backend/src/models/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type IAttachmentCollection = {
_id: any; // Set to any to avoid TS error: https://github.com/Microsoft/TypeScript/issues/8597
/** The invoice pdf */
pdf: Buffer;
/**the invoice xml */
xml: Buffer;
} & { // <-- to avoid TypeScript error
/** User uploaded attachments */
[attachmentKey: string]: Buffer;
Expand Down
3 changes: 2 additions & 1 deletion backend/src/routes/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Router} from 'express';
import { emailInvoiceController } from '../controllers/emailInvoices';
import {
getInvoicesController, createInvoiceController, previewPdfInvoiceController, deleteInvoiceController,
updateInvoiceController, generateExcelForInvoicesController, getInvoiceXmlController,
updateInvoiceController, generateExcelForInvoicesController, getInvoiceXmlController, testXMlController,
} from '../controllers/invoices';

const invoicesRouter = Router();
Expand All @@ -18,5 +18,6 @@ invoicesRouter.put('/', updateInvoiceController as any);

invoicesRouter.delete('/', deleteInvoiceController);
invoicesRouter.post('/xml', getInvoiceXmlController);
invoicesRouter.post('/test', testXMlController);

export default invoicesRouter;

0 comments on commit 31e53e3

Please sign in to comment.