@@ -3,7 +3,9 @@ import { BanService } from "./ban";
33import { MESSAGE } from "../../helper/message" ;
44import { UserService } from "../db/user" ;
55import { WarningServiceDb } from "../db/user/warning" ;
6-
6+ import { Logger } from "../../config/logger" ;
7+ import { SafeExecution } from "../../decorators/SafeExecution" ;
8+ const logger = new Logger ( { file :"warnService.log" , level :'debug' , timestampFormat :'locale' , } )
79export class WarnService {
810 private userId : number ;
911 private ctx : Context ;
@@ -20,10 +22,11 @@ export class WarnService {
2022 /**
2123 * Adds a warning to a user.
2224 */
25+ // @SafeExecution ()
2326 async warn ( reason : string = "unknown" ) {
2427 let user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
25-
2628 if ( ! user ) {
29+ logger . info ( 'User not found, creating new user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
2730 user = await this . userRepo . createUser ( this . ctx , this . userId ) ;
2831 }
2932 // Create and save the new warning
@@ -32,16 +35,19 @@ export class WarnService {
3235 reason,
3336 } ) ;
3437 await this . warnRepo . save ( warning ) ;
35-
38+ logger . info ( 'Warning added to user' , 'WARN_SERVICE' , { userId : this . userId , warningId : warning . id } ) ;
3639 // Update user warnings list
3740 user . warnings . push ( warning ) ;
3841 await this . userRepo . save ( user ) ;
42+ logger . debug ( 'User warnings updated' , 'WARN_SERVICE' , { userId : this . userId , warningCount : user . warnings . length } ) ;
3943
4044 // Check warning count
41- const warningCount = await this . warnRepo . count ( user )
45+ const warningCount = await this . warnRepo . count ( user ) ;
4246 if ( warningCount >= 3 ) {
47+ logger . warn ( 'User has reached warning limit' , 'WARN_SERVICE' , { userId : this . userId , warningCount } ) ;
4348 const msg : string = await new BanService ( this . ctx , this . userId ) . ban ( ) ;
44- await this . warnRepo . clear ( { user } ) ;
49+ await this . warnRepo . clear ( user . id ) ;
50+ logger . info ( 'User banned and warnings cleared' , 'WARN_SERVICE' , { userId : this . userId , message : msg } ) ;
4551 return { warning, banned : true , message : msg } ;
4652 }
4753 return { warning, banned : false , count : warningCount } ;
@@ -50,29 +56,39 @@ export class WarnService {
5056 /**
5157 * Clears all warnings for a user.
5258 */
59+ @SafeExecution ( )
5360 async clear ( ) {
61+ logger . debug ( 'Attempting to clear warnings for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
5462 const user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
5563 if ( ! user ) {
64+ logger . info ( 'No warnings found for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
5665 return MESSAGE . NO_WARNINGS ( ) ;
5766 }
5867
59- await this . warnRepo . clear ( user . warnings ! ) ;
68+ await this . warnRepo . clear ( user . id ) ;
6069 user . warnings = [ ] ;
6170 await this . userRepo . save ( user ) ;
71+ logger . info ( 'All warnings cleared for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
6272
6373 return MESSAGE . WARN_CLEAR ( ) ;
6474 }
6575
6676 /**
6777 * Counts the number of warnings for a user.
6878 */
79+ @SafeExecution ( )
6980 async count ( ) : Promise < number > {
70- const user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
81+ logger . debug ( 'Counting warnings for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
7182
83+ const user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
7284 if ( ! user ) {
85+ logger . info ( 'No user found for warning count' , 'WARN_SERVICE' , { userId : this . userId } ) ;
7386 return 0 ;
7487 }
7588
76- return user . warnings . length ;
89+ const warningCount = user . warnings . length ;
90+ logger . debug ( 'Warning count for user' , 'WARN_SERVICE' , { userId : this . userId , warningCount } ) ;
91+
92+ return warningCount ;
7793 }
7894}
0 commit comments