Skip to content

Commit a6054e4

Browse files
committed
Add auto-review logic to reject banned users' stories
1 parent c5e6664 commit a6054e4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

backend/src/business/stories/StoriesService.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { getRepository } from 'typeorm';
12
import Story from '../../entities/Story';
3+
import User from '../../entities/User';
24
import StoryState from '../../enum/StoryState';
35
import StoryRepository from '../../repositories/StoryRepository';
46
import {
@@ -20,6 +22,7 @@ function getStoryOrThrow(
2022

2123
async function onStorySubmitted(storyId: Story['id']): Promise<void> {
2224
const story = await getStoryOrThrow(storyId, StoryState.SUBMITTED);
25+
const userRepository = getRepository(User);
2326

2427
const hasSubmittedBefore = story.hasEverSubmitted;
2528

@@ -36,6 +39,22 @@ async function onStorySubmitted(storyId: Story['id']): Promise<void> {
3639
await StoryRepository().update(story.id, {
3740
hasEverSubmitted: true,
3841
});
42+
43+
try {
44+
// If user with this email is banned, reject the story
45+
const maybeUser = await userRepository.findOneBy({
46+
email: story.storytellerEmail ?? '',
47+
});
48+
const isUserBanned = maybeUser?.isBanned ?? false;
49+
if (isUserBanned) {
50+
await StoryRepository().update(story.id, {
51+
state: StoryState.REJECTED,
52+
lastReviewer: 'system',
53+
});
54+
}
55+
} catch (e) {
56+
console.error('Error auto-reviewing story', e);
57+
}
3958
}
4059

4160
async function onStoryPublished(storyId: Story['id']): Promise<void> {

0 commit comments

Comments
 (0)