<commit_before>/*
 The Conflict of Interest (COI) module of Kuali Research
 Copyright © 2005-2016 Kuali, Inc.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

import {
  createAdditionalReviewer,
  deleteAdditionalReviewer,
  updateAdditionalReviewer,
  getReviewerForDisclosureAndUser,
  getDisclosuresForReviewer,
  saveRecommendation,
  saveProjectRecommendation
} from '../db/additional-reviewer-db';
import { getReviewers } from '../services/auth-service/auth-service';
import { ROLES, DATE_TYPE } from '../../coi-constants';
const { ADMIN, REVIEWER } = ROLES;
import { allowedRoles } from '../middleware/role-check';
import { OK, ACCEPTED } from '../../http-status-codes';
import wrapAsync from './wrap-async';
import {
  createAndSendReviewerAssignedNotification,
  createAndSendReviewCompleteNotification,
  createAndSendReviewerUnassignNotification
} from '../services/notification-service/notification-service';
import useKnex from '../middleware/request-knex';

export const init = app => {
  app.post(
    '/api/coi/additional-reviewers',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, body, userInfo, hostname, dbInfo} = req;
      let result;
      await knex.transaction(async (knexTrx) => {
        result = await createAdditionalReviewer(knexTrx, body, userInfo);
      });

      await createAndSendReviewerAssignedNotification(
        dbInfo,
        hostname,
        userInfo,
        result.id
      );

      res.send(result);
    }
  ));

  app.delete(
    '/api/coi/additional-reviewers/:id',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, dbInfo, hostname, userInfo, params} = req;

      await createAndSendReviewerUnassignNotification(
        dbInfo,
        hostname,
        userInfo,
        params.id
      );

      await knex.transaction(async (knexTrx) => {
        await deleteAdditionalReviewer(knexTrx, params.id);
      });
      res.sendStatus(OK);
    }
  ));

  app.put(
    '/api/coi/additional-reviewers/:id',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async ({knex, params, body}, res) =>
    {
      await knex.transaction(async (knexTrx) => {
        await updateAdditionalReviewer(knexTrx, params.id, body);
      });
      res.sendStatus(OK);
    }
  ));

  app.put(
    '/api/coi/additional-reviewers/complete-review/:disclosureId',
    allowedRoles(REVIEWER),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, userInfo, params, dbInfo, hostname, headers} = req;
      let additionalReviewer;
      await knex.transaction(async (knexTrx) => {
        additionalReviewer = await getReviewerForDisclosureAndUser(
          knexTrx,
          userInfo.schoolId,
          params.disclosureId
        );
        const dates = JSON.parse(additionalReviewer[0].dates);
        dates.push({type: DATE_TYPE.COMPLETED, date: new Date()});
        const updates = {
          active: false,
          dates
        };
        await updateAdditionalReviewer(
          knexTrx,
          additionalReviewer[0].id,
          updates
        );<commit_msg>Sending a notification when a reviewer is reassigned.<commit_after>/*
 The Conflict of Interest (COI) module of Kuali Research
 Copyright © 2005-2016 Kuali, Inc.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

import {
  createAdditionalReviewer,
  deleteAdditionalReviewer,
  updateAdditionalReviewer,
  getReviewerForDisclosureAndUser,
  getDisclosuresForReviewer,
  saveRecommendation,
  saveProjectRecommendation
} from '../db/additional-reviewer-db';
import { getReviewers } from '../services/auth-service/auth-service';
import { ROLES, DATE_TYPE } from '../../coi-constants';
const { ADMIN, REVIEWER } = ROLES;
import { allowedRoles } from '../middleware/role-check';
import { OK, ACCEPTED } from '../../http-status-codes';
import wrapAsync from './wrap-async';
import {
  createAndSendReviewerAssignedNotification,
  createAndSendReviewCompleteNotification,
  createAndSendReviewerUnassignNotification
} from '../services/notification-service/notification-service';
import useKnex from '../middleware/request-knex';

export const init = app => {
  app.post(
    '/api/coi/additional-reviewers',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, body, userInfo, hostname, dbInfo} = req;
      let result;
      await knex.transaction(async (knexTrx) => {
        result = await createAdditionalReviewer(knexTrx, body, userInfo);
      });

      await createAndSendReviewerAssignedNotification(
        dbInfo,
        hostname,
        userInfo,
        result.id
      );

      res.send(result);
    }
  ));

  app.delete(
    '/api/coi/additional-reviewers/:id',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, dbInfo, hostname, userInfo, params} = req;

      await createAndSendReviewerUnassignNotification(
        dbInfo,
        hostname,
        userInfo,
        params.id
      );

      await knex.transaction(async (knexTrx) => {
        await deleteAdditionalReviewer(knexTrx, params.id);
      });
      res.sendStatus(OK);
    }
  ));

  app.put(
    '/api/coi/additional-reviewers/:id',
    allowedRoles(ADMIN),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, body, userInfo, hostname, dbInfo, params} = req;
      await knex.transaction(async (knexTrx) => {
        await updateAdditionalReviewer(knexTrx, params.id, body);
      });
      
      await createAndSendReviewerAssignedNotification(
          dbInfo,
          hostname,
          userInfo,
          params.id
      );

      res.sendStatus(OK);
    }
  ));

  app.put(
    '/api/coi/additional-reviewers/complete-review/:disclosureId',
    allowedRoles(REVIEWER),
    useKnex,
    wrapAsync(async (req, res) =>
    {
      const {knex, userInfo, params, dbInfo, hostname, headers} = req;
      let additionalReviewer;
      await knex.transaction(async (knexTrx) => {
        additionalReviewer = await getReviewerForDisclosureAndUser(
          knexTrx,
          userInfo.schoolId,
          params.disclosureId
        );
        const dates = JSON.parse(additionalReviewer[0].dates);
        dates.push({type: DATE_TYPE.COMPLETED, date: new Date()});
        const updates = {
          active: false,
          dates
        };
        await updateAdditionalReviewer(
          knexTrx,
          additionalReviewer[0].id,
          updates
        );
{'fraction_non_alphanumeric': 0.10336757248732284, 'fraction_numerical': 0.0028604862826680537, 'mean_word_length': 2.877016129032258, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7386596', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2234, 'n_words': 591}