mirror of
https://github.com/fergalmoran/radio-otherway.git
synced 2026-01-03 07:36:10 +00:00
Fix up the notifications
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { StatusCodes } from "http-status-codes";
|
import { StatusCodes } from "http-status-codes";
|
||||||
import { Shows, Users } from "@/lib/db/collections";
|
import { Shows, Users } from "@/lib/db/collections";
|
||||||
import { sendWhatsApp } from "@/lib/util/notifications/sms";
|
import { sendSMS, sendWhatsApp } from "@/lib/util/notifications/sms";
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
if (req.method !== "POST") {
|
if (req.method !== "POST") {
|
||||||
@@ -13,15 +13,30 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const users = await Users.getNotifiable();
|
const users = await Users.getNotifiable();
|
||||||
//iterate through every user and get their notifications
|
//iterate through every user and get their notifications
|
||||||
//and send 'em off
|
//and send 'em off
|
||||||
users?.forEach(user => {
|
if (!users) {
|
||||||
|
res.status(StatusCodes.METHOD_NOT_ALLOWED);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const user of users) {
|
||||||
const message = (process.env.WHATSAPP_SHOW_HOUR as string)
|
const message = (process.env.WHATSAPP_SHOW_HOUR as string)
|
||||||
.replace("{{1}}", user.displayName as string)
|
.replace("{{1}}", user.displayName as string)
|
||||||
.replace("{{2}}", show.creator);
|
.replace("{{2}}", show.creator);
|
||||||
if (user.mobileNumber) {
|
if (user.mobileNumber) {
|
||||||
sendWhatsApp(user.mobileNumber, message);
|
if (user.notificationsWhatsapp) {
|
||||||
|
await sendWhatsApp(user.mobileNumber, message);
|
||||||
|
}
|
||||||
|
if (user.notificationsMobile) {
|
||||||
|
await sendSMS(user.mobileNumber, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
if (user.email && user.notificationsEmail) {
|
||||||
|
await sendEmail(user.email, "New show upcoming on Radio Otherway", message);
|
||||||
|
}
|
||||||
|
if (user.notificationsBrowser) {
|
||||||
|
// await sendEmail(user.email, "New show upcoming on Radio Otherway", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
res.status(StatusCodes.OK).json({ status: "OK" });
|
res.status(StatusCodes.OK).json({ status: "OK" });
|
||||||
res.end();
|
res.end();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user