Skip to content

Commit 885d480

Browse files
committed
Refactor remove resource
1 parent be5fbe4 commit 885d480

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { isNil, get } = require('lodash')
88
const Kafka = require('no-kafka')
99
const healthcheck = require('topcoder-healthcheck-dropin')
1010
const logger = require('./common/logger')
11-
const helper = require('./common/helper')
11+
// const helper = require('./common/helper')
1212
const { getKafkaOptions } = require('./common/utils')
1313
const ProcessorService = require('./services/ProcessorService')
1414

src/services/ProjectService.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const helper = require('../common/helper')
22
const logger = require('../common/logger')
3+
const { toInteger } = require('lodash')
34
const Constants = require('../constants')
45

56
const QUERY_CHECK_RESOURCE_EXISTS = 'SELECT COUNT(*) as num FROM resource WHERE project_id = %d AND resource_role_id = %d AND user_id = %d'
@@ -12,8 +13,8 @@ const QUERY_CHECK_RESOURCE_EXISTS = 'SELECT COUNT(*) as num FROM resource WHERE
1213
* @return the result
1314
*/
1415
async function resourceExists (challengeId, roleId, userId) {
15-
const result = helper.queryDataFromDB(QUERY_CHECK_RESOURCE_EXISTS, [challengeId, roleId, userId])
16-
logger.debug(`resourceExists ${JSON.stringify([challengeId, roleId, userId])} result: ${JSON.stringify(result)}`)
16+
const result = helper.queryDataFromDB(QUERY_CHECK_RESOURCE_EXISTS, [challengeId, roleId, toInteger(userId)])
17+
logger.debug(`resourceExists ${JSON.stringify([challengeId, roleId, toInteger(userId)])} result: ${JSON.stringify(result)}`)
1718
if (result && result.length > 0) {
1819
return result[0].num > 0
1920
}
@@ -92,6 +93,7 @@ const QUERY_DELETE_RESOURCE = 'DELETE FROM resource WHERE resource_id = ?'
9293
* @param operatorId operator id
9394
*/
9495
async function removeResource (resource) {
96+
logger.debug(`removeResource ${JSON.stringify(resource)}`)
9597
await helper.executeSQLonDB(QUERY_DELETE_RES_INFO, [resource.resourceid])
9698
await helper.executeSQLonDB(QUERY_DELETE_SUBMISSION, [resource.resourceid])
9799
await helper.executeSQLonDB(QUERY_DELETE_RESOURCE, [resource.resourceid])

src/services/ResourceDirectManager.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const RegistrationDAO = require('../dao/RegistrationDAO')
33
const SequenceDAO = require('../dao/SequenceDAO')
44
const logger = require('../common/logger')
55
const config = require('config')
6+
const { find, toString } = require('lodash')
67

78
/**
89
* Assign the given roleId to the specified userId in the given project.
@@ -51,38 +52,23 @@ async function assignRole (legacyChallengeId, roleId, userId, handle) {
5152

5253
/**
5354
* Assign the given roleId to the specified userId in the given project.
54-
* @param operatorId
5555
* @param legacyChallengeId
5656
* the id of the contest/challenge.
5757
* @param roleId
5858
* the id of the role.
5959
* @param userId
6060
* the id of the user.
61-
* @param phase
62-
* the <code>Phase</code> associated with the resource.
63-
* @param addNotification
64-
* whether to add notification.
65-
* @param addForumWatch
66-
* whether to add forum watch.
67-
* @param isStudio
68-
* whether assign to studio contest.
69-
* @param checkTerm
70-
* whether to check terms and conditions.
7161
*/
7262
async function removeRole (legacyChallengeId, roleId, userId) {
73-
logger.debug('Checking that User ' + userId + ' has role ' + roleId + ' for the project ' + legacyChallengeId)
74-
let found = await ProjectServices.resourceExists(legacyChallengeId, roleId, userId)
75-
if (!found) {
63+
const resources = await ProjectServices.searchResources(legacyChallengeId, roleId)
64+
const existingResource = find(resources, r => toString(r.userid) === toString(userId))
65+
66+
if (!existingResource) {
7667
logger.error('removeRole Resource Not Found')
7768
throw new Error('User ' + userId + ' does not have role ' + roleId + ' for the project ' + legacyChallengeId)
7869
}
79-
const resources = await ProjectServices.searchResources(legacyChallengeId, roleId)
80-
for (const resource of resources) {
81-
logger.debug(`removeRole - Loop ${JSON.stringify(resource)}`)
82-
if (+resource.userid === userId) {
83-
await ProjectServices.removeResource(resource)
84-
}
85-
}
70+
71+
await ProjectServices.removeResource(existingResource)
8672
}
8773

8874
/**

0 commit comments

Comments
 (0)