11package com .example .solidconnection .admin .service ;
22
3+ import static java .time .ZoneOffset .UTC ;
4+
35import com .example .solidconnection .admin .dto .UserBanRequest ;
46import com .example .solidconnection .chat .repository .ChatMessageRepository ;
57import com .example .solidconnection .common .exception .CustomException ;
1113import com .example .solidconnection .siteuser .domain .UserStatus ;
1214import com .example .solidconnection .siteuser .repository .SiteUserRepository ;
1315import com .example .solidconnection .siteuser .repository .UserBanRepository ;
14- import static java .time .ZoneOffset .UTC ;
15-
1616import java .time .ZonedDateTime ;
1717import java .util .List ;
18-
1918import lombok .RequiredArgsConstructor ;
2019import lombok .extern .slf4j .Slf4j ;
21-
2220import org .springframework .scheduling .annotation .Scheduled ;
2321import org .springframework .stereotype .Service ;
2422import org .springframework .transaction .annotation .Transactional ;
@@ -35,18 +33,19 @@ public class AdminUserBanService {
3533 private final ChatMessageRepository chatMessageRepository ;
3634
3735 @ Transactional
38- public void banUser (long userId , UserBanRequest request ) {
39- ZonedDateTime now = ZonedDateTime .now (UTC );
40- validateNotAlreadyBanned (userId , now );
36+ public void banUser (long userId , long adminId , UserBanRequest request ) {
37+ SiteUser user = siteUserRepository .findById (userId )
38+ .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
39+ validateNotAlreadyBanned (userId );
4140 validateReportExists (userId );
4241
42+ user .updateUserStatus (UserStatus .BANNED );
4343 updateReportedContentIsDeleted (userId , true );
44- createUserBan (userId , request , now );
45- updateUserStatus (userId , UserStatus .BANNED );
44+ createUserBan (userId , adminId , request );
4645 }
4746
48- private void validateNotAlreadyBanned (long userId , ZonedDateTime now ) {
49- if (userBanRepository .existsByBannedUserIdAndIsUnbannedFalseAndExpiredAtAfter (userId , now )) {
47+ private void validateNotAlreadyBanned (long userId ) {
48+ if (userBanRepository .existsByBannedUserIdAndIsExpiredFalseAndExpiredAtAfter (userId , ZonedDateTime . now ( UTC ) )) {
5049 throw new CustomException (ErrorCode .ALREADY_BANNED_USER );
5150 }
5251 }
@@ -62,42 +61,53 @@ private void updateReportedContentIsDeleted(long userId, boolean isDeleted) {
6261 chatMessageRepository .updateReportedChatMessagesIsDeleted (userId , isDeleted );
6362 }
6463
65- private void createUserBan (long userId , UserBanRequest request , ZonedDateTime now ) {
64+ private void createUserBan (long userId , long adminId , UserBanRequest request ) {
65+ ZonedDateTime now = ZonedDateTime .now (UTC );
6666 ZonedDateTime expiredAt = now .plusDays (request .duration ().getDays ());
67- UserBan userBan = new UserBan (userId , request .duration (), expiredAt );
67+ UserBan userBan = new UserBan (userId , adminId , request .duration (), expiredAt );
6868 userBanRepository .save (userBan );
6969 }
7070
71- private void updateUserStatus (long userId , UserStatus status ) {
72- SiteUser user = siteUserRepository .findById (userId )
73- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
74- user .updateUserStatus (status );
75- }
76-
7771 @ Transactional
7872 public void unbanUser (long userId , long adminId ) {
79- UserBan userBan = findBannedUser (userId , ZonedDateTime .now (UTC ));
73+ SiteUser user = siteUserRepository .findById (userId )
74+ .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
75+ UserBan userBan = findActiveBan (userId );
8076 userBan .manuallyUnban (adminId );
81- processUnban (userId );
77+
78+ user .updateUserStatus (UserStatus .REPORTED );
79+ updateReportedContentIsDeleted (userId , false );
8280 }
8381
84- private UserBan findBannedUser (long userId , ZonedDateTime now ) {
82+ private UserBan findActiveBan (long userId ) {
8583 return userBanRepository
86- .findTopByBannedUserIdAndIsUnbannedFalseAndExpiredAtAfterOrderByCreatedAtDesc (userId , now )
84+ .findByBannedUserIdAndIsExpiredFalseAndExpiredAtAfter (userId , ZonedDateTime . now ( UTC ) )
8785 .orElseThrow (() -> new CustomException (ErrorCode .NOT_BANNED_USER ));
8886 }
8987
9088 @ Transactional
9189 @ Scheduled (cron = "0 0 0 * * *" )
9290 public void expireUserBans () {
93- List <UserBan > expiredBans = userBanRepository .findAllByIsUnbannedFalseAndExpiredAtBefore (ZonedDateTime .now (UTC ));
94- for (UserBan userBan : expiredBans ) {
95- processUnban (userBan .getBannedUserId ());
91+ try {
92+ ZonedDateTime now = ZonedDateTime .now (UTC );
93+ List <Long > expiredUserIds = userBanRepository .findExpiredBannedUserIds (now );
94+
95+ if (expiredUserIds .isEmpty ()) {
96+ return ;
97+ }
98+
99+ userBanRepository .bulkExpireUserBans (now );
100+ siteUserRepository .bulkUpdateUserStatus (expiredUserIds , UserStatus .REPORTED );
101+ bulkUpdateReportedContentIsDeleted (expiredUserIds );
102+ log .info ("Finished processing expired blocks:: userIds={}" , expiredUserIds );
103+ } catch (Exception e ) {
104+ log .error ("Failed to process expired blocks" , e );
96105 }
97106 }
98107
99- private void processUnban ( long userId ) {
100- updateReportedContentIsDeleted ( userId , false );
101- updateUserStatus ( userId , UserStatus . REPORTED );
108+ private void bulkUpdateReportedContentIsDeleted ( List < Long > expiredUserIds ) {
109+ postRepository . bulkUpdateReportedPostsIsDeleted ( expiredUserIds , false );
110+ chatMessageRepository . bulkUpdateReportedChatMessagesIsDeleted ( expiredUserIds , false );
102111 }
112+
103113}
0 commit comments