Skip to content

Commit

Permalink
Merge pull request #1461 from SciCatProject/fix_patch_dataset
Browse files Browse the repository at this point in the history
fix: patch dataset
  • Loading branch information
nitrosx authored Oct 28, 2024
2 parents 95f06d2 + 8bf7c94 commit 63fdbf5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ export class DatasetsController {
@Req() request: Request,
@Param("pid") pid: string,
@Body()
updateDatasetDto:
updateDatasetObsoleteDto:
| PartialUpdateRawDatasetObsoleteDto
| PartialUpdateDerivedDatasetObsoleteDto,
): Promise<OutputDatasetObsoleteDto | null> {
Expand All @@ -1233,12 +1233,15 @@ export class DatasetsController {
}

// NOTE: Default validation pipe does not validate union types. So we need custom validation.
await this.validateDatasetObsolete(
updateDatasetDto,
foundDataset.type === "raw"
? PartialUpdateRawDatasetObsoleteDto
: PartialUpdateDerivedDatasetObsoleteDto,
);
const validatedUpdateDatasetObsoleteDto =
(await this.validateDatasetObsolete(
updateDatasetObsoleteDto,
foundDataset.type === "raw"
? PartialUpdateRawDatasetObsoleteDto
: PartialUpdateDerivedDatasetObsoleteDto,
)) as
| PartialUpdateRawDatasetObsoleteDto
| PartialUpdateDerivedDatasetObsoleteDto;

// NOTE: We need DatasetClass instance because casl module can not recognize the type from dataset mongo database model. If other fields are needed can be added later.
const datasetInstance =
Expand All @@ -1256,9 +1259,14 @@ export class DatasetsController {
throw new ForbiddenException("Unauthorized to update this dataset");
}

return this.convertCurrentToObsoleteSchema(
const updateDatasetDto = this.convertObsoleteToCurrentSchema(
validatedUpdateDatasetObsoleteDto,
) as UpdateDatasetDto;

const res = this.convertCurrentToObsoleteSchema(
await this.datasetsService.findByIdAndUpdate(pid, updateDatasetDto),
);
return res;
}

// PUT /datasets/:id
Expand Down
6 changes: 5 additions & 1 deletion test/DatasetAuthorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ describe("0300: DatasetAuthorization: Test access to dataset", () => {
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
.expect(TestData.SuccessfulPatchStatusCode)
.expect("Content-Type", /json/);
.expect("Content-Type", /json/)
.then((res) => {
res.body["pid"].should.be.equal(datasetPid2);
res.body["isPublished"].should.be.equal(true);
});
});

it("0350: full query for datasets for User 2", async () => {
Expand Down
48 changes: 48 additions & 0 deletions test/RawDataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,54 @@ describe("1900: RawDataset: Raw Datasets", () => {
);
});

it("0125: should update proposal of the dataset", async () => {
return request(appUrl)
.patch("/api/v3/datasets/" + pid)
.send(TestData.PatchProposal1)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
.expect(TestData.SuccessfulPatchStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("proposalId").and.be.string;
res.body.should.have
.property("proposalId")
.and.be.equal(TestData.PatchProposal1["proposalId"]);
});
});

it("0126: should update instrument of the dataset", async () => {
return request(appUrl)
.patch("/api/v3/datasets/" + pid)
.send(TestData.PatchInstrument1)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
.expect(TestData.SuccessfulPatchStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("instrumentId").and.be.string;
res.body.should.have
.property("instrumentId")
.and.be.equal(TestData.PatchInstrument1["instrumentId"]);
});
});

it("0127: should update sample of the dataset", async () => {
return request(appUrl)
.patch("/api/v3/datasets/" + pid)
.send(TestData.PatchSample1)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
.expect(TestData.SuccessfulPatchStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("sampleId").and.be.string;
res.body.should.have
.property("sampleId")
.and.be.equal(TestData.PatchSample1["sampleId"]);
});
});

it("0130: should update comment of the dataset", async () => {
return request(appUrl)
.patch("/api/v3/datasets/" + pid)
Expand Down
12 changes: 12 additions & 0 deletions test/TestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,18 @@ const TestData = {
name: "ESS-wrong",
},

PatchProposal1: {
proposalId: "10.540.16635/20240124",
},

PatchInstrument1: {
instrumentId: "fb0f3b58-92c9-11ef-9aeb-632c6e2960a1",
},

PatchSample1: {
sampleId: "f3cfc114-92c9-11ef-8ed4-f3b97158e36b",
},

PatchComment: {
comment: "test",
},
Expand Down

0 comments on commit 63fdbf5

Please sign in to comment.