@@ -31,11 +31,13 @@ const currentUserSchema = Joi.object().keys({
31
31
handle : Joi . string ( ) . required ( ) ,
32
32
roles : Joi . array ( ) . required ( ) ,
33
33
} ) ;
34
- const projectSchema = {
34
+ const updateProjectSchema = {
35
35
project : {
36
36
id : Joi . string ( ) . required ( ) ,
37
37
title : Joi . string ( ) . required ( ) ,
38
38
tcDirectId : Joi . number ( ) . required ( ) ,
39
+ //NOTE: `PATCH /challenges/:challengeId` requires the tags not empty
40
+ tags : Joi . array ( ) . items ( Joi . string ( ) . required ( ) ) . min ( 1 ) . required ( ) ,
39
41
repoUrl : Joi . string ( ) . required ( ) ,
40
42
repoUrls : Joi . array ( ) . required ( ) ,
41
43
rocketChatWebhook : Joi . string ( ) . allow ( null ) ,
@@ -57,6 +59,8 @@ const createProjectSchema = {
57
59
project : {
58
60
title : Joi . string ( ) . required ( ) ,
59
61
tcDirectId : Joi . number ( ) . required ( ) ,
62
+ //NOTE: `PATCH /challenges/:challengeId` requires the tags not empty
63
+ tags : Joi . array ( ) . items ( Joi . string ( ) . required ( ) ) . min ( 1 ) . required ( ) ,
60
64
repoUrl : Joi . string ( ) . required ( ) ,
61
65
copilot : Joi . string ( ) . allow ( null ) ,
62
66
rocketChatWebhook : Joi . string ( ) . allow ( null ) ,
@@ -125,7 +129,7 @@ async function _ensureEditPermissionAndGetInfo(projectId, currentUser) {
125
129
* @param {String } repoUrl the repository url
126
130
* @param {Object } project the new project
127
131
* @param {String } currentUser the topcoder current user
128
- * @returns {void }
132
+ * @returns {Array } challengeUUIDs
129
133
* @private
130
134
*/
131
135
async function _createOrMigrateRepository ( repoUrl , project , currentUser ) {
@@ -159,6 +163,9 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
159
163
catch ( err ) {
160
164
throw new Error ( `Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
161
165
}
166
+
167
+ const oldProject = await dbHelper . getById ( models . Project , oldRepo . projectId ) ;
168
+ return _ . isEqual ( oldProject . tags , project . tags ) ? [ ] : challengeUUIDs ;
162
169
} else {
163
170
try {
164
171
await dbHelper . create ( models . Repository , {
@@ -175,6 +182,8 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
175
182
throw new Error ( `Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
176
183
}
177
184
}
185
+
186
+ return [ ] ;
178
187
}
179
188
180
189
/**
@@ -206,16 +215,32 @@ async function create(project, currentUser) {
206
215
207
216
const createdProject = await dbHelper . create ( models . Project , project ) ;
208
217
218
+ let challengeUUIDsList = [ ] ;
209
219
// TODO: The following db operation should/could be moved into one transaction
210
220
for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
211
221
try {
212
- await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
222
+ const challengeUUIDs = await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
223
+ if ( ! _ . isEmpty ( challengeUUIDs ) ) {
224
+ challengeUUIDsList . append ( challengeUUIDs ) ;
225
+ }
213
226
}
214
227
catch ( err ) {
215
228
throw new Error ( `Create or migrate repository failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
216
229
}
217
230
}
218
231
232
+ // NOTE: Will update challenge tags even if the project is created with archived at this step, currently.
233
+ if ( ! _ . isEmpty ( challengeUUIDsList ) ) {
234
+ const projectTagsUpdatedEvent = {
235
+ event : 'challengeTags.update' ,
236
+ data : {
237
+ challengeUUIDsList,
238
+ tags : project . tags ,
239
+ } ,
240
+ } ;
241
+ await kafka . send ( JSON . stringify ( projectTagsUpdatedEvent ) ) ;
242
+ }
243
+
219
244
return createdProject ;
220
245
}
221
246
@@ -253,32 +278,50 @@ async function update(project, currentUser) {
253
278
*/
254
279
project . owner = dbProject . owner ;
255
280
project . copilot = project . copilot !== undefined ? project . copilot . toLowerCase ( ) : null ;
256
- Object . entries ( project ) . map ( ( item ) => {
257
- dbProject [ item [ 0 ] ] = item [ 1 ] ;
258
- return item ;
259
- } ) ;
260
281
261
282
// TODO: move the following logic into one dynamoose transaction
262
- const repos = await dbHelper . queryRepositoriesByProjectId ( dbProject . id ) ;
283
+ const repos = await dbHelper . queryRepositoriesByProjectId ( project . id ) ;
263
284
285
+ let challengeUUIDsList = [ ] ;
264
286
for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
265
287
if ( repos . find ( repo => repo . url === repoUrl ) ) {
266
288
const repoId = repos . find ( repo => repo . url === repoUrl ) . id
267
289
await dbHelper . update ( models . Repository , repoId , { archived : project . archived } ) ;
290
+ if ( ! _ . isEqual ( dbProject . tags , project . tags ) ) {
291
+ // NOTE: delay query of challengeUUIDs into topcoder-x-processor
292
+ challengeUUIDsList . append ( repoUrl ) ;
293
+ }
268
294
} else {
269
295
try {
270
- await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
296
+ const challengeUUIDs = await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
297
+ if ( ! _ . isEmpty ( challengeUUIDs ) ) {
298
+ challengeUUIDsList . append ( challengeUUIDs ) ;
299
+ }
271
300
}
272
301
catch ( err ) {
273
302
throw new Error ( `Create or migrate repository failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
274
303
}
275
304
}
276
305
}
277
- dbProject . updatedAt = new Date ( ) ;
278
- return await dbHelper . update ( models . Project , dbProject . id , dbProject ) ;
306
+ project . updatedAt = new Date ( ) ;
307
+ const updatedProject = await dbHelper . update ( models . Project , project . id , project ) ;
308
+
309
+ // NOTE: Will update challenge tags even if the project is changed to archived at this step, currently.
310
+ if ( ! _ . isEmpty ( challengeUUIDsList ) ) {
311
+ const projectTagsUpdatedEvent = {
312
+ event : 'challengeTags.update' ,
313
+ data : {
314
+ challengeUUIDsList,
315
+ tags : project . tags ,
316
+ } ,
317
+ } ;
318
+ await kafka . send ( JSON . stringify ( projectTagsUpdatedEvent ) ) ;
319
+ }
320
+
321
+ return updatedProject ;
279
322
}
280
323
281
- update . schema = projectSchema ;
324
+ update . schema = updateProjectSchema ;
282
325
283
326
/**
284
327
* gets all projects
0 commit comments