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",
communityFeedback: "1447726591189975210",
featureRequests: "1447726510369931295",
formSubmissions: "1410435042898874471",
formSubmissions: "1448445144071147520",
gaming: "1385797656307175468",
general: "1385797320389431336",
menteeChat: "1400589073613062204",
+3 -4
View File
@@ -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;
}
+24 -15
View File
@@ -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<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,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),
});
};
+13 -10
View File
@@ -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<FormSubmission>; 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) {