1
1
import express from 'express' ;
2
2
import { Op } from 'sequelize' ;
3
3
import { like , searchLimit , searchOffset } from '#utils/database' ;
4
- import { authContext } from '#utils/auth' ;
4
+ import { authContext , authDiscord } from '#utils/auth' ;
5
5
import { pick , omit } from 'lodash-es' ;
6
6
import {
7
7
Team ,
@@ -11,6 +11,7 @@ import {
11
11
LevelStats ,
12
12
Level ,
13
13
Setting ,
14
+ BnKuskiRule ,
14
15
} from '../data/models' ;
15
16
16
17
const router = express . Router ( ) ;
@@ -88,6 +89,82 @@ const ChangeSettings = async data => {
88
89
return 1 ;
89
90
} ;
90
91
92
+ const BnKuskiRuleAttributes = [
93
+ 'BattleTypes' ,
94
+ 'Designers' ,
95
+ 'LevelPatterns' ,
96
+ 'BattleAttributes' ,
97
+ 'MinDuration' ,
98
+ 'MaxDuration' ,
99
+ 'IgnoreList' ,
100
+ ] ;
101
+
102
+ const BnSettings = async DiscordId => {
103
+ const get = await Setting . findOne ( {
104
+ where : { DiscordId } ,
105
+ attributes : [ 'KuskiIndex' , 'DiscordId' , 'BnEnabled' ] ,
106
+ include : [
107
+ {
108
+ model : BnKuskiRule ,
109
+ as : 'BnKuskiRules' ,
110
+ attributes : BnKuskiRuleAttributes ,
111
+ } ,
112
+ ] ,
113
+ } ) ;
114
+ return get ;
115
+ } ;
116
+
117
+ const AllActiveBnSettings = async ( ) => {
118
+ const get = await Setting . findAll ( {
119
+ attributes : [ 'KuskiIndex' , 'DiscordId' , 'BnEnabled' ] ,
120
+ include : [
121
+ {
122
+ model : BnKuskiRule ,
123
+ as : 'BnKuskiRules' ,
124
+ attributes : BnKuskiRuleAttributes ,
125
+ required : true ,
126
+ } ,
127
+ ] ,
128
+ } ) ;
129
+ return get ;
130
+ } ;
131
+
132
+ const ChangeBnEnabledSetting = async data => {
133
+ const setting = await Setting . findOne ( {
134
+ where : { DiscordId : data . DiscordId } ,
135
+ attributes : [ 'KuskiIndex' ] ,
136
+ } ) ;
137
+ if ( setting ?. KuskiIndex ) {
138
+ await Setting . update (
139
+ { BnEnabled : data . BnEnabled } ,
140
+ { where : { KuskiIndex : setting . KuskiIndex } } ,
141
+ ) ;
142
+ }
143
+ return 1 ;
144
+ } ;
145
+
146
+ const ChangeBnSettings = async data => {
147
+ const setting = await Setting . findOne ( {
148
+ where : { DiscordId : data . DiscordId } ,
149
+ attributes : [ 'KuskiIndex' ] ,
150
+ } ) ;
151
+ const KuskiIndex = setting ?. KuskiIndex ;
152
+ if ( KuskiIndex && data . BnKuskiRules . length > 0 ) {
153
+ // first time setup, turn notification on by default
154
+ const currentRules = await BnKuskiRule . findAll ( { where : { KuskiIndex } } ) ;
155
+ const isFirstTimeSetup = currentRules . length === 0 ;
156
+ if ( isFirstTimeSetup ) {
157
+ await Setting . update ( { BnEnabled : 1 } , { where : { KuskiIndex } } ) ;
158
+ }
159
+
160
+ // override existing rules with new ones
161
+ await BnKuskiRule . destroy ( { where : { KuskiIndex } } ) ;
162
+ const newRules = data . BnKuskiRules . map ( rule => ( { ...rule , KuskiIndex } ) ) ;
163
+ await BnKuskiRule . bulkCreate ( newRules ) ;
164
+ }
165
+ return 1 ;
166
+ } ;
167
+
91
168
const Player = async ( IdentifierType , KuskiIdentifier , currentUser ) => {
92
169
const query = {
93
170
where : { } ,
@@ -310,6 +387,34 @@ router
310
387
res . sendStatus ( 401 ) ;
311
388
}
312
389
} )
390
+ . get ( '/bn/:DiscordId/linked' , authDiscord , async ( req , res ) => {
391
+ const data = await Setting . findOne ( {
392
+ where : { DiscordId : req . params . DiscordId } ,
393
+ } ) ;
394
+ res . json ( Boolean ( data ) ) ;
395
+ } )
396
+ . get ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
397
+ const data = await BnSettings ( req . params . DiscordId ) ;
398
+ res . json ( data ) ;
399
+ } )
400
+ . get ( '/bn' , authDiscord , async ( req , res ) => {
401
+ const data = await AllActiveBnSettings ( ) ;
402
+ res . json ( data ) ;
403
+ } )
404
+ . post ( '/bn/:DiscordId/toggle/:BnEnabled' , authDiscord , async ( req , res ) => {
405
+ const data = await ChangeBnEnabledSetting ( {
406
+ DiscordId : req . params . DiscordId ,
407
+ BnEnabled : Number ( req . params . BnEnabled ) ,
408
+ } ) ;
409
+ res . json ( data ) ;
410
+ } )
411
+ . post ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
412
+ const data = await ChangeBnSettings ( {
413
+ ...req . body ,
414
+ DiscordId : req . params . DiscordId ,
415
+ } ) ;
416
+ res . json ( data ) ;
417
+ } )
313
418
. post ( '/ignore/:Kuski' , async ( req , res ) => {
314
419
const auth = authContext ( req ) ;
315
420
if ( auth . auth ) {
0 commit comments