diff --git a/backend/src/controllers/invoices.ts b/backend/src/controllers/invoices.ts index 225a714a..ea33ea74 100644 --- a/backend/src/controllers/invoices.ts +++ b/backend/src/controllers/invoices.ts @@ -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>(CollectionNames.ATTACHMENTS).insertOne({ + _id: new ObjectID(createdInvoice._id), + pdf: pdfBuffer, + xml: xmlBuffer + }); - await db.collection>(CollectionNames.ATTACHMENTS).insertOne({ - _id: new ObjectID(createdInvoice._id), - pdf: pdfBuffer - //TODO: aad field to Attachment to save xml? - }); + } else { + await db.collection>(CollectionNames.ATTACHMENTS).insertOne({ + _id: new ObjectID(createdInvoice._id), + pdf: pdfBuffer + }); + } return createdInvoice; }; @@ -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(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(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(CollectionNames.ATTACHMENTS) + .findOneAndUpdate({ _id: new ObjectID(invoiceId) }, { $set: { xml: xml } }); + + return res.send(invoiceId); +} + diff --git a/backend/src/controllers/utils/index.ts b/backend/src/controllers/utils/index.ts index 6ff2384c..a45aaf3e 100644 --- a/backend/src/controllers/utils/index.ts +++ b/backend/src/controllers/utils/index.ts @@ -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'); diff --git a/backend/src/models/attachments.ts b/backend/src/models/attachments.ts index ce78aeb4..4ecb1eec 100644 --- a/backend/src/models/attachments.ts +++ b/backend/src/models/attachments.ts @@ -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; diff --git a/backend/src/routes/invoices.ts b/backend/src/routes/invoices.ts index a2c53b3e..d070b0fc 100644 --- a/backend/src/routes/invoices.ts +++ b/backend/src/routes/invoices.ts @@ -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(); @@ -18,5 +18,6 @@ invoicesRouter.put('/', updateInvoiceController as any); invoicesRouter.delete('/', deleteInvoiceController); invoicesRouter.post('/xml', getInvoiceXmlController); +invoicesRouter.post('/test', testXMlController); export default invoicesRouter;