@@ -3,53 +3,60 @@ import { BanService } from "./ban";
33import { MESSAGE } from "../../helper/message" ;
44import { UserService } from "../db/user" ;
55import { WarningServiceDb } from "../db/user/warning" ;
6- import { Logger } from "../../config/logger" ;
76import { SafeExecution } from "../../decorators/SafeExecution" ;
8- const logger = new Logger ( { file : "warnService.log" , level : 'debug' , timestampFormat : 'locale' , } )
7+ import { GroupSettingsService } from "../db/group" ;
98export class WarnService {
109 private userId : number ;
1110 private ctx : Context ;
1211 private userRepo : UserService ;
1312 private warnRepo : WarningServiceDb ;
14-
13+ private groupRepo : GroupSettingsService
1514 constructor ( ctx : Context , userId : number ) {
1615 this . userId = userId ;
1716 this . ctx = ctx ;
1817 this . userRepo = new UserService ( ) ;
1918 this . warnRepo = new WarningServiceDb ( ) ;
19+ this . groupRepo = new GroupSettingsService ( )
2020 }
2121
2222 /**
2323 * Adds a warning to a user.
2424 */
2525 // @SafeExecution ()
2626 async warn ( reason : string = "unknown" ) {
27- let user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
27+ let user = await this . userRepo . findByRelations (
28+ this . userId ,
29+ "warnings" ,
30+ ) ;
2831 if ( ! user ) {
29- logger . info ( 'User not found, creating new user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
3032 user = await this . userRepo . createUser ( this . ctx , this . userId ) ;
3133 }
34+ const groupId = this . ctx . chat ?. id ! ;
35+
36+ // Check if user is already a member of the group, if not, add them
37+ const group = await this . groupRepo . getByGroupId ( groupId ) ;
38+ if ( ! group ) {
39+ await this . groupRepo . init ( this . ctx )
40+ }
41+
3242 // Create and save the new warning
33- const warning = await this . warnRepo . create ( {
43+ const warning = this . warnRepo . create ( {
3444 user,
45+ group : group ! ,
3546 reason,
3647 } ) ;
37- await this . warnRepo . save ( warning ) ;
38- logger . info ( 'Warning added to user' , 'WARN_SERVICE' , { userId : this . userId , warningId : warning . id } ) ;
48+ await this . warnRepo . save ( warning )
3949 // Update user warnings list
4050 user . warnings . push ( warning ) ;
4151 await this . userRepo . save ( user ) ;
42- logger . debug ( 'User warnings updated' , 'WARN_SERVICE' , { userId : this . userId , warningCount : user . warnings . length } ) ;
43-
4452 // Check warning count
4553 const warningCount = await this . warnRepo . count ( user ) ;
4654 if ( warningCount >= 3 ) {
47- logger . warn ( 'User has reached warning limit' , 'WARN_SERVICE' , { userId : this . userId , warningCount } ) ;
4855 const msg : string = await new BanService ( this . ctx , this . userId ) . ban ( ) ;
4956 await this . warnRepo . clear ( user . id ) ;
50- logger . info ( 'User banned and warnings cleared' , 'WARN_SERVICE' , { userId : this . userId , message : msg } ) ;
5157 return { warning, banned : true , message : msg } ;
5258 }
59+
5360 return { warning, banned : false , count : warningCount } ;
5461 }
5562
@@ -58,18 +65,14 @@ export class WarnService {
5865 */
5966 @SafeExecution ( )
6067 async clear ( ) {
61- logger . debug ( 'Attempting to clear warnings for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
6268 const user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
6369 if ( ! user ) {
64- logger . info ( 'No warnings found for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
6570 return MESSAGE . NO_WARNINGS ( ) ;
6671 }
6772
6873 await this . warnRepo . clear ( user . id ) ;
6974 user . warnings = [ ] ;
7075 await this . userRepo . save ( user ) ;
71- logger . info ( 'All warnings cleared for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
72-
7376 return MESSAGE . WARN_CLEAR ( ) ;
7477 }
7578
@@ -78,17 +81,12 @@ export class WarnService {
7881 */
7982 @SafeExecution ( )
8083 async count ( ) : Promise < number > {
81- logger . debug ( 'Counting warnings for user' , 'WARN_SERVICE' , { userId : this . userId } ) ;
82-
8384 const user = await this . userRepo . findByRelations ( this . userId , "warnings" ) ;
8485 if ( ! user ) {
85- logger . info ( 'No user found for warning count' , 'WARN_SERVICE' , { userId : this . userId } ) ;
8686 return 0 ;
8787 }
8888
8989 const warningCount = user . warnings . length ;
90- logger . debug ( 'Warning count for user' , 'WARN_SERVICE' , { userId : this . userId , warningCount } ) ;
91-
9290 return warningCount ;
9391 }
9492}
0 commit comments