Add FIRST_RUN_AGE variable
Allows setting the maximum age of voicemails to fetch on the first run. Other changes: - Refactor `from` to `age` in function parameters - Improve README
This commit is contained in:
20
src/index.ts
20
src/index.ts
@ -30,6 +30,22 @@ function getExtensionToTicketGroupMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
const DEFAULT_FIRST_RUN_AGE = 86400;
|
||||
|
||||
function getTicketizeConfig() {
|
||||
const firstRunAge = process.env.FIRST_RUN_AGE
|
||||
? parseInt(process.env.FIRST_RUN_AGE)
|
||||
: DEFAULT_FIRST_RUN_AGE;
|
||||
if (isNaN(firstRunAge) || firstRunAge <= 0) {
|
||||
throw new Error("FIRST_RUN_AGE must be a valid positive integer");
|
||||
}
|
||||
|
||||
return {
|
||||
firstRunAge,
|
||||
extensionToTicketGroup: getExtensionToTicketGroupMapping(),
|
||||
};
|
||||
}
|
||||
|
||||
async function initSonar() {
|
||||
const sonar = new Sonar(process.env.SONAR_URL!, process.env.SONAR_TOKEN!);
|
||||
// simple query to test API cedentials
|
||||
@ -79,9 +95,7 @@ async function main() {
|
||||
const db = await initDB();
|
||||
|
||||
console.log("Starting ticketizer...");
|
||||
const intervals = ticketize(sonar, rcsdk, db, {
|
||||
extensionToTicketGroup: getExtensionToTicketGroupMapping(),
|
||||
});
|
||||
const intervals = ticketize(sonar, rcsdk, db, getTicketizeConfig());
|
||||
|
||||
["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => {
|
||||
process.on(sig, async () => {
|
||||
|
@ -49,6 +49,7 @@ function rcapi(short: string, version = "v1.0") {
|
||||
}
|
||||
|
||||
interface TicketizeConfig {
|
||||
firstRunAge: number;
|
||||
extensionToTicketGroup: { [key: string]: number };
|
||||
}
|
||||
|
||||
@ -62,7 +63,7 @@ export function ticketize(
|
||||
sonar: Sonar,
|
||||
rcsdk: SDK,
|
||||
db: Knex,
|
||||
{ extensionToTicketGroup }: TicketizeConfig
|
||||
{ firstRunAge, extensionToTicketGroup }: TicketizeConfig
|
||||
) {
|
||||
/**
|
||||
* Uploads a file to Sonar, returning its ID.
|
||||
@ -92,17 +93,17 @@ export function ticketize(
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `extensionId`s messages that are up to `from` seconds old.
|
||||
* Returns `extensionId`s messages that are up to `age` seconds old.
|
||||
*
|
||||
* @param extensionId
|
||||
* @param from how many seconds ago to retrieve messages from
|
||||
* @param age the maximum age (in seconds) of voicemails to fetch
|
||||
*/
|
||||
async function getExtensionVoicemails(extensionId: number, from = 86000) {
|
||||
async function getExtensionVoicemails(extensionId: number, age = 86000) {
|
||||
const result = await rcsdk.get(
|
||||
rcapi(`/account/~/extension/${extensionId}/message-store`),
|
||||
{
|
||||
messageType: "VoiceMail",
|
||||
dateFrom: new Date(Date.now() - from * 1000).toISOString(),
|
||||
dateFrom: new Date(Date.now() - age * 1000).toISOString(),
|
||||
}
|
||||
);
|
||||
return (await result.json()).records as RCMessage[];
|
||||
@ -305,16 +306,13 @@ export function ticketize(
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and stores the voicemails for `extension` that are up to `from`
|
||||
* Retrieves and stores the voicemails for `extension` that are up to `age`
|
||||
* seconds old.
|
||||
* @param extension
|
||||
* @param from
|
||||
* @param age
|
||||
*/
|
||||
async function storeExtensionVoicemails(
|
||||
extension: RCExtension,
|
||||
from: number
|
||||
) {
|
||||
const messages = await getExtensionVoicemails(extension.id, from);
|
||||
async function storeExtensionVoicemails(extension: RCExtension, age: number) {
|
||||
const messages = await getExtensionVoicemails(extension.id, age);
|
||||
const isStored = await Promise.all(
|
||||
messages.map((message) => isMessageStored(message.id))
|
||||
);
|
||||
@ -335,15 +333,15 @@ export function ticketize(
|
||||
|
||||
/**
|
||||
* Fetch and store new voicemails. If this is the first run, we get the last
|
||||
* day's worth of voicemails. Otherwise, we fetch only the last 15 minutes.
|
||||
* day's worth of voicemails. Otherwise, we fetch only the last 5 minutes.
|
||||
*
|
||||
* @param firstRun whether this is the first run or not
|
||||
* @param firstRun whether this is the first run
|
||||
*/
|
||||
async function fetchAndStoreNewVoicemails(firstRun = false) {
|
||||
const extensions = await getValidRCExtensions();
|
||||
return Promise.all(
|
||||
extensions.map((extension) =>
|
||||
storeExtensionVoicemails(extension, firstRun ? 86400 : 900)
|
||||
storeExtensionVoicemails(extension, firstRun ? firstRunAge : 300)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user