@@ -4,18 +4,17 @@ import { Message } from 'typegram'
4
4
import { UserDocument } from '../models/user'
5
5
import { FilterQuery } from 'mongoose' ;
6
6
const OrderEvents = require ( './modules/events/orders' ) ;
7
-
8
- const { limit } = require ( '@grammyjs/ratelimiter' ) ;
7
+ import { limit } from "@grammyjs/ratelimiter"
9
8
const schedule = require ( 'node-schedule' ) ;
10
- const {
9
+ import {
11
10
Order ,
12
11
User ,
13
12
PendingPayment ,
14
13
Community ,
15
14
Dispute ,
16
15
Config ,
17
- } = require ( '../models' ) ;
18
- const { getCurrenciesWithPrice, deleteOrderFromChannel, removeAtSymbol } = require ( '../util' ) ;
16
+ } from '../models' ;
17
+ import { getCurrenciesWithPrice , deleteOrderFromChannel , removeAtSymbol } from '../util' ;
19
18
const {
20
19
commandArgsMiddleware,
21
20
stageMiddleware,
@@ -55,32 +54,34 @@ const {
55
54
validateLightningAddress,
56
55
} = require ( './validations' ) ;
57
56
import * as messages from './messages' ;
58
- const {
57
+ import {
59
58
attemptPendingPayments ,
60
59
cancelOrders ,
61
60
deleteOrders ,
62
61
calculateEarnings ,
63
62
attemptCommunitiesPendingPayments ,
64
63
deleteCommunity ,
65
64
nodeInfo ,
66
- } = require ( '../jobs' ) ;
67
- const { logger } = require ( '../logger' ) ;
65
+ } from '../jobs' ;
66
+ import { logger } from "../logger" ;
67
+ import { ICommunity , IUsernameId } from '../models/community' ;
68
+
68
69
export interface MainContext extends Context {
69
70
match : Array < string > | null ;
70
71
i18n : I18nContext ;
71
72
user : UserDocument ;
72
73
admin : UserDocument ;
73
74
}
74
75
75
- interface OrderQuery {
76
+ export interface OrderQuery {
76
77
status ?: string ;
77
78
buyer_id ?: string ;
78
79
seller_id ?: string ;
79
80
}
80
81
81
82
const askForConfirmation = async ( user : UserDocument , command : string ) => {
82
83
try {
83
- let orders = [ ] ;
84
+ let orders : any [ ] = [ ] ;
84
85
if ( command === '/cancel' ) {
85
86
const where : FilterQuery < OrderQuery > = {
86
87
$and : [
@@ -133,6 +134,7 @@ const askForConfirmation = async (user: UserDocument, command: string) => {
133
134
return orders ;
134
135
} catch ( error ) {
135
136
logger . error ( error ) ;
137
+ return null ;
136
138
}
137
139
} ;
138
140
@@ -145,7 +147,7 @@ has the same condition.
145
147
The problem mentioned above is similar to this issue:
146
148
https://github.com/telegraf/telegraf/issues/1319#issuecomment-766360594
147
149
*/
148
- const ctxUpdateAssertMsg = "ctx.update.message.text is not available." ;
150
+ export const ctxUpdateAssertMsg = "ctx.update.message.text is not available." ;
149
151
150
152
const initialize = ( botToken : string , options : Partial < Telegraf . Options < MainContext > > ) : Telegraf < MainContext > => {
151
153
const i18n = new I18n ( {
@@ -215,7 +217,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
215
217
const [ val ] = await validateParams ( ctx , 2 , '\\<_on/off_\\>' ) ;
216
218
if ( ! val ) return ;
217
219
let config = await Config . findOne ( ) ;
218
- if ( ! config ) {
220
+ if ( config === null ) {
219
221
config = new Config ( ) ;
220
222
}
221
223
config . maintenance = false ;
@@ -262,11 +264,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
262
264
throw new Error ( ctxUpdateAssertMsg ) ;
263
265
}
264
266
const params = ctx . update . message . text . split ( ' ' ) ;
265
- const [ command , orderId ] = params . filter ( ( el ) => el ) ;
267
+ const [ command , orderId ] = params . filter ( ( el : string ) => el ) ;
266
268
267
269
if ( ! orderId ) {
268
270
const orders = await askForConfirmation ( ctx . user , command ) ;
269
- if ( ! orders . length ) return await ctx . reply ( `${ command } <order Id>` ) ;
271
+ if ( orders === null || orders . length === 0 ) return await ctx . reply ( `${ command } <order Id>` ) ;
270
272
271
273
return await messages . showConfirmationButtons ( ctx , orders , command ) ;
272
274
} else if ( ! ( await validateObjectId ( ctx , orderId ) ) ) {
@@ -325,7 +327,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
325
327
if ( ! ( await validateObjectId ( ctx , orderId ) ) ) return ;
326
328
const order = await Order . findOne ( { _id : orderId } ) ;
327
329
328
- if ( ! order ) return ;
330
+ if ( order === null ) return ;
329
331
330
332
// We look for a dispute for this order
331
333
const dispute = await Dispute . findOne ( { order_id : order . _id } ) ;
@@ -367,10 +369,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
367
369
368
370
order . status = 'CANCELED_BY_ADMIN' ;
369
371
order . canceled_by = ctx . admin . _id ;
370
- const buyer = await User . findOne ( { _id : order . buyer_id } ) ;
371
- const seller = await User . findOne ( { _id : order . seller_id } ) ;
372
372
await order . save ( ) ;
373
373
OrderEvents . orderUpdated ( order ) ;
374
+ const buyer = await User . findOne ( { _id : order . buyer_id } ) ;
375
+ const seller = await User . findOne ( { _id : order . seller_id } ) ;
376
+ if ( buyer === null || seller === null ) throw Error ( "buyer and/or seller were not found in DB" ) ;
374
377
// we sent a private message to the admin
375
378
await messages . successCancelOrderMessage ( ctx , ctx . admin , order , ctx . i18n ) ;
376
379
// we sent a private message to the seller
@@ -390,11 +393,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
390
393
throw new Error ( ctxUpdateAssertMsg ) ;
391
394
}
392
395
const params = ctx . update . message . text . split ( ' ' ) ;
393
- const [ command , orderId ] = params . filter ( ( el ) => el ) ;
396
+ const [ command , orderId ] = params . filter ( ( el : string ) => el ) ;
394
397
395
398
if ( ! orderId ) {
396
399
const orders = await askForConfirmation ( ctx . user , command ) ;
397
- if ( ! orders . length ) return await ctx . reply ( `${ command } <order Id>` ) ;
400
+ if ( orders === null || orders . length === 0 ) return await ctx . reply ( `${ command } <order Id>` ) ;
398
401
399
402
return await messages . showConfirmationButtons ( ctx , orders , command ) ;
400
403
} else if ( ! ( await validateObjectId ( ctx , orderId ) ) ) {
@@ -445,7 +448,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
445
448
await order . save ( ) ;
446
449
OrderEvents . orderUpdated ( order ) ;
447
450
// We delete the messages related to that order from the channel
448
- await deleteOrderFromChannel ( order , bot . telegram ) ;
451
+ await deleteOrderFromChannel ( order , bot . telegram as any ) ;
449
452
}
450
453
// we sent a private message to the user
451
454
await messages . successCancelAllOrdersMessage ( ctx ) ;
@@ -462,7 +465,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
462
465
if ( ! ( await validateObjectId ( ctx , orderId ) ) ) return ;
463
466
464
467
const order = await Order . findOne ( { _id : orderId } ) ;
465
- if ( ! order ) return ;
468
+ if ( order === null ) return ;
466
469
467
470
// Check if the order status is already PAID_HOLD_INVOICE
468
471
if ( order . status === 'PAID_HOLD_INVOICE' ) {
@@ -498,10 +501,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
498
501
}
499
502
500
503
order . status = 'COMPLETED_BY_ADMIN' ;
501
- const buyer = await User . findOne ( { _id : order . buyer_id } ) ;
502
- const seller = await User . findOne ( { _id : order . seller_id } ) ;
503
504
await order . save ( ) ;
504
505
OrderEvents . orderUpdated ( order ) ;
506
+ const buyer = await User . findOne ( { _id : order . buyer_id } ) ;
507
+ const seller = await User . findOne ( { _id : order . seller_id } ) ;
508
+ if ( buyer === null || seller === null ) throw Error ( "buyer and/or seller were not found in DB" ) ;
505
509
// we sent a private message to the admin
506
510
await messages . successCompleteOrderMessage ( ctx , order ) ;
507
511
// we sent a private message to the seller
@@ -525,11 +529,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
525
529
if ( ! ( await validateObjectId ( ctx , orderId ) ) ) return ;
526
530
const order = await Order . findOne ( { _id : orderId } ) ;
527
531
528
- if ( ! order ) return ;
532
+ if ( order === null ) return ;
529
533
530
534
const buyer = await User . findOne ( { _id : order . buyer_id } ) ;
531
535
const seller = await User . findOne ( { _id : order . seller_id } ) ;
532
-
536
+ if ( buyer === null || seller === null ) throw Error ( "buyer and/or seller were not found in DB" ) ;
533
537
await messages . checkOrderMessage ( ctx , order , buyer , seller ) ;
534
538
} catch ( error ) {
535
539
logger . error ( error ) ;
@@ -543,7 +547,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
543
547
if ( ! ( await validateObjectId ( ctx , orderId ) ) ) return ;
544
548
const order = await Order . findOne ( { _id : orderId } ) ;
545
549
546
- if ( ! order ) return ;
550
+ if ( order === null ) return ;
547
551
if ( ! order . hash ) return ;
548
552
549
553
const invoice = await getInvoice ( { hash : order . hash } ) ;
@@ -567,7 +571,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
567
571
568
572
const order = await Order . findOne ( { hash } ) ;
569
573
570
- if ( ! order ) return ;
574
+ if ( order === null ) return ;
571
575
await subscribeInvoice ( bot , hash , true ) ;
572
576
ctx . reply ( `hash resubscribed` ) ;
573
577
} catch ( error : any ) {
@@ -606,11 +610,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
606
610
throw new Error ( ctxUpdateAssertMsg ) ;
607
611
}
608
612
const params = ctx . update . message . text . split ( ' ' ) ;
609
- const [ command , orderId ] = params . filter ( ( el ) => el ) ;
613
+ const [ command , orderId ] = params . filter ( ( el : string ) => el ) ;
610
614
611
615
if ( ! orderId ) {
612
616
const orders = await askForConfirmation ( ctx . user , command ) ;
613
- if ( ! orders . length ) return await ctx . reply ( `${ command } <order Id>` ) ;
617
+ if ( orders === null || orders . length === 0 ) return await ctx . reply ( `${ command } <order Id>` ) ;
614
618
615
619
return await messages . showConfirmationButtons ( ctx , orders , command ) ;
616
620
} else if ( ! ( await validateObjectId ( ctx , orderId ) ) ) {
@@ -637,7 +641,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
637
641
const user = await User . findOne ( {
638
642
$or : [ { username } , { tg_id : username } ] ,
639
643
} ) ;
640
- if ( ! user ) {
644
+ if ( user === null ) {
641
645
await messages . notFoundUserMessage ( ctx ) ;
642
646
return ;
643
647
}
@@ -648,6 +652,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
648
652
const community = await Community . findById (
649
653
ctx . admin . default_community_id
650
654
) ;
655
+ if ( community === null ) throw Error ( "Community was not found in DB" ) ;
651
656
community . banned_users . push ( {
652
657
id : user . _id ,
653
658
username : user . username ,
@@ -680,7 +685,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
680
685
const user = await User . findOne ( {
681
686
$or : [ { username } , { tg_id : username } ] ,
682
687
} ) ;
683
- if ( ! user ) {
688
+ if ( user === null ) {
684
689
await messages . notFoundUserMessage ( ctx ) ;
685
690
return ;
686
691
}
@@ -691,8 +696,9 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
691
696
const community = await Community . findById (
692
697
ctx . admin . default_community_id
693
698
) ;
694
- community . banned_users = community . banned_users . filter (
695
- ( el : any ) => el . id !== user . id
699
+ if ( community === null ) throw Error ( "Community was not found in DB" ) ;
700
+ community . banned_users = community . banned_users . toObject ( ) . filter (
701
+ ( el : IUsernameId ) => el . id !== user . id
696
702
) ;
697
703
await community . save ( ) ;
698
704
} else {
@@ -739,7 +745,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
739
745
try {
740
746
const command = '/setinvoice' ;
741
747
const orders = await askForConfirmation ( ctx . user , command ) ;
742
- if ( ! orders . length ) return await ctx . reply ( ctx . i18n . t ( 'setinvoice_no_response' ) ) ;
748
+ if ( orders === null || ! orders . length ) return await ctx . reply ( ctx . i18n . t ( 'setinvoice_no_response' ) ) ;
743
749
744
750
return await messages . showConfirmationButtons ( ctx , orders , command ) ;
745
751
} catch ( error ) {
@@ -859,6 +865,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
859
865
bot . command ( 'info' , userMiddleware , async ( ctx : MainContext ) => {
860
866
try {
861
867
const config = await Config . findOne ( { } ) ;
868
+ if ( config === null ) throw Error ( "Config was not found in DB" ) ;
862
869
await messages . showInfoMessage ( ctx , ctx . user , config ) ;
863
870
} catch ( error ) {
864
871
logger . error ( error ) ;
0 commit comments