From 56692f7e09ee2aea7b3a93bd84112524be443124 Mon Sep 17 00:00:00 2001 From: martintrajanovski Date: Tue, 3 Dec 2024 08:47:34 +0100 Subject: [PATCH] feat: migrate dataset principalInvestigator to principalInvestigators --- ...105905-multiple-principal-investigators.js | 31 +++++++++ src/datasets/datasets.controller.ts | 65 +++++++++++++++---- src/datasets/datasets.service.spec.ts | 12 ++-- src/datasets/dto/update-dataset.dto.ts | 7 +- src/datasets/schemas/dataset.schema.ts | 4 +- 5 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 migrations/20241202105905-multiple-principal-investigators.js diff --git a/migrations/20241202105905-multiple-principal-investigators.js b/migrations/20241202105905-multiple-principal-investigators.js new file mode 100644 index 000000000..4ae53f70c --- /dev/null +++ b/migrations/20241202105905-multiple-principal-investigators.js @@ -0,0 +1,31 @@ +module.exports = { + async up(db, client) { + await db.collection("Dataset").updateMany({}, [ + { + $set: { + principalInvestigators: ["$principalInvestigator"], + }, + }, + ]); + + await db + .collection("Dataset") + .updateMany({}, { $unset: { principalInvestigator: "" } }); + }, + + async down(db, client) { + await db.collection("Dataset").updateMany({}, [ + { + $set: { + principalInvestigator: { + $arrayElemAt: ["$principalInvestigators", 0], + }, + }, + }, + ]); + + await db + .collection("Dataset") + .updateMany({}, { $unset: { principalInvestigators: "" } }); + }, +}; diff --git a/src/datasets/datasets.controller.ts b/src/datasets/datasets.controller.ts index 32eb8d34b..085661ad2 100644 --- a/src/datasets/datasets.controller.ts +++ b/src/datasets/datasets.controller.ts @@ -418,10 +418,28 @@ export class DatasetsController { whereFilter.instrumentIds = whereFilter.instrumentId; delete whereFilter.instrumentId; } + if ("investigator" in whereFilter) { + if (typeof whereFilter.investigator === "string") { + whereFilter.principalInvestigators = { + $in: [whereFilter.investigator], + }; + } else { + whereFilter.principalInvestigators = whereFilter.investigator; + } + + delete whereFilter.investigator; + } if ("principalInvestigator" in whereFilter) { - whereFilter.investigator = whereFilter.principalInvestigator; + if (typeof whereFilter.investigator === "string") { + whereFilter.principalInvestigators = { + $in: [whereFilter.principalInvestigator], + }; + } else { + whereFilter.principalInvestigators = whereFilter.principalInvestigator; + } delete whereFilter.principalInvestigator; } + return whereFilter; } convertObsoleteToCurrentSchema( @@ -457,11 +475,18 @@ export class DatasetsController { (inputObsoleteDataset as CreateRawDatasetObsoleteDto).instrumentId, ]; } + if ("principalInvestigator" in inputObsoleteDataset) { + propertiesModifier.principalInvestigators = [ + (inputObsoleteDataset as CreateRawDatasetObsoleteDto) + .principalInvestigator, + ]; + } } else { if ("investigator" in inputObsoleteDataset) { - propertiesModifier.principalInvestigator = ( - inputObsoleteDataset as CreateDerivedDatasetObsoleteDto - ).investigator; + propertiesModifier.principalInvestigators = [ + (inputObsoleteDataset as CreateDerivedDatasetObsoleteDto) + .investigator, + ]; } } @@ -503,18 +528,34 @@ export class DatasetsController { ): OutputDatasetObsoleteDto { const propertiesModifier: Record = {}; if (inputDataset) { - if ("proposalIds" in inputDataset) { - propertiesModifier.proposalId = inputDataset.proposalIds![0]; + if ("proposalIds" in inputDataset && inputDataset.proposalIds?.length) { + propertiesModifier.proposalId = inputDataset.proposalIds[0]; + } + if ("sampleIds" in inputDataset && inputDataset.sampleIds?.length) { + propertiesModifier.sampleId = inputDataset.sampleIds[0]; } - if ("sampleIds" in inputDataset) { - propertiesModifier.sampleId = inputDataset.sampleIds![0]; + if ( + "instrumentIds" in inputDataset && + inputDataset.instrumentIds?.length + ) { + propertiesModifier.instrumentId = inputDataset.instrumentIds[0]; } - if ("instrumentIds" in inputDataset) { - propertiesModifier.instrumentId = inputDataset.instrumentIds![0]; + + if ( + "principalInvestigators" in inputDataset && + inputDataset.principalInvestigators?.length + ) { + propertiesModifier.principalInvestigator = + inputDataset.principalInvestigators[0]; } + if (inputDataset.type == "derived") { - if ("investigator" in inputDataset) { - propertiesModifier.investigator = inputDataset.principalInvestigator; + if ( + "investigator" in inputDataset && + inputDataset.principalInvestigators?.length + ) { + propertiesModifier.investigator = + inputDataset.principalInvestigators[0]; } } } diff --git a/src/datasets/datasets.service.spec.ts b/src/datasets/datasets.service.spec.ts index 9cea8700e..cd14bbd4a 100644 --- a/src/datasets/datasets.service.spec.ts +++ b/src/datasets/datasets.service.spec.ts @@ -19,7 +19,7 @@ const mockDataset: DatasetClass = { pid: "testPid", owner: "testOwner", ownerEmail: "testOwner@email.com", - instrumentId: "testInstrumentId", + instrumentIds: ["testInstrumentId"], orcidOfOwner: "https://0000.0000.0000.0001", contactEmail: "testContact@email.com", sourceFolder: "/nfs/groups/beamlines/test/123456", @@ -59,24 +59,20 @@ const mockDataset: DatasetClass = { createdAt: new Date("2021-11-11T12:29:02.083Z"), updatedAt: new Date("2021-11-11T12:29:02.083Z"), techniques: [], - principalInvestigator: "testInvestigator", + principalInvestigators: ["testInvestigator"], endTime: new Date("2021-12-11T12:29:02.083Z"), creationLocation: "test", dataFormat: "Test Format", scientificMetadata: {}, - proposalId: "ABCDEF", - sampleId: "testSampleId", - attachments: [], + proposalIds: ["ABCDEF"], + sampleIds: ["testSampleId"], accessGroups: [], createdBy: "test user", - datablocks: [], - origdatablocks: [], ownerGroup: "test", relationships: [], sharedWith: [], updatedBy: "test", instrumentGroup: "test", - investigator: "test", inputDatasets: [], usedSoftware: [], jobParameters: {}, diff --git a/src/datasets/dto/update-dataset.dto.ts b/src/datasets/dto/update-dataset.dto.ts index 99c6c3eaf..b2453f56d 100644 --- a/src/datasets/dto/update-dataset.dto.ts +++ b/src/datasets/dto/update-dataset.dto.ts @@ -291,13 +291,14 @@ export class UpdateDatasetDto extends OwnableDto { readonly dataQualityMetrics?: number; @ApiProperty({ - type: String, - required: true, + type: [String], + required: false, description: "First name and last name of principal investigator(s). If multiple PIs are present, use a semicolon separated list. This field is required if the dataset is a Raw dataset.", }) + @IsOptional() @IsString() - readonly principalInvestigator: string; + readonly principalInvestigators?: string[]; @ApiProperty({ type: Date, diff --git a/src/datasets/schemas/dataset.schema.ts b/src/datasets/schemas/dataset.schema.ts index b7e9ca8ef..0dc048ceb 100644 --- a/src/datasets/schemas/dataset.schema.ts +++ b/src/datasets/schemas/dataset.schema.ts @@ -362,8 +362,8 @@ export class DatasetClass extends OwnableClass { description: "First name and last name of principal investigator(s). If multiple PIs are present, use a semicolon separated list. This field is required if the dataset is a Raw dataset.", }) - @Prop({ type: String, required: false }) - principalInvestigator?: string; + @Prop({ type: [String], required: false }) + principalInvestigators?: string[]; @ApiProperty({ type: Date,