Skip to content

Commit

Permalink
chore(history): add api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markkaylor committed Apr 22, 2024
1 parent 113a853 commit 032b508
Show file tree
Hide file tree
Showing 4 changed files with 454 additions and 203 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ const createHistoryVersionController = ({ strapi }: { strapi: Core.Strapi }) =>
return {
async findMany(ctx) {
const contentTypeUid = ctx.query.contentType as UID.ContentType;
const isSingleType = strapi.getModel(contentTypeUid).kind === 'singleType';
const isSingleType = strapi.getModel(contentTypeUid)?.kind === 'singleType';

if (isSingleType && !contentTypeUid) {
throw new errors.ForbiddenError('contentType is required');
}

if (!contentTypeUid && !ctx.query.documentId) {
if (!isSingleType && (!contentTypeUid || !ctx.query.documentId)) {
throw new errors.ForbiddenError('contentType and documentId are required');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
};

const localesService = strapi.plugin('i18n')?.service('locales');

const getDefaultLocale = async () => (localesService ? localesService.getDefaultLocale() : null);

const getLocaleDictionary = async () => {
if (!localesService) return {};

Expand Down Expand Up @@ -163,8 +166,9 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
? { documentId: result.documentId, locale: context.params?.locale }
: { documentId: context.params.documentId, locale: context.params?.locale };

const defaultLocale = localesService ? await localesService.getDefaultLocale() : null;
const defaultLocale = await getDefaultLocale();
const locale = documentContext.locale || defaultLocale;

const document = await strapi.documents(contentTypeUid).findOne({
documentId: documentContext.documentId,
locale,
Expand Down Expand Up @@ -251,14 +255,16 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
results: HistoryVersions.HistoryVersionDataResponse[];
pagination: HistoryVersions.Pagination;
}> {

const locale = params.locale || (await getDefaultLocale());
const [{ results, pagination }, localeDictionary] = await Promise.all([
query.findPage({
...params,
where: {
$and: [
{ contentType: params.contentType },
...(params.documentId ? [{ relatedDocumentId: params.documentId }] : []),
...(params.locale ? [{ locale: params.locale }] : []),
...(locale ? [{ locale }] : []),
],
},
populate: ['createdBy'],
Expand Down Expand Up @@ -497,6 +503,7 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
const data = omit(['id', ...Object.keys(schemaDiff.removed)], dataWithoutMissingRelations);
const restoredDocument = await strapi.documents(version.contentType).update({
documentId: version.relatedDocumentId,
locale: version.locale,
data,
});

Expand Down

0 comments on commit 032b508

Please sign in to comment.