@@ -3,13 +3,12 @@ const _ = require('lodash')
33const util = require ( 'util' )
44const helper = require ( '../common/helper' )
55
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'
117const 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'
1210const QUERY_INSERT_GROUP_CONTEST_ELIGIBILITY = 'INSERT INTO group_contest_eligibility (contest_eligibility_id, group_id) VALUES(?, ?)'
11+
1312const QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY = 'DELETE FROM group_contest_eligibility WHERE contest_eligibility_id = ? AND group_id = ?'
1413const QUERY_DELETE_CONTEST_ELIGIBILITY = 'DELETE FROM contest_eligibility WHERE contest_eligibility_id = ?'
1514
@@ -25,15 +24,20 @@ async function prepare (connection, sql) {
2524 return Promise . promisifyAll ( stmt )
2625}
2726
27+ /**
28+ * Get groups for a challenge
29+ * @param {Number } challengeLegacyId the legacy challenge ID
30+ */
2831async function getGroupsForChallenge ( challengeLegacyId ) {
2932 // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
3033 const connection = await helper . getInformixConnection ( )
3134 let groupIds = [ ]
3235 try {
3336 // 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 )
3741 // logger.debug(`Groups Found for ${challengeLegacyId} - ${JSON.stringify(groupIds)}`)
3842 }
3943 // logger.debug(`No groups Found for ${challengeLegacyId}`)
@@ -48,23 +52,25 @@ async function getGroupsForChallenge (challengeLegacyId) {
4852 return groupIds
4953}
5054
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+ */
5160async 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+ }
5266 const connection = await helper . getInformixConnection ( )
5367
5468 try {
5569 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 )
6874 await connection . commitTransactionAsync ( )
6975 } catch ( e ) {
7076 logger . error ( `Error in 'addGroupToChallenge' ${ e } , rolling back transaction` )
@@ -76,114 +82,96 @@ async function addGroupToChallenge (challengeLegacyId, groupLegacyId) {
7682 }
7783}
7884
85+ /**
86+ * Remove group from a challenge
87+ * @param {Number } challengeLegacyId the legacy challenge ID
88+ * @param {Number } groupLegacyId the group ID
89+ */
7990async function removeGroupFromChallenge ( challengeLegacyId , groupLegacyId ) {
8091 const connection = await helper . getInformixConnection ( )
8192
8293 try {
8394 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 )
98101 }
99102 }
100-
101103 await connection . commitTransactionAsync ( )
102104 } catch ( e ) {
103105 logger . error ( `Error in 'removeGroupFromChallenge' ${ e } , rolling back transaction` )
104106 await connection . rollbackTransactionAsync ( )
105107 throw e
106108 } finally {
107- logger . info ( `Group ${ groupLegacyId } removed to challenge ${ challengeLegacyId } ` )
109+ logger . info ( `Group ${ groupLegacyId } removed from challenge ${ challengeLegacyId } ` )
108110 await connection . closeAsync ( )
109111 }
110112}
111113
112114/**
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
117118 */
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
124124}
125125
126126/**
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
131130 */
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 )
137136}
138137
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 ) {
140144 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]' )
147149}
148150
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 ) {
150158 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 ] )
164160}
165161
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 ] )
181172
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 ] )
187175}
188176
189177module . exports = {
0 commit comments