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

Commit 5f2da15

Browse files
authored
Merge pull request #109 from Tolfix/dev
v3.4
2 parents 6d7045d + 83bcc69 commit 5f2da15

File tree

11 files changed

+397
-30
lines changed

11 files changed

+397
-30
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.3",
3+
"version": "v3.4",
44
"description": "Central Payment Gateway",
55
"main": "./build/Main.js",
66
"dependencies": {
+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
import TransactionsModel from "../../Database/Models/Transactions.model";
2+
import Logger from "../../Lib/Logger";
3+
import inquirer from 'inquirer';
4+
import CustomerModel from "../../Database/Models/Customers/Customer.model";
5+
import InvoiceModel from "../../Database/Models/Invoices.model";
6+
import { A_CC_Payments } from "../../Types/PaymentMethod";
7+
import { currencyCodes } from "../../Lib/Currencies";
8+
import dateFormat from "date-and-time";
9+
import mainEvent from "../../Events/Main.event";
10+
11+
export default
12+
{
13+
name: 'Transactions',
14+
description: 'Get all transactions jobs',
15+
args: [
16+
{
17+
name: 'action',
18+
type: "list",
19+
message: "Select the transaction job you want to run",
20+
choices: [
21+
{
22+
name: 'Show transactions',
23+
value: 'show_transactions',
24+
},
25+
{
26+
name: 'Add transaction',
27+
value: 'add_transaction',
28+
},
29+
{
30+
name: 'Delete transaction',
31+
value: 'delete_transaction',
32+
}
33+
],
34+
}
35+
],
36+
method: async ({action}: {action: string}) =>
37+
{
38+
switch(action)
39+
{
40+
case 'show_transactions':
41+
{
42+
const transaction = await TransactionsModel.find();
43+
Logger.info(`Transactions:`, transaction);
44+
break;
45+
}
46+
case 'add_transaction':
47+
{
48+
const action1 = [
49+
{
50+
name: "statement",
51+
type: "list",
52+
message: "Select the transaction statement",
53+
choices: [
54+
{
55+
name: "Income",
56+
value: "income",
57+
},
58+
{
59+
name: "Expense",
60+
value: "expense",
61+
}
62+
],
63+
}
64+
]
65+
66+
const {statement} = await inquirer.prompt(action1);
67+
const income_action = [
68+
{
69+
name: "customer_uid",
70+
type: 'search-list',
71+
message: 'Customer',
72+
choices: (await CustomerModel.find()).map(e =>
73+
{
74+
return {
75+
name: `${e.personal.first_name} ${e.personal.last_name} (${e.id})`,
76+
value: e.id,
77+
}
78+
})
79+
},
80+
{
81+
name: "invoice_uid",
82+
type: 'search-list',
83+
message: 'Invoice',
84+
choices: (await InvoiceModel.find()).map(e =>
85+
{
86+
return {
87+
name: `#${e.id} (${e.amount} ${e.currency})`,
88+
value: e.id,
89+
}
90+
})
91+
},
92+
{
93+
name: "amount",
94+
type: 'number',
95+
message: 'Amount',
96+
97+
},
98+
{
99+
name: "fees",
100+
type: 'number',
101+
message: 'Fees',
102+
},
103+
{
104+
name: "payment_method",
105+
type: 'search-list',
106+
message: 'Payment method',
107+
choices: A_CC_Payments
108+
},
109+
{
110+
name: 'currency',
111+
type: 'search-list',
112+
message: 'Enter the currency',
113+
choices: currencyCodes
114+
},
115+
{
116+
name: 'date',
117+
type: 'input',
118+
message: 'Enter the date',
119+
default: dateFormat.format(new Date(), 'YYYY-MM-DD'),
120+
}
121+
];
122+
const expense_action = [
123+
{
124+
name: "invoice_id",
125+
type: 'input',
126+
message: 'Invoice ID',
127+
},
128+
{
129+
name: "company",
130+
type: 'input',
131+
message: 'Company',
132+
},
133+
{
134+
name: "amount",
135+
type: 'number',
136+
message: 'Amount',
137+
138+
},
139+
{
140+
name: "fees",
141+
type: 'number',
142+
message: 'Fees',
143+
},
144+
{
145+
name: "payment_method",
146+
type: 'search-list',
147+
message: 'Payment method',
148+
choices: A_CC_Payments
149+
},
150+
{
151+
name: 'currency',
152+
type: 'search-list',
153+
message: 'Enter the currency',
154+
choices: currencyCodes
155+
},
156+
{
157+
name: "description",
158+
type: 'input',
159+
message: 'Description',
160+
},
161+
{
162+
name: "notes",
163+
type: 'input',
164+
message: 'Notes',
165+
},
166+
{
167+
name: 'date',
168+
type: 'input',
169+
message: 'Enter the date',
170+
default: dateFormat.format(new Date(), 'YYYY-MM-DD'),
171+
},
172+
]
173+
174+
const picked_action = statement === 'income' ? income_action : expense_action;
175+
176+
const result = await inquirer.prompt(picked_action);
177+
178+
switch(statement)
179+
{
180+
case 'income':
181+
{
182+
const {customer_uid, invoice_uid, amount, fees, payment_method, currency, date} = result;
183+
const transaction = await (new TransactionsModel({
184+
customer_uid,
185+
invoice_uid,
186+
amount,
187+
fees,
188+
payment_method,
189+
currency,
190+
date,
191+
statement: 'income',
192+
})).save();
193+
Logger.info(`Transaction:`, transaction);
194+
mainEvent.emit('transaction_created', transaction);
195+
break;
196+
}
197+
case 'expense':
198+
{
199+
const {invoice_id, company, amount, fees, payment_method, currency, date, description, notes} = result;
200+
const transaction = await (new TransactionsModel({
201+
statement: 'expense',
202+
expense_information: {
203+
invoice_id,
204+
company,
205+
description,
206+
notes,
207+
},
208+
amount,
209+
fees,
210+
payment_method,
211+
currency,
212+
date,
213+
})).save();
214+
Logger.info(`Transaction:`, transaction);
215+
mainEvent.emit('transaction_created', transaction);
216+
break;
217+
}
218+
}
219+
break;
220+
}
221+
case 'delete_transaction':
222+
{
223+
const transaction = await TransactionsModel.find();
224+
const action1 = [
225+
{
226+
name: "transaction_id",
227+
type: "search-list",
228+
message: "Select the transaction you want to delete",
229+
choices: transaction.map(e =>
230+
{
231+
return {
232+
name: `#${e.id} (${e.amount} ${e.currency})`,
233+
value: e.id,
234+
}
235+
}
236+
)
237+
}
238+
]
239+
const {transaction_id} = await inquirer.prompt(action1);
240+
const transaction_to_delete = await TransactionsModel.findOne({id: transaction_id});
241+
if(!transaction_to_delete)
242+
return Logger.error(`Transaction with id ${transaction_id} not found`);
243+
244+
await transaction_to_delete.remove();
245+
Logger.info(`Transaction deleted:`, transaction_to_delete);
246+
mainEvent.emit('transaction_deleted', transaction_to_delete);
247+
248+
break;
249+
}
250+
}
251+
return true;
252+
}
253+
}

src/Cache/reCache.ts

+7
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,18 @@ export async function reCache_Configs()
172172
await c.save();
173173
}
174174

