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
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