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 #106 from Tolfix/dev
Browse files Browse the repository at this point in the history
v3.2
  • Loading branch information
Tolfx authored Apr 15, 2022
2 parents 1ef8515 + 54671b0 commit c96fcec
Show file tree
Hide file tree
Showing 24 changed files with 841 additions and 325 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpg-api",
"version": "v3.1",
"version": "v3.2",
"description": "Central Payment Gateway",
"main": "./build/Main.js",
"dependencies": {
Expand Down Expand Up @@ -39,6 +39,8 @@
"graphql-compose-mongoose": "^9.7.1",
"graphql-resolvers": "^0.4.2",
"inquirer": "^8.2.2",
"inquirer-search-checkbox": "^1.0.0",
"inquirer-search-list": "^1.2.6",
"jimp": "^0.16.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.1.4",
Expand All @@ -48,6 +50,7 @@
"nodemailer": "^6.7.0",
"npm": "^7.20.5",
"paypal-rest-sdk": "^1.8.1",
"pdfkit-table": "^0.1.86",
"pg": "^8.7.1",
"pg-hstore": "^2.3.4",
"prompt": "^1.2.0",
Expand Down
192 changes: 4 additions & 188 deletions src/Admin/AdminHandler.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { stripIndent } from "common-tags";
import prompt from "prompt";
import { CacheAdmin } from "../Cache/Admin.cache";
import { cron_chargeStripePayment, cron_notifyInvoices, cron_notifyLateInvoicePaid } from "../Cron/Methods/Invoices.cron.methods";
import AdminModel from "../Database/Models/Administrators.model";
import ConfigModel from "../Database/Models/Configs.model";
import Logger from "../Lib/Logger";
import { getPlugins, installPlugin } from "../Plugins/PluginHandler";
import createAdmin from "./CreateAdmin";
import chalk from "chalk";
import clear from "clear";
import figlet from "figlet";
import Prompt, { cacheCommands } from "./Commands/Prompt";
import inquirer from 'inquirer';

export interface ICommandsAdmin
{
[key: string]: {
description: string;
method: any;
[key: string]: any;
}
}

export default class AdminHandler
{

Expand All @@ -35,6 +18,8 @@ export default class AdminHandler
})
)
)
inquirer.registerPrompt('search-list', require('inquirer-search-list'));
inquirer.registerPrompt('search-checkbox', require('inquirer-search-checkbox'));
this.action();
}

Expand Down Expand Up @@ -67,178 +52,9 @@ export default class AdminHandler
}
}).catch((error) =>
{
console.log(error)
});
}


private async show_emails()
{
return new Promise(async (resolve,) =>
{
// Get our config from database
const config = (await ConfigModel.find())[0];
Logger.info(`Emails:`, config.smtp_emails);
resolve(true);
});
}

private async add_email()
{
return new Promise((resolve) =>
{
prompt.get([
{
name: "email",
description: "Email for administrator",
required: true
},
], async (err, result) =>
{
const email = result.email as string;
Logger.info(`Adding email..`);
// Check if email is valid
// eslint-disable-next-line no-useless-escape
if(!email.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/))
{
Logger.error(`Invalid email`);
return resolve(false);
}
// Get our config from database
const config = (await ConfigModel.find())[0];

config.smtp_emails.push(email);

// Save our config
await config.save();
return resolve(true)
});
});
}

private async delete_email()
{
return new Promise((resolve) =>
{
prompt.get([
{
name: "email",
description: "Email for administrator",
required: true
},
], async (err, result) =>
{
const email = result.email as string;
Logger.info(`Deleting email..`);
// Get our config from database
const config = (await ConfigModel.find())[0];

// Remove email
config.smtp_emails = config.smtp_emails.filter(e => e !== email);

// Save our config
await config.save();
return resolve(true)
});
Logger.error(error);
this.action();
});
}


private async add_webhook()
{
return new Promise((resolve) =>
{
prompt.get([
{
name: "url",
description: "URL for webhook",
required: true
},
], async (err, result) =>
{
const url = result.url as string;
Logger.info(`Adding webhook..`);
// Get our config from database
const config = (await ConfigModel.find())[0];

// Add webhook
config.webhooks_urls.push(url);

// Save our config
await config.save();
return resolve(true)
});
});
}

