generated from nhcarrigan/template
feat: migrate to dedicated log platform
This commit is contained in:
+112
-53
@@ -10,7 +10,7 @@ import rawBody from "fastify-raw-body";
|
||||
import StripeApp from "stripe";
|
||||
import { applicationData } from "../config/applicationData.js";
|
||||
import { auth } from "../modules/auth.js";
|
||||
import { sendDiscord } from "../modules/discord.js";
|
||||
import { pipeLog } from "../modules/pipeLog.js";
|
||||
import { sendMail } from "../modules/sendMail.js";
|
||||
import { validateWebhook } from "../modules/validateWebhook.js";
|
||||
import { errorSchema } from "../schemas/errorSchema.js";
|
||||
@@ -85,7 +85,7 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
return;
|
||||
}
|
||||
const { application, level, message } = request.body;
|
||||
await sendDiscord(`[${level}]: ${application}`, message);
|
||||
await pipeLog(application, message, level);
|
||||
await response.status(200).send({ success: true });
|
||||
} catch (error) {
|
||||
await errorHandler(error, "Log Webhook");
|
||||
@@ -108,9 +108,10 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n${stack}`,
|
||||
);
|
||||
await sendDiscord(
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n\`\`\`\n${stack}\n\`\`\``,
|
||||
await pipeLog(
|
||||
application,
|
||||
`${context} - ${message}\n${stack}`,
|
||||
"error",
|
||||
);
|
||||
await response.status(200).send({ success: true });
|
||||
} catch (error) {
|
||||
@@ -132,7 +133,7 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
}
|
||||
const { application, message } = request.body;
|
||||
await sendMail(`[UPTIME]: ${application}`, message);
|
||||
await sendDiscord(`[UPTIME]: ${application}`, message);
|
||||
await pipeLog(application, message, "info");
|
||||
await response.status(200).send({ success: true });
|
||||
} catch (error) {
|
||||
await errorHandler(error, "Uptime Webhook");
|
||||
@@ -141,13 +142,17 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
},
|
||||
);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Body must be capitalised for Fastify.
|
||||
server.post<{ Body: Entitlement; Headers: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Header must be formatted for Fastify.
|
||||
"x-signature-ed25519": string;
|
||||
server.post<{
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Header must be formatted for Fastify.
|
||||
"x-signature-timestamp": string;
|
||||
}; }>(
|
||||
Body: Entitlement;
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Header must be formatted for Fastify.
|
||||
Headers: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Header must be formatted for Fastify.
|
||||
"x-signature-ed25519": string;
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Header must be formatted for Fastify.
|
||||
"x-signature-timestamp": string;
|
||||
};
|
||||
}>(
|
||||
"/entitlement",
|
||||
// eslint-disable-next-line complexity -- Fuck off.
|
||||
async(request, response) => {
|
||||
@@ -157,9 +162,10 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
const isValid = await validateWebhook(request);
|
||||
if (!isValid) {
|
||||
await response.status(401).send({ success: false });
|
||||
void sendDiscord(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
await pipeLog(
|
||||
appInfo?.name ?? applicationId,
|
||||
"An invalid webhook signature was received.",
|
||||
"error",
|
||||
);
|
||||
void sendMail(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
@@ -169,9 +175,10 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
}
|
||||
await response.status(204).send();
|
||||
if (type === 0) {
|
||||
void sendDiscord(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
await pipeLog(
|
||||
appInfo?.name ?? applicationId,
|
||||
"Received a ping from Discord.",
|
||||
"info",
|
||||
);
|
||||
void sendMail(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
@@ -184,9 +191,10 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
guild_id: guildId,
|
||||
ends_at: endsAt,
|
||||
} = event.data;
|
||||
await sendDiscord(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
await pipeLog(
|
||||
appInfo?.name ?? applicationId,
|
||||
`Entitlement purchased!\n- **User ID**: ${userId}\n- **Guild ID**: ${guildId}\n- **Ends At**: ${endsAt}`,
|
||||
"info",
|
||||
);
|
||||
await sendMail(
|
||||
`[ENTITLEMENT]: ${appInfo?.name ?? applicationId}`,
|
||||
@@ -211,53 +219,109 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Nah fam.
|
||||
const raw = request.rawBody as Buffer;
|
||||
const event = await stripe.webhooks.constructEventAsync(
|
||||
raw,
|
||||
request.headers["stripe-signature"] ?? "",
|
||||
process.env.STRIPE_WEBHOOK_SECRET ?? "",
|
||||
).catch(() => {
|
||||
return null;
|
||||
});
|
||||
const event = await stripe.webhooks.
|
||||
constructEventAsync(
|
||||
raw,
|
||||
request.headers["stripe-signature"] ?? "",
|
||||
process.env.STRIPE_WEBHOOK_SECRET ?? "",
|
||||
).
|
||||
catch(() => {
|
||||
return null;
|
||||
});
|
||||
if (event === null) {
|
||||
await response.status(400).send({
|
||||
error: "Invalid Stripe webhook signature.",
|
||||
});
|
||||
void sendDiscord(
|
||||
await pipeLog(
|
||||
`[STRIPE]: Invalid Webhook Signature`,
|
||||
`Received an invalid webhook signature from Stripe.\n- **Headers**: ${JSON.stringify(request.headers)}\n- **Body**: ${JSON.stringify(raw, null, 2)}`,
|
||||
`Received an invalid webhook signature from Stripe.\n- **Headers**: ${JSON.stringify(
|
||||
request.headers,
|
||||
)}\n- **Body**: ${JSON.stringify(raw, null, 2)}`,
|
||||
"error",
|
||||
);
|
||||
return;
|
||||
}
|
||||
await response.status(200).send({ received: true });
|
||||
if (event.type === "checkout.session.completed") {
|
||||
const checkoutSessionCompleted = event.data.object;
|
||||
await sendDiscord(`[STRIPE]: Checkout Session Completed`, `A checkout session has been completed.\n - **ITEMS**: ${checkoutSessionCompleted.line_items?.data.map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(datum.quantity)})`;
|
||||
}).join(", ") ?? "unknown"}\n- **TOTAL**: ${String(checkoutSessionCompleted.amount_total)}`);
|
||||
await sendMail(`[STRIPE]: Checkout Session Completed`, `A checkout session has been completed.\n - **ITEMS**: ${checkoutSessionCompleted.line_items?.data.map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(datum.quantity)})`;
|
||||
}).join(", ") ?? "unknown"}\n- **TOTAL**: ${String(checkoutSessionCompleted.amount_total)}`);
|
||||
await pipeLog(
|
||||
"Stripe",
|
||||
`A checkout session has been completed.\n - **ITEMS**: ${
|
||||
checkoutSessionCompleted.line_items?.data.
|
||||
map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(
|
||||
datum.quantity,
|
||||
)})`;
|
||||
}).
|
||||
join(", ") ?? "unknown"
|
||||
}\n- **TOTAL**: ${String(checkoutSessionCompleted.amount_total)}`,
|
||||
"info",
|
||||
);
|
||||
await sendMail(
|
||||
`[STRIPE]: Checkout Session Completed`,
|
||||
`A checkout session has been completed.\n - **ITEMS**: ${
|
||||
checkoutSessionCompleted.line_items?.data.
|
||||
map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(
|
||||
datum.quantity,
|
||||
)})`;
|
||||
}).
|
||||
join(", ") ?? "unknown"
|
||||
}\n- **TOTAL**: ${String(checkoutSessionCompleted.amount_total)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (event.type === "invoice.paid") {
|
||||
const invoicePaid = event.data.object;
|
||||
await sendDiscord(`[STRIPE]: Invoice Paid`, `An invoice has been paid.\n - **ITEMS**: ${invoicePaid.lines.data.map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(datum.quantity)})`;
|
||||
}).join(", ")}\n- **TOTAL**: ${String(invoicePaid.amount_paid)}`);
|
||||
await sendMail(`[STRIPE]: Invoice Paid`, `An invoice has been paid.\n - **ITEMS**: ${invoicePaid.lines.data.map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(datum.quantity)})`;
|
||||
}).join(", ")}\n- **TOTAL**: ${String(invoicePaid.amount_paid)}`);
|
||||
await pipeLog(
|
||||
"Stripe",
|
||||
`An invoice has been paid.\n - **ITEMS**: ${invoicePaid.lines.data.
|
||||
map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(
|
||||
datum.quantity,
|
||||
)})`;
|
||||
}).
|
||||
join(", ")}\n- **TOTAL**: ${String(invoicePaid.amount_paid)}`,
|
||||
"info",
|
||||
);
|
||||
await sendMail(
|
||||
`[STRIPE]: Invoice Paid`,
|
||||
`An invoice has been paid.\n - **ITEMS**: ${invoicePaid.lines.data.
|
||||
map((datum) => {
|
||||
return `${datum.description ?? "unknown"} (${String(
|
||||
datum.quantity,
|
||||
)})`;
|
||||
}).
|
||||
join(", ")}\n- **TOTAL**: ${String(invoicePaid.amount_paid)}`,
|
||||
);
|
||||
}
|
||||
if (event.type === "payment_intent.succeeded") {
|
||||
const paymentIntentSucceeded = event.data.object;
|
||||
await sendDiscord(`[STRIPE]: Payment Intent Succeeded`, `A payment intent has succeeded.\n- **AMOUNT**: ${String(paymentIntentSucceeded.amount)}`);
|
||||
await sendMail(`[STRIPE]: Payment Intent Succeeded`, `A payment intent has succeeded.\n- **AMOUNT**: ${String(paymentIntentSucceeded.amount)}`);
|
||||
await pipeLog(
|
||||
"Stripe",
|
||||
`A payment intent has succeeded.\n- **AMOUNT**: ${String(
|
||||
paymentIntentSucceeded.amount,
|
||||
)}`,
|
||||
"info",
|
||||
);
|
||||
await sendMail(
|
||||
`[STRIPE]: Payment Intent Succeeded`,
|
||||
`A payment intent has succeeded.\n- **AMOUNT**: ${String(
|
||||
paymentIntentSucceeded.amount,
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
if (event.type === "subscription_schedule.completed") {
|
||||
const subscriptionScheduleCompleted = event.data.object;
|
||||
// Then define and call a function to handle the event subscription_schedule.completed
|
||||
await sendDiscord(`[STRIPE]: Subscription Completed`, `A subscription has been completed.\n- **ID**: ${subscriptionScheduleCompleted.id}`);
|
||||
await sendMail(`[STRIPE]: Subscription Completed`, `A subscription has been completed.\n- **ID**: ${subscriptionScheduleCompleted.id}`);
|
||||
await pipeLog(
|
||||
"Stripe",
|
||||
`A subscription has been completed.\n- **ID**: ${subscriptionScheduleCompleted.id}`,
|
||||
"info",
|
||||
);
|
||||
await sendMail(
|
||||
`[STRIPE]: Subscription Completed`,
|
||||
`A subscription has been completed.\n- **ID**: ${subscriptionScheduleCompleted.id}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
await errorHandler(error, "Stripe Webhook");
|
||||
@@ -266,6 +330,7 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
});
|
||||
|
||||
server.listen({ port: 5003 }, (error) => {
|
||||
// eslint-disable-next-line max-lines -- This block is long because of logging.
|
||||
const application = "Alert Server";
|
||||
if (error) {
|
||||
const { message, stack } = error;
|
||||
@@ -274,16 +339,13 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n${String(stack)}`,
|
||||
);
|
||||
void sendDiscord(
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n\`\`\`\n${String(stack)}\n\`\`\``,
|
||||
);
|
||||
void pipeLog(application, `${message}\n\n${String(stack)}`, "error");
|
||||
return;
|
||||
}
|
||||
const level = "debug";
|
||||
const message = `Server listening on port 5003.`;
|
||||
void sendMail(`[${level}]: ${application}`, message);
|
||||
void sendDiscord(`[${level}]: ${application}`, message);
|
||||
void pipeLog(application, message, level);
|
||||
});
|
||||
} catch (error) {
|
||||
const application = "Alert Server";
|
||||
@@ -294,9 +356,6 @@ export const instantiateServer = async(): Promise<void> => {
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n${stack}`,
|
||||
);
|
||||
void sendDiscord(
|
||||
`[ERROR]: ${context} - ${application}`,
|
||||
`${message}\n\n\`\`\`\n${stack}\n\`\`\``,
|
||||
);
|
||||
void pipeLog(application, `${message}\n\n${stack}`, "error");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user