175+
if(!c.extra)
176+
{
177+
c.extra = {};
178+
await c.save();
179+
}
180+
175181
CacheConfig.set("smtp", c.smtp);
176182
CacheConfig.set("smtp_emails", c.smtp_emails);
177183
CacheConfig.set("payment_methods", c.payment_methods);
178184
CacheConfig.set("company", c.company);
179185
CacheConfig.set("webhooks_urls", c.webhooks_urls);
186+
CacheConfig.set("extra", c.extra);
180187

181188
return resolve(true);
182189
});

src/Config.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@ export const DebugMode = process.env.DEBUG === "true";
1010
export const HomeDir = ((__dirname.replace("\\build", "")).replace("/build", ""));
1111
export const JWT_Access_Token = process.env.JWT_ACCESS_TOKEN ?? "";
1212
export const d_Days = parseInt(process.env.D_DAYS ?? "30");
13-
export const Domain = process.env.DOMAIN ?? "localhost";
14-
export const Http_Schema = process.env.HTTP_SCHEMA ?? "http";
15-
export const PORT = process.env.PORT ?? 8080;
13+
export const Domain = process.env.DOMAIN ||= "localhost";
14+
export const Http_Schema = process.env.HTTP_SCHEMA ||= "http";
15+
export const PORT = process.env.PORT ||= "8080";
1616
export const Full_Domain = `${Http_Schema}://${Domain === "localhost" ? `localhost:${PORT}` : Domain}`;
1717

1818
// API
1919
export const Express_Session_Secret = process.env.SESSION_SECRET ?? require("crypto").randomBytes(20).toString("hex");
2020

