feat: update form logic for new platform
Node.js CI / Lint and Test (push) Successful in 26s

This commit is contained in:
2025-12-10 14:52:36 -08:00
parent 53274ec38c
commit 9d9d0809d7
4 changed files with 41 additions and 30 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ export const ids = {
bugReports: "1447723804330823763", bugReports: "1447723804330823763",
communityFeedback: "1447726591189975210", communityFeedback: "1447726591189975210",
featureRequests: "1447726510369931295", featureRequests: "1447726510369931295",
formSubmissions: "1410435042898874471", formSubmissions: "1448445144071147520",
gaming: "1385797656307175468", gaming: "1385797656307175468",
general: "1385797320389431336", general: "1385797320389431336",
menteeChat: "1400589073613062204", menteeChat: "1400589073613062204",
+3 -4
View File
@@ -4,9 +4,8 @@
* @author Naomi Carrigan * @author Naomi Carrigan
*/ */
/* eslint-disable @typescript-eslint/naming-convention -- Baserow uses snake case */
export interface FormSubmission { export interface FormSubmission {
table_id: number; [key: string]: unknown;
items: Array<{ id: number }>; id: number;
manualSort: number;
} }
+24 -15
View File
@@ -5,7 +5,6 @@
*/ */
import { MessageFlags } from "discord.js"; import { MessageFlags } from "discord.js";
import { formIds } from "../config/forms.js";
import { ids } from "../config/ids.js"; import { ids } from "../config/ids.js";
import { logger } from "../utils/logger.js"; import { logger } from "../utils/logger.js";
import type { Amari } from "../interfaces/amari.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. // eslint-disable-next-line max-lines-per-function -- only long because of analytics.
export const processFormSubmission = async( export const processFormSubmission = async(
amari: Amari, amari: Amari,
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. request: FastifyRequest<{
request: FastifyRequest<{ Body: FormSubmission }>, // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
Body: Array<FormSubmission>;
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
Querystring: { form?: string };
}>,
response: FastifyReply, response: FastifyReply,
): Promise<void> => { ): Promise<void> => {
const { secret } = request.headers; const { authorization: secret } = request.headers;
if (secret !== process.env.BASEROW_SECRET if (
|| process.env.BASEROW_SECRET === undefined) { secret !== process.env.BASEROW_SECRET
|| secret === undefined
) {
await response.status(403).send({ message: "Invalid Secret Provided." }); await response.status(403).send({ message: "Invalid Secret Provided." });
return; return;
} }
const { form } = request.query;
await response.status(204).send(); 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); ?? await amari.discord.channels.fetch(ids.channels.formSubmissions);
if (channel?.isSendable() !== true) { if (channel?.isSendable() !== true) {
await logger.log( await logger.log(
@@ -41,16 +48,16 @@ export const processFormSubmission = async(
); );
return; return;
} }
const { table_id: table, items } = request.body; const submissionIds = request.body.map((item) => {
const rowIds = items.map((item) => { return `${item.id.toString()} (${item.manualSort.toString()})`;
return item.id; });
}).join(", ");
const tableName = formIds[table];
await channel.send({ await channel.send({
components: [ components: [
{ {
content: `${tableName ?? "Unknown Form"} Submission Received!\n\nRow ID(s): ${rowIds}`, content: `${
type: 10, form ?? "Unknown Form"
} Submission Received!\n\nRow ID(s): ${submissionIds.join(", ")}`,
type: 10,
}, },
{ {
components: [ components: [
@@ -68,5 +75,7 @@ export const processFormSubmission = async(
], ],
flags: [ MessageFlags.IsComponentsV2 ], flags: [ MessageFlags.IsComponentsV2 ],
}); });
await logger.metric("processed_form_submission", 1, { table: String(table) }); await logger.metric("processed_form_submission", 1, {
table: String(request.body),
});
}; };
+13 -10
View File
@@ -70,7 +70,7 @@ export const instantiateServer = (amari: Amari): void => {
}); });
server.post<{ server.post<{
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
Body: GithubPayload; Body: GithubPayload;
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
Querystring: { secret: string }; Querystring: { secret: string };
@@ -87,16 +87,19 @@ export const instantiateServer = (amari: Amari): void => {
server. server.
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard. // eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
post<{ Body: FormSubmission }>("/form", async(request, response) => { post<{ Body: Array<FormSubmission>; Querystring: { form: string } }>(
try { "/form",
await processFormSubmission(amari, request, response); async(request, response) => {
} catch (error) { try {
if (!(error instanceof Error)) { await processFormSubmission(amari, request, response);
return; } catch (error) {
if (!(error instanceof Error)) {
return;
}
await logger.error("/form route", error);
} }
await logger.error("/form route", error); },
} );
});
server.listen({ port: 7044 }, (error) => { server.listen({ port: 7044 }, (error) => {
if (error) { if (error) {