@@ -67,74 +67,6 @@ async function ensureAccessibleForChallenge(user, challenge) {
67
67
}
68
68
}
69
69
70
- /**
71
- * Filter challenges by groups access
72
- * @param {Object } currentUser the user who perform operation
73
- * @param {Array } challenges the challenges to filter
74
- * @returns {Array } the challenges that can be accessed by current user
75
- */
76
- async function filterChallengesByGroupsAccess ( currentUser , challenges ) {
77
- const res = [ ] ;
78
- let userGroups ;
79
- const needToCheckForGroupAccess = ! currentUser
80
- ? true
81
- : ! currentUser . isMachine && ! hasAdminRole ( currentUser ) ;
82
- const subGroupsMap = { } ;
83
- for ( const challenge of challenges ) {
84
- challenge . groups = _ . filter (
85
- challenge . groups ,
86
- ( g ) => ! _ . includes ( [ "null" , "undefined" ] , _ . toString ( g ) . toLowerCase ( ) )
87
- ) ;
88
- let expandedGroups = [ ] ;
89
- if (
90
- ! challenge . groups ||
91
- _ . get ( challenge , "groups.length" , 0 ) === 0 ||
92
- ! needToCheckForGroupAccess
93
- ) {
94
- res . push ( challenge ) ;
95
- } else if ( currentUser ) {
96
- // get user groups if not yet
97
- if ( _ . isNil ( userGroups ) ) {
98
- userGroups = await helper . getUserGroups ( currentUser . userId ) ;
99
- }
100
- // Expand challenge groups by subGroups
101
- // results are being saved on a hashmap for efficiency
102
- for ( const group of challenge . groups ) {
103
- let subGroups ;
104
- if ( subGroupsMap [ group ] ) {
105
- subGroups = subGroupsMap [ group ] ;
106
- } else {
107
- subGroups = await helper . expandWithSubGroups ( group ) ;
108
- subGroupsMap [ group ] = subGroups ;
109
- }
110
- expandedGroups = [ ..._ . concat ( expandedGroups , subGroups ) ] ;
111
- }
112
- // check if there is matched group
113
- // logger.debug('Groups', challenge.groups, userGroups)
114
- if ( _ . find ( expandedGroups , ( group ) => ! ! _ . find ( userGroups , ( ug ) => ug . id === group ) ) ) {
115
- res . push ( challenge ) ;
116
- }
117
- }
118
- }
119
- return res ;
120
- }
121
-
122
- /**
123
- * Ensure the user can access the challenge by groups access
124
- * @param {Object } currentUser the user who perform operation
125
- * @param {Object } challenge the challenge to check
126
- */
127
- async function ensureAccessibleByGroupsAccess ( currentUser , challenge ) {
128
- const filtered = await filterChallengesByGroupsAccess ( currentUser , [ challenge ] ) ;
129
- if ( filtered . length === 0 ) {
130
- throw new errors . ForbiddenError ( `ensureAccessibleByGroupsAccess :: You don't have access to this group!
131
- Current User: ${ JSON . stringify ( currentUser ) }
132
- Challenge: ${ JSON . stringify ( challenge ) }
133
- Filtered: ${ JSON . stringify ( filtered ) }
134
- ` ) ;
135
- }
136
- }
137
-
138
70
/**
139
71
* Search challenges by legacyId
140
72
* @param {Object } currentUser the user who perform operation
@@ -2281,22 +2213,25 @@ function sanitizeData(data, challenge) {
2281
2213
* @returns {Object } the deleted challenge
2282
2214
*/
2283
2215
async function deleteChallenge ( currentUser , challengeId ) {
2284
- const challenge = await challengeDomain . lookup ( getLookupCriteria ( "id" , challengeId ) ) ;
2285
- if ( challenge . status !== constants . challengeStatuses . New ) {
2286
- throw new errors . BadRequestError (
2287
- `Challenge with status other than "${ constants . challengeStatuses . New } " cannot be removed`
2288
- ) ;
2216
+ const { items } = await challengeDomain . scan ( {
2217
+ criteria : getScanCriteria ( { id : challengeId , status : constants . challengeStatuses . New } ) ,
2218
+ } ) ;
2219
+ const challenge = _ . first ( items ) ;
2220
+ if ( ! challenge ) {
2221
+ throw new errors . NotFoundError ( `Challenge with id: ${ challengeId } doesn't exist or is not in New status` ) ;
2289
2222
}
2290
- // check groups authorization
2291
- await ensureAccessibleByGroupsAccess ( currentUser , challenge ) ;
2292
- // check if user are allowed to delete the challenge
2293
- await ensureAccessibleForChallenge ( currentUser , challenge ) ;
2223
+ // ensure user can modify challenge
2224
+ await helper . ensureUserCanModifyChallenge ( currentUser , challenge ) ;
2294
2225
// delete DB record
2295
- await challengeDomain . delete ( getLookupCriteria ( "id" , challengeId ) ) ;
2226
+ const { items : deletedItems } = await challengeDomain . delete ( getLookupCriteria ( "id" , challengeId ) ) ;
2227
+ if ( ! _ . find ( deletedItems , { id : challengeId } ) ) {
2228
+ throw new errors . Internal ( `There was an error deleting the challenge with id: ${ challengeId } ` ) ;
2229
+ }
2296
2230
// delete ES document
2297
2231
await esClient . delete ( {
2298
2232
index : config . get ( "ES.ES_INDEX" ) ,
2299
2233
refresh : config . get ( "ES.ES_REFRESH" ) ,
2234
+ type : config . get ( "ES.OPENSEARCH" ) == "false" ? config . get ( "ES.ES_TYPE" ) : undefined ,
2300
2235
id : challengeId ,
2301
2236
} ) ;
2302
2237
await helper . postBusEvent ( constants . Topics . ChallengeDeleted , {
0 commit comments