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,94 @@ 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
+ if ( setting ?. KuskiIndex ) {
152
+ // get BnKuskiRules from user
153
+ const rules = await BnKuskiRule . findAll ( {
154
+ where : { KuskiIndex : setting . KuskiIndex } ,
155
+ } ) ;
156
+
157
+ // first time setup, turn notification on by default
158
+ const isFirstTimeSetup = ! rules . length && data . BnKuskiRules . length > 0 ;
159
+ if ( isFirstTimeSetup ) {
160
+ await Setting . update (
161
+ { BnEnabled : 1 } ,
162
+ { where : { KuskiIndex : setting . KuskiIndex } } ,
163
+ ) ;
164
+ }
165
+
166
+ // override existing rules with new ones
167
+ await BnKuskiRule . destroy ( { where : { KuskiIndex : setting . KuskiIndex } } ) ;
168
+ await Promise . all (
169
+ data . BnKuskiRules . map ( async rule => {
170
+ await BnKuskiRule . create ( {
171
+ KuskiIndex : setting . KuskiIndex ,
172
+ ...rule ,
173
+ } ) ;
174
+ } ) ,
175
+ ) ;
176
+ }
177
+ return 1 ;
178
+ } ;
179
+
91
180
const Player = async ( IdentifierType , KuskiIdentifier , currentUser ) => {
92
181
const query = {
93
182
where : { } ,
@@ -310,6 +399,34 @@ router
310
399
res . sendStatus ( 401 ) ;
311
400
}
312
401
} )
402
+ . get ( '/bn/:DiscordId/linked' , authDiscord , async ( req , res ) => {
403
+ const data = await Setting . findOne ( {
404
+ where : { DiscordId : req . params . DiscordId } ,
405
+ } ) ;
406
+ res . json ( Boolean ( data ) ) ;
407
+ } )
408
+ . get ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
409
+ const data = await BnSettings ( req . params . DiscordId ) ;
410
+ res . json ( data ) ;
411
+ } )
412
+ . get ( '/bn' , authDiscord , async ( req , res ) => {
413
+ const data = await AllActiveBnSettings ( ) ;
414
+ res . json ( data ) ;
415
+ } )
416
+ . post ( '/bn/:DiscordId/toggle/:BnEnabled' , authDiscord , async ( req , res ) => {
417
+ const data = await ChangeBnEnabledSetting ( {
418
+ DiscordId : req . params . DiscordId ,
419
+ BnEnabled : Number ( req . params . BnEnabled ) ,
420
+ } ) ;
421
+ res . json ( data ) ;
422
+ } )
423
+ . post ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
424
+ const data = await ChangeBnSettings ( {
425
+ ...req . body ,
426
+ DiscordId : req . params . DiscordId ,
427
+ } ) ;
428
+ res . json ( data ) ;
429
+ } )
313
430
. post ( '/ignore/:Kuski' , async ( req , res ) => {
314
431
const auth = authContext ( req ) ;
315
432
if ( auth . auth ) {
0 commit comments