Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clinical id service integration #1176

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 221 additions & 47 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@types/express-serve-static-core": "4.16.7",
"@types/jsonwebtoken": "^8.3.2",
"@types/lodash": "^4.14.134",
"@types/memoizee": "^0.4.11",
"@types/migrate-mongo": "^7.0.0",
"@types/mocha": "^9.1.1",
"@types/mongodb": "^3.1.28",
Expand All @@ -66,6 +67,7 @@
"@types/morgan": "^1.7.35",
"@types/multer": "^1.3.7",
"@types/mysql": "^2.15.9",
"@types/ms": "^0.7.34",
"@types/node": "^12.0.10",
"@types/node-fetch": "^2.5.0",
"@types/node-vault": "^0.9.0",
Expand Down Expand Up @@ -105,6 +107,7 @@
"adm-zip": "^0.4.16",
"apollo-server-core": "^3.12.0",
"async": "^3.0.1",
"axios": "^1.6.7",
"bcrypt-nodejs": "^0.0.3",
"bluebird": "^3.5.5",
"body-parser": "^1.19.0",
Expand All @@ -124,6 +127,7 @@
"kafkajs": "^1.12.0",
"kind-of": "^6.0.3",
"lodash": "^4.17.21",
"memoizee": "^0.4.17",
"migrate-mongo": "^8.2.3",
"mock-http-server": "^1.4.1",
"mongoose": "^5.13.14",
Expand All @@ -132,6 +136,7 @@
"morgan": "^1.10.0",
"mquery": ">=3.2.3",
"multer": "^1.4.2",
"ms": "^2.1.3",
"mysql": "^2.18.1",
"node-fetch": "^2.6.7",
"node-vault": "^0.9.11",
Expand Down
94 changes: 31 additions & 63 deletions src/clinical/donor-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import mongoose, { PaginateModel } from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';
import { DeepReadonly } from 'deep-freeze';
import { F, MongooseUtils, notEmpty } from '../utils';
import { setEntityIds, setEntityIdsForDonors } from './id-generator';

export const SUBMITTER_ID = 'submitterId';
export const SPECIMEN_SUBMITTER_ID = 'specimen.submitterId';
Expand Down Expand Up @@ -109,20 +110,22 @@ export interface DonorRepository {
): Promise<DeepReadonly<Donor> | undefined>;
iterateAllByProgramId(programId: string): AsyncIterable<DeepReadonly<Donor>>;
create(donor: DeepReadonly<Partial<Donor>>): Promise<DeepReadonly<Donor>>;
update(donor: DeepReadonly<Donor>): Promise<DeepReadonly<Donor>>;
updateAll(donors: DeepReadonly<Donor>[]): Promise<DeepReadonly<Donor>[]>;
update(donor: Donor): Promise<DeepReadonly<Donor>>;
updateAll(donors: Donor[]): Promise<DeepReadonly<Donor>[]>;
countBy(filter: any): Promise<number>;
}

// Mongoose implementation of the DonorRepository
export const donorDao: DonorRepository = {
async insertDonors(donors: Donor[]) {
await mongoose.connection.db.collection('donors').insertMany(donors);
const donorsWithIds = await setEntityIdsForDonors(donors);
await mongoose.connection.db.collection('donors').insertMany(donorsWithIds);
},
async updateDonor(donor: Donor) {
const donorsWithIds = await setEntityIds(donor);
Comment on lines 119 to +125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both setEntityIds functions throw errors when network errors occur, should there be some sort of error handling here to handle failures or is this now expected to be handled by the caller of updateDonor and insertDonors?

await mongoose.connection.db
.collection('donors')
.findOneAndUpdate({ donorId: donor.donorId }, { $set: donor });
.findOneAndUpdate({ donorId: donor.donorId }, { $set: donorsWithIds });
},
async countBy(filter: any) {
return await DonorModel.count(filter).exec();
Expand Down Expand Up @@ -413,27 +416,41 @@ export const donorDao: DonorRepository = {
return iterateAllByProgramId(programId);
},

async update(donor: DeepReadonly<Donor>) {
const newDonor = new DonorModel(donor);
async update(donor: Donor) {
const dnr = await setEntityIds(donor);
const newDonor = new DonorModel(await setEntityIds(donor));
unsetIsNewFlagForUpdate(newDonor);

await newDonor.save();
return F(MongooseUtils.toPojo(newDonor) as Donor);
},

async updateAll(donors: DeepReadonly<Donor>[]) {
const newDonors = donors.map((donor) => {
const newDonor = new DonorModel(donor);
async updateAll(donors: Donor[]) {
const newDonors = donors.map(async (donor) => {
const newDonor = new DonorModel(await setEntityIds(donor));
unsetIsNewFlagForUpdate(newDonor);
return newDonor;
});

const results = await Promise.all(newDonors.map((donor) => donor.save()));
return newDonors.map((donor) => F(MongooseUtils.toPojo(donor) as Donor));
await Promise.all(
newDonors.map((donor) => {
donor.then(async (d) => {
await d.save();
});
}),
);
const result = await Promise.all(
newDonors.map((donor) =>
donor.then((d) => {
return F(MongooseUtils.toPojo(d) as Donor);
}),
),
);

return result;
},

async create(donor: DeepReadonly<Donor>) {
const newDonor = new DonorModel(donor);
async create(donor: Partial<Donor>) {
const newDonor = new DonorModel(await setEntityIds(donor));
const doc = await newDonor.save();
return F(MongooseUtils.toPojo(newDonor) as Donor);
},
Expand Down Expand Up @@ -656,58 +673,9 @@ DonorSchema.index({ 'specimens.samples.submitterId': 1, programId: 1 }, { unique
* multiple times, and that makes them hard to test because tests depend
* on resetting the config and bootstraping but global variables keep their state.
*/
DonorSchema.plugin(AutoIncrement, {
inc_field: 'donorId',
start_seq: process.env.DONOR_ID_SEED || 250000,
});

DonorSchema.plugin(mongoosePaginate);

SpecimenSchema.plugin(AutoIncrement, {
inc_field: 'specimenId',
start_seq: process.env.SPECIMEN_ID_SEED || 210000,
});

SampleSchema.plugin(AutoIncrement, {
inc_field: 'sampleId',
start_seq: process.env.SAMPLE_ID_SEED || 610000,
});

FollowUpSchema.plugin(AutoIncrement, {
inc_field: 'followUpId',
start_seq: 1,
});

PrimaryDiagnosisSchema.plugin(AutoIncrement, {
inc_field: 'primaryDiagnosisId',
start_seq: 1,
});

FamilyHistorySchema.plugin(AutoIncrement, {
inc_field: 'familyHistoryId',
start_seq: 1,
});

ExposureSchema.plugin(AutoIncrement, {
inc_field: 'exposureId',
start_seq: 1,
});

BiomarkerSchema.plugin(AutoIncrement, {
inc_field: 'biomarkerId',
start_seq: 1,
});

ComorbiditySchema.plugin(AutoIncrement, {
inc_field: 'comorbidityId',
start_seq: 1,
});

TreatmentSchema.plugin(AutoIncrement, {
inc_field: 'treatmentId',
start_seq: 1,
});

export let DonorModel = mongoose.model<DonorDocument>('Donor', DonorSchema) as PaginateModel<
DonorDocument
>;
Loading