-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change notifications to be batched by client (#212)
* Change notifications to be batched by client Now, POST /notification/batch to get a set of batch ids POST /notification/send for each batch id to send notifs This makes it so the client requesting API handles pagination & sending requests so we don't run into Vercel's 10 second timeout * Add send-notif script
- Loading branch information
1 parent
40cc072
commit 1d45d16
Showing
4 changed files
with
194 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import inquirer from "inquirer"; | ||
import axios from "axios"; | ||
|
||
const options = [ | ||
{ | ||
type: "list", | ||
name: "host", | ||
message: "Select host (prod or local):", | ||
choices: ["https://adonix.hackillinois.org", "http://localhost:3000"], | ||
}, | ||
{ | ||
type: "password", | ||
name: "token", | ||
message: "Enter auth jwt token:", | ||
}, | ||
{ | ||
type: "list", | ||
name: "type", | ||
message: "Select notification topic type:", | ||
choices: ["role", "foodWave", "eventId", "staffShift"], | ||
}, | ||
{ | ||
type: "input", | ||
name: "id", | ||
message: "Enter notification topic:", | ||
}, | ||
{ | ||
type: "input", | ||
name: "title", | ||
message: "Enter notification title:", | ||
}, | ||
{ | ||
type: "input", | ||
name: "body", | ||
message: "Enter notification body:", | ||
}, | ||
]; | ||
|
||
const optionsConfirm = [ | ||
{ | ||
type: "confirm", | ||
name: "confirm", | ||
message: "Is the above correct?", | ||
}, | ||
]; | ||
|
||
async function main() { | ||
const answers = await inquirer.prompt(options); | ||
const confirm = await inquirer.prompt(optionsConfirm); | ||
|
||
if (!confirm.confirm) { | ||
return; | ||
} | ||
|
||
const config = { | ||
headers: { | ||
Authorization: answers.token, | ||
}, | ||
}; | ||
|
||
const batchResponse = await axios.post( | ||
`${answers.host}/notification/batch`, | ||
{ | ||
title: answers.title, | ||
body: answers.body, | ||
[answers.type]: answers.id, | ||
}, | ||
config, | ||
); | ||
|
||
if (batchResponse.status != 200) { | ||
throw new Error(`Failed to get notification batches: ${batchResponse}`); | ||
} | ||
|
||
const batches = batchResponse.data.batches; | ||
|
||
const sendResponses = await Promise.all( | ||
batches.map((batchId) => | ||
axios.post( | ||
`${answers.host}/notification/send`, | ||
{ | ||
batchId, | ||
}, | ||
config, | ||
), | ||
), | ||
); | ||
|
||
let sent = 0; | ||
let failed = 0; | ||
|
||
sendResponses.forEach((response) => { | ||
sent += response.data.sent.length; | ||
failed += response.data.failed.length; | ||
}); | ||
|
||
console.log(`Notification sent to ${sent}, failed to send to ${failed}`); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters