Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #102 from Tolfix/dev
Browse files Browse the repository at this point in the history
v3.1
  • Loading branch information
Tolfx authored Mar 26, 2022
2 parents 8f5444a + b3af9eb commit 6c5c4c5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpg-api",
"version": "v3.0",
"version": "v3.1",
"description": "Central Payment Gateway",
"main": "./build/Main.js",
"dependencies": {
Expand Down
3 changes: 0 additions & 3 deletions src/Cache/Invoices.cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { IInvoice } from "@interface/Invoice.interface";

/**
* @deprecated
*/
export const CacheInvoice = new Map<IInvoice["uid"], IInvoice>();
15 changes: 11 additions & 4 deletions src/Cache/reCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -226,5 +233,5 @@ export async function reCache()
// await reCache_Transactions();
// await reCache_Orders();
await reCache_Images();
// await reCache_Invoices();
await reCache_Invoices();
}
2 changes: 1 addition & 1 deletion src/Lib/Invoices/CreatePDFInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function createPDFInvoice(invoice: IInvoice): Promise<string>
},
"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,
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Quotes/CreateQuotePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
},
"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,
Expand Down
29 changes: 22 additions & 7 deletions src/Middlewares/EnsureAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("=="))
{
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Models/BaseModelAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export default class BaseModelAPI<IModel extends { uid: string }>

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);
});
Expand Down
16 changes: 16 additions & 0 deletions src/Server/Routes/v2/Invoices/Invoices.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}

}
22 changes: 17 additions & 5 deletions test/Orders.rest
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/Quotes.rest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Content-Type: application/json
Authorization: Basic 123:123

{
"customer_uid": 0,
"customer_uid": 1,
"items": {
"name": "item1",
"price": 100,
Expand Down

0 comments on commit 6c5c4c5

Please sign in to comment.