mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import type { ActionFunctionArgs } from "react-router";
|
|
import { redirect } from "react-router";
|
|
import { requireUser } from "~/features/auth/core/user.server";
|
|
import * as PlusSuggestionRepository from "~/features/plus-suggestions/PlusSuggestionRepository.server";
|
|
import {
|
|
nextNonCompletedVoting,
|
|
rangeToMonthYear,
|
|
} from "~/features/plus-voting/core";
|
|
import {
|
|
badRequestIfFalsy,
|
|
errorToastIfFalsy,
|
|
parseRequestPayload,
|
|
} from "~/utils/remix.server";
|
|
import { plusSuggestionPage } from "~/utils/urls";
|
|
import { followUpCommentActionSchema } from "../plus-suggestions-schemas";
|
|
import { canAddCommentToSuggestionBE } from "../plus-suggestions-utils";
|
|
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
const data = await parseRequestPayload({
|
|
request,
|
|
schema: followUpCommentActionSchema,
|
|
});
|
|
const user = requireUser();
|
|
|
|
const votingMonthYear = rangeToMonthYear(
|
|
badRequestIfFalsy(nextNonCompletedVoting(new Date())),
|
|
);
|
|
|
|
const suggestions =
|
|
await PlusSuggestionRepository.findAllByMonth(votingMonthYear);
|
|
|
|
errorToastIfFalsy(
|
|
canAddCommentToSuggestionBE({
|
|
suggestions,
|
|
user,
|
|
suggested: { id: data.suggestedId },
|
|
targetPlusTier: data.tier,
|
|
}),
|
|
"No permissions to add this comment",
|
|
);
|
|
|
|
await PlusSuggestionRepository.create({
|
|
authorId: user.id,
|
|
suggestedId: data.suggestedId,
|
|
text: data.comment,
|
|
tier: data.tier,
|
|
...votingMonthYear,
|
|
});
|
|
|
|
throw redirect(plusSuggestionPage({ tier: data.tier }));
|
|
};
|