Skip to content

Commit

Permalink
Merge pull request #629 from HDRUK/Release-bug-fixes
Browse files Browse the repository at this point in the history
Release bug fixes
  • Loading branch information
kandaj authored Jan 10, 2022
2 parents cf8ba80 + cdb8e5c commit 004244c
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 159 deletions.
63 changes: 36 additions & 27 deletions src/resources/course/v1/course.route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import { ROLES } from '../../user/user.roles';
import { Data } from '../../tool/data.model';
import { DataUseRegister } from '../../dataUseRegister/dataUseRegister.model';
import { Course } from '../course.model';
import passport from 'passport';
import { utils } from '../../auth';
Expand Down Expand Up @@ -104,38 +105,46 @@ router.get('/:id', async (req, res) => {
},
},
]);
query.exec((err, data) => {
query.exec(async (err, data) => {
if (data.length > 0) {
var p = Data.aggregate([
{
$match: {
$and: [{ relatedObjects: { $elemMatch: { objectId: req.params.id } } }],
},
},
]);
p.exec((err, relatedData) => {
relatedData.forEach(dat => {
dat.relatedObjects.forEach(x => {
if (x.objectId === req.params.id && dat.id !== req.params.id) {
let relatedObject = {
objectId: dat.id,
reason: x.reason,
objectType: dat.type,
user: x.user,
updated: x.updated,
};
data[0].relatedObjects = [relatedObject, ...(data[0].relatedObjects || [])];
}
});
});
let relatedData = await Data.find({
relatedObjects: { $elemMatch: { objectId: req.params.objectId } },
activeflag: 'active',
});

if (err) return res.json({ success: false, error: err });
let relatedDataFromCourses = await Course.find({
relatedObjects: { $elemMatch: { objectId: req.params.id } },
activeflag: 'active',
});

let relatedDataFromDatauses = await DataUseRegister.find({
relatedObjects: { $elemMatch: { objectId: req.params.id } },
activeflag: 'active',
});

return res.json({
success: true,
data: data,
relatedData = [...relatedData, ...relatedDataFromCourses, ...relatedDataFromDatauses];

relatedData.forEach(dat => {
dat.relatedObjects.forEach(x => {
if (x.objectId === req.params.id && dat.id !== req.params.id) {
let relatedObject = {
objectId: dat.id,
reason: x.reason,
objectType: dat.type,
user: x.user,
updated: x.updated,
};
data[0].relatedObjects = [relatedObject, ...(data[0].relatedObjects || [])];
}
});
});

if (err) return res.json({ success: false, error: err });

return res.json({
success: true,
data: data,
});
} else {
return res.status(404).send(`Course not found for Id: ${escape(id)}`);
}
Expand Down
40 changes: 40 additions & 0 deletions src/resources/dataUseRegister/__mocks__/dataUseRegisters.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const dataUseRegisterUploads = [
accessType: 'accessType',
privacyEnhancements: 'privacy Enhancements',
researchOutputs: 'research Outputs',
relatedObjects: [],
},
{
projectTitle: 'This is another test data use register',
Expand Down Expand Up @@ -404,3 +405,42 @@ export const authors = [
{ _id: '607db9c6e1f9d3704d570d93', firstname: 'James', lastname: 'Smith' },
{ _id: '5fb628de6f3f9767bd2d9281', firstname: 'Michael', lastname: 'Howard' },
];

export const editedDataUseObject = {
projectTitle: 'This a test data use register with an edited title',
projectIdText: 'this is the project id',
datasetNames: [
'This is the dataset title',
'http://localhost:3000/dataset/f725187f-7352-482b-a43b-64ebc96e66f2',
'http://localhost:3000/dataset/c6d6bbd3-74ed-46af-841d-ac5e05f4da41',
'http://localhost:3000/dataset/e55df485-5acd-4606-bbb8-668d4c06380a',
],
applicantNames: [' Michael Donnelly', 'http://localhost:3000/person/8495781222000176', 'http://localhost:3000/person/4495285946631793'],
organisationName: 'organisation name',
organisationSector: 'organisation sector',
applicantId: 'applicant id',
fundersAndSponsors: 'funder1 , funder2 , funder3 ',
accreditedResearcherStatus: 'accredited Researcher Status',
sublicenceArrangements: 'sublicence Arrangements',
laySummary: 'lay Summary',
publicBenefitStatement: 'public Benefit Statement',
requestCategoryType: 'request Category Type',
technicalSummary: 'technical Summary',
otherApprovalCommittees: 'other Approval Committees',
projectStartDate: '2021-09-25',
projectEndDate: '2021-09-30',
latestApprovalDate: '2021-09-21',
dataSensitivityLevel: 'data Sensitivity Level',
legalBasisForDataArticle6: 'legal Basis For Data 6',
legalBasisForDataArticle9: 'legal Basis For Data 9',
dutyOfConfidentiality: 'duty Of Confidentiality',
nationalDataOptOut: 'national Data Opt Out',
requestFrequency: 'request Frequency',
datasetLinkageDescription: 'data Processing Description',
confidentialDataDescription: 'confidential Data Description',
accessDate: '2021-09-26',
accessType: 'accessType',
privacyEnhancements: 'privacy Enhancements',
researchOutputs: 'research Outputs',
relatedObjects: [],
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import sinon from 'sinon';
import DataUseRegisterService from '../dataUseRegister.service';
import DataUseRegisterRepository from '../dataUseRegister.repository';
import dataUseRegisterUtil from '../dataUseRegister.util';
import { dataUseRegisterUploadsWithDuplicates, dataUseRegisterUploads } from '../__mocks__/dataUseRegisters';
import { dataUseRegisterUploadsWithDuplicates, dataUseRegisterUploads, editedDataUseObject } from '../__mocks__/dataUseRegisters';
import { uploader } from '../__mocks__/dataUseRegisterUsers';
import dbHandler from '../../../config/in-memory-db';

beforeAll(async () => {
await dbHandler.connect();
await dbHandler.loadData({ datauseregisters: [dataUseRegisterUploads[0]] });
});

afterAll(async () => await dbHandler.closeDatabase());

describe('DataUseRegisterService', function () {
describe('filterDuplicateDataUseRegisters', function () {
Expand Down Expand Up @@ -58,4 +67,62 @@ describe('DataUseRegisterService', function () {
expect(result[0].projectIdText).toEqual(dataUseRegisterUploads[0].projectIdText);
});
});

describe('buildUpdateObject', () => {
const dataUseRegisterRepository = new DataUseRegisterRepository();
const dataUseRegisterService = new DataUseRegisterService(dataUseRegisterRepository);

it('Should return a valid update object when a dataset is approved', async () => {
const dataUserRegister = dataUseRegisterUploads[0];
const dataUseRegisterPayload = { activeflag: 'active', rejectionReason: '' };
const user = uploader;

const updateObj = await dataUseRegisterService.buildUpdateObject(dataUserRegister, dataUseRegisterPayload, user);

const expectedResponse = { relatedObjects: [], activeflag: 'active', rejectionReason: '' };

expect(updateObj).toEqual(expectedResponse);
});

it('Should return a valid update object when a dataset is rejected', async () => {
const dataUserRegister = dataUseRegisterUploads[0];
const dataUseRegisterPayload = { activeflag: 'rejected', rejectionReason: 'This dataset is rejected' };
const user = uploader;

const updateObj = await dataUseRegisterService.buildUpdateObject(dataUserRegister, dataUseRegisterPayload, user);

const expectedResponse = { relatedObjects: [], activeflag: 'rejected', rejectionReason: 'This dataset is rejected' };

expect(updateObj).toEqual(expectedResponse);
});

it('Should return a valid update object when a dataset is archived', async () => {
const dataUserRegister = dataUseRegisterUploads[0];
const dataUseRegisterPayload = { activeflag: 'archived', rejectionReason: '' };
const user = uploader;

const updateObj = await dataUseRegisterService.buildUpdateObject(dataUserRegister, dataUseRegisterPayload, user);

const expectedResponse = { relatedObjects: [], activeflag: 'archived', rejectionReason: '' };

expect(updateObj).toEqual(expectedResponse);
});

it('Should return a valid update object when a dataset is edited', async () => {
const dataUserRegister = dataUseRegisterUploads[0];
const dataUseRegisterPayload = editedDataUseObject;
const user = uploader;

const updateObj = await dataUseRegisterService.buildUpdateObject(dataUserRegister, dataUseRegisterPayload, user);

const expectedResponse = {
relatedObjects: [],
fundersAndSponsors: ['funder1', 'funder2', 'funder3'],
otherApprovalCommittees: ['other Approval Committees'],
projectTitle: 'This a test data use register with an edited title',
};

expect(updateObj).toEqual(expectedResponse);
});
});
});
65 changes: 41 additions & 24 deletions src/resources/dataUseRegister/dataUseRegister.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Controller from '../base/controller';
import { logger } from '../utilities/logger';
import constants from './../utilities/constants.util';
import { Data } from '../tool/data.model';
import { Course } from '../course/course.model';
import { TeamModel } from '../team/team.model';
import teamController from '../team/team.controller';
import emailGenerator from '../utilities/emailGenerator.util';
Expand All @@ -12,6 +13,7 @@ import { filtersService } from '../filters/dependency';

import { DataUseRegister } from '../dataUseRegister/dataUseRegister.model';
import { isEmpty, isUndefined } from 'lodash';
import { UserModel } from '../user/user.model';

const logCategory = 'dataUseRegister';

Expand Down Expand Up @@ -58,31 +60,44 @@ export default class DataUseRegisterController extends Controller {
}

// Reverse look up
var p = Data.aggregate([{ $match: { $and: [{ relatedObjects: { $elemMatch: { objectId: id } } }] } }]);
p.exec((err, relatedData) => {
if (!isEdit) {
relatedData.forEach(dat => {
dat.relatedObjects.forEach(x => {
if (x.objectId === id && dat.id !== id) {
if (typeof dataUseRegister.relatedObjects === 'undefined') dataUseRegister.relatedObjects = [];
dataUseRegister.relatedObjects.push({
objectId: dat.id,
reason: x.reason,
objectType: dat.type,
user: x.user,
updated: x.updated,
});
}
});
});
}
if (err) return res.json({ success: false, error: err });
let relatedData = await Data.find({
relatedObjects: { $elemMatch: { objectId: req.params.id } },
activeflag: 'active',
});

// Return the dataUseRegister
return res.status(200).json({
success: true,
...dataUseRegister,
let relatedDataFromCourses = await Course.find({
relatedObjects: { $elemMatch: { objectId: req.params.id } },
activeflag: 'active',
});

let relatedDataFromDatauses = await DataUseRegister.find({
relatedObjects: { $elemMatch: { objectId: req.params.id } },
activeflag: 'active',
});

relatedData = [...relatedData, ...relatedDataFromCourses, ...relatedDataFromDatauses];

if (!isEdit) {
relatedData.forEach(dat => {
dat.relatedObjects.forEach(x => {
if (x.objectId === id && dat.id !== id) {
if (typeof dataUseRegister.relatedObjects === 'undefined') dataUseRegister.relatedObjects = [];
dataUseRegister.relatedObjects.push({
objectId: dat.id,
reason: x.reason,
objectType: dat.type,
user: x.user,
updated: x.updated,
});
}
});
});
}

// Return the dataUseRegister
return res.status(200).json({
success: true,
...dataUseRegister,
});
} catch (err) {
// Return error response if something goes wrong
Expand Down Expand Up @@ -348,7 +363,9 @@ export default class DataUseRegisterController extends Controller {

async createNotifications(type, context, dataUseRegister, publisher) {
const { rejectionReason } = context;
const { id, projectTitle, user: uploader } = dataUseRegister;
const { id, projectTitle, user: uploaderID } = dataUseRegister;

const uploader = await UserModel.findOne({ _id: uploaderID });

switch (type) {
case constants.dataUseRegisterNotifications.DATAUSEAPPROVED: {
Expand Down
Loading

0 comments on commit 004244c

Please sign in to comment.