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 #109 from Tolfix/dev
Browse files Browse the repository at this point in the history
v3.4
  • Loading branch information
Tolfx authored Apr 20, 2022
2 parents 6d7045d + 83bcc69 commit 5f2da15
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 30 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.3",
"version": "v3.4",
"description": "Central Payment Gateway",
"main": "./build/Main.js",
"dependencies": {
Expand Down
253 changes: 253 additions & 0 deletions src/Admin/Commands/Transaction.prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import TransactionsModel from "../../Database/Models/Transactions.model";
import Logger from "../../Lib/Logger";
import inquirer from 'inquirer';
import CustomerModel from "../../Database/Models/Customers/Customer.model";
import InvoiceModel from "../../Database/Models/Invoices.model";
import { A_CC_Payments } from "../../Types/PaymentMethod";
import { currencyCodes } from "../../Lib/Currencies";
import dateFormat from "date-and-time";
import mainEvent from "../../Events/Main.event";

export default
{
name: 'Transactions',
description: 'Get all transactions jobs',
args: [
{
name: 'action',
type: "list",
message: "Select the transaction job you want to run",
choices: [
{
name: 'Show transactions',
value: 'show_transactions',
},
{
name: 'Add transaction',
value: 'add_transaction',
},
{
name: 'Delete transaction',
value: 'delete_transaction',
}
],
}
],
method: async ({action}: {action: string}) =>
{
switch(action)
{
case 'show_transactions':
{
const transaction = await TransactionsModel.find();
Logger.info(`Transactions:`, transaction);
break;
}
case 'add_transaction':
{
const action1 = [
{
name: "statement",
type: "list",
message: "Select the transaction statement",
choices: [
{
name: "Income",
value: "income",
},
{
name: "Expense",
value: "expense",
}
],
}
]

const {statement} = await inquirer.prompt(action1);
const income_action = [
{
name: "customer_uid",
type: 'search-list',
message: 'Customer',
choices: (await CustomerModel.find()).map(e =>
{
return {
name: `${e.personal.first_name} ${e.personal.last_name} (${e.id})`,
value: e.id,
}
})
},
{
name: "invoice_uid",
type: 'search-list',
message: 'Invoice',
choices: (await InvoiceModel.find()).map(e =>
{
return {
name: `#${e.id} (${e.amount} ${e.currency})`,
value: e.id,
}
})
},
{
name: "amount",
type: 'number',
message: 'Amount',

},
{
name: "fees",
type: 'number',
message: 'Fees',
},
{
name: "payment_method",
type: 'search-list',
message: 'Payment method',
choices: A_CC_Payments
},
{
name: 'currency',
type: 'search-list',
message: 'Enter the currency',
choices: currencyCodes
},
{
name: 'date',
type: 'input',
message: 'Enter the date',
default: dateFormat.format(new Date(), 'YYYY-MM-DD'),
}
];
const expense_action = [
{
name: "invoice_id",
type: 'input',
message: 'Invoice ID',
},
{
name: "company",
type: 'input',
message: 'Company',
},
{
name: "amount",
type: 'number',
message: 'Amount',

},
{
name: "fees",
type: 'number',
message: 'Fees',
},
{
name: "payment_method",
type: 'search-list',
message: 'Payment method',
choices: A_CC_Payments
},
{
name: 'currency',
type: 'search-list',
message: 'Enter the currency',
choices: currencyCodes
},
{
name: "description",
type: 'input',
message: 'Description',
},
{
name: "notes",
type: 'input',
message: 'Notes',
},
{
name: 'date',
type: 'input',
message: 'Enter the date',
default: dateFormat.format(new Date(), 'YYYY-MM-DD'),
},
]

const picked_action = statement === 'income' ? income_action : expense_action;

const result = await inquirer.prompt(picked_action);

switch(statement)
{
case 'income':
{
const {customer_uid, invoice_uid, amount, fees, payment_method, currency, date} = result;
const transaction = await (new TransactionsModel({
customer_uid,
invoice_uid,
amount,
fees,
payment_method,
currency,
date,
statement: 'income',
})).save();
Logger.info(`Transaction:`, transaction);
mainEvent.emit('transaction_created', transaction);
break;
}
case 'expense':
{
const {invoice_id, company, amount, fees, payment_method, currency, date, description, notes} = result;
const transaction = await (new TransactionsModel({
statement: 'expense',
expense_information: {
invoice_id,
company,
description,
notes,
},
amount,
fees,
payment_method,
currency,
date,
})).save();
Logger.info(`Transaction:`, transaction);
mainEvent.emit('transaction_created', transaction);
break;
}
}
break;
}
case 'delete_transaction':
{
const transaction = await TransactionsModel.find();
const action1 = [
{
name: "transaction_id",
type: "search-list",
message: "Select the transaction you want to delete",
choices: transaction.map(e =>
{
return {
name: `#${e.id} (${e.amount} ${e.currency})`,
value: e.id,
}
}
)
}
]
const {transaction_id} = await inquirer.prompt(action1);
const transaction_to_delete = await TransactionsModel.findOne({id: transaction_id});
if(!transaction_to_delete)
return Logger.error(`Transaction with id ${transaction_id} not found`);

await transaction_to_delete.remove();
Logger.info(`Transaction deleted:`, transaction_to_delete);
mainEvent.emit('transaction_deleted', transaction_to_delete);

break;
}
}
return true;
}
}
7 changes: 7 additions & 0 deletions src/Cache/reCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,18 @@ export async function reCache_Configs()
await c.save();
}

