This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselfServiceReviewerService.js
87 lines (82 loc) · 2.94 KB
/
selfServiceReviewerService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Number of reviewers Service
* Interacts with InformixDB
*/
const util = require('util')
const logger = require('../common/logger')
const helper = require('../common/helper')
const QUERY_GET_ENTRY = 'SELECT parameter FROM phase_criteria WHERE project_phase_id = %d'
const QUERY_CREATE = 'INSERT INTO phase_criteria (project_phase_id, phase_criteria_type_id, parameter, create_user, create_date, modify_user, modify_date) VALUES (?, 6, ?, ?, CURRENT, ?, CURRENT)'
const QUERY_UPDATE = 'UPDATE phase_criteria SET parameter = ?, modify_user = ?, modify_date = CURRENT WHERE project_phase_id = ?'
const QUERY_DELETE = 'DELETE FROM phase_criteria WHERE project_phase_id = ?'
/**
* Prepare Informix statement
* @param {Object} connection the Informix connection
* @param {String} sql the sql
* @return {Object} Informix statement
*/
async function prepare (connection, sql) {
// logger.debug(`Preparing SQL ${sql}`)
const stmt = await connection.prepareAsync(sql)
return Promise.promisifyAll(stmt)
}
/**
* Get entry
* @param {Number} phaseId the phase ID
*/
async function getEntry (phaseId) {
// logger.debug(`Getting Groups for Challenge ${challengeLegacyId}`)
const connection = await helper.getInformixConnection()
let result = null
try {
result = await connection.queryAsync(util.format(QUERY_GET_ENTRY, phaseId))
} catch (e) {
logger.error(`Error in 'getEntry' ${e}`)
throw e
} finally {
await connection.closeAsync()
}
return result
}
/**
* Enable timeline notifications
* @param {Number} phaseId the legacy challenge ID
* @param {Number} typeId the type ID
* @param {Any} value the value
* @param {String} createdBy the created by
*/
async function createOrSetNumberOfReviewers (phaseId, value, createdBy) {
const connection = await helper.getInformixConnection()
let result = null
try {
await connection.beginTransactionAsync()
const [existing] = await getEntry(phaseId)
if (existing) {
if (value) {
const query = await prepare(connection, QUERY_UPDATE)
logger.info(`Will update with values: ${value}, ${createdBy}, ${phaseId}`)
result = await query.executeAsync([value, createdBy, phaseId])
} else {
const query = await prepare(connection, QUERY_DELETE)
logger.info(`Will delete with values: ${phaseId}`)
result = await query.executeAsync([phaseId])
}
} else {
const query = await prepare(connection, QUERY_CREATE)
logger.info(`Will delete with values: ${phaseId}, ${value}, ${createdBy}, ${createdBy}`)
result = await query.executeAsync([phaseId, value, createdBy, createdBy])
}
await connection.commitTransactionAsync()
} catch (e) {
logger.error(`Error in 'createOrSetNumberOfReviewers' ${e}, rolling back transaction`)
await connection.rollbackTransactionAsync()
throw e
} finally {
await connection.closeAsync()
}
return result
}
module.exports = {
getEntry,
createOrSetNumberOfReviewers
}