Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 23 additions & 12 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,9 @@ export class DatasetsController {
}
}

const excludeHistory =
Array.isArray(fields) && !fields.includes("history");

if (!excludeHistory)
propertiesModifier.history = convertGenericHistoriesToObsoleteHistories(
await this.historyService.find({
documentId: inputDataset._id,
subsystem: "Dataset",
}),
inputDataset,
if (Array.isArray(fields) && fields.includes("history"))
propertiesModifier.history = await this.convertToObsoleteHistory(
inputDataset._id,
);
}

Expand All @@ -615,6 +608,19 @@ export class DatasetsController {
return plainToInstance(OutputDatasetObsoleteDto, outputDataset);
}

private async convertToObsoleteHistory(datasetId: string) {
const currentDataset = (await this.datasetsService.findOne({
where: { pid: datasetId },
})) as DatasetDocument;
return convertGenericHistoriesToObsoleteHistories(
await this.historyService.find({
documentId: datasetId,
subsystem: "Dataset",
}),
currentDataset,
);
}

// POST https://scicat.ess.eu/api/v3/datasets
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
Expand Down Expand Up @@ -991,7 +997,9 @@ export class DatasetsController {

if (datasets && datasets.length > 0) {
outputDatasets = await Promise.all(
datasets.map((dataset) => this.convertCurrentToObsoleteSchema(dataset)),
datasets.map((dataset) =>
this.convertCurrentToObsoleteSchema(dataset, parsedFilters.fields),
),
);
}

Expand Down Expand Up @@ -1281,7 +1289,10 @@ export class DatasetsController {
});
if (dataset.length == 0)
throw new ForbiddenException("Unauthorized access");
return dataset[0] as OutputDatasetObsoleteDto;
return {
...dataset[0],
history: await this.convertToObsoleteHistory(dataset[0].pid),
};
}

// PATCH /datasets/:id
Expand Down
8 changes: 2 additions & 6 deletions src/datasets/utils/history.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
DatasetDocument,
} from "src/datasets/schemas/dataset.schema";
import { computeDeltaWithOriginals } from "src/common/utils/delta.util";
import { cloneDeep } from "lodash";

const IGNORE_FIELDS = ["updatedAt", "updatedBy", "_id"];

Expand Down Expand Up @@ -93,17 +92,14 @@ export function convertGenericHistoriesToObsoleteHistories(
histories: GenericHistory[],
currentDataset: DatasetDocument | DatasetClass,
): HistoryClass[] {
let currentDatasetCopy;
if ("$clone" in currentDataset) currentDatasetCopy = currentDataset.$clone();
else currentDatasetCopy = cloneDeep(currentDataset);
const result: HistoryClass[] = [];
for (const history of histories) {
const obsoleteHistory = convertGenericHistoryToObsoleteHistory(
history,
currentDatasetCopy,
currentDataset,
);
for (const key of Object.keys(history.before || {})) {
(currentDatasetCopy as unknown as Record<string, unknown>)[key] =
(currentDataset as unknown as Record<string, unknown>)[key] =
history.before?.[key];
}
result.push(obsoleteHistory);
Expand Down
1 change: 0 additions & 1 deletion test/TestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ const TestData = {
}
},
sharedWith: [],
history: "JEST_ANY",
size: 0,
sourceFolder: "/iramjet/tif",
sourceFolderHost: "nfs://file.your.site",
Expand Down
53 changes: 42 additions & 11 deletions test/jest-e2e-tests/datasetHistory.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ describe("Test v3 history in datasetLifecycle", () => {
await app.close();
});

it("Should check v3 built history", async () => {
it("Should check v3 built history with field including history", async () => {
const filter = { fields: ["history"] };
await request(app.getHttpServer())
.get(`/api/v3/datasets/${encodeURIComponent(dsId)}`)
.get("/api/v3/datasets")
.query({ filter: JSON.stringify(filter) })
.auth(token, { type: "bearer" })
.expect(TestData.SuccessfulGetStatusCode)
.then((res) => {
expect(res.body.history.length).toBe(1);
const lifecycle = res.body.history[0].datasetlifecycle;
expect(res.body[0].history.length).toBe(1);
const lifecycle = res.body[0].history[0].datasetlifecycle;
expect(lifecycle.previousValue.archivable).toBe(true);
expect(lifecycle.currentValue.archivable).toBe(false);
expect(lifecycle.previousValue.retrievable).toBeDefined();
Expand All @@ -74,26 +76,55 @@ describe("Test v3 history in datasetLifecycle", () => {
it("Should check v3 built history with field not including history", async () => {
const filter = { fields: ["datasetName"] };
await request(app.getHttpServer())
.get(`/api/v3/datasets/${encodeURIComponent(dsId)}`)
.get("/api/v3/datasets")
.query({ filter: JSON.stringify(filter) })
.auth(token, { type: "bearer" })
.expect(TestData.SuccessfulGetStatusCode)
.then((res) => {
expect(res.body.datasetName).toBeDefined();
expect(res.body.history).toBeUndefined();
expect(res.body[0].datasetName).toBeDefined();
expect(res.body[0].history).toBeUndefined();
});
});

it("Should check v3 built history with field including history", async () => {
["", "/fullquery"].forEach((t) => {
it(`Should check v3 built history without fields ${t}`, async () => {
await request(app.getHttpServer())
.get(`/api/v3/datasets${t}`)
.auth(token, { type: "bearer" })
.expect(TestData.SuccessfulGetStatusCode)
.then((res) => {
expect(res.body[0].datasetName).toBeDefined();
expect(res.body[0].history).toBeUndefined();
});
});
});

it("Should check v3 built history with field including history and datasetName", async () => {
const filter = { fields: ["datasetName", "history"] };
await request(app.getHttpServer())
.get(`/api/v3/datasets/${encodeURIComponent(dsId)}`)
.get("/api/v3/datasets")
.query({ filter: JSON.stringify(filter) })
.auth(token, { type: "bearer" })
.expect(TestData.SuccessfulGetStatusCode)
.then((res) => {
expect(res.body.datasetName).toBeDefined();
expect(res.body.history).toBeDefined();
expect(res.body[0].datasetName).toBeDefined();
expect(res.body[0].history).toBeDefined();
});
});

it("Should check v3 built history by ID", async () => {
await request(app.getHttpServer())
.get(`/api/v3/datasets/${encodeURIComponent(dsId)}`)
.auth(token, { type: "bearer" })
.expect(TestData.SuccessfulGetStatusCode)
.then((res) => {
expect(res.body.history.length).toBe(1);
const lifecycle = res.body.history[0].datasetlifecycle;
expect(lifecycle.previousValue.archivable).toBe(true);
expect(lifecycle.currentValue.archivable).toBe(false);
expect(lifecycle.previousValue.retrievable).toBeDefined();
expect(lifecycle.currentValue.retrievable).toBeUndefined();
expect(typeof lifecycle.previousValue._id).toBe("string");
});
});
});