generated from nhcarrigan/template
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ export const ids = {
|
||||
bugReports: "1447723804330823763",
|
||||
communityFeedback: "1447726591189975210",
|
||||
featureRequests: "1447726510369931295",
|
||||
formSubmissions: "1410435042898874471",
|
||||
formSubmissions: "1448445144071147520",
|
||||
gaming: "1385797656307175468",
|
||||
general: "1385797320389431336",
|
||||
menteeChat: "1400589073613062204",
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention -- Baserow uses snake case */
|
||||
|
||||
export interface FormSubmission {
|
||||
table_id: number;
|
||||
items: Array<{ id: number }>;
|
||||
[key: string]: unknown;
|
||||
id: number;
|
||||
manualSort: number;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
import { MessageFlags } from "discord.js";
|
||||
import { formIds } from "../config/forms.js";
|
||||
import { ids } from "../config/ids.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
@@ -21,18 +20,26 @@ import type { FastifyRequest, FastifyReply } from "fastify";
|
||||
// eslint-disable-next-line max-lines-per-function -- only long because of analytics.
|
||||
export const processFormSubmission = async(
|
||||
amari: Amari,
|
||||
request: FastifyRequest<{
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
request: FastifyRequest<{ Body: FormSubmission }>,
|
||||
Body: Array<FormSubmission>;
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
Querystring: { form?: string };
|
||||
}>,
|
||||
response: FastifyReply,
|
||||
): Promise<void> => {
|
||||
const { secret } = request.headers;
|
||||
if (secret !== process.env.BASEROW_SECRET
|
||||
|| process.env.BASEROW_SECRET === undefined) {
|
||||
const { authorization: secret } = request.headers;
|
||||
if (
|
||||
secret !== process.env.BASEROW_SECRET
|
||||
|| secret === undefined
|
||||
) {
|
||||
await response.status(403).send({ message: "Invalid Secret Provided." });
|
||||
return;
|
||||
}
|
||||
const { form } = request.query;
|
||||
await response.status(204).send();
|
||||
const channel = amari.discord.channels.cache.get(ids.channels.formSubmissions)
|
||||
const channel
|
||||
= amari.discord.channels.cache.get(ids.channels.formSubmissions)
|
||||
?? await amari.discord.channels.fetch(ids.channels.formSubmissions);
|
||||
if (channel?.isSendable() !== true) {
|
||||
await logger.log(
|
||||
@@ -41,15 +48,15 @@ export const processFormSubmission = async(
|
||||
);
|
||||
return;
|
||||
}
|
||||
const { table_id: table, items } = request.body;
|
||||
const rowIds = items.map((item) => {
|
||||
return item.id;
|
||||
}).join(", ");
|
||||
const tableName = formIds[table];
|
||||
const submissionIds = request.body.map((item) => {
|
||||
return `${item.id.toString()} (${item.manualSort.toString()})`;
|
||||
});
|
||||
await channel.send({
|
||||
components: [
|
||||
{
|
||||
content: `${tableName ?? "Unknown Form"} Submission Received!\n\nRow ID(s): ${rowIds}`,
|
||||
content: `${
|
||||
form ?? "Unknown Form"
|
||||
} Submission Received!\n\nRow ID(s): ${submissionIds.join(", ")}`,
|
||||
type: 10,
|
||||
},
|
||||
{
|
||||
@@ -68,5 +75,7 @@ export const processFormSubmission = async(
|
||||
],
|
||||
flags: [ MessageFlags.IsComponentsV2 ],
|
||||
});
|
||||
await logger.metric("processed_form_submission", 1, { table: String(table) });
|
||||
await logger.metric("processed_form_submission", 1, {
|
||||
table: String(request.body),
|
||||
});
|
||||
};
|
||||
|
||||
+5
-2
@@ -87,7 +87,9 @@ export const instantiateServer = (amari: Amari): void => {
|
||||
|
||||
server.
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
post<{ Body: FormSubmission }>("/form", async(request, response) => {
|
||||
post<{ Body: Array<FormSubmission>; Querystring: { form: string } }>(
|
||||
"/form",
|
||||
async(request, response) => {
|
||||
try {
|
||||
await processFormSubmission(amari, request, response);
|
||||
} catch (error) {
|
||||
@@ -96,7 +98,8 @@ export const instantiateServer = (amari: Amari): void => {
|
||||
}
|
||||
await logger.error("/form route", error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
server.listen({ port: 7044 }, (error) => {
|
||||
if (error) {
|
||||
|
||||
Reference in New Issue
Block a user