@@ -3,14 +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_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)'
118
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 '
1310const QUERY_INSERT_GROUP_CONTEST_ELIGIBILITY = 'INSERT INTO group_contest_eligibility (contest_eligibility_id, group_id) VALUES(?, ?)'
11+
1412const QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY = 'DELETE FROM group_contest_eligibility WHERE contest_eligibility_id = ? AND group_id = ?'
1513const QUERY_DELETE_CONTEST_ELIGIBILITY = 'DELETE FROM contest_eligibility WHERE contest_eligibility_id = ?'
1614
@@ -26,15 +24,20 @@ async function prepare (connection, sql) {
2624 return Promise . promisifyAll ( stmt )
2725}
2826
27+ /**
28+ * Get groups for a challenge
29+ * @param {Number } challengeLegacyId the legacy challenge ID
30+ */
2931async function getGroupsForChallenge ( challengeLegacyId ) {
3032 // logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
3133 const connection = await helper . getInformixConnection ( )
3234 let groupIds = [ ]
3335 try {
3436 // 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 )
3841 // logger.debug(`Groups Found for ${challengeLegacyId} - ${JSON.stringify(groupIds)}`)
3942 }
4043 // logger.debug(`No groups Found for ${challengeLegacyId}`)
@@ -49,23 +52,25 @@ async function getGroupsForChallenge (challengeLegacyId) {
4952 return groupIds
5053}
5154
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+ */
5260async 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+ }
5366 const connection = await helper . getInformixConnection ( )
5467
5568 try {
5669 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 )
6974 await connection . commitTransactionAsync ( )
7075 } catch ( e ) {
7176 logger . error ( `Error in 'addGroupToChallenge' ${ e } , rolling back transaction` )
@@ -77,28 +82,24 @@ async function addGroupToChallenge (challengeLegacyId, groupLegacyId) {
7782 }
7883}
7984
85+ /**
86+ * Remove group from a challenge
87+ * @param {Number } challengeLegacyId the legacy challenge ID
88+ * @param {Number } groupLegacyId the group ID
89+ */
8090async function removeGroupFromChallenge ( challengeLegacyId , groupLegacyId ) {
8191 const connection = await helper . getInformixConnection ( )
8292
8393 try {
8494 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 )
99101 }
100102 }
101-
102103 await connection . commitTransactionAsync ( )
103104 } catch ( e ) {
104105 logger . error ( `Error in 'removeGroupFromChallenge' ${ e } , rolling back transaction` )
@@ -111,86 +112,65 @@ async function removeGroupFromChallenge (challengeLegacyId, groupLegacyId) {
111112}
112113
113114/**
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
119118 */
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
131124}
132125
133126/**
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
138130 */
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 )
144136}
145137
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 ) {
147144 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]' )
154149}
155150
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 ) {
157158 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 ] )
171160}
172161
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 ] )
188171
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 ] )
194174}
195175
196176module . exports = {
0 commit comments