style: move button
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { submissionWebhookMessage } from "@/lib/submission-webhook";
|
||||
import type {
|
||||
NormalizedSubmissionAnswer,
|
||||
SubmissionQuestion,
|
||||
} from "@/lib/submission-intake";
|
||||
|
||||
const questions: SubmissionQuestion[] = [
|
||||
{
|
||||
id: "name",
|
||||
type: "text",
|
||||
label: "Character name",
|
||||
required: true,
|
||||
options: [],
|
||||
allowOther: false,
|
||||
},
|
||||
];
|
||||
|
||||
const answers: NormalizedSubmissionAnswer[] = [
|
||||
{
|
||||
questionId: "name",
|
||||
value: "Erika",
|
||||
displayValue: "Erika",
|
||||
},
|
||||
];
|
||||
|
||||
test("uses the configured template", () => {
|
||||
expect(submissionWebhookMessage({
|
||||
template: "New answer: {1} by {username} ({id})",
|
||||
isUpdate: false,
|
||||
userName: "Gunshiz",
|
||||
discordId: "123",
|
||||
questions,
|
||||
answers,
|
||||
oldAnswersByQuestionId: {},
|
||||
})).toBe("New answer: Erika by Gunshiz (123)");
|
||||
});
|
||||
|
||||
describe("default templates", () => {
|
||||
test("sends a new-submission message when its template is blank", () => {
|
||||
expect(submissionWebhookMessage({
|
||||
template: null,
|
||||
isUpdate: false,
|
||||
userName: "Gunshiz",
|
||||
discordId: "123",
|
||||
questions,
|
||||
answers,
|
||||
oldAnswersByQuestionId: {},
|
||||
})).toBe("New submission from Gunshiz (123).");
|
||||
});
|
||||
|
||||
test("sends an update message when its template is blank", () => {
|
||||
expect(submissionWebhookMessage({
|
||||
template: " ",
|
||||
isUpdate: true,
|
||||
userName: "Gunshiz",
|
||||
discordId: "123",
|
||||
questions,
|
||||
answers,
|
||||
oldAnswersByQuestionId: { name: "Old name" },
|
||||
})).toContain("Submission updated by Gunshiz (123).");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
answersByTemplateIndex,
|
||||
submissionUpdateText,
|
||||
type NormalizedSubmissionAnswer,
|
||||
type SubmissionQuestion,
|
||||
} from "@/lib/submission-intake";
|
||||
|
||||
const NEW_SUBMISSION_TEMPLATE = "New submission from {username} ({id}).";
|
||||
const UPDATED_SUBMISSION_TEMPLATE = "Submission updated by {username} ({id}).\n{update}";
|
||||
|
||||
type SubmissionWebhookMessageOptions = {
|
||||
template: string | null;
|
||||
isUpdate: boolean;
|
||||
userName: string | null | undefined;
|
||||
discordId: string;
|
||||
questions: SubmissionQuestion[];
|
||||
answers: NormalizedSubmissionAnswer[];
|
||||
oldAnswersByQuestionId: Record<string, string>;
|
||||
};
|
||||
|
||||
export function submissionWebhookMessage({
|
||||
template,
|
||||
isUpdate,
|
||||
userName,
|
||||
discordId,
|
||||
questions,
|
||||
answers,
|
||||
oldAnswersByQuestionId,
|
||||
}: SubmissionWebhookMessageOptions) {
|
||||
const answersByIndex = answersByTemplateIndex(questions, answers);
|
||||
const updatesText = isUpdate
|
||||
? submissionUpdateText(questions, answers, oldAnswersByQuestionId)
|
||||
: "";
|
||||
const messageTemplate = template?.trim()
|
||||
|| (isUpdate ? UPDATED_SUBMISSION_TEMPLATE : NEW_SUBMISSION_TEMPLATE);
|
||||
|
||||
return messageTemplate.replace(/\{([^}]+)\}/g, (_match, variable: string) => {
|
||||
const key = variable.toLowerCase();
|
||||
if (key === "username" || key === "displayname") {
|
||||
return userName || "Unknown User";
|
||||
}
|
||||
if (key === "id") {
|
||||
return discordId;
|
||||
}
|
||||
if (key === "update") {
|
||||
return updatesText;
|
||||
}
|
||||
return answersByIndex[variable] || "Not Provided";
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user