diff --git a/src/config/ids.ts b/src/config/ids.ts index 7e903b0..022e34a 100644 --- a/src/config/ids.ts +++ b/src/config/ids.ts @@ -9,7 +9,7 @@ export const ids = { bugReports: "1447723804330823763", communityFeedback: "1447726591189975210", featureRequests: "1447726510369931295", - formSubmissions: "1410435042898874471", + formSubmissions: "1448445144071147520", gaming: "1385797656307175468", general: "1385797320389431336", menteeChat: "1400589073613062204", diff --git a/src/interfaces/formSubmission.ts b/src/interfaces/formSubmission.ts index d7eeb89..93b472f 100644 --- a/src/interfaces/formSubmission.ts +++ b/src/interfaces/formSubmission.ts @@ -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; } diff --git a/src/modules/processFormSubmission.ts b/src/modules/processFormSubmission.ts index 5d9df15..a3173b7 100644 --- a/src/modules/processFormSubmission.ts +++ b/src/modules/processFormSubmission.ts @@ -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, - // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. - request: FastifyRequest<{ Body: FormSubmission }>, + request: FastifyRequest<{ + // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. + Body: Array; + // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. + Querystring: { form?: string }; + }>, response: FastifyReply, ): Promise => { - 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,16 +48,16 @@ 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}`, - type: 10, + content: `${ + form ?? "Unknown Form" + } Submission Received!\n\nRow ID(s): ${submissionIds.join(", ")}`, + type: 10, }, { components: [ @@ -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), + }); }; diff --git a/src/server/serve.ts b/src/server/serve.ts index ebbf62f..1dadf47 100644 --- a/src/server/serve.ts +++ b/src/server/serve.ts @@ -70,7 +70,7 @@ export const instantiateServer = (amari: Amari): void => { }); server.post<{ - // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. + // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. Body: GithubPayload; // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. Querystring: { secret: string }; @@ -87,16 +87,19 @@ export const instantiateServer = (amari: Amari): void => { server. // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. - post<{ Body: FormSubmission }>("/form", async(request, response) => { - try { - await processFormSubmission(amari, request, response); - } catch (error) { - if (!(error instanceof Error)) { - return; + post<{ Body: Array; Querystring: { form: string } }>( + "/form", + async(request, response) => { + try { + await processFormSubmission(amari, request, response); + } catch (error) { + if (!(error instanceof Error)) { + return; + } + await logger.error("/form route", error); } - await logger.error("/form route", error); - } - }); + }, + ); server.listen({ port: 7044 }, (error) => { if (error) {