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

Integrates the new PUT metadata endpoint #973

Merged
merged 12 commits into from
Apr 4, 2023
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
4 changes: 4 additions & 0 deletions src/app/utilities/api-clients/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export default class datasets {
});
}

static putMetadata(datasetID, editionID, versionID, body) {
return http.put(`${publishingDatasetControllerURL}/datasets/${datasetID}/editions/${editionID}/versions/${versionID}/metadata`, body, true);
}

static getVersionsList(datasetID, editionID) {
return http.get(`${publishingDatasetControllerURL}/datasets/${datasetID}/editions/${editionID}/versions`).then(response => {
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class CantabularMetadataController extends Component {
const editionID = this.props.params.editionID;
const versionID = this.props.params.versionID;
await this.getTopics();
this.getMetadata(datasetID, editionID, versionID);
await this.getMetadata(datasetID, editionID, versionID);
}

getTopics = async () => {
Expand Down Expand Up @@ -465,6 +465,8 @@ export class CantabularMetadataController extends Component {
lastEditedBy: nonCantDatasetMetadata.collection_last_edited_by || null,
versionIsPublished: version.state === "published",
state: dataset.state,
collectionState: collectionState,
versionEtag: nonCantDatasetMetadata.version_etag || "",
};
} catch (error) {
log.event("error mapping metadata to to state", log.data({ datasetID: dataset.id, versionID: version.id }), log.error(error));
Expand Down Expand Up @@ -570,6 +572,8 @@ export class CantabularMetadataController extends Component {
datasetState: mapped.state,
lastEditedBy: mapped.lastEditedBy,
versionIsPublished: mapped.versionIsPublished,
collectionState: mapped.collectionState,
versionEtag: mapped.versionEtag,
});

this.checksMandatoryFieldsReturnedByMetadataExtractor(this.state.fieldsReturned.title, this.state.fieldsReturned.dimensions);
Expand Down Expand Up @@ -903,6 +907,7 @@ export class CantabularMetadataController extends Component {
dimensions: [...this.state.metadata.dimensions],
collection_id: this.props.params.collectionID,
collection_state: this.mapCollectionState(isSubmittingForReview, isMarkingAsReviewed),
version_etag: this.state.versionEtag,
};
};

Expand Down Expand Up @@ -931,35 +936,72 @@ export class CantabularMetadataController extends Component {
);
};

saveMetadata = (datasetID, editionID, versionID, body, isSubmittingForReview, isMarkingAsReviewed) => {
saveMetadata = async (datasetID, editionID, versionID, body, isSubmittingForReview, isMarkingAsReviewed) => {
this.setState({ isSaving: true });
return datasets
.putEditMetadata(datasetID, editionID, versionID, body)
.then(() => {
this.setState(() => {
return { isSaving: false, allowPreview: true, disableCancel: true };
});
if (this.state.collectionState === "") {
return datasets
.putEditMetadata(datasetID, editionID, versionID, body)
.then(() => {
this.setState(() => {
return { isSaving: false, allowPreview: true, disableCancel: true };
});

notifications.add({
type: "positive",
message: `${this.state.metadata.title} saved!`,
isDismissable: true,
notifications.add({
type: "positive",
message: `${this.state.metadata.title} saved!`,
isDismissable: true,
});

if (isMarkingAsReviewed || isSubmittingForReview) {
this.props.dispatch(push("/florence/collections/" + this.props.params.collectionID));
}
})
.catch(error => {
this.setState({ isSaving: false, allowPreview: false, disableCancel: false });
log.event("save metadata: error PUTting metadata to controller", log.data({ datasetID, editionID, versionID }), log.error());
notifications.add({
type: "warning",
message: `An error occurred when saving this dataset version metadata. Please try again`,
isDismissable: true,
});
console.error("save metadata: error PUTting metadata to controller", error);
});
} else {
// always retrieve the metadata before the putMetadata endpoint is called to make sure that the latest versionEtag is passed down in the payload
const datasetMetadata = await datasets.getEditMetadata(
this.props.params.datasetID,
this.props.params.editionID,
this.props.params.versionID
);
body.version_etag = datasetMetadata.version_etag;
return datasets
.putMetadata(datasetID, editionID, versionID, body)
.then(() => {
this.setState(() => {
return { isSaving: false, allowPreview: true, disableCancel: true };
});

if (isMarkingAsReviewed || isSubmittingForReview) {
this.props.dispatch(push("/florence/collections/" + this.props.params.collectionID));
}
})
.catch(error => {
this.setState({ isSaving: false, allowPreview: false, disableCancel: false });
log.event("save metadata: error PUTting metadata to controller", log.data({ datasetID, editionID, versionID }), log.error());
notifications.add({
type: "warning",
message: `An error occurred when saving this dataset version metadata. Please try again`,
isDismissable: true,
notifications.add({
type: "positive",
message: `${this.state.metadata.title} saved!`,
isDismissable: true,
});

if (isMarkingAsReviewed || isSubmittingForReview) {
this.props.dispatch(push("/florence/collections/" + this.props.params.collectionID));
}
})
.catch(error => {
this.setState({ isSaving: false, allowPreview: false, disableCancel: false });
log.event("save metadata: error PUTting metadata to controller", log.data({ datasetID, editionID, versionID }), log.error());
notifications.add({
type: "warning",
message: `An error occurred when saving this dataset version metadata. Please try again`,
isDismissable: true,
});
console.error("save metadata: error PUTting metadata to controller", error);
});
console.error("save metadata: error PUTting metadata to controller", error);
});
}
};

retrieveDatasetMetadata = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jest.mock("../../../utilities/api-clients/datasets", () => {
putEditMetadata: jest.fn(() => {
return Promise.resolve();
}),
putMetadata: jest.fn(),
get: jest.fn(() => {
return Promise.resolve();
}),
Expand Down Expand Up @@ -289,6 +290,7 @@ const mockedSavedNonCantDatasetMetadata = {
collection_id: "123",
collection_state: "inProgress",
collection_last_edited_by: "[email protected]",
version_etag: "test_etag",
};

const mockedNewNonCantDatasetMetadata = {
Expand Down Expand Up @@ -555,13 +557,17 @@ describe("Allowing preview functionality", () => {
expect(component.state("allowPreview")).toBe(false);
});

it("disables preview if saving edits fails", async () => {
datasets.putEditMetadata.mockImplementationOnce(() => Promise.reject({ status: 500 }));
await component.instance().saveMetadata();
it("disables preview if saving edits fails - collection state is 'inProgress' ", async () => {
component.setState({ collectionState: "inProgress", versionEtag: "test_eTag" });
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.reject({ status: 500 }));
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, false);
expect(component.state("allowPreview")).toBe(false);
});

it("enables preview if saving edits successful", async () => {
it("enables preview if saving edits successful - collection state is an empty string", async () => {
component.setState({ collectionState: "" });
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata();
expect(component.state("allowPreview")).toBe(true);
});
Expand Down Expand Up @@ -609,41 +615,86 @@ describe("Calling saveMetadata", () => {
dispatchedActions = [];
});

it("updates isSaving state to show it's fetching data for all datasets", () => {
it("updates isSaving state to show it's fetching data for all datasets - collection state is 'inProgress'", () => {
component.setState({ collectionState: "inProgress", versionEtag: "test_eTag" });
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
expect(component.state("isSaving")).toBe(false);

component.instance().saveMetadata();
component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, false);
expect(component.state("isSaving")).toBe(true);
});

it("updates isSaving state to show it has fetched data for all datasets", async () => {
it("updates isSaving state to show it has fetched data for all datasets - collection state is an empty string", async () => {
component.setState({ collectionState: "" });
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata();
expect(datasets.putEditMetadata).toHaveBeenCalledTimes(1);
expect(datasets.putMetadata).toHaveBeenCalledTimes(0);
expect(component.state("isSaving")).toBe(false);
});

it("updates isSaving state correctly on failure to fetch data for all datasets", async () => {
datasets.putEditMetadata.mockImplementationOnce(() => Promise.reject({ status: 500 }));
await component.instance().saveMetadata();
it("updates isSaving state correctly on failure to fetch data for all datasets - collection state is 'inProgress'", async () => {
component.setState({ collectionState: "inProgress", versionEtag: "test_eTag" });
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, false);
expect(component.state("isSaving")).toBe(false);
expect(datasets.putMetadata).toHaveBeenCalledTimes(1);
expect(datasets.putEditMetadata).toHaveBeenCalledTimes(0);
});

it("on error: creates notification", async () => {
component.setState({ collectionState: "inProgress", versionEtag: "test_eTag" });
expect(mockedNotifications.length).toBe(0);
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.reject({ status: 500 }));
datasets.putEditMetadata.mockImplementationOnce(() => Promise.reject({ status: 500 }));
await component.instance().saveMetadata();
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, false);
expect(mockedNotifications.length).toBe(1);
expect(datasets.putMetadata).toHaveBeenCalledTimes(1);
expect(datasets.putEditMetadata).toHaveBeenCalledTimes(0);
});

it("on success: creates notifcation", async () => {
it("on success: creates notification when the collection state is an empty string", async () => {
component.setState({ collectionState: "" });
expect(mockedNotifications.length).toBe(0);
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata();
expect(mockedNotifications.length).toBe(1);
});

it("on success: redirects if state is 'complete' or 'reviewed'", async () => {
await component.instance().saveMetadata(datasetID, editionID, versionID, {}, true, false);
it("on success: creates notification when the collection state is 'inProgress' ", async () => {
component.setState({ collectionState: "inProgress", versionEtag: "test_eTag" });
expect(mockedNotifications.length).toBe(0);
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, false);
expect(mockedNotifications.length).toBe(1);
});

it("on success: redirects if state is 'complete'", async () => {
component.setState({ collectionState: "complete", versionEtag: "test_eTag" });
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, true, false);
expect(dispatchedActions[0].payload.method).toBe("push");
expect(dispatchedActions[0].payload.args[0]).toBe("/florence/collections/123");
expect(datasets.putMetadata).toHaveBeenCalledTimes(1);
expect(datasets.putEditMetadata).toHaveBeenCalledTimes(0);
});
it("on success: redirects if state is 'reviewed'", async () => {
component.setState({ collectionState: "reviewed", versionEtag: "test_eTag" });
datasets.getEditMetadata.mockImplementationOnce(() => mockedSavedNonCantDatasetMetadata);
datasets.putMetadata.mockImplementationOnce(() => Promise.resolve());
datasets.putEditMetadata.mockImplementationOnce(() => Promise.resolve());
await component.instance().saveMetadata(datasetID, editionID, versionID, { version_etag: "previous_eTag" }, false, true);
expect(dispatchedActions[0].payload.method).toBe("push");
expect(dispatchedActions[0].payload.args[0]).toBe("/florence/collections/123");
expect(datasets.putMetadata).toHaveBeenCalledTimes(1);
expect(datasets.putEditMetadata).toHaveBeenCalledTimes(0);
});
});

Expand Down