@@ -3,13 +3,12 @@ const _ = require('lodash')
3
3
const util = require ( 'util' )
4
4
const helper = require ( '../common/helper' )
5
5
6
- const QUERY_GET_ELIGIBILITY_ID = 'SELECT limit 1 * FROM contest_eligibility WHERE contest_id = %d'
7
- const QUERY_GET_GROUP_ELIGIBILITY_ID = 'SELECT limit 1 * FROM group_contest_eligibility WHERE contest_eligibility_id = %d AND group_id = %d'
8
- const QUERY_GET_GROUPS = 'SELECT group_id FROM group_contest_eligibility WHERE contest_eligibility_id = %d'
9
- const QUERY_GET_GROUPS_COUNT = 'SELECT count(*) as cnt FROM group_contest_eligibility WHERE contest_eligibility_id = %d'
10
-
6
+ const QUERY_GET_CONTEST_ELIGIBILITIES_IDS = 'SELECT contest_eligibility_id FROM contest_eligibility WHERE contest_id = %d'
11
7
const QUERY_INSERT_CONTEST_ELIGIBILITY = 'INSERT INTO contest_eligibility (contest_eligibility_id, contest_id, is_studio) VALUES(contest_eligibility_seq.NEXTVAL, ?, 0)'
8
+
9
+ const QUERY_GET_GROUPS = 'SELECT contest_eligibility_id, group_id FROM group_contest_eligibility WHERE contest_eligibility_id in'
12
10
const QUERY_INSERT_GROUP_CONTEST_ELIGIBILITY = 'INSERT INTO group_contest_eligibility (contest_eligibility_id, group_id) VALUES(?, ?)'
11
+
13
12
const QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY = 'DELETE FROM group_contest_eligibility WHERE contest_eligibility_id = ? AND group_id = ?'
14
13
const QUERY_DELETE_CONTEST_ELIGIBILITY = 'DELETE FROM contest_eligibility WHERE contest_eligibility_id = ?'
15
14
@@ -25,15 +24,20 @@ async function prepare (connection, sql) {
25
24
return Promise . promisifyAll ( stmt )
26
25
}
27
26
27
+ /**
28
+ * Get groups for a challenge
29
+ * @param {Number } challengeLegacyId the legacy challenge ID
30
+ */
28
31
async function getGroupsForChallenge ( challengeLegacyId ) {
29
32
// logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
30
33
const connection = await helper . getInformixConnection ( )
31
34
let groupIds = [ ]
32
35
try {
33
36
// await connection.beginTransactionAsync()
34
- const eligibilityId = await getChallengeEligibilityId ( connection , challengeLegacyId )
35
- if ( eligibilityId ) {
36
- groupIds = await getGroupIdsForEligibilityId ( connection , eligibilityId )
37
+ const eligibilityIds = await getChallengeEligibilityIds ( connection , challengeLegacyId )
38
+ if ( eligibilityIds && eligibilityIds . length > 0 ) {
39
+ const groups = await getGroupsForEligibilityIds ( connection , eligibilityIds )
40
+ groupIds = _ . map ( groups , g => g . group_id )
37
41
// logger.debug(`Groups Found for ${challengeLegacyId} - ${JSON.stringify(groupIds)}`)
38
42
}
39
43
// logger.debug(`No groups Found for ${challengeLegacyId}`)
@@ -48,23 +52,25 @@ async function getGroupsForChallenge (challengeLegacyId) {
48
52
return groupIds
49
53
}
50
54
55
+ /**
56
+ * Add a group to a challenge
57
+ * @param {Number } challengeLegacyId the legacy challenge ID
58
+ * @param {Number } groupLegacyId the legacy group ID
59
+ */
51
60
async function addGroupToChallenge ( challengeLegacyId , groupLegacyId ) {
61
+ const existingGroups = await getGroupsForChallenge ( challengeLegacyId )
62
+ if ( existingGroups . indexOf ( groupLegacyId ) > - 1 ) {
63
+ logger . info ( `Group ${ groupLegacyId } is already assigned to challenge ${ challengeLegacyId } . Skipping...` )
64
+ return
65
+ }
52
66
const connection = await helper . getInformixConnection ( )
53
67
54
68
try {
55
69
await connection . beginTransactionAsync ( )
56
- let eligibilityId = await getChallengeEligibilityId ( connection , challengeLegacyId )
57
- if ( ! eligibilityId ) {
58
- eligibilityId = await createChallengeEligibilityRecord ( connection , challengeLegacyId )
59
- }
60
-
61
- const groupMappingExists = await groupEligbilityExists ( connection , eligibilityId , groupLegacyId )
62
- if ( groupMappingExists ) {
63
- logger . warn ( `Group Relation Already Exists for ${ groupMappingExists } - ${ eligibilityId } ${ groupLegacyId } ` )
64
- } else {
65
- await createGroupEligibilityRecord ( connection , eligibilityId , groupLegacyId )
66
- }
67
-
70
+ // create eligibility entry
71
+ const eligibilityId = await createContestEligibility ( connection , challengeLegacyId )
72
+ // create group association
73
+ await createGroupContestEligibility ( connection , eligibilityId , groupLegacyId )
68
74
await connection . commitTransactionAsync ( )
69
75
} catch ( e ) {
70
76
logger . error ( `Error in 'addGroupToChallenge' ${ e } , rolling back transaction` )
@@ -76,114 +82,96 @@ async function addGroupToChallenge (challengeLegacyId, groupLegacyId) {
76
82
}
77
83
}
78
84
85
+ /**
86
+ * Remove group from a challenge
87
+ * @param {Number } challengeLegacyId the legacy challenge ID
88
+ * @param {Number } groupLegacyId the group ID
89
+ */
79
90
async function removeGroupFromChallenge ( challengeLegacyId , groupLegacyId ) {
80
91
const connection = await helper . getInformixConnection ( )
81
92
82
93
try {
83
94
await connection . beginTransactionAsync ( )
84
- const eligibilityId = await getChallengeEligibilityId ( connection , challengeLegacyId )
85
- if ( ! eligibilityId ) {
86
- throw new Error ( `Eligibility not found for legacyId ${ challengeLegacyId } ` )
87
- }
88
- const groupEligibilityRecord = await groupEligbilityExists ( connection , eligibilityId , groupLegacyId )
89
-
90
- if ( groupEligibilityRecord ) {
91
- await deleteGroupEligibilityRecord ( connection , eligibilityId , groupLegacyId )
92
- // logger.debug('Getting Groups Count')
93
- const { groupsCount } = await getCountOfGroupsInEligibilityRecord ( connection , eligibilityId )
94
- // logger.debug(`${groupsCount} groups exist`)
95
- if ( groupsCount <= 0 ) {
96
- logger . debug ( 'No groups exist, deleting eligibility group' )
97
- await deleteEligibilityRecord ( connection , eligibilityId )
95
+ const eligibilityIds = await getChallengeEligibilityIds ( connection , challengeLegacyId )
96
+ if ( eligibilityIds && eligibilityIds . length > 0 ) {
97
+ const groups = await getGroupsForEligibilityIds ( connection , eligibilityIds )
98
+ const groupToRemove = _ . find ( groups , g => g . group_id === groupLegacyId )
99
+ if ( groupToRemove ) {
100
+ await clearData ( connection , groupToRemove . contest_eligibility_id , groupToRemove . group_id )
98
101
}
99
102
}
100
-
101
103
await connection . commitTransactionAsync ( )
102
104
} catch ( e ) {
103
105
logger . error ( `Error in 'removeGroupFromChallenge' ${ e } , rolling back transaction` )
104
106
await connection . rollbackTransactionAsync ( )
105
107
throw e
106
108
} finally {
107
- logger . info ( `Group ${ groupLegacyId } removed to challenge ${ challengeLegacyId } ` )
109
+ logger . info ( `Group ${ groupLegacyId } removed from challenge ${ challengeLegacyId } ` )
108
110
await connection . closeAsync ( )
109
111
}
110
112
}
111
113
112
114
/**
113
- * Gets the eligibility ID of a legacyId
114
- * @param {Object } connection
115
- * @param {Number } challengeLegacyId
116
- * @returns {Object } { eligibilityId }
115
+ * Get group IDs
116
+ * @param {Object } connection the connection
117
+ * @param {Array } eligibilityIds the eligibility IDs
117
118
*/
118
- async function getChallengeEligibilityId ( connection , challengeLegacyId ) {
119
- // get the challenge eligibility record, if one doesn't exist, create it and return the id
120
- // logger.info(`getChallengeEligibilityId Query: ${util.format(QUERY_GET_ELIGIBILITY_ID, challengeLegacyId)}`)
121
- const result = await connection . queryAsync ( util . format ( QUERY_GET_ELIGIBILITY_ID , challengeLegacyId ) )
122
- // logger.info(`getChallengeEligibilityId Result: ${JSON.stringify(result)}`)
123
- return ( result && result [ 0 ] ) ? result [ 0 ] . contest_eligibility_id : false
119
+ async function getGroupsForEligibilityIds ( connection , eligibilityIds ) {
120
+ const query = `${ QUERY_GET_GROUPS } (${ eligibilityIds . join ( ', ' ) } )`
121
+ // logger.debug(`getGroupIdsForEligibilityId ${query}`)
122
+ const result = await connection . queryAsync ( query )
123
+ return result
124
124
}
125
125
126
126
/**
127
- * @param {Object } connection
128
- * @param {Number } eligibilityId
129
- * @param {Number } groupLegacyId
130
- * @returns {Object } DB Result
127
+ * Gets the eligibility IDs
128
+ * @param {Object } connection the connection
129
+ * @param {Number } challengeLegacyId the legacy challenge ID
131
130
*/
132
- async function groupEligbilityExists ( connection , eligibilityId , groupLegacyId ) {
133
- // logger.debug(`groupEligibiltyExists query ${ util.format(QUERY_GET_GROUP_ELIGIBILITY_ID, eligibilityId, groupLegacyId)}` )
134
- const result = await connection . queryAsync ( util . format ( QUERY_GET_GROUP_ELIGIBILITY_ID , eligibilityId , groupLegacyId ) )
135
- // logger.debug(`groupEligibiltyExists result ${JSON.stringify(result)} ${JSON.stringify(result[0])}` )
136
- return ( result && result [ 0 ] ) || false
131
+ async function getChallengeEligibilityIds ( connection , challengeLegacyId ) {
132
+ const query = util . format ( QUERY_GET_CONTEST_ELIGIBILITIES_IDS , challengeLegacyId )
133
+ // logger.debug(`getGroupIdsForEligibilityId ${query}` )
134
+ const result = await connection . queryAsync ( query )
135
+ return _ . map ( result , r => r . contest_eligibility_id )
137
136
}
138
137
139
- async function createChallengeEligibilityRecord ( connection , challengeLegacyId ) {
138
+ /**
139
+ * Create a contest eligibility
140
+ * @param {Object } connection the connection
141
+ * @param {Number } legacyChallengeId the legacy challenge ID
142
+ */
143
+ async function createContestEligibility ( connection , legacyChallengeId ) {
140
144
const query = await prepare ( connection , QUERY_INSERT_CONTEST_ELIGIBILITY )
141
- const result = await query . executeAsync ( [ challengeLegacyId ] )
142
- if ( result ) {
143
- const idResult = await connection . queryAsync ( util . format ( QUERY_GET_ELIGIBILITY_ID , challengeLegacyId ) )
144
- return idResult [ 0 ] . contest_eligibility_id
145
- }
146
- return false
145
+ await query . executeAsync ( [ legacyChallengeId ] )
146
+ const ids = await getChallengeEligibilityIds ( connection , legacyChallengeId )
147
+ const groups = await getGroupsForEligibilityIds ( connection , ids )
148
+ return _ . get ( _ . filter ( ids , id => ! _ . find ( groups , g => g . contest_eligibility_id === id ) ) , '[0]' )
147
149
}
148
150
149
- async function createGroupEligibilityRecord ( connection , eligibilityId , groupLegacyId ) {
151
+ /**
152
+ * Create group contest eligibility
153
+ * @param {Object } connection the connection
154
+ * @param {Number } eligibilityId the eligibility ID
155
+ * @param {Number } groupId the group ID
156
+ */
157
+ async function createGroupContestEligibility ( connection , eligibilityId , groupId ) {
150
158
const query = await prepare ( connection , QUERY_INSERT_GROUP_CONTEST_ELIGIBILITY )
151
- const result = await query . executeAsync ( [ eligibilityId , groupLegacyId ] )
152
- if ( result ) {
153
- const idResult = await connection . queryAsync ( util . format ( QUERY_GET_GROUP_ELIGIBILITY_ID , eligibilityId , groupLegacyId ) )
154
- return idResult [ 0 ]
155
- }
156
- return result
157
- }
158
-
159
- async function deleteGroupEligibilityRecord ( connection , eligibilityId , groupLegacyId ) {
160
- const query = await prepare ( connection , QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY )
161
- const result = await query . executeAsync ( [ eligibilityId , groupLegacyId ] )
162
- // logger.debug(`deleteGroupEligibilityRecord ${JSON.stringify(result)}`)
163
- return result
159
+ return await query . executeAsync ( [ eligibilityId , groupId ] )
164
160
}
165
161
166
- async function deleteEligibilityRecord ( connection , eligibilityId ) {
167
- const query = await prepare ( connection , QUERY_DELETE_CONTEST_ELIGIBILITY )
168
- // logger.debug(`deleteEligibilityRecord Query ${JSON.stringify(query)}`)
169
- const result = await query . executeAsync ( [ eligibilityId ] )
170
- // logger.debug(`deleteEligibilityRecord ${JSON.stringify(result)}`)
171
- return result
172
- }
173
-
174
- async function getCountOfGroupsInEligibilityRecord ( connection , eligibilityId ) {
175
- const query = util . format ( QUERY_GET_GROUPS_COUNT , eligibilityId )
176
- // logger.debug(`Query! ${query}`)
177
- const result = await connection . queryAsync ( query )
178
- // logger.debug(`getCountOfGroupsInEligibilityRecord ${JSON.stringify(result)}`)
179
- return { groupsCount : result [ 0 ] . cnt || 0 }
180
- }
162
+ /**
163
+ * Removes entries from group_contest_eligibility and contest_eligibility
164
+ * @param {Object } connection the connection
165
+ * @param {Number } eligibilityId the eligibility ID
166
+ * @param {Number } groupId the group ID
167
+ */
168
+ async function clearData ( connection , eligibilityId , groupId ) {
169
+ let query
170
+ query = await prepare ( connection , QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY )
171
+ await query . executeAsync ( [ eligibilityId , groupId ] )
181
172
182
- async function getGroupIdsForEligibilityId ( connection , eligibilityId ) {
183
- const query = util . format ( QUERY_GET_GROUPS , eligibilityId )
184
- // logger.debug(`getGroupIdsForEligibilityId ${query}`)
185
- const result = await connection . queryAsync ( query )
186
- return _ . map ( result , r => r . group_id )
173
+ query = await prepare ( connection , QUERY_DELETE_CONTEST_ELIGIBILITY )
174
+ await query . executeAsync ( [ eligibilityId ] )
187
175
}
188
176
189
177
module . exports = {
0 commit comments