Brandon Nicholls

RESCOI-873: Enhanced logging module

Showing 47 changed files with 529 additions and 369 deletions
......@@ -49,6 +49,7 @@
"classnames": "2.2.5",
"cookie-parser": "1.4.3",
"cookies-js": "1.2.2",
"debug": "2.3.3",
"draft-js": "0.9.1",
"express": "4.14.0",
"knex": "0.12.5",
......
......@@ -34,7 +34,6 @@ import authentication from './middleware/authentication';
import apiAuthentication from './middleware/api-authentication';
import renderView from './middleware/render-view';
import {noCache} from './middleware/cache';
import Log from './log';
import methodChecker from './middleware/method-checker';
import ErrorLogger from './middleware/error-logger';
import { LOG_LEVEL } from '../coi-constants';
......@@ -45,6 +44,8 @@ import scheduleExpirationCheck from './expiration-check';
import impersonationLogger from './middleware/impersonation-logger';
import flags from '../feature-flags.json'; // eslint-disable-line
import initFeatureFlags from './feature-flags';
import {createLogger} from './log';
const log = createLogger('App');
const DEFAULT_PORT = 8090;
......@@ -54,14 +55,14 @@ function conditionallyLogRequests(app, logLevel) {
if (logLevel <= LOG_LEVEL.INFO) {
app.use((req, res, next) => {
const startTime = new Date().getTime();
Log.info('request received', req);
log.info('request received', req);
res.on('finish', () => {
const elapsedTime = new Date().getTime() - startTime;
Log.info(`request finished - ${elapsedTime}ms`, req);
log.info(`request finished - ${elapsedTime}ms`, req);
});
res.on('close', () => {
const elapsedTime = new Date().getTime() - startTime;
Log.info(`request closed - ${elapsedTime}ms`, req);
log.info(`request closed - ${elapsedTime}ms`, req);
});
next();
});
......@@ -71,7 +72,7 @@ function conditionallyLogRequests(app, logLevel) {
function configureProxy(app) {
const TRUST_PROXY = process.env.TRUST_PROXY;
if (TRUST_PROXY) {
Log.info(`Using TRUST_PROXY value of ${TRUST_PROXY}`);
log.info(`Using TRUST_PROXY value of ${TRUST_PROXY}`);
if (TRUST_PROXY.toLowerCase() === 'true') {
app.set('trust proxy', true);
......@@ -106,11 +107,11 @@ export function run() {
logLevel = config.logLevel;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
Log.error(e);
log.error(e);
}
else {
logLevel = process.env.LOG_LEVEL;
Log.info('extensions not found');
log.info('extensions not found');
}
}
......
......@@ -18,9 +18,22 @@
/* eslint-disable no-var, prefer-arrow-callback, prefer-template, no-console */
var _ = require('lodash');
function defaultAppLoggers() {
if (_.isEmpty(process.env.DEBUG)) {
process.env.DEBUG = 'coi:*';
var debug = require('debug');
debug.enable(process.env.DEBUG);
}
}
defaultAppLoggers();
require('babel-polyfill');
var app = require('./app');
var Log = require('./log').default;
var createLogger = require('./log').createLogger;
const log = createLogger('Bootstrap');
var application = app.run();
var portNumber = application.get('portNumber');
......@@ -29,11 +42,11 @@ var server = application.listen(portNumber);
console.log('Listening on port ' + portNumber + ' in ' + application.get('env') + ' mode');
process.on('uncaughtException', function(err) {
Log.error('Uncaught exception: ' + err);
Log.error(err);
Log.error('waiting for pending connections to clear');
log.error('Uncaught exception: ' + err);
log.error(err);
log.error('waiting for pending connections to clear');
server.close(function() {
Log.error('shutting down');
log.error('shutting down');
process.exit(1);
});
});
\ No newline at end of file
......
......@@ -16,15 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
import {
createAdditionalReviewer,
unassignAdditionalReviewer,
updateAdditionalReviewer,
getReviewerForDisclosureAndUser,
getDisclosuresForReviewer,
saveRecommendation,
saveProjectRecommendation
} from '../db/additional-reviewer-db';
import ReviewerDB from '../db/additional-reviewer-db';
import { getReviewers } from '../services/auth-service/auth-service';
import { ROLES, DATE_TYPE } from '../../coi-constants';
const { ADMIN, REVIEWER } = ROLES;
......@@ -48,7 +40,7 @@ export const init = app => {
const {knex, body, userInfo, hostname, dbInfo} = req;
let result;
await knex.transaction(async (knexTrx) => {
result = await createAdditionalReviewer(
result = await ReviewerDB.createAdditionalReviewer(
knexTrx,
body,
userInfo.displayName
......@@ -82,7 +74,10 @@ export const init = app => {
);
await knex.transaction(async (knexTrx) => {
await unassignAdditionalReviewer(knexTrx, params.id);
await ReviewerDB.unassignAdditionalReviewer(
knexTrx,
params.id
);
});
res.sendStatus(OK);
}
......@@ -96,7 +91,11 @@ export const init = app => {
{
const {knex, body, userInfo, hostname, dbInfo, params} = req;
await knex.transaction(async (knexTrx) => {
await updateAdditionalReviewer(knexTrx, params.id, body);
await ReviewerDB.updateAdditionalReviewer(
knexTrx,
params.id,
body
);
});
await createAndSendReviewerAssignedNotification(
......@@ -119,7 +118,7 @@ export const init = app => {
const {knex, userInfo, params, dbInfo, hostname, headers} = req;
let additionalReviewer;
await knex.transaction(async (knexTrx) => {
additionalReviewer = await getReviewerForDisclosureAndUser(
additionalReviewer = await ReviewerDB.getReviewerForDisclosureAndUser( // eslint-disable-line max-len
knexTrx,
userInfo.schoolId,
params.disclosureId
......@@ -130,7 +129,7 @@ export const init = app => {
active: false,
dates
};
await updateAdditionalReviewer(
await ReviewerDB.updateAdditionalReviewer(
knexTrx,
additionalReviewer[0].id,
updates
......@@ -171,7 +170,10 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, userInfo}, res) =>
{
const results = await getDisclosuresForReviewer(knex, userInfo.schoolId);
const results = await ReviewerDB.getDisclosuresForReviewer(
knex,
userInfo.schoolId
);
res.send(results);
}
));
......@@ -192,7 +194,7 @@ export const init = app => {
);
const existingReviewerIds = (
await getReviewerForDisclosureAndUser(
await ReviewerDB.getReviewerForDisclosureAndUser(
knex,
undefined,
params.disclosureId
......@@ -218,7 +220,7 @@ export const init = app => {
wrapAsync(async ({knex, userInfo, params, body}, res) =>
{
await knex.transaction(async (knexTrx) => {
await saveRecommendation(
await ReviewerDB.saveRecommendation(
knexTrx,
userInfo.schoolId,
params.disclosureId,
......@@ -237,7 +239,7 @@ export const init = app => {
wrapAsync(async ({knex, userInfo, params, body}, res) =>
{
await knex.transaction(async (knexTrx) => {
await saveProjectRecommendation(
await ReviewerDB.saveProjectRecommendation(
knexTrx,
userInfo.schoolId,
params.disclosureId,
......
......@@ -16,15 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
import {
getConfig,
archiveConfig,
setConfig,
getArchivedConfig
} from '../db/config-db';
import {
updateAllProjectPersons
} from '../db/project-db';
import ConfigDB from '../db/config-db';
import ProjectDB from '../db/project-db';
import { ROLES } from '../../coi-constants';
const { ADMIN } = ROLES;
import { allowedRoles } from '../middleware/role-check';
......@@ -38,15 +31,26 @@ export async function saveConfig(req, res) {
let config;
await knex.transaction(async (knexTrx) => {
await setConfig(dbInfo, knexTrx, userInfo.schoolId, body, hostname);
config = await getConfig(dbInfo, knexTrx, hostname);
await ConfigDB.setConfig(
dbInfo,
knexTrx,
userInfo.schoolId,
body,
hostname
);
config = await ConfigDB.getConfig(dbInfo, knexTrx, hostname);
config.general = body.general;
await archiveConfig(knexTrx, userInfo.schoolId, userInfo.username, config);
await ConfigDB.archiveConfig(
knexTrx,
userInfo.schoolId,
userInfo.username,
config
);
});
res.send(config);
await updateAllProjectPersons(knex, req);
await ProjectDB.updateAllProjectPersons(knex, req);
await handleNotifications();
}
......@@ -57,7 +61,7 @@ export const init = app => {
useKnex,
wrapAsync(async ({dbInfo, knex, hostname}, res) =>
{
const result = await getConfig(dbInfo, knex, hostname);
const result = await ConfigDB.getConfig(dbInfo, knex, hostname);
res.send(result);
}
));
......@@ -68,7 +72,7 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, params}, res) =>
{
const result = await getArchivedConfig(knex, params.id);
const result = await ConfigDB.getArchivedConfig(knex, params.id);
res.send(result);
}
));
......
This diff is collapsed. Click to expand it.
......@@ -21,7 +21,7 @@ const { ADMIN } = ROLES;
import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
import useKnex from '../middleware/request-knex';
import {getFeatureFlags, setFeatureFlagState} from '../db/features-db';
import FeaturesDB from '../db/features-db';
import {ACCEPTED} from '../../http-status-codes';
export const init = app => {
......@@ -30,7 +30,7 @@ export const init = app => {
allowedRoles(ADMIN),
useKnex,
wrapAsync(async ({knex}, res) => {
const flags = await getFeatureFlags(knex);
const flags = await FeaturesDB.getFeatureFlags(knex);
res.send(flags);
})
);
......@@ -41,7 +41,11 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, params, body}, res) =>
{
await setFeatureFlagState(knex, params.key, body.active === true);
await FeaturesDB.setFeatureFlagState(
knex,
params.key,
body.active === true
);
res.sendStatus(ACCEPTED);
}
));
......
......@@ -17,16 +17,11 @@
*/
import {getFileStream} from '../services/file-service/file-service';
import {
isDisclosureUsers,
isFinancialEntityUsers,
getDisclosureForFinancialEntity
} from '../db/common-db';
import { getDisclosureIdsForReviewer } from '../db/additional-reviewer-db';
import CommonDB from '../db/common-db';
import ReviewerDB from '../db/additional-reviewer-db';
import { ROLES, FILE_TYPE, LANES } from '../../coi-constants';
import * as FileDb from '../db/file-db';
import FileDb from '../db/file-db';
import multer from 'multer';
import Log from '../log';
import {
FORBIDDEN,
ACCEPTED,
......@@ -36,6 +31,8 @@ import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
import archiver from 'archiver';
import useKnex from '../middleware/request-knex';
import {createLogger} from '../log';
const log = createLogger('FileController');
let upload = multer({dest: process.env.LOCAL_FILE_DESTINATION || 'uploads/' });
try {
......@@ -47,7 +44,7 @@ try {
}
catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
Log.error(err);
log.error(err);
}
}
......@@ -66,7 +63,7 @@ async function userHasPermissionForMultiFileUpload(
) {
if (fileType === FILE_TYPE.FINANCIAL_ENTITY) {
if (userInfo.coiRole === ROLES.USER) {
const permitted = await isFinancialEntityUsers(
const permitted = await CommonDB.isFinancialEntityUsers(
knex,
refId,
userInfo.schoolId
......@@ -77,11 +74,11 @@ async function userHasPermissionForMultiFileUpload(
}
if (userInfo.coiRole === ROLES.REVIEWER) {
const disclosureId = await getDisclosureForFinancialEntity(
const disclosureId = await CommonDB.getDisclosureForFinancialEntity(
knex,
refId
);
const reviewerDisclosures = await getDisclosureIdsForReviewer(
const reviewerDisclosures = await ReviewerDB.getDisclosureIdsForReviewer(
knex,
userInfo.schoolId
);
......@@ -91,7 +88,7 @@ async function userHasPermissionForMultiFileUpload(
}
} else {
if (userInfo.coiRole === ROLES.USER) {
const permitted = await isDisclosureUsers(
const permitted = await CommonDB.isDisclosureUsers(
knex,
refId,
userInfo.schoolId
......@@ -102,7 +99,7 @@ async function userHasPermissionForMultiFileUpload(
}
if (userInfo.coiRole === ROLES.REVIEWER) {
const reviewerDisclosures = await getDisclosureIdsForReviewer(
const reviewerDisclosures = await ReviewerDB.getDisclosureIdsForReviewer(
knex,
userInfo.schoolId
);
......
......@@ -21,10 +21,7 @@ const { ADMIN } = ROLES;
import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
import useKnex from '../middleware/request-knex';
import {
getLatestNotifications,
notificationTemplateIsForCOI
} from '../db/notifications-db';
import NotificationsDB from '../db/notifications-db';
import {FORBIDDEN, NOT_FOUND} from '../../http-status-codes';
import {
getNotificationReceiptDetail
......@@ -36,7 +33,9 @@ export const init = app => {
allowedRoles(ADMIN),
useKnex,
wrapAsync(async ({knex}, res) => {
const notificationRequests = await getLatestNotifications(knex);
const notificationRequests = await NotificationsDB.getLatestNotifications(
knex
);
res.send(notificationRequests);
})
);
......@@ -57,7 +56,10 @@ export const init = app => {
return;
}
const valid = await notificationTemplateIsForCOI(knex, detail.templateId);
const valid = await NotificationsDB.notificationTemplateIsForCOI(
knex,
detail.templateId
);
if (!valid) {
res.sendStatus(FORBIDDEN);
return;
......
......@@ -16,18 +16,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
import * as PIDB from '../db/pi-db';
import * as PIReviewDB from '../db/pi-review-db';
import {
isDisclosureUsers,
isFinancialEntityUsers,
isProjectUsers
} from '../db/common-db';
import PIDB from '../db/pi-db';
import PIReviewDB from '../db/pi-review-db';
import CommonDB from '../db/common-db';
import { ROLES } from '../../coi-constants';
const { ADMIN, REVIEWER } = ROLES;
import { allowedRoles } from '../middleware/role-check';
import { FORBIDDEN, NO_CONTENT, ACCEPTED } from '../../http-status-codes';
import { getDisclosureIdsForReviewer } from '../db/additional-reviewer-db';
import ReviewerDB from '../db/additional-reviewer-db';
import {
createAndSendResubmitNotification
} from '../services/notification-service/notification-service';
......@@ -119,7 +115,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const isSubmitter = await isDisclosureUsers(
const isSubmitter = await CommonDB.isDisclosureUsers(
knex,
params.disclosureId,
userInfo.schoolId
......@@ -178,7 +174,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const isOwner = await isFinancialEntityUsers(
const isOwner = await CommonDB.isFinancialEntityUsers(
knex,
params.entityId,
userInfo.schoolId
......@@ -238,7 +234,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const isOwner = await isFinancialEntityUsers(
const isOwner = await CommonDB.isFinancialEntityUsers(
knex,
params.entityId,
userInfo.schoolId
......@@ -296,7 +292,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo}, res) => {
const isOwner = await isFinancialEntityUsers(
const isOwner = await CommonDB.isFinancialEntityUsers(
knex,
params.entityId,
userInfo.schoolId
......@@ -354,12 +350,12 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const [isEntityOwner, isProjectOwner] = await Promise.all([
isFinancialEntityUsers(
CommonDB.isFinancialEntityUsers(
knex,
params.entityId,
userInfo.schoolId
),
isProjectUsers(
CommonDB.isProjectUsers(
knex,
params.projectId,
userInfo.schoolId
......@@ -419,7 +415,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const isSubmitter = await isDisclosureUsers(
const isSubmitter = await CommonDB.isDisclosureUsers(
knex,
params.disclosureId,
userInfo.schoolId
......@@ -478,7 +474,7 @@ export const init = app => {
wrapAsync(async (req, res) => {
const {dbInfo, params, userInfo, hostname, headers, body, knex} = req;
const isSubmitter = isDisclosureUsers(
const isSubmitter = CommonDB.isDisclosureUsers(
knex,
params.disclosureId,
userInfo.schoolId
......@@ -529,7 +525,7 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, params, userInfo}, res) => {
if (userInfo.coiRole === REVIEWER) {
const reviewerDisclosureIds = await getDisclosureIdsForReviewer(
const reviewerDisclosureIds = await ReviewerDB.getDisclosureIdsForReviewer( // eslint-disable-line max-len
knex,
userInfo.schoolId
);
......@@ -555,7 +551,7 @@ export const init = app => {
allowedRoles('ANY'),
useKnex,
wrapAsync(async ({knex, params, userInfo, body}, res) => {
const isOwner = await isFinancialEntityUsers(
const isOwner = await CommonDB.isFinancialEntityUsers(
knex,
params.entityId,
userInfo.schoolId
......
......@@ -16,13 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
import {
saveProject,
getProjects,
updateProjectPersonDispositionType,
getProjectStatuses,
getProjectStatus
} from '../db/project-db';
import ProjectDB from '../db/project-db';
import { ROLES } from '../../coi-constants';
import { OK, BAD_REQUEST } from '../../http-status-codes';
const { ADMIN } = ROLES;
......@@ -30,8 +24,9 @@ import { allowedRoles } from '../middleware/role-check';
import { filterProjects } from '../services/project-service/project-service';
import wrapAsync from './wrap-async';
import projectIsValid from '../validators/project';
import Log from '../log';
import useKnex from '../middleware/request-knex';
import {createLogger} from '../log';
const log = createLogger('ProjectController');
export const init = app => {
app.post(
......@@ -44,7 +39,7 @@ export const init = app => {
if (!projectIsValid(body)) {
res.status(BAD_REQUEST);
res.json(projectIsValid.errors);
Log.error(
log.error(
`An invalid project was pushed to /api/coi/projects
${JSON.stringify(projectIsValid.errors)}`
);
......@@ -53,7 +48,7 @@ export const init = app => {
let result;
await knex.transaction(async (knexTrx) => {
result = await saveProject(knexTrx, req, body);
result = await ProjectDB.saveProject(knexTrx, req, body);
});
if (!result) {
res.sendStatus(OK);
......@@ -72,7 +67,7 @@ export const init = app => {
useKnex,
wrapAsync(async ({knex, userInfo, query, dbInfo, headers}, res) =>
{
const projects = await getProjects(knex, userInfo.schoolId);
const projects = await ProjectDB.getProjects(knex, userInfo.schoolId);
if (query.filter) {
const result = await filterProjects(
dbInfo,
......@@ -94,7 +89,7 @@ export const init = app => {
wrapAsync(async ({body, params, knex}, res) =>
{
await knex.transaction(async (knexTrx) => {
await updateProjectPersonDispositionType(
await ProjectDB.updateProjectPersonDispositionType(
knexTrx,
body,
parseInt(params.id)
......@@ -110,7 +105,7 @@ export const init = app => {
useKnex,
wrapAsync(async ({dbInfo, knex, params, headers}, res) => {
const {sourceId, projectId} = params;
const result = await getProjectStatuses(
const result = await ProjectDB.getProjectStatuses(
dbInfo,
knex,
sourceId,
......@@ -128,7 +123,7 @@ export const init = app => {
wrapAsync(async ({dbInfo, knex, params, headers}, res) =>
{
const {sourceId, projectId, personId} = params;
const result = await getProjectStatus(
const result = await ProjectDB.getProjectStatus(
dbInfo,
knex,
sourceId,
......
......@@ -16,12 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
import {
getTravelLogEntries,
createTravelLogEntry,
deleteTravelLogEntry,
updateTravelLogEntry
} from '../db/travel-log-db';
import TravelLogDB from '../db/travel-log-db';
import {OK} from '../../http-status-codes';
import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
......@@ -43,7 +38,7 @@ export const init = app => {
filter = 'all'
} = query;
const result = await getTravelLogEntries(
const result = await TravelLogDB.getTravelLogEntries(
knex,
userInfo.schoolId,
sortColumn,
......@@ -65,7 +60,11 @@ export const init = app => {
{
let result;
await knex.transaction(async (knexTrx) => {
result = await createTravelLogEntry(knexTrx, body, userInfo);
result = await TravelLogDB.createTravelLogEntry(
knexTrx,
body,
userInfo
);
});
res.send(result);
}
......@@ -81,7 +80,7 @@ export const init = app => {
wrapAsync(async ({knex, dbInfo, params, userInfo}, res) =>
{
await knex.transaction(async (knexTrx) => {
await deleteTravelLogEntry(
await TravelLogDB.deleteTravelLogEntry(
dbInfo,
knexTrx,
params.id,
......@@ -103,7 +102,7 @@ export const init = app => {
{
let result;
await knex.transaction(async (knexTrx) => {
result = await updateTravelLogEntry(
result = await TravelLogDB.updateTravelLogEntry(
dbInfo,
knexTrx,
body,
......
......@@ -18,7 +18,8 @@
import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
import Log from '../log';
import {createLogger} from '../log';
const log = createLogger('UserController');
let getAuthorizationInfo;
try {
......@@ -26,7 +27,7 @@ try {
getAuthorizationInfo = extensions.getAuthorizationInfo;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
Log.error(e);
log.error(e);
}
getAuthorizationInfo = (dbInfo) => { //eslint-disable-line no-unused-vars
return {
......
......@@ -17,8 +17,12 @@
*/
import { DATE_TYPE } from '../../coi-constants';
import {addLoggers} from '../log';
export async function getAdditionalReviewer(knex, id) {
const AdditionalReviewerDB = {};
export default AdditionalReviewerDB;
AdditionalReviewerDB.getAdditionalReviewer = async function (knex, id) {
const reviewer = await knex('additional_reviewer')
.first(
'name',
......@@ -36,9 +40,12 @@ export async function getAdditionalReviewer(knex, id) {
reviewer.dates = JSON.parse(reviewer.dates);
}
return reviewer;
}
};
export async function getDisclosuresForReviewer(knex, schoolId) {
AdditionalReviewerDB.getDisclosuresForReviewer = async function(
knex,
schoolId
) {
return await knex('additional_reviewer as ar')
.select(
'd.type_cd as typeCd',
......@@ -61,9 +68,12 @@ export async function getDisclosuresForReviewer(knex, schoolId) {
'ar.user_id': schoolId,
'ar.active': true
});
}
};
export async function getDisclosureIdsForReviewer(knex, schoolId) {
AdditionalReviewerDB.getDisclosureIdsForReviewer = async function(
knex,
schoolId
) {
const reviewers = await knex('additional_reviewer')
.select('disclosure_id as disclosureId')
.where({
......@@ -74,13 +84,13 @@ export async function getDisclosureIdsForReviewer(knex, schoolId) {
return reviewers.map(reviewer => {
return reviewer.disclosureId.toString();
});
}
};
export async function getReviewerForDisclosureAndUser(
knex,
schoolId,
disclosureId
) {
AdditionalReviewerDB.getReviewerForDisclosureAndUser = async function (
knex,
schoolId,
disclosureId
) {
const criteria = {
disclosure_id: disclosureId,
active: true
......@@ -93,9 +103,13 @@ export async function getReviewerForDisclosureAndUser(
return knex('additional_reviewer')
.select('id','dates', 'user_id as userId')
.where(criteria);
}
};
export async function createAdditionalReviewer(knex, reviewer, displayName) {
AdditionalReviewerDB.createAdditionalReviewer = async function (
knex,
reviewer,
displayName
) {
reviewer.dates = [
{
type: DATE_TYPE.ASSIGNED,
......@@ -118,9 +132,9 @@ export async function createAdditionalReviewer(knex, reviewer, displayName) {
reviewer.id = parseInt(id[0]);
return reviewer;
}
};
export async function unassignAdditionalReviewer(knex, id) {
AdditionalReviewerDB.unassignAdditionalReviewer = async function (knex, id) {
const preExistingDates = await knex
.first('dates as dates')
.from('additional_reviewer')
......@@ -144,9 +158,9 @@ export async function unassignAdditionalReviewer(knex, id) {
dates: JSON.stringify(dates)
})
.where({id});
}
};
export function updateAdditionalReviewer(knex, id, updates) {
AdditionalReviewerDB.updateAdditionalReviewer = function (knex, id, updates) {
if (updates.dates) {
updates.dates = JSON.stringify(updates.dates);
}
......@@ -157,25 +171,29 @@ export function updateAdditionalReviewer(knex, id, updates) {
dates: updates.dates
})
.where({id});
}
};
async function findAdditionalReviewer(knex, userId, disclosureId) {
AdditionalReviewerDB.findAdditionalReviewer = async function (
knex,
userId,
disclosureId
) {
return await knex('additional_reviewer')
.first('id')
.where({
user_id: userId,
disclosure_id: disclosureId
});
}
export async function saveRecommendation(
knex,
schoolId,
disclosureId,
declarationId,
dispositionType
) {
const additionalReviewer = await findAdditionalReviewer(
};
AdditionalReviewerDB.saveRecommendation = async function (
knex,
schoolId,
disclosureId,
declarationId,
dispositionType
) {
const additionalReviewer = await AdditionalReviewerDB.findAdditionalReviewer(
knex,
schoolId,
disclosureId
......@@ -212,16 +230,16 @@ export async function saveRecommendation(
declaration_id: declarationId,
disposition_type_id: dispositionType
});
}
export async function saveProjectRecommendation(
knex,
schoolId,
disclosureId,
projectPersonId,
dispositionType
) {
const additionalReviewer = await findAdditionalReviewer(
};
AdditionalReviewerDB.saveProjectRecommendation = async function (
knex,
schoolId,
disclosureId,
projectPersonId,
dispositionType
) {
const additionalReviewer = await AdditionalReviewerDB.findAdditionalReviewer(
knex,
schoolId,
disclosureId
......@@ -258,4 +276,6 @@ export async function saveProjectRecommendation(
project_person_id: projectPersonId,
disposition_type_id: dispositionType
});
}
};
addLoggers({AdditionalReviewerDB});
......
......@@ -16,11 +16,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
export function usingMySql(knex) {
import {addLoggers} from '../log';
const CommonDB = {};
export default CommonDB;
CommonDB.usingMySql = function(knex) {
return knex.client.config.client === 'mysql';
}
};
export async function isDisclosureUsers(knex, disclosureId, userId) {
CommonDB.isDisclosureUsers = async function (knex, disclosureId, userId) {
const result = await knex