mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-02 16:20:36 -05:00
Can't delete suggestion if it has comments
This commit is contained in:
@@ -46,7 +46,11 @@ interface CanDeleteCommentArgs {
|
||||
}
|
||||
export function canDeleteComment(args: CanDeleteCommentArgs) {
|
||||
if (isFirstSuggestion(args)) {
|
||||
return allTruthy([!isVotingActive(), isOwnComment(args)]);
|
||||
return allTruthy([
|
||||
!isVotingActive(),
|
||||
isOwnComment(args),
|
||||
suggestionHasNoOtherComments(args),
|
||||
]);
|
||||
}
|
||||
|
||||
return isOwnComment(args);
|
||||
@@ -106,6 +110,21 @@ function isOwnComment({ author, user }: CanDeleteCommentArgs) {
|
||||
return author.id === user?.id;
|
||||
}
|
||||
|
||||
function suggestionHasNoOtherComments({
|
||||
suggestions,
|
||||
suggestionId,
|
||||
}: Pick<CanDeleteCommentArgs, "suggestionId" | "suggestions">) {
|
||||
for (const suggestedUser of Object.values(suggestions).flat()) {
|
||||
for (const suggestion of suggestedUser.suggestions) {
|
||||
if (suggestion.id !== suggestionId) continue;
|
||||
|
||||
return suggestedUser.suggestions.length === 1;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Invalid suggestion id: ${suggestionId}`);
|
||||
}
|
||||
|
||||
interface CanSuggestNewUserFEArgs {
|
||||
user?: Pick<User, "id" | "plusTier">;
|
||||
suggestions: plusSuggestions.FindVisibleForUser;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// xxx: should instead take array of functions to improve efficiency (not run them all if not necessary)
|
||||
export function allTruthy(arr: unknown[]) {
|
||||
return arr.every(Boolean);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user