diff --git a/package.json b/package.json index 98fce936..c97c5346 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cpg-api", - "version": "v3.0", + "version": "v3.1", "description": "Central Payment Gateway", "main": "./build/Main.js", "dependencies": { diff --git a/src/Cache/Invoices.cache.ts b/src/Cache/Invoices.cache.ts index 02379c56..0caada6b 100644 --- a/src/Cache/Invoices.cache.ts +++ b/src/Cache/Invoices.cache.ts @@ -1,6 +1,3 @@ import { IInvoice } from "@interface/Invoice.interface"; -/** - * @deprecated - */ export const CacheInvoice = new Map(); \ No newline at end of file diff --git a/src/Cache/reCache.ts b/src/Cache/reCache.ts index d1a15a09..575a42c4 100644 --- a/src/Cache/reCache.ts +++ b/src/Cache/reCache.ts @@ -207,10 +207,17 @@ export async function reCache_Images() return new Promise(async (resolve) => { const invoice = await InvoiceModel.find(); - for (const o of invoice) + for await(const o of invoice) { - Logger.cache(`Caching invoice ${o.uid}`); - CacheInvoice.set(o.uid, o); + // check if invoice has currency + if(!o.currency) + { + const companyCurrency = await Company_Currency(); + o.currency = companyCurrency.toLocaleUpperCase() as TPaymentCurrency; + await o.save(); + } + Logger.cache(`Caching invoice ${o.uid}`); + CacheInvoice.set(o.uid, o); } return resolve(true); }); @@ -226,5 +233,5 @@ export async function reCache() // await reCache_Transactions(); // await reCache_Orders(); await reCache_Images(); - // await reCache_Invoices(); + await reCache_Invoices(); } \ No newline at end of file diff --git a/src/Lib/Invoices/CreatePDFInvoice.ts b/src/Lib/Invoices/CreatePDFInvoice.ts index fc6f622f..deafff10 100644 --- a/src/Lib/Invoices/CreatePDFInvoice.ts +++ b/src/Lib/Invoices/CreatePDFInvoice.ts @@ -53,7 +53,7 @@ export default function createPDFInvoice(invoice: IInvoice): Promise }, "taxNotation": "vat", "settings": { - "currency": (!Customer.currency ? await Company_Currency() : Customer.currency).toUpperCase(), + "currency": (!invoice.currency ? await Company_Currency() : invoice.currency).toUpperCase(), "margin-top": 25, "margin-right": 25, "margin-left": 25, diff --git a/src/Lib/Quotes/CreateQuotePdf.ts b/src/Lib/Quotes/CreateQuotePdf.ts index 126b32c8..4f59dc80 100644 --- a/src/Lib/Quotes/CreateQuotePdf.ts +++ b/src/Lib/Quotes/CreateQuotePdf.ts @@ -42,7 +42,7 @@ export default function createQuotePdf(quote: IQuotes): Promise }, "taxNotation": "vat", "settings": { - "currency": (!Customer.currency ? await Company_Currency() : Customer.currency).toUpperCase(), + "currency": (!quote.currency ? await Company_Currency() : quote.currency).toUpperCase(), "margin-top": 25, "margin-right": 25, "margin-left": 25, diff --git a/src/Middlewares/EnsureAdmin.ts b/src/Middlewares/EnsureAdmin.ts index 2e10ddff..e02f77a7 100644 --- a/src/Middlewares/EnsureAdmin.ts +++ b/src/Middlewares/EnsureAdmin.ts @@ -12,20 +12,32 @@ export default function EnsureAdmin(eR = false) { const authHeader = req.headers['authorization']; - if(!authHeader) - return eR ? Promise.resolve(false) : APIError("Missing 'authorization' in header")(res); + const tokenQuery = req.query.access_token; + if(!authHeader && !tokenQuery) + return eR ? Promise.resolve(false) : APIError({ + text: "Missing 'authorization' in header" + })(res); - const b64auth = (authHeader).split(' '); + let b64auth: string[]; + if(authHeader) + b64auth = authHeader.split(' '); - if(!b64auth[0].toLocaleLowerCase().match(/basic|bearer/g)) - return eR ? Promise.resolve(false) : APIError("Missing 'basic' or 'bearer' in authorization")(res); + if(tokenQuery) + b64auth = ["query", tokenQuery as string]; + // @ts-ignore + if(!b64auth[0].toLocaleLowerCase().match(/basic|bearer|query/g)) + return eR ? Promise.resolve(false) : APIError("Missing 'basic' or 'bearer' in authorization")(res); + + // @ts-ignore if(!b64auth[1]) return eR ? Promise.resolve(false) : APIError("Missing 'buffer' in authorization")(res); - + + // @ts-ignore if(b64auth[0].toLocaleLowerCase() === "basic") { // Check if buffer, or base64 + // @ts-ignore let [login, password] = (Buffer.isBuffer(b64auth[1]) ? Buffer.from(b64auth[1], 'base64') : b64auth[1]).toString().split(':'); if(login.includes("==") || password.includes("==")) { @@ -52,9 +64,12 @@ export default function EnsureAdmin(eR = false) return eR ? Promise.resolve(true) : next?.(); } - if(b64auth[0].toLocaleLowerCase() === "bearer") + // @ts-ignore + if(b64auth[0].toLocaleLowerCase() === "bearer" || b64auth[0].toLocaleLowerCase() === "query") { + // @ts-ignore const token = (Buffer.isBuffer(b64auth[1]) ? Buffer.from(b64auth[1], 'base64') : b64auth[1]).toString(); + !eR ? Logger.warning(`Authoring admin with token: ${token}`) : null; try diff --git a/src/Models/BaseModelAPI.ts b/src/Models/BaseModelAPI.ts index c67115ad..fde3cff3 100644 --- a/src/Models/BaseModelAPI.ts +++ b/src/Models/BaseModelAPI.ts @@ -58,7 +58,13 @@ export default class BaseModelAPI res.setHeader("X-Total-Pages", result.totalPages); res.setHeader("X-Total", result.totalCount); - + if(query["include_x"]) + return resolve({ + // @ts-ignore + data: r, + totalPages: result.totalPages, + totalCount: result.totalCount + }) resolve(r); }).catch(reject); }); diff --git a/src/Server/Routes/v2/Invoices/Invoices.config.ts b/src/Server/Routes/v2/Invoices/Invoices.config.ts index 2b3d6761..a6692065 100644 --- a/src/Server/Routes/v2/Invoices/Invoices.config.ts +++ b/src/Server/Routes/v2/Invoices/Invoices.config.ts @@ -71,6 +71,22 @@ class InvoiceRouter res.end(result, "base64"); }); + + this.router.get("/:uid/preview", EnsureAdmin(), async (req, res) => + { + const invoice = await InvoiceModel.findOne({ id: req.params.uid }); + + if(!invoice) + return res.status(404).send("Invoice not found"); + + const result = await createPDFInvoice(invoice); + + res.writeHead(200, { + 'Content-Type': "application/pdf", + }); + + res.end(result, "base64"); + }); } } \ No newline at end of file diff --git a/test/Orders.rest b/test/Orders.rest index d139bcd6..838f8c70 100644 --- a/test/Orders.rest +++ b/test/Orders.rest @@ -1,6 +1,22 @@ POST http://localhost:8080/v2/orders/place Content-Type: application/json -Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo1LCJlbWFpbCI6ImpvaG5AZG9lLmNvbSJ9LCJleHAiOjE2MzgzNzcwODIsImlhdCI6MTYzODI5MDY4Mn0.ZkrdxAMKi1Jh5cDMdC8ZY40_id1PVNptaJidGP7iLTE +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoxLCJlbWFpbCI6InRlc3RAdG9sZml4LmNvbSJ9LCJleHAiOjE2NDc0Mzc0MjYsImlhdCI6MTY0NzM1MTAyNn0.6U6SUhsq76Ad0uDjm9Z3XbiKDY-YXZ320i6WRcvGnJA + +{ + "payment_method": "credit_card", + "products": [ + { + "product_id": 0, + "quantity": 1 + } + ] +} + +### + +POST http://localhost:8080/v2/orders/place +Content-Type: application/json +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoxLCJlbWFpbCI6InRlc3RAdG9sZml4LmNvbSJ9LCJleHAiOjE2NDcxOTE3NzksImlhdCI6MTY0NzEwNTM3OX0.8YmWtcAXgddGCvqPSk0O-Cfu5ceaOAx8uexiBPMkgj4 { "payment_method": "credit_card", @@ -9,10 +25,6 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo1L "product_id": 3, "quantity": 1, "configurable_options": [ - { - "id": 1, - "option_index": 1 - }, { "id": 1, "option_index": 1 diff --git a/test/Quotes.rest b/test/Quotes.rest index 44b8e36f..742657b4 100644 --- a/test/Quotes.rest +++ b/test/Quotes.rest @@ -3,7 +3,7 @@ Content-Type: application/json Authorization: Basic 123:123 { - "customer_uid": 0, + "customer_uid": 1, "items": { "name": "item1", "price": 100,