Can't delete suggestion if it has comments

This commit is contained in:
Kalle
2022-06-04 10:57:31 +03:00
parent 6f3812bbdc
commit ed12978073
2 changed files with 21 additions and 1 deletions

View File

@@ -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;

View File

@@ -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);
}