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

Commit 6c5c4c5

Browse files
authored
Merge pull request #102 from Tolfix/dev
v3.1
2 parents 8f5444a + b3af9eb commit 6c5c4c5

File tree

10 files changed

+77
-24
lines changed

10 files changed

+77
-24
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cpg-api",
3-
"version": "v3.0",
3+
"version": "v3.1",
44
"description": "Central Payment Gateway",
55
"main": "./build/Main.js",
66
"dependencies": {

src/Cache/Invoices.cache.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
import { IInvoice } from "@interface/Invoice.interface";
22

3-
/**
4-
* @deprecated
5-
*/
63
export const CacheInvoice = new Map<IInvoice["uid"], IInvoice>();

src/Cache/reCache.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,17 @@ export async function reCache_Images()
207207
return new Promise(async (resolve) =>
208208
{
209209
const invoice = await InvoiceModel.find();
210-
for (const o of invoice)
210+
for await(const o of invoice)
211211
{
212-
Logger.cache(`Caching invoice ${o.uid}`);
213-
CacheInvoice.set(o.uid, o);
212+
// check if invoice has currency
213+
if(!o.currency)
214+
{
215+
const companyCurrency = await Company_Currency();
216+
o.currency = companyCurrency.toLocaleUpperCase() as TPaymentCurrency;
217+
await o.save();
218+
}
219+
Logger.cache(`Caching invoice ${o.uid}`);
220+
CacheInvoice.set(o.uid, o);
214221
}
215222
return resolve(true);
216223
});
@@ -226,5 +233,5 @@ export async function reCache()
226233
// await reCache_Transactions();
227234
// await reCache_Orders();
228235
await reCache_Images();
229-
// await reCache_Invoices();
236+
await reCache_Invoices();
230237
}

src/Lib/Invoices/CreatePDFInvoice.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function createPDFInvoice(invoice: IInvoice): Promise<string>
5353
},
5454
"taxNotation": "vat",
5555
"settings": {
56-
"currency": (!Customer.currency ? await Company_Currency() : Customer.currency).toUpperCase(),
56+
"currency": (!invoice.currency ? await Company_Currency() : invoice.currency).toUpperCase(),
5757
"margin-top": 25,
5858
"margin-right": 25,
5959
"margin-left": 25,

src/Lib/Quotes/CreateQuotePdf.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
4242
},
4343
"taxNotation": "vat",
4444
"settings": {
45-
"currency": (!Customer.currency ? await Company_Currency() : Customer.currency).toUpperCase(),
45+
"currency": (!quote.currency ? await Company_Currency() : quote.currency).toUpperCase(),
4646
"margin-top": 25,
4747
"margin-right": 25,
4848
"margin-left": 25,

src/Middlewares/EnsureAdmin.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,32 @@ export default function EnsureAdmin(eR = false)
1212
{
1313

1414
const authHeader = req.headers['authorization'];
15-
if(!authHeader)
16-
return eR ? Promise.resolve(false) : APIError("Missing 'authorization' in header")(res);
15+
const tokenQuery = req.query.access_token;
16+
if(!authHeader && !tokenQuery)
17+
return eR ? Promise.resolve(false) : APIError({
18+
text: "Missing 'authorization' in header"
19+
})(res);
1720

18-
const b64auth = (authHeader).split(' ');
21+
let b64auth: string[];
22+
if(authHeader)
23+
b64auth = authHeader.split(' ');
1924

20-
if(!b64auth[0].toLocaleLowerCase().match(/basic|bearer/g))
21-
return eR ? Promise.resolve(false) : APIError("Missing 'basic' or 'bearer' in authorization")(res);
25+
if(tokenQuery)
26+
b64auth = ["query", tokenQuery as string];
2227

28+
// @ts-ignore
29+
if(!b64auth[0].toLocaleLowerCase().match(/basic|bearer|query/g))
30+
return eR ? Promise.resolve(false) : APIError("Missing 'basic' or 'bearer' in authorization")(res);
31+
32+
// @ts-ignore
2333
if(!b64auth[1])
2434
return eR ? Promise.resolve(false) : APIError("Missing 'buffer' in authorization")(res);
25-
35+
36+
// @ts-ignore
2637
if(b64auth[0].toLocaleLowerCase() === "basic")
2738
{
2839
// Check if buffer, or base64
40+
// @ts-ignore
2941
let [login, password] = (Buffer.isBuffer(b64auth[1]) ? Buffer.from(b64auth[1], 'base64') : b64auth[1]).toString().split(':');
3042
if(login.includes("==") || password.includes("=="))
3143
{
@@ -52,9 +64,12 @@ export default function EnsureAdmin(eR = false)
5264
return eR ? Promise.resolve(true) : next?.();
5365
}
5466

55-
if(b64auth[0].toLocaleLowerCase() === "bearer")
67+
// @ts-ignore
68+
if(b64auth[0].toLocaleLowerCase() === "bearer" || b64auth[0].toLocaleLowerCase() === "query")
5669
{
70+
// @ts-ignore
5771
const token = (Buffer.isBuffer(b64auth[1]) ? Buffer.from(b64auth[1], 'base64') : b64auth[1]).toString();
72+
5873
!eR ? Logger.warning(`Authoring admin with token: ${token}`) : null;
5974

6075
try

src/Models/BaseModelAPI.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ export default class BaseModelAPI<IModel extends { uid: string }>
5858

5959
res.setHeader("X-Total-Pages", result.totalPages);
6060
res.setHeader("X-Total", result.totalCount);
61-
61+
if(query["include_x"])
62+
return resolve({
63+
// @ts-ignore
64+
data: r,
65+
totalPages: result.totalPages,
66+
totalCount: result.totalCount
67+
})
6268
resolve(r);
6369
}).catch(reject);
6470
});

