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