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

feat(history): restore version #20102

Merged
merged 9 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NavLink, useParams, type To } from 'react-router-dom';
import { COLLECTION_TYPES } from '../../constants/collections';
import { useHistoryContext } from '../pages/History';
import { useRestoreVersionMutation } from '../services/historyVersion';

interface VersionHeaderProps {
headerId: string;
}
Expand Down Expand Up @@ -45,7 +46,10 @@ export const VersionHeader = ({ headerId }: VersionHeaderProps) => {
};

const handleRestore = async () => {
await restoreVersion({ params: { versionId: version.id } });
await restoreVersion({
params: { versionId: version.id },
body: { contentType: version.contentType },
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ const historyVersionsApi = contentManagerApi.injectEndpoints({
}),
restoreVersion: builder.mutation<RestoreHistoryVersion.Response, RestoreHistoryVersion.Request>(
{
query({ params }) {
query({ params, body }) {
return {
url: `/content-manager/history-versions/${params.versionId}/restore`,
method: 'PUT',
data: body,
};
},
invalidatesTags: ['HistoryVersion'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,22 @@ const createHistoryVersionController = ({ strapi }: { strapi: Core.Strapi }) =>
},

async restoreVersion(ctx) {
markkaylor marked this conversation as resolved.
Show resolved Hide resolved
const versionId: RestoreHistoryVersion.Request['params']['versionId'] = ctx.params.versionId;
// @ts-expect-error can't tell koa what I want
const request: RestoreHistoryVersion.Request = ctx.request;
markkaylor marked this conversation as resolved.
Show resolved Hide resolved

const restoredDocument = await getService(strapi, 'history').restoreVersion(versionId);
const permissionChecker = getContentManagerService('permission-checker').create({
userAbility: ctx.state.userAbility,
model: request.body.contentType,
});

if (!restoredDocument) {
throw new errors.ApplicationError('Failed to restore version');
if (permissionChecker.cannot.update()) {
throw new errors.ForbiddenError();
}

const restoredDocument = await getService(strapi, 'history').restoreVersion(
request.params.versionId
);

return { data: { documentId: restoredDocument.documentId } };
markkaylor marked this conversation as resolved.
Show resolved Hide resolved
},
} satisfies Core.Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Core, Modules, UID, Data, Schema, Struct } from '@strapi/types';
import { contentTypes } from '@strapi/utils';
import { contentTypes, errors } from '@strapi/utils';
import { omit, pick } from 'lodash/fp';

import { scheduleJob } from 'node-schedule';
Expand Down Expand Up @@ -415,7 +415,7 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
contentTypeSchemaAttributes
) as Struct.SchemaAttributes;
// Set all deleted relation values to null
const dataWithoutMissingRelationsPromise = Object.entries(sanitizedSchemaAttributes).reduce(
const dataWithoutMissingRelations = await Object.entries(sanitizedSchemaAttributes).reduce(
async (
previousRelationAttributesPromise: Promise<Record<string, unknown>>,
[name, attribute]: [string, Schema.Attribute.AnyAttribute]
Expand Down Expand Up @@ -491,14 +491,16 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
Promise.resolve(structuredClone(dataWithoutAddedAttributes))
markkaylor marked this conversation as resolved.
Show resolved Hide resolved
);

// Create a new draft with the version data
const dataWithoutMissingRelations = await dataWithoutMissingRelationsPromise;
const data = omit(['id', ...Object.keys(schemaDiff.removed)], dataWithoutMissingRelations);
const restoredDocument = await strapi.documents(version.contentType).update({
documentId: version.relatedDocumentId,
data,
});

if (!restoredDocument) {
throw new errors.ApplicationError('Failed to restore version');
}

return restoredDocument;
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export declare namespace RestoreHistoryVersion {
params: {
versionId: Data.ID;
};
body: {
contentType: UID.ContentType;
};
}

export type Response =
Expand Down