Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ Next, you need to set up the environment variables. This can be done right in th

Most of the above variables need to be set with values configured on or obtained from other servers (e.g. FreePBX, the scheduling server, etc.). Unless you make modifications to this application, `DS_URL`, in particular, must be configured to provide the user ID of a nighttime duty supervisor. Further, the application is currently written to query that URL with `?token=TOKENHERE` where `TOKENHERE` is set in `DS_URL_TOKEN`.

### Supervisor file

`sups.json` (see `sups.json.example`) lists every supervisor's `id`, `external_phone_numbers`, and `internal_extensions`, plus two flags:

| Flag | Effect |
| -------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `day` | Rung by both the main and backup ring groups during the day |
| `backup_night` | Rung by the backup ring group at night, and by the main ring group too if no night supervisor is scheduled or found here |

Only a literal `true` turns a flag on. A missing flag, `false`, or a string like `"true"` all count as off.

With all that set, you should be able to start the app using something like `docker compose up -d dialert`. This may vary depending on your configuration.

## Credits
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"name": "dialert",
"description": "**DiALERT** checks a schedule and changes a PBX ring group accordingly. It was created for @rpiambulance for use by their duty supervisors.",
"version": "1.0.0",
"version": "1.0.1",
"main": "server.js",
"repository": {
"type": "git",
Expand Down
84 changes: 31 additions & 53 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,35 @@ const getSups = async () => {
return JSON.parse(sups);
};

const generateDsNumberList = async (nightSup) => {
const { supervisors: sups } = await getSups();
let dsNumberList = "";
for (const sup of sups) {
if (sup.id == nightSup) {
for (num of sup.external_phone_numbers) {
dsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
dsNumberList += `${num}-`;
}
}
}
dsNumberList = dsNumberList.slice(0, -1);
return dsNumberList;
};
//a sup's numbers, formatted for a FreePBX ring group extension list
const formatNumbers = (sup) => [
...(sup.external_phone_numbers || []).map((num) => `${num}#`),
...(sup.internal_extensions || []),
];

const generateBackupDsNumberList = async () => {
const generateNumberList = async (include) => {
const { supervisors: sups } = await getSups();
let backupDsNumberList = "";
for (const sup of sups) {
if (sup.backup_night) {
for (num of sup.external_phone_numbers) {
backupDsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
backupDsNumberList += `${num}-`;
}
}
}
backupDsNumberList = backupDsNumberList.slice(0, -1);
return backupDsNumberList;
return sups
.filter(include)
.flatMap(formatNumbers)
.join("-");
};

const generateDayDsNumberList = async () => {
const { supervisors: sups } = await getSups();
let dayDsNumberList = "";
for (const sup of sups) {
if (sup.day) {
for (num of sup.external_phone_numbers) {
dayDsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
dayDsNumberList += `${num}-`;
}
}
}
dayDsNumberList = dayDsNumberList.slice(0, -1);
return dayDsNumberList;
};
const generateDsNumberList = (nightSup) =>
generateNumberList((sup) => sup.id == nightSup);

//only an explicit `true` in sups.json opts a supervisor in
const generateBackupDsNumberList = () =>
generateNumberList((sup) => sup.backup_night === true);

const generateDayDsNumberList = () =>
generateNumberList((sup) => sup.day === true);

const handleDay = async () => {
const dsNumberList = await generateDayDsNumberList();
if (!dsNumberList) {
console.log('No supervisors have "day": true in sups.json');
}
let accessToken;
try {
accessToken = await client.getToken(tokenParams, { json: true });
Expand Down Expand Up @@ -178,13 +154,15 @@ const handleDay = async () => {
};

const handleNight = async (nightSup) => {
let dsNumberList;
let backupDsNumberList;
if (nightSup <= 0) {
dsNumberList = await generateBackupDsNumberList();
} else {
dsNumberList = await generateDsNumberList(nightSup);
backupDsNumberList = await generateBackupDsNumberList();
const backupDsNumberList = await generateBackupDsNumberList();
let dsNumberList = nightSup > 0 ? await generateDsNumberList(nightSup) : "";
if (!dsNumberList) {
//no night sup scheduled, or they aren't in sups.json:
//the backup_night sups take the main group too
console.log(
`No numbers for night sup "${nightSup}"; using backup_night supervisors`
);
dsNumberList = backupDsNumberList;
}
let accessToken;
try {
Expand Down
Loading