2121
// Database
22-
export const MongoDB_URI = process.env.MONGO_URI ?? "mongodb://localhost/cpg";
22+
export const MongoDB_URI = process.env.MONGO_URI ||= "mongodb://localhost/cpg";
2323
// // Postgres
24-
export const Postgres_User = process.env.POSTGRES_USER ?? "";
25-
export const Postgres_Password = process.env.POSTGRES_PASSWORD ?? "";
26-
export const Postgres_Database = process.env.POSTGRES_DATABASE ?? "";
27-
export const Postgres_Port = parseInt(process.env.POSTGRES_PORT ?? "5432");
28-
export const Postgres_Host = process.env.POSTGRES_HOST ?? "localhost";
24+
export const Postgres_User = process.env.POSTGRES_USER ||= "";
25+
export const Postgres_Password = process.env.POSTGRES_PASSWORD ||= "";
26+
export const Postgres_Database = process.env.POSTGRES_DATABASE ||= "";
27+
export const Postgres_Port = parseInt(process.env.POSTGRES_PORT ||= "5432");
28+
export const Postgres_Host = process.env.POSTGRES_HOST ||= "localhost";
2929

3030
// osTicket configs
31-
export const osticket_url = process.env.OSTICKET_URL ?? "";
32-
export const osticket_api_key = process.env.OSTICKET_API_KEY ?? "";
31+
export const osticket_url = process.env.OSTICKET_URL ||= "";
32+
export const osticket_api_key = process.env.OSTICKET_API_KEY ||= "";
3333

3434
// Stripe
35-
export const Stripe_SK_Test = process.env.STRIPE_SK_TEST ?? "";
36-
export const Stripe_SK_Live = process.env.STRIPE_SK_LIVE ?? "";
37-
export const Stripe_PK_Public_Test = process.env.STRIPE_SK_PUBLIC_TEST ?? "";
38-
export const Stripe_PK_Public = process.env.STRIPE_SK_PUBLIC ?? "";
39-
export const Stripe_Webhook_Secret = process.env.STRIPE_WEBHOOK_SECRET ?? "";
35+
export const Stripe_SK_Test = process.env.STRIPE_SK_TEST ||= "";
36+
export const Stripe_SK_Live = process.env.STRIPE_SK_LIVE ||= "";
37+
export const Stripe_PK_Public_Test = process.env.STRIPE_SK_PUBLIC_TEST ||= "";
38+
export const Stripe_PK_Public = process.env.STRIPE_SK_PUBLIC ||= "";
39+
export const Stripe_Webhook_Secret = process.env.STRIPE_WEBHOOK_SECRET ||= "";
4040

4141
// Swish
42-
export const Swish_Payee_Number = process.env.SWISH_PAYEE_NUMBER ?? "";
42+
export const Swish_Payee_Number = process.env.SWISH_PAYEE_NUMBER ||= "";
4343

4444
// Paypal
45-
export const Paypal_Client_Id = process.env.PAYPAL_CLIENT_ID ?? "";
46-
export const Paypal_Client_Secret = process.env.PAYPAL_CLIENT_SECRET ?? "";
45+
export const Paypal_Client_Id = process.env.PAYPAL_CLIENT_ID ||= "";
46+
export const Paypal_Client_Secret = process.env.PAYPAL_CLIENT_SECRET ||= "";
4747

4848
// CPG stuff..
4949
export const CPG_Customer_Panel_Domain = process.env.CPG_CUSTOMER_PANEL_DOMAIN;

src/Database/Models/Configs.model.ts

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const ConfigsSchema = new Schema
3030
default: [],
3131
},
3232

33+
extra: {
34+
type: Object,
35+
default: {},
36+
}
37+
3338
}
3439
);
3540

src/Database/Models/Transactions.model.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const TransactionsSchema = new Schema
1919

2020
customer_uid: {
2121
type: Number || String,
22-
required: true
22+
required: false
2323
},
2424

2525
invoice_uid: {
@@ -60,6 +60,32 @@ const TransactionsSchema = new Schema
6060
default: 'income',
6161
},
6262

63+
expense_information: {
64+
type: {
65+
invoice_id: {
66+
type: Number,
67+
required: false,
68+
},
69+
company: {
70+
type: String,
71+
required: false,
72+
},
73+
description: {
74+
type: String,
75+
required: false,
76+
},
77+
notes: {
78+
type: String,
79+
required: false,
80+
},
81+
extra: {
82+
type: Schema.Types.Mixed,
83+
required: false,
84+
},
85+
},
86+
required: false,
87+
}
88+
6389
},
6490
{
6591
timestamps: true,

0 commit comments

Comments
 (0)