feat: add endpoint for entitlement events

This commit is contained in:
2025-07-06 12:49:55 -07:00
parent 33e416af83
commit 5d4621df83
7 changed files with 367 additions and 6 deletions

View File

@ -5,12 +5,16 @@
*/
import fastify from "fastify";
import fastifyRawBody from "fastify-raw-body";
import { applicationData } from "../config/applicationData.js";
import { auth } from "../modules/auth.js";
import { sendDiscord } from "../modules/discord.js";
import { sendMail } from "../modules/sendMail.js";
import { validateWebhook } from "../modules/validateWebhook.js";
import { errorSchema } from "../schemas/errorSchema.js";
import { logSchema } from "../schemas/logSchema.js";
import { uptimeSchema } from "../schemas/uptimeSchema.js";
import type { Entitlement } from "../interfaces/entitlement.js";
import type { Error } from "../interfaces/error.js";
import type { Log } from "../interfaces/log.js";
import type { Uptime } from "../interfaces/uptime.js";
@ -63,6 +67,15 @@ export const instantiateServer = (): void => {
logger: false,
});
server.register(fastifyRawBody, {
encoding: "utf8",
field: "rawBody",
global: false,
jsonContentTypes: [],
routes: [],
runFirst: true,
});
server.get("/", (_request, response) => {
response.header("Content-Type", "text/html");
response.send(html);
@ -90,7 +103,10 @@ export const instantiateServer = (): void => {
return;
}
const { application, context, stack, message } = request.body;
await sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${stack}`);
await sendMail(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n${stack}`,
);
await sendDiscord(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n\`\`\`\n${stack}\n\`\`\``,
@ -115,13 +131,68 @@ export const instantiateServer = (): void => {
},
);
// eslint-disable-next-line @typescript-eslint/naming-convention -- Body must be capitalised for Fastify.
server.post<{ Body: Entitlement }>(
"/entitlement",
{ config: { rawBody: true } },
async(request, response) => {
if (!validateWebhook(request)) {
await response.status(401).send({ success: false });
void sendDiscord(
"[NOTIFICATION]: Entitlement Event",
"An invalid webhook signature was received.",
);
void sendMail(
"[NOTIFICATION]: Entitlement Event",
"An invalid webhook signature was received.",
);
return;
}
const { type } = request.body;
if (type === 0) {
await response.status(204).send();
void sendDiscord(
"[NOTIFICATION]: Entitlement Event",
"Received a ping from Discord.",
);
void sendMail(
"[NOTIFICATION]: Entitlement Event",
"Received a ping from Discord.",
);
return;
}
await response.status(204).send();
const { application_id: applicationId, event } = request.body;
const {
user_id: userId,
guild_id: guildId,
ends_at: endsAt,
} = event.data;
const appInfo = applicationData[applicationId];
await sendDiscord(
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
`Entitlement purchased!\n- **User ID**: ${userId}\n- **Guild ID**: ${guildId}\n- **Ends At**: ${endsAt}`,
);
await sendMail(
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
`Entitlement purchased!\n- **User ID**: ${userId}\n- **Guild ID**: ${guildId}\n- **Ends At**: ${endsAt}`,
);
},
);
server.listen({ port: 5003 }, (error) => {
const application = "Alert Server";
if (error) {
const { message, stack } = error;
const context = "Server Startup";
void sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${String(stack)}`);
void sendDiscord(`[ERROR]: ${context} - ${application}`, `${message}\n\n\`\`\`\n${String(stack)}\n\`\`\``);
void sendMail(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n${String(stack)}`,
);
void sendDiscord(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n\`\`\`\n${String(stack)}\n\`\`\``,
);
return;
}
const level = "debug";
@ -134,7 +205,13 @@ export const instantiateServer = (): void => {
const context = "Server Startup";
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Totally being lazy.
const { message, stack } = error as Error;
void sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${stack}`);
void sendDiscord(`[ERROR]: ${context} - ${application}`, `${message}\n\n\`\`\`\n${stack}\n\`\`\``);
void sendMail(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n${stack}`,
);
void sendDiscord(
`[ERROR]: ${context} - ${application}`,
`${message}\n\n\`\`\`\n${stack}\n\`\`\``,
);
}
};