if(!c.extra)
{
c.extra = {};
await c.save();
}

CacheConfig.set("smtp", c.smtp);
CacheConfig.set("smtp_emails", c.smtp_emails);
CacheConfig.set("payment_methods", c.payment_methods);
CacheConfig.set("company", c.company);
CacheConfig.set("webhooks_urls", c.webhooks_urls);
CacheConfig.set("extra", c.extra);

return resolve(true);
});
Expand Down
38 changes: 19 additions & 19 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ export const DebugMode = process.env.DEBUG === "true";
export const HomeDir = ((__dirname.replace("\\build", "")).replace("/build", ""));
export const JWT_Access_Token = process.env.JWT_ACCESS_TOKEN ?? "";
export const d_Days = parseInt(process.env.D_DAYS ?? "30");
export const Domain = process.env.DOMAIN ?? "localhost";
export const Http_Schema = process.env.HTTP_SCHEMA ?? "http";
export const PORT = process.env.PORT ?? 8080;
export const Domain = process.env.DOMAIN ||= "localhost";
export const Http_Schema = process.env.HTTP_SCHEMA ||= "http";
export const PORT = process.env.PORT ||= "8080";
export const Full_Domain = `${Http_Schema}://${Domain === "localhost" ? `localhost:${PORT}` : Domain}`;

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

// Database
export const MongoDB_URI = process.env.MONGO_URI ?? "mongodb://localhost/cpg";
export const MongoDB_URI = process.env.MONGO_URI ||= "mongodb://localhost/cpg";
// // Postgres
export const Postgres_User = process.env.POSTGRES_USER ?? "";
export const Postgres_Password = process.env.POSTGRES_PASSWORD ?? "";
export const Postgres_Database = process.env.POSTGRES_DATABASE ?? "";
export const Postgres_Port = parseInt(process.env.POSTGRES_PORT ?? "5432");
export const Postgres_Host = process.env.POSTGRES_HOST ?? "localhost";
export const Postgres_User = process.env.POSTGRES_USER ||= "";
export const Postgres_Password = process.env.POSTGRES_PASSWORD ||= "";
export const Postgres_Database = process.env.POSTGRES_DATABASE ||= "";
export const Postgres_Port = parseInt(process.env.POSTGRES_PORT ||= "5432");
export const Postgres_Host = process.env.POSTGRES_HOST ||= "localhost";

// osTicket configs
export const osticket_url = process.env.OSTICKET_URL ?? "";
export const osticket_api_key = process.env.OSTICKET_API_KEY ?? "";
export const osticket_url = process.env.OSTICKET_URL ||= "";
export const osticket_api_key = process.env.OSTICKET_API_KEY ||= "";

// Stripe
export const Stripe_SK_Test = process.env.STRIPE_SK_TEST ?? "";
export const Stripe_SK_Live = process.env.STRIPE_SK_LIVE ?? "";
export const Stripe_PK_Public_Test = process.env.STRIPE_SK_PUBLIC_TEST ?? "";
export const Stripe_PK_Public = process.env.STRIPE_SK_PUBLIC ?? "";
export const Stripe_Webhook_Secret = process.env.STRIPE_WEBHOOK_SECRET ?? "";
export const Stripe_SK_Test = process.env.STRIPE_SK_TEST ||= "";
export const Stripe_SK_Live = process.env.STRIPE_SK_LIVE ||= "";
export const Stripe_PK_Public_Test = process.env.STRIPE_SK_PUBLIC_TEST ||= "";
export const Stripe_PK_Public = process.env.STRIPE_SK_PUBLIC ||= "";
export const Stripe_Webhook_Secret = process.env.STRIPE_WEBHOOK_SECRET ||= "";

// Swish
export const Swish_Payee_Number = process.env.SWISH_PAYEE_NUMBER ?? "";
export const Swish_Payee_Number = process.env.SWISH_PAYEE_NUMBER ||= "";

// Paypal
export const Paypal_Client_Id = process.env.PAYPAL_CLIENT_ID ?? "";
export const Paypal_Client_Secret = process.env.PAYPAL_CLIENT_SECRET ?? "";
export const Paypal_Client_Id = process.env.PAYPAL_CLIENT_ID ||= "";
export const Paypal_Client_Secret = process.env.PAYPAL_CLIENT_SECRET ||= "";

// CPG stuff..
export const CPG_Customer_Panel_Domain = process.env.CPG_CUSTOMER_PANEL_DOMAIN;
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Models/Configs.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const ConfigsSchema = new Schema
default: [],
},

extra: {
type: Object,
default: {},
}

}
);

Expand Down
28 changes: 27 additions & 1 deletion src/Database/Models/Transactions.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const TransactionsSchema = new Schema

customer_uid: {
type: Number || String,
required: true
required: false
},

invoice_uid: {
Expand Down Expand Up @@ -60,6 +60,32 @@ const TransactionsSchema = new Schema
default: 'income',
},

expense_information: {
type: {
invoice_id: {
type: Number,
required: false,
},
company: {
type: String,
required: false,
},
description: {
type: String,
required: false,
},
notes: {
type: String,
required: false,
},
extra: {
type: Schema.Types.Mixed,
required: false,
},
},
required: false,
}

},
{
timestamps: true,
Expand Down
Loading

0 comments on commit 5f2da15

Please sign in to comment.