@@ -2,13 +2,14 @@ import { Context } from 'grammy';
22import { AdminValidationService } from './validation' ;
33import { ServiceProvider } from '../../../service/database/ServiceProvider' ;
44import { MuteService } from './Mute' ;
5+ import logger from '../../../utils/logger' ;
56
67export class WarnService {
78 static async warnUser ( ctx : Context ) : Promise < {
89 warningApplied : boolean ;
910 isWarningLimitReached : boolean ;
1011 warnings : number ;
11- } > {
12+ } | null > {
1213 const validationResult = await AdminValidationService . validateContext ( ctx ) ;
1314 if ( ! validationResult ) {
1415 return {
@@ -22,6 +23,10 @@ export class WarnService {
2223 const input = ctx . message ?. text ! . split ( / \s + / ) . slice ( 1 ) ;
2324 const reason = input ! . join ( ' ' ) ?. toLowerCase ( ) || 'reason is not set for warning' ;
2425 const [ groupService , userService , warnService ] = await Promise . all ( [ services . getGroupService ( ) , services . getUserService ( ) , services . getWarnsService ( ) ] ) ;
26+ if ( ! groupService || ! userService || ! warnService ) {
27+ logger . warn ( 'services unavailable. Skipping command execution.' ) ;
28+ return null ;
29+ }
2530 let group = ( await groupService . getByGroupId ( groupId ) ) ! ;
2631 let user = ( await userService . getByTelegramId ( userId ) ) ! ;
2732 let warn = await warnService . getByGroupId ( groupId ) ;
@@ -72,7 +77,7 @@ export class WarnService {
7277 warnings : updatedUser . warnings ,
7378 } ;
7479 }
75- static async removeWarn ( ctx : Context ) : Promise < { warningRemoved : boolean ; warnings : number } > {
80+ static async removeWarn ( ctx : Context ) : Promise < { warningRemoved : boolean ; warnings : number } | null > {
7681 const validationResult = await AdminValidationService . validateContext ( ctx ) ;
7782 if ( ! validationResult ) {
7883 return { warningRemoved : false , warnings : 0 } ;
@@ -81,6 +86,10 @@ export class WarnService {
8186 const { userId } = validationResult ;
8287 const services = ServiceProvider . getInstance ( ) ;
8388 const userService = await services . getUserService ( ) ;
89+ if ( ! userService ) {
90+ logger . warn ( 'services unavailable. Skipping command execution.' ) ;
91+ return null ;
92+ }
8493 let user = ( await userService . getByTelegramId ( userId ) ) ! ;
8594 const updatedUser = {
8695 ...user ,
@@ -92,9 +101,13 @@ export class WarnService {
92101 warnings : updatedUser . warnings ,
93102 } ;
94103 }
95- static async getUserWarnById ( ctx : Context , userId : number ) : Promise < { warnings : number } > {
104+ static async getUserWarnById ( ctx : Context , userId : number ) : Promise < { warnings : number } | null > {
96105 const services = ServiceProvider . getInstance ( ) ;
97106 const userService = await services . getUserService ( ) ;
107+ if ( ! userService ) {
108+ logger . warn ( 'services unavailable. Skipping command execution.' ) ;
109+ return null ;
110+ }
98111 let user = await userService . getByTelegramId ( userId ) ;
99112 let replyMessage = ctx . message ?. reply_to_message ?. from ;
100113 const userData = { first_name : replyMessage ?. first_name ! , id : userId , username : replyMessage ?. username ! } ;
@@ -103,7 +116,7 @@ export class WarnService {
103116 }
104117 return { warnings : user . warnings } ;
105118 }
106- static async getAllWarns ( ctx : Context ) : Promise < string > {
119+ static async getAllWarns ( ctx : Context ) : Promise < string | null > {
107120 const replyMessage = ctx . from ;
108121 // Ensure the user ID of the replied message is valid
109122 const userId = replyMessage ! . id ! ;
@@ -113,6 +126,10 @@ export class WarnService {
113126 // Initialize services
114127 const services = ServiceProvider . getInstance ( ) ;
115128 const [ groupService , userService , warnService ] = await Promise . all ( [ services . getGroupService ( ) , services . getUserService ( ) , services . getWarnsService ( ) ] ) ;
129+ if ( ! userService || ! groupService || ! warnService ) {
130+ logger . warn ( 'services unavailable. Skipping command execution.' ) ;
131+ return null ;
132+ }
116133 let group = await groupService . getByGroupId ( groupId ) ;
117134 let user = await userService . getByTelegramId ( userId ) ;
118135 const userData = { first_name : ctx ! . message ?. reply_to_message ?. from ?. first_name ! , id : userId , username : ctx . message ?. reply_to_message ?. from ?. username ! } ;
0 commit comments