generated from nhcarrigan/template
71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
/**
|
|
* @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 ],
|
|
});
|
|
};
|