Compare commits
1 Commits
master
...
01c234782e
Author | SHA1 | Date | |
---|---|---|---|
01c234782e |
15
README.md
15
README.md
@ -8,15 +8,14 @@
|
|||||||
# Required API endpoint/authentication variables
|
# Required API endpoint/authentication variables
|
||||||
SONAR_URL=https://instance.sonar.software/api/graphql
|
SONAR_URL=https://instance.sonar.software/api/graphql
|
||||||
SONAR_TOKEN=
|
SONAR_TOKEN=
|
||||||
RINGCENTRAL_CLIENT_ID=
|
RC_APP_KEY=
|
||||||
RINGCENTRAL_CLIENT_SECRET=
|
RC_APP_SECRET=
|
||||||
RINGCENTRAL_USERNAME=
|
RC_LOGIN_USERNAME=
|
||||||
RINGCENTRAL_EXTENSION=
|
RC_LOGIN_EXT=
|
||||||
RINGCENTRAL_PASSWORD=
|
RC_LOGIN_PASSWORD=
|
||||||
|
|
||||||
# Set to 'sandbox' to use RingCentral's sandbox API
|
# Set to any value to enable use of RingCentral's sandbox API
|
||||||
# Any other value will result in using RingCentral's production API
|
RC_SANDBOX=
|
||||||
RINGCENTRAL_SERVER=production
|
|
||||||
|
|
||||||
# The database to use
|
# The database to use
|
||||||
# valid options: pg, sqlite
|
# valid options: pg, sqlite
|
||||||
|
39
src/index.ts
39
src/index.ts
@ -9,11 +9,11 @@ function checkEnv() {
|
|||||||
[
|
[
|
||||||
"SONAR_URL",
|
"SONAR_URL",
|
||||||
"SONAR_TOKEN",
|
"SONAR_TOKEN",
|
||||||
"RINGCENTRAL_CLIENT_ID",
|
"RC_APP_KEY",
|
||||||
"RINGCENTRAL_CLIENT_SECRET",
|
"RC_APP_SECRET",
|
||||||
"RINGCENTRAL_USERNAME",
|
"RC_LOGIN_USERNAME",
|
||||||
"RINGCENTRAL_EXTENSION",
|
"RC_LOGIN_EXT",
|
||||||
"RINGCENTRAL_PASSWORD",
|
"RC_LOGIN_PASSWORD",
|
||||||
"EXTENSION_TICKET_GROUPS",
|
"EXTENSION_TICKET_GROUPS",
|
||||||
].forEach((env) => {
|
].forEach((env) => {
|
||||||
if (process.env[env] === undefined) {
|
if (process.env[env] === undefined) {
|
||||||
@ -59,25 +59,22 @@ async function initSonar() {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
logger.info(`Authenticated to Sonar as '${user.me.name}'`);
|
logger.info(`Authenticated to Sonar as '${user.me.name}'.`);
|
||||||
return sonar;
|
return sonar;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initRingCentralSDK() {
|
async function initRingCentralSDK() {
|
||||||
const sdk = new SDK({
|
const sdk = new SDK({
|
||||||
server:
|
server: SDK.server[process.env.RC_SANDBOX ? "sandbox" : "production"],
|
||||||
SDK.server[
|
clientId: process.env.RC_APP_KEY,
|
||||||
process.env.RINGCENTRAL_SERVER === "sandbox" ? "sandbox" : "production"
|
clientSecret: process.env.RC_APP_SECRET,
|
||||||
],
|
|
||||||
clientId: process.env.RINGCENTRAL_CLIENT_ID,
|
|
||||||
clientSecret: process.env.RINGCENTRAL_CLIENT_SECRET,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const login = () =>
|
const login = () =>
|
||||||
sdk.login({
|
sdk.login({
|
||||||
username: process.env.RINGCENTRAL_USERNAME,
|
username: process.env.RC_LOGIN_USERNAME,
|
||||||
extension: process.env.RINGCENTRAL_EXTENSION,
|
extension: process.env.RC_LOGIN_EXT,
|
||||||
password: process.env.RINGCENTRAL_PASSWORD,
|
password: process.env.RC_LOGIN_PASSWORD,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -91,11 +88,11 @@ async function initRingCentralSDK() {
|
|||||||
platform.on(platform.events.refreshError, async (err) => {
|
platform.on(platform.events.refreshError, async (err) => {
|
||||||
logger.error("Refresh token error:", err);
|
logger.error("Refresh token error:", err);
|
||||||
await login();
|
await login();
|
||||||
logger.info("RingCentral re-authentication successful");
|
logger.info("RingCentral re-authentication successful.");
|
||||||
});
|
});
|
||||||
|
|
||||||
await login();
|
await login();
|
||||||
logger.info("Authenticated to RingCentral");
|
logger.info("Authenticated to RingCentral.");
|
||||||
|
|
||||||
return sdk;
|
return sdk;
|
||||||
}
|
}
|
||||||
@ -104,7 +101,7 @@ async function initDB() {
|
|||||||
const db = knex(knexConfig);
|
const db = knex(knexConfig);
|
||||||
if (!process.env.DB_SKIP_MIGRATIONS) {
|
if (!process.env.DB_SKIP_MIGRATIONS) {
|
||||||
await db.migrate.latest();
|
await db.migrate.latest();
|
||||||
logger.info("Database migrations run successfully");
|
logger.info("Database migrations run successfully.");
|
||||||
}
|
}
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
@ -117,13 +114,12 @@ async function main() {
|
|||||||
const rcsdk = await initRingCentralSDK();
|
const rcsdk = await initRingCentralSDK();
|
||||||
const db = await initDB();
|
const db = await initDB();
|
||||||
|
|
||||||
logger.info("Starting");
|
logger.info("Starting ticketizer...");
|
||||||
|
|
||||||
const intervals = ticketize(sonar, rcsdk, db, getTicketizeConfig());
|
const intervals = ticketize(sonar, rcsdk, db, getTicketizeConfig());
|
||||||
|
|
||||||
["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => {
|
["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => {
|
||||||
process.on(sig, async () => {
|
process.on(sig, async () => {
|
||||||
logger.info(`Caught ${sig}, shutting down...`);
|
logger.info(`\nCaught ${sig}, shutting down...`);
|
||||||
const results = await Promise.allSettled(
|
const results = await Promise.allSettled(
|
||||||
intervals.map((interval) => interval.clear())
|
intervals.map((interval) => interval.clear())
|
||||||
);
|
);
|
||||||
@ -134,7 +130,6 @@ async function main() {
|
|||||||
logger.error(result.reason);
|
logger.error(result.reason);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await rcsdk.logout();
|
|
||||||
process.exit(errors ? 1 : 0);
|
process.exit(errors ? 1 : 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -340,7 +340,7 @@ export function ticketize(
|
|||||||
* @param firstRun whether this is the first run
|
* @param firstRun whether this is the first run
|
||||||
*/
|
*/
|
||||||
async function fetchAndStoreNewVoicemails(firstRun = false) {
|
async function fetchAndStoreNewVoicemails(firstRun = false) {
|
||||||
logger.verbose("Checking for new voicemails");
|
logger.info("Checking for new voicemails");
|
||||||
const extensions = await getValidRCExtensions();
|
const extensions = await getValidRCExtensions();
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
extensions.map((extension) =>
|
extensions.map((extension) =>
|
||||||
|
11
src/util.ts
11
src/util.ts
@ -1,17 +1,10 @@
|
|||||||
import PhoneNumber from "awesome-phonenumber";
|
import PhoneNumber from "awesome-phonenumber";
|
||||||
import winston, { format } from "winston";
|
import winston from "winston";
|
||||||
|
|
||||||
export const DEBUG = !!process.env.DEBUG;
|
export const DEBUG = !!process.env.DEBUG;
|
||||||
export const logger = winston.createLogger({
|
export const logger = winston.createLogger({
|
||||||
level: DEBUG ? "debug" : process.env.LOG_LEVEL ?? "info",
|
level: DEBUG ? "debug" : process.env.LOG_LEVEL ?? "info",
|
||||||
transports: [new winston.transports.Console()],
|
transports: [new winston.transports.()],
|
||||||
format: format.combine(
|
|
||||||
format.errors({ stack: true }),
|
|
||||||
format.printf(
|
|
||||||
({ level, message, stack }) =>
|
|
||||||
`${level}: ${message}${stack ? "\n" + stack : ""}`
|
|
||||||
)
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export function getNationalNumber(input: string) {
|
export function getNationalNumber(input: string) {
|
||||||
|
Reference in New Issue
Block a user