Skip to content

Commit

Permalink
Merge pull request #1536 from SciCatProject/migrate-principal-investi…
Browse files Browse the repository at this point in the history
…gator

feat: migrate dataset principalInvestigator to principalInvestigators
  • Loading branch information
martin-trajanovski authored Dec 11, 2024
2 parents a16e8e4 + fabe5aa commit 4730537
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 33 deletions.
31 changes: 31 additions & 0 deletions migrations/20241202105905-multiple-principal-investigators.js
Original file line number Diff line number Diff line change
@@ -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: "" } });
},
};
65 changes: 53 additions & 12 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,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(
Expand Down Expand Up @@ -463,15 +481,22 @@ export class DatasetsController {
(inputObsoleteDataset as CreateRawDatasetObsoleteDto).instrumentId,
];
}
if ("principalInvestigator" in inputObsoleteDataset) {
propertiesModifier.principalInvestigators = [
(inputObsoleteDataset as CreateRawDatasetObsoleteDto)
.principalInvestigator,
];
}
} else if (
inputObsoleteDataset instanceof CreateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof UpdateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof PartialUpdateDerivedDatasetObsoleteDto
) {
if ("investigator" in inputObsoleteDataset) {
propertiesModifier.principalInvestigator = (
inputObsoleteDataset as CreateDerivedDatasetObsoleteDto
).investigator;
propertiesModifier.principalInvestigators = [
(inputObsoleteDataset as CreateDerivedDatasetObsoleteDto)
.investigator,
];
}
}

Expand Down Expand Up @@ -516,18 +541,34 @@ export class DatasetsController {
): OutputDatasetObsoleteDto {
const propertiesModifier: Record<string, unknown> = {};
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];
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/datasets/datasets.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mockDataset: DatasetClass = {
pid: "testPid",
owner: "testOwner",
ownerEmail: "[email protected]",
instrumentId: "testInstrumentId",
instrumentIds: ["testInstrumentId"],
orcidOfOwner: "https://0000.0000.0000.0001",
contactEmail: "[email protected]",
sourceFolder: "/nfs/groups/beamlines/test/123456",
Expand Down Expand Up @@ -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: {},
Expand Down
10 changes: 6 additions & 4 deletions src/datasets/dto/update-dataset.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,14 @@ export class UpdateDatasetDto extends OwnableDto {

@ApiProperty({
type: String,
required: true,
required: false,
isArray: true,
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.",
"First and last name of principal investigator(s). Multiple PIs can be provided as separate strings in the array. This field is required if the dataset is a Raw dataset.",
})
@IsString()
readonly principalInvestigator: string;
@IsOptional()
@IsString({ each: true })
readonly principalInvestigators?: string[];

@ApiProperty({
type: Date,
Expand Down
7 changes: 4 additions & 3 deletions src/datasets/schemas/dataset.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,12 @@ export class DatasetClass extends OwnableClass {
@ApiProperty({
type: String,
required: false,
isArray: true,
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.",
"First and last name of principal investigator(s). Multiple PIs can be provided as separate strings in the array. 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,
Expand Down
2 changes: 1 addition & 1 deletion test/DatasetCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("2400: CustomDataset: Custom Type Datasets", () => {
before(() => {
db.collection("Dataset").deleteMany({});
});
beforeEach(async() => {
beforeEach(async () => {
accessTokenAdminIngestor = await utils.getToken(appUrl, {
username: "adminIngestor",
password: TestData.Accounts["adminIngestor"]["password"],
Expand Down
10 changes: 5 additions & 5 deletions test/TestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ const TestData = {
},

CustomDatasetCorrectMin: {
principalInvestigator: faker.internet.email(),
principalInvestigators: [faker.internet.email()],
owner: faker.internet.username(),
contactEmail: faker.internet.email(),
sourceFolder: faker.system.directoryPath(),
Expand All @@ -492,7 +492,7 @@ const TestData = {
},

CustomDatasetCorrect: {
principalInvestigator: "[email protected]",
principalInvestigators: ["[email protected]"],
inputDatasets: ["/data/input/file1.dat"],
usedSoftware: [
"https://gitlab.psi.ch/ANALYSIS/csaxs/commit/7d5888bfffc440bb613bc7fa50adc0097853446c",
Expand Down Expand Up @@ -522,7 +522,7 @@ const TestData = {
},

CustomDatasetWrongType: {
principalInvestigator: "[email protected]",
principalInvestigators: ["[email protected]"],
jobParameters: {
nscans: 10,
},
Expand All @@ -540,7 +540,7 @@ const TestData = {
},

CustomDatasetWrongData: {
principalInvestigator: "[email protected]",
principalInvestigators: ["[email protected]"],
jobParameters: {
nscans: 10,
},
Expand All @@ -559,7 +559,7 @@ const TestData = {
},

CustomDatasetIncompleteData: {
principalInvestigator: "[email protected]",
principalInvestigators: ["[email protected]"],
jobParameters: {
nscans: 10,
},
Expand Down

0 comments on commit 4730537

Please sign in to comment.