generated from nhcarrigan/template
This commit is contained in:
+1
-1
@@ -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",
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
request: FastifyRequest<{
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||||
request: FastifyRequest<{ Body: FormSubmission }>,
|
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,15 +48,15 @@ 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: `${
|
||||||
|
form ?? "Unknown Form"
|
||||||
|
} Submission Received!\n\nRow ID(s): ${submissionIds.join(", ")}`,
|
||||||
type: 10,
|
type: 10,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -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),
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-2
@@ -87,7 +87,9 @@ 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 } }>(
|
||||||
|
"/form",
|
||||||
|
async(request, response) => {
|
||||||
try {
|
try {
|
||||||
await processFormSubmission(amari, request, response);
|
await processFormSubmission(amari, request, response);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -96,7 +98,8 @@ export const instantiateServer = (amari: Amari): void => {
|
|||||||
}
|
}
|
||||||
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user