private async delete_webhook()
{
return new Promise((resolve) =>
{
prompt.get([
{
name: "url",
description: "URL for webhook",
required: true
},
], async (err, result) =>
{
const url = result.url as string;
Logger.info(`Deleting webhook..`);
// Get our config from database
const config = (await ConfigModel.find())[0];

// Remove webhook
config.webhooks_urls = config.webhooks_urls.filter((e: any) => e !== url);

// Save our config
await config.save();
return resolve(true)
});
});
}

private async show_webhooks()
{
return new Promise(async (resolve) =>
{
// Get our config from database
const config = (await ConfigModel.find())[0];
Logger.info(`Webhooks:`, config.webhooks_urls);
resolve(true);
});
}

private async show_plugins()
{
return new Promise(async (resolve) =>
{
// Get our config from database
const plugins = getPlugins()
Logger.info(`Plugins:`, ...plugins);
resolve(true);
});
}

private async update_plugin()
{
return new Promise(async (resolve) =>
{
prompt.get([
{
name: "plugin",
description: "Plugin",
required: false,
type: "string",
},
], async (err, result) =>
{
Logger.info(`Updating plugins..`);
const plugin = result.plugin as string;
if(plugin)
await installPlugin(`${plugin}@latest`);
return resolve(true)
});
});
}
}
3 changes: 0 additions & 3 deletions src/Admin/Commands/Admin.prompt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* eslint-disable no-case-declarations */
import Logger from "../../Lib/Logger";
import prompt from "prompt";
import { CacheConfig } from "../../Cache/Configs.cache";
import ConfigModel from "../../Database/Models/Configs.model";
import updateSMTP from "../updateSMTP";
import AdminModel from "../../Database/Models/Administrators.model";
import createAdmin from "../CreateAdmin";

Expand Down
3 changes: 2 additions & 1 deletion src/Admin/Commands/Company.prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Logger from "../../Lib/Logger";
import prompt from "prompt";
import { CacheConfig } from "../../Cache/Configs.cache";
import ConfigModel from "../../Database/Models/Configs.model";
import { TPaymentCurrency } from "../../Lib/Currencies";

export default
{
Expand Down Expand Up @@ -133,7 +134,7 @@ export default
email: result.email as string,
logo_url: result.logo_url as string,
tax_registered: result.tax_registered === "true",
currency: result.currency as string,
currency: result.currency as TPaymentCurrency,
website: result.website as string
};

Expand Down
102 changes: 102 additions & 0 deletions src/Admin/Commands/Email.prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* eslint-disable no-case-declarations */
import Logger from "../../Lib/Logger";
import prompt from "prompt";
import ConfigModel from "../../Database/Models/Configs.model";

export default
{
name: 'Emails',
description: 'Get all email jobs',
args: [
{
name: 'action',
type: "list",
message: "Select the email job you want to run",
choices: [
{
name: 'Show emails',
value: 'show_emails',
},
{
name: 'Add email',
value: 'add_email',
},
{
name: 'Delete email',
value: 'delete_email',
}
],
}
],
method: async ({action}: {action: string}) =>
{
switch(action)
{
case 'show_emails':
return new Promise(async (resolve,) =>
{
// Get our config from database
const config = (await ConfigModel.find())[0];
Logger.info(`Emails:`, config.smtp_emails);
resolve(true);
});

case 'add_email':
return new Promise((resolve) =>
{
prompt.get([
{
name: "email",
description: "Email for administrator",
required: true
},
], async (err, result) =>
{
const email = result.email as string;
Logger.info(`Adding email..`);
// Check if email is valid
// eslint-disable-next-line no-useless-escape
if(!email.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/))
{
Logger.error(`Invalid email`);
return resolve(false);
}
// Get our config from database
const config = (await ConfigModel.find())[0];

config.smtp_emails.push(email);

// Save our config
await config.save();
return resolve(true)
});
});
case 'delete_email':
return new Promise((resolve) =>
{
prompt.get([
{
name: "email",
description: "Email for administrator",
required: true
},
], async (err, result) =>
{
const email = result.email as string;
Logger.info(`Deleting email..`);
// Get our config from database
const config = (await ConfigModel.find())[0];

// Remove email
config.smtp_emails = config.smtp_emails.filter(e => e !== email);

// Save our config
await config.save();
return resolve(true)
});
});

}
return true;
}
}
Loading

0 comments on commit c96fcec

Please sign in to comment.