src/Server/Routes/v2/Invoices/Invoices.config.ts

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ class InvoiceRouter
7171

7272
res.end(result, "base64");
7373
});
74+
75+
this.router.get("/:uid/preview", EnsureAdmin(), async (req, res) =>
76+
{
77+
const invoice = await InvoiceModel.findOne({ id: req.params.uid });
78+
79+
if(!invoice)
80+
return res.status(404).send("Invoice not found");
81+
82+
const result = await createPDFInvoice(invoice);
83+
84+
res.writeHead(200, {
85+
'Content-Type': "application/pdf",
86+
});
87+
88+
res.end(result, "base64");
89+
});
7490
}
7591

7692
}

test/Orders.rest

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
POST http://localhost:8080/v2/orders/place
22
Content-Type: application/json
3-
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo1LCJlbWFpbCI6ImpvaG5AZG9lLmNvbSJ9LCJleHAiOjE2MzgzNzcwODIsImlhdCI6MTYzODI5MDY4Mn0.ZkrdxAMKi1Jh5cDMdC8ZY40_id1PVNptaJidGP7iLTE
3+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoxLCJlbWFpbCI6InRlc3RAdG9sZml4LmNvbSJ9LCJleHAiOjE2NDc0Mzc0MjYsImlhdCI6MTY0NzM1MTAyNn0.6U6SUhsq76Ad0uDjm9Z3XbiKDY-YXZ320i6WRcvGnJA
4+
5+
{
6+
"payment_method": "credit_card",
7+
"products": [
8+
{
9+
"product_id": 0,
10+
"quantity": 1
11+
}
12+
]
13+
}
14+
15+
###
16+
17+
POST http://localhost:8080/v2/orders/place
18+
Content-Type: application/json
19+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoxLCJlbWFpbCI6InRlc3RAdG9sZml4LmNvbSJ9LCJleHAiOjE2NDcxOTE3NzksImlhdCI6MTY0NzEwNTM3OX0.8YmWtcAXgddGCvqPSk0O-Cfu5ceaOAx8uexiBPMkgj4
420

521
{
622
"payment_method": "credit_card",
@@ -9,10 +25,6 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo1L
925
"product_id": 3,
1026
"quantity": 1,
1127
"configurable_options": [
12-
{
13-
"id": 1,
14-
"option_index": 1
15-
},
1628
{
1729
"id": 1,
1830
"option_index": 1

test/Quotes.rest

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Content-Type: application/json
33
Authorization: Basic 123:123
44

55
{
6-
"customer_uid": 0,
6+
"customer_uid": 1,
77
"items": {
88
"name": "item1",
99
"price": 100,

0 commit comments

Comments
 (0)