-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from srm-kzilla/lsd/feat/notifications
Lsd/feat/notifications
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 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,33 @@ | ||
import { NextFunction, Request, Response, Router } from "express"; | ||
import validateQuery from "../middlewares/validate-query"; | ||
import { validateWebhook } from "../middlewares/validate-webhook"; | ||
import { notificationsService } from "./notifications.service"; | ||
import { | ||
notificationsRequestSchema, | ||
notificationsRequest, | ||
} from "./notifications.schema"; | ||
const router = Router(); | ||
|
||
const handleNotifications = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const response = await notificationsService( | ||
req.body as notificationsRequest | ||
); | ||
res.status(201).json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
router.post( | ||
"/notifications", | ||
validateWebhook(), | ||
validateQuery("body", notificationsRequestSchema), | ||
handleNotifications | ||
); | ||
|
||
export default router; |
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,23 @@ | ||
import * as yup from "yup"; | ||
import { emailSchema } from "../../models/email"; | ||
|
||
export const notificationsRequestSchema = yup.object({ | ||
emails: yup.array().of(emailSchema).required(), | ||
title: yup.string().required(), | ||
body: yup.string().required(), | ||
}); | ||
|
||
export type notificationsRequest = yup.InferType< | ||
typeof notificationsRequestSchema | ||
>; | ||
|
||
export interface notificationUserSchema { | ||
email: string; | ||
discordID: string; | ||
} | ||
|
||
export interface getDiscordUserDetailSchema { | ||
userIDArray: Array<notificationUserSchema>; | ||
failedEmails: Array<string>; | ||
successEmails: Array<string>; | ||
} |
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,64 @@ | ||
import { checkInDBSchema } from "../../models/event"; | ||
import { getDbClient } from "../../utils/database"; | ||
import { getDiscordBot } from "../../utils/discord"; | ||
import { | ||
notificationsRequest, | ||
notificationUserSchema, | ||
} from "./notifications.schema"; | ||
import { MessageEmbed } from "discord.js"; | ||
import { COLORS, CONSTANTS } from "../../utils/constants"; | ||
|
||
export const getDiscordID = async (emailArray: Array<string>) => { | ||
const db = (await getDbClient()).db().collection(`jack-notifications`); | ||
const userIDArray: notificationUserSchema[] = []; | ||
const failedEmails: string[] = []; | ||
for (let index = 0; index < emailArray.length; index++) { | ||
const userEmail = emailArray[index]; | ||
const user = await db.findOne<checkInDBSchema>({ | ||
email: userEmail, | ||
}); | ||
if (!user) { | ||
failedEmails.push(userEmail); | ||
} else { | ||
const userID = user.discordID; | ||
userIDArray.push({ email: userEmail, discordID: userID }); | ||
} | ||
} | ||
return { userIDArray, failedEmails }; | ||
}; | ||
|
||
export const notificationsService = async (data: notificationsRequest) => { | ||
try { | ||
const client = await getDiscordBot(); | ||
const notificationArray = await getDiscordID(data.emails); | ||
const failedUsers = notificationArray.failedEmails; | ||
if (notificationArray?.userIDArray.length && client) { | ||
const notification = await (Promise as any).allSettled( | ||
notificationArray.userIDArray.map(async (userDetails) => { | ||
try { | ||
const embed = new MessageEmbed() | ||
.setColor(COLORS.PURPLE_HEX) | ||
.setTitle(data.title) | ||
.setDescription(data.body) | ||
.setThumbnail(CONSTANTS.SRMKZILLA_GRADIENT_LOGO) | ||
.setTimestamp() | ||
.setFooter(CONSTANTS.FOOTER, CONSTANTS.FOOTER_LOGO_URL); | ||
const user = await client.users.fetch(userDetails.discordID, false); | ||
await user.send(embed); | ||
} catch (err) { | ||
failedUsers.push(userDetails.email); | ||
} | ||
}) | ||
); | ||
} | ||
return { | ||
status: true, | ||
failed: failedUsers, | ||
}; | ||
} catch (error: any) { | ||
throw { | ||
code: error.code || 500, | ||
message: error.message || "Internal Server Error", | ||
}; | ||
} | ||
}; |
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