Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 996958c

Browse files
fix issue with multiple groups
1 parent 209e587 commit 996958c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/services/groupsService.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const util = require('util')
44
const helper = require('../common/helper')
55

66
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'
78
const QUERY_GET_GROUP_ELIGIBILITY_ID = 'SELECT limit 1 * FROM group_contest_eligibility WHERE contest_eligibility_id = %d AND group_id = %d'
89
const QUERY_GET_GROUPS = 'SELECT group_id FROM group_contest_eligibility WHERE contest_eligibility_id = %d'
910
const QUERY_GET_GROUPS_COUNT = 'SELECT count(*) as cnt FROM group_contest_eligibility WHERE contest_eligibility_id = %d'
1011

11-
const QUERY_INSERT_CONTEST_ELIGIBILITY = 'INSERT INTO contest_eligibility (contest_eligibility_id, contest_id, is_studio) VALUES(contest_eligibility_seq.NEXTVAL, ?, 0)'
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, ?)'
1213
const QUERY_INSERT_GROUP_CONTEST_ELIGIBILITY = 'INSERT INTO group_contest_eligibility (contest_eligibility_id, group_id) VALUES(?, ?)'
1314
const QUERY_DELETE_GROUP_CONTEST_ELIGIBILITY = 'DELETE FROM group_contest_eligibility WHERE contest_eligibility_id = ? AND group_id = ?'
1415
const QUERY_DELETE_CONTEST_ELIGIBILITY = 'DELETE FROM contest_eligibility WHERE contest_eligibility_id = ?'
@@ -53,9 +54,9 @@ async function addGroupToChallenge (challengeLegacyId, groupLegacyId) {
5354

5455
try {
5556
await connection.beginTransactionAsync()
56-
let eligibilityId = await getChallengeEligibilityId(connection, challengeLegacyId)
57+
let eligibilityId = await getChallengeEligibilityId(connection, challengeLegacyId, groupLegacyId)
5758
if (!eligibilityId) {
58-
eligibilityId = await createChallengeEligibilityRecord(connection, challengeLegacyId)
59+
eligibilityId = await createChallengeEligibilityRecord(connection, challengeLegacyId, groupLegacyId)
5960
}
6061

6162
const groupMappingExists = await groupEligbilityExists(connection, eligibilityId, groupLegacyId)
@@ -81,7 +82,7 @@ async function removeGroupFromChallenge (challengeLegacyId, groupLegacyId) {
8182

8283
try {
8384
await connection.beginTransactionAsync()
84-
const eligibilityId = await getChallengeEligibilityId(connection, challengeLegacyId)
85+
const eligibilityId = await getChallengeEligibilityId(connection, challengeLegacyId, groupLegacyId)
8586
if (!eligibilityId) {
8687
throw new Error(`Eligibility not found for legacyId ${challengeLegacyId}`)
8788
}
@@ -113,12 +114,18 @@ async function removeGroupFromChallenge (challengeLegacyId, groupLegacyId) {
113114
* Gets the eligibility ID of a legacyId
114115
* @param {Object} connection
115116
* @param {Number} challengeLegacyId
117+
* @param {Number} groupId
116118
* @returns {Object} { eligibilityId }
117119
*/
118-
async function getChallengeEligibilityId (connection, challengeLegacyId) {
120+
async function getChallengeEligibilityId (connection, challengeLegacyId, groupId) {
119121
// get the challenge eligibility record, if one doesn't exist, create it and return the id
120122
// logger.info(`getChallengeEligibilityId Query: ${util.format(QUERY_GET_ELIGIBILITY_ID, challengeLegacyId)}`)
121-
const result = await connection.queryAsync(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+
}
122129
// logger.info(`getChallengeEligibilityId Result: ${JSON.stringify(result)}`)
123130
return (result && result[0]) ? result[0].contest_eligibility_id : false
124131
}
@@ -136,9 +143,9 @@ async function groupEligbilityExists (connection, eligibilityId, groupLegacyId)
136143
return (result && result[0]) || false
137144
}
138145

139-
async function createChallengeEligibilityRecord (connection, challengeLegacyId) {
146+
async function createChallengeEligibilityRecord (connection, challengeLegacyId, groupId) {
140147
const query = await prepare(connection, QUERY_INSERT_CONTEST_ELIGIBILITY)
141-
const result = await query.executeAsync([challengeLegacyId])
148+
const result = await query.executeAsync([challengeLegacyId, groupId])
142149
if (result) {
143150
const idResult = await connection.queryAsync(util.format(QUERY_GET_ELIGIBILITY_ID, challengeLegacyId))
144151
return idResult[0].contest_eligibility_id

0 commit comments

Comments
 (0)