Compare commits

..

2 Commits

Author SHA1 Message Date
48a3e60623 Change env variables
RC_* to RINGCENTRAL_*
APP_KEY and APP_SECRET repalced with CLIENT_ID and CLIENT_SECRET to
reflect RingCentral's descriptions

Change RC_SANDBOX to RINGCENTRAL_SERVER and changed its behaviour.
2021-03-15 11:57:10 -06:00
9a61396db6 Use winston for logging 2021-03-15 09:46:40 -06:00
4 changed files with 39 additions and 26 deletions

View File

@ -8,14 +8,15 @@
# Required API endpoint/authentication variables
SONAR_URL=https://instance.sonar.software/api/graphql
SONAR_TOKEN=
RC_APP_KEY=
RC_APP_SECRET=
RC_LOGIN_USERNAME=
RC_LOGIN_EXT=
RC_LOGIN_PASSWORD=
RINGCENTRAL_CLIENT_ID=
RINGCENTRAL_CLIENT_SECRET=
RINGCENTRAL_USERNAME=
RINGCENTRAL_EXTENSION=
RINGCENTRAL_PASSWORD=
# Set to any value to enable use of RingCentral's sandbox API
RC_SANDBOX=
# Set to 'sandbox' to use RingCentral's sandbox API
# Any other value will result in using RingCentral's production API
RINGCENTRAL_SERVER=production
# The database to use
# valid options: pg, sqlite

View File

@ -9,11 +9,11 @@ function checkEnv() {
[
"SONAR_URL",
"SONAR_TOKEN",
"RC_APP_KEY",
"RC_APP_SECRET",
"RC_LOGIN_USERNAME",
"RC_LOGIN_EXT",
"RC_LOGIN_PASSWORD",
"RINGCENTRAL_CLIENT_ID",
"RINGCENTRAL_CLIENT_SECRET",
"RINGCENTRAL_USERNAME",
"RINGCENTRAL_EXTENSION",
"RINGCENTRAL_PASSWORD",
"EXTENSION_TICKET_GROUPS",
].forEach((env) => {
if (process.env[env] === undefined) {
@ -59,22 +59,25 @@ async function initSonar() {
}
`
);
logger.info(`Authenticated to Sonar as '${user.me.name}'.`);
logger.info(`Authenticated to Sonar as '${user.me.name}'`);
return sonar;
}
async function initRingCentralSDK() {
const sdk = new SDK({
server: SDK.server[process.env.RC_SANDBOX ? "sandbox" : "production"],
clientId: process.env.RC_APP_KEY,
clientSecret: process.env.RC_APP_SECRET,
server:
SDK.server[
process.env.RINGCENTRAL_SERVER === "sandbox" ? "sandbox" : "production"
],
clientId: process.env.RINGCENTRAL_CLIENT_ID,
clientSecret: process.env.RINGCENTRAL_CLIENT_SECRET,
});
const login = () =>
sdk.login({
username: process.env.RC_LOGIN_USERNAME,
extension: process.env.RC_LOGIN_EXT,
password: process.env.RC_LOGIN_PASSWORD,
username: process.env.RINGCENTRAL_USERNAME,
extension: process.env.RINGCENTRAL_EXTENSION,
password: process.env.RINGCENTRAL_PASSWORD,
});
if (DEBUG) {
@ -88,11 +91,11 @@ async function initRingCentralSDK() {
platform.on(platform.events.refreshError, async (err) => {
logger.error("Refresh token error:", err);
await login();
logger.info("RingCentral re-authentication successful.");
logger.info("RingCentral re-authentication successful");
});
await login();
logger.info("Authenticated to RingCentral.");
logger.info("Authenticated to RingCentral");
return sdk;
}
@ -101,7 +104,7 @@ async function initDB() {
const db = knex(knexConfig);
if (!process.env.DB_SKIP_MIGRATIONS) {
await db.migrate.latest();
logger.info("Database migrations run successfully.");
logger.info("Database migrations run successfully");
}
return db;
}
@ -114,12 +117,13 @@ async function main() {
const rcsdk = await initRingCentralSDK();
const db = await initDB();
logger.info("Starting ticketizer...");
logger.info("Starting");
const intervals = ticketize(sonar, rcsdk, db, getTicketizeConfig());
["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => {
process.on(sig, async () => {
logger.info(`\nCaught ${sig}, shutting down...`);
logger.info(`Caught ${sig}, shutting down...`);
const results = await Promise.allSettled(
intervals.map((interval) => interval.clear())
);
@ -130,6 +134,7 @@ async function main() {
logger.error(result.reason);
}
});
await rcsdk.logout();
process.exit(errors ? 1 : 0);
});
});

View File

@ -340,7 +340,7 @@ export function ticketize(
* @param firstRun whether this is the first run
*/
async function fetchAndStoreNewVoicemails(firstRun = false) {
logger.info("Checking for new voicemails");
logger.verbose("Checking for new voicemails");
const extensions = await getValidRCExtensions();
return Promise.all(
extensions.map((extension) =>

View File

@ -1,10 +1,17 @@
import PhoneNumber from "awesome-phonenumber";
import winston from "winston";
import winston, { format } from "winston";
export const DEBUG = !!process.env.DEBUG;
export const logger = winston.createLogger({
level: DEBUG ? "debug" : process.env.LOG_LEVEL ?? "info",
transports: [new winston.transports.Console()],
format: format.combine(
format.errors({ stack: true }),
format.printf(
({ level, message, stack }) =>
`${level}: ${message}${stack ? "\n" + stack : ""}`
)
),
});
export function getNationalNumber(input: string) {