feat: webhook for forms
Node.js CI / Lint and Test (push) Successful in 45s

This commit is contained in:
2025-08-27 18:55:22 -07:00
parent b92fe0a730
commit d5c0abe4d8
6 changed files with 140 additions and 2 deletions
+70
View File
@@ -0,0 +1,70 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
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";
import type { FormSubmission } from "../interfaces/formSubmission.js";
import type { FastifyRequest, FastifyReply } from "fastify";
/**
* Logs when a form is submitted through our Baserow instance.
* @param amari - Amari's intance.
* @param request - The fastify request payload.
* @param response - The fastify reply class.
*/
export const processFormSubmission = async(
amari: Amari,
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
request: FastifyRequest<{ Body: FormSubmission }>,
response: FastifyReply,
): Promise<void> => {
const { secret } = request.headers;
if (secret !== process.env.BASEROW_SECRET
|| process.env.BASEROW_SECRET === undefined) {
await response.status(403).send({ message: "Invalid Secret Provided." });
return;
}
await response.status(204).send();
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(
"warn",
"Form submission channel does not exist or is not sendable.",
);
return;
}
const { table_id: table, items } = request.body;
const rowIds = items.map((item) => {
return item.id;
}).join(", ");
const tableName = formIds[table];
await channel.send({
components: [
{
content: `${tableName ?? "Unknown Form"} Submission Received!\n\nRow ID(s): ${rowIds}`,
type: 10,
},
{
components: [
{
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement for Discord.
custom_id: "resolve",
disabled: false,
label: "Confirm",
style: 3,
type: 2,
},
],
type: 1,
},
],
flags: [ MessageFlags.IsComponentsV2 ],
});
};