|
5 | 5 | const logger = require('../common/logger') |
6 | 6 | const util = require('util') |
7 | 7 | const helper = require('../common/helper') |
| 8 | +const IDGenerator = require('../common/idGenerator') |
| 9 | + |
| 10 | +const prizeIdGen = new IDGenerator('prize_id_seq') |
8 | 11 |
|
9 | 12 | const QUERY_GET_CHALLENGE_PRIZES = 'SELECT prize_id, place, prize_amount, number_of_submissions FROM prize WHERE prize_type_id = %d and project_id = %d' |
10 | 13 | const QUERY_UPDATE_CHALLENGE_PRIZE = 'UPDATE prize SET prize_amount = ?, number_of_submissions = ? WHERE prize_id = %d and project_id = %d' |
| 14 | +const QUERY_CREATE_CHALLENGE_PRIZE = 'INSERT INTO prize (prize_id, project_id, place, prize_amount, prize_type_id, number_of_submissions, create_user, create_date, modify_user, modify_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' |
| 15 | +const QUERY_DELETE_CHALLENGE_PRIZE = 'DELETE FROM prize WHERE prize_id = %d and project_id = %d' |
11 | 16 |
|
12 | 17 | /** |
13 | 18 | * Prepare Informix statement |
@@ -60,13 +65,70 @@ async function updatePrize (prizeId, challengeLegacyId, prizeAmount, numberOfSub |
60 | 65 | await connection.rollbackTransactionAsync() |
61 | 66 | throw e |
62 | 67 | } finally { |
63 | | - logger.info(`Phase ${prizeId} has been updated`) |
| 68 | + logger.info(`Prize ${prizeId} has been updated`) |
| 69 | + await connection.closeAsync() |
| 70 | + } |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Creates a new prize in IFX |
| 76 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 77 | + * @param {Number} place the placement |
| 78 | + * @param {Number} prizeAmount the prize amount |
| 79 | + * @param {Number} prizeTypeId the prize type ID |
| 80 | + * @param {Number} numberOfSubmissions the number of submissions that will receive the prize |
| 81 | + * @param {String} createdBy the creator user |
| 82 | + */ |
| 83 | +async function createPrize (challengeLegacyId, place, prizeAmount, prizeTypeId, numberOfSubmissions, createdBy) { |
| 84 | + const connection = await helper.getInformixConnection() |
| 85 | + let result = null |
| 86 | + let prizeId |
| 87 | + try { |
| 88 | + // await connection.beginTransactionAsync() |
| 89 | + prizeId = await prizeIdGen.getNextId() |
| 90 | + const currentDateIso = new Date().toISOString().replace('T', ' ').replace('Z', '').split('.')[0] |
| 91 | + const query = await prepare(connection, QUERY_CREATE_CHALLENGE_PRIZE) |
| 92 | + result = await query.executeAsync([prizeId, challengeLegacyId, place, prizeAmount, prizeTypeId, numberOfSubmissions, createdBy, currentDateIso, createdBy, currentDateIso]) |
| 93 | + // await connection.commitTransactionAsync() |
| 94 | + } catch (e) { |
| 95 | + logger.error(`Error in 'createPrize' ${e}, rolling back transaction`) |
| 96 | + await connection.rollbackTransactionAsync() |
| 97 | + throw e |
| 98 | + } finally { |
| 99 | + logger.info(`Prize ${prizeId} has been updated`) |
| 100 | + await connection.closeAsync() |
| 101 | + } |
| 102 | + return result |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Deletes a prize from IFX |
| 107 | + * @param {Number} challengeLegacyId the legacy challenge ID |
| 108 | + * @param {Number} prizeId the prize ID |
| 109 | + */ |
| 110 | +async function deletePrize (challengeLegacyId, prizeId) { |
| 111 | + const connection = await helper.getInformixConnection() |
| 112 | + let result = null |
| 113 | + try { |
| 114 | + // await connection.beginTransactionAsync() |
| 115 | + const query = await prepare(connection, QUERY_DELETE_CHALLENGE_PRIZE) |
| 116 | + result = await query.executeAsync([prizeId, challengeLegacyId]) |
| 117 | + // await connection.commitTransactionAsync() |
| 118 | + } catch (e) { |
| 119 | + logger.error(`Error in 'deletePrize' ${e}, rolling back transaction`) |
| 120 | + await connection.rollbackTransactionAsync() |
| 121 | + throw e |
| 122 | + } finally { |
| 123 | + logger.info(`Prize ${prizeId} has been deleted`) |
64 | 124 | await connection.closeAsync() |
65 | 125 | } |
66 | 126 | return result |
67 | 127 | } |
68 | 128 |
|
69 | 129 | module.exports = { |
70 | 130 | getChallengePrizes, |
71 | | - updatePrize |
| 131 | + updatePrize, |
| 132 | + createPrize, |
| 133 | + deletePrize |
72 | 134 | } |
0 commit comments