Skip to content

Commit

Permalink
fix(migrations): elasticsearch 5.37.0 002 validation (#3453)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Aug 8, 2023
1 parent a757729 commit f58d4c1
Show file tree
Hide file tree
Showing 32 changed files with 1,214 additions and 214 deletions.
14 changes: 9 additions & 5 deletions packages/api-headless-cms-ddb-es/src/dynamoDb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { createRichTextStorageTransformPlugin } from "./storage/richText";
import { createLongTextStorageTransformPlugin } from "./storage/longText";
import { createDateStorageTransformPlugin } from "./storage/date";

export default () => [
createRichTextStorageTransformPlugin(),
createLongTextStorageTransformPlugin(),
createDateStorageTransformPlugin()
];
export default () => createDynamoDbPlugins();

export const createDynamoDbPlugins = () => {
return [
createRichTextStorageTransformPlugin(),
createLongTextStorageTransformPlugin(),
createDateStorageTransformPlugin()
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const getEntryData = (input: CmsEntry): CmsEntry => {
return output;
};

const getESLatestEntryData = async (plugins: PluginsContainer, entry: CmsEntry) => {
export const getESLatestEntryData = async (plugins: PluginsContainer, entry: CmsEntry) => {
return compress(plugins, {
...getEntryData(entry),
latest: true,
Expand All @@ -68,7 +68,7 @@ const getESLatestEntryData = async (plugins: PluginsContainer, entry: CmsEntry)
});
};

const getESPublishedEntryData = async (plugins: PluginsContainer, entry: CmsEntry) => {
export const getESPublishedEntryData = async (plugins: PluginsContainer, entry: CmsEntry) => {
return compress(plugins, {
...getEntryData(entry),
published: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ describe("fieldValidations", () => {
dateTimeZ: defaultFruitData.dateTimeZ,
rating: null,
isSomething: null,
description: "",
description: null,
slug: "apple"
},
error: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,40 +272,6 @@ describe("Republish entries", () => {
});
});

test("should not allow republishing of unpublished entries", async () => {
const group = await setupGroup();
await setupModel(group, "category");

const { createCategory, republishCategory } = useCategoryManageHandler(manageOpts);

/**
* Create test categories.
*/
const [appleResponse] = await createCategory({
data: {
title: "Apple",
slug: "apple"
}
});
const apple = appleResponse.data.createCategory.data;

const [appleRepublishResponse] = await republishCategory({
revision: apple.id
});
expect(appleRepublishResponse).toEqual({
data: {
republishCategory: {
data: null,
error: {
message: "Entry with given ID is not published!",
code: "NOT_PUBLISHED_ERROR",
data: expect.any(Object)
}
}
}
});
});

/**
* This test checks values directly in the storage operations, so we make sure there are required values in ref objects.
* We check in both latest and published records because in different storages that can be two different records.
Expand Down
26 changes: 10 additions & 16 deletions packages/api-headless-cms/src/crud/contentEntry.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
});

storageEntry = await entryToStorageTransform(context, model, entry);

const result = await storageOperations.entries.create(model, {
entry,
storageEntry
Expand All @@ -719,7 +720,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
input
});

return result;
return entry;
} catch (ex) {
await onEntryCreateError.publish({
error: ex,
Expand Down Expand Up @@ -848,7 +849,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
original: originalEntry,
storageEntry: result
});
return result;
return entry;
} catch (ex) {
await onEntryCreateRevisionError.publish({
entry,
Expand Down Expand Up @@ -973,7 +974,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
input,
original: originalEntry
});
return result;
return entry;
} catch (ex) {
await onEntryUpdateError.publish({
entry,
Expand Down Expand Up @@ -1057,15 +1058,6 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
}

const originalEntry = await entryFromStorageTransform(context, model, originalStorageEntry);
/**
* We can only process published entries.
*/
if (originalEntry.status !== "published") {
throw new WebinyError("Entry with given ID is not published!", "NOT_PUBLISHED_ERROR", {
id,
original: originalEntry
});
}

const values = await referenceFieldsMapping({
context,
Expand All @@ -1076,6 +1068,8 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm

const entry: CmsEntry = {
...originalEntry,
status: STATUS_PUBLISHED,
publishedOn: originalEntry.publishedOn || new Date().toISOString(),
savedOn: new Date().toISOString(),
webinyVersion: context.WEBINY_VERSION,
values
Expand Down Expand Up @@ -1116,9 +1110,9 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
await onEntryAfterRepublish.publish({
entry,
model,
storageEntry
storageEntry: result
});
return result;
return entry;
} catch (ex) {
await onEntryRepublishError.publish({
entry,
Expand Down Expand Up @@ -1392,7 +1386,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
storageEntry: result,
model
});
return result;
return entry;
} catch (ex) {
await onEntryPublishError.publish({
entry,
Expand Down Expand Up @@ -1464,7 +1458,7 @@ export const createContentEntryCrud = (params: CreateContentEntryCrudParams): Cm
model
});

return result;
return entry;
} catch (ex) {
await onEntryUnpublishError.publish({
entry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { CmsDynamicZoneTemplate, CmsModelDynamicZoneField } from "~/types";
import { StorageTransformPlugin } from "~/plugins";

const convertToStorage = (value: any, templates: CmsDynamicZoneTemplate[]) => {
const convertToStorage = (value: Record<string, any>, templates: CmsDynamicZoneTemplate[]) => {
// Only one key is allowed in the input object.
const inputType = Object.keys(value)[0];
const template = templates.find(tpl => tpl.gqlTypeName === inputType);
if (!template) {
if (template) {
return { ...value[inputType], _templateId: template.id };
}
/**
* There is a possibility that the value is already in the storage format, so there is no need to transform it again.
* We are going to check:
* 1. value is an object
* 2. it contains a _templateId key
* 3. the key is a valid template id - at this point we know it is already converted
*/
if (!value || typeof value !== "object") {
return undefined;
} else if (!value._templateId) {
return undefined;
}

return { ...value[inputType], _templateId: template.id };
const tpl = templates.find(tpl => tpl.id === value._templateId);
return tpl ? value : undefined;
};

interface TemplateValueFromStorage {
Expand All @@ -22,12 +34,33 @@ const convertFromStorage = (
templates: CmsDynamicZoneTemplate[]
) => {
const template = templates.find(tpl => value._templateId === tpl.id);
if (!template) {

if (template) {
// We keep the `_templateId` property, to simplify further processing.
return { [template.gqlTypeName]: value };
}
/**
* There is a possibility that the template was not found because value is in the original format.
* We are going to check:
* 1. value is an object
* 2. it contains a key - only one
* 3. the key is a valid template gqlTypeName
*/
/**
* Value must be an object
*/
if (!value || typeof value !== "object") {
return undefined;
}

// We keep the `_templateId` property, to simplify further processing.
return { [template.gqlTypeName]: value };
const keys = Object.keys(value);
if (keys.length !== 1) {
return undefined;
}
if (!keys[0]) {
return undefined;
}
const tpl = templates.find(tpl => tpl.gqlTypeName === keys[0]);
return tpl ? value : undefined;
};

export const createDynamicZoneStorageTransform = () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/api-headless-cms/src/utils/entryStorage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import WebinyError from "@webiny/error";
import { StorageTransformPlugin } from "~/plugins/StorageTransformPlugin";
import { CmsEntry, CmsModel, CmsModelField, CmsContext } from "~/types";
import { CmsContext, CmsEntry, CmsModel, CmsModelField } from "~/types";
import { getBaseFieldType } from "~/utils/getBaseFieldType";

interface GetStoragePluginFactory {
(context: CmsContext): (fieldType: string) => StorageTransformPlugin<any>;
(context: Pick<CmsContext, "plugins">): (fieldType: string) => StorageTransformPlugin<any>;
}

const getStoragePluginFactory: GetStoragePluginFactory = context => {
Expand Down Expand Up @@ -41,7 +41,7 @@ const getStoragePluginFactory: GetStoragePluginFactory = context => {
* This should be used when transforming the whole entry.
*/
const entryStorageTransform = async (
context: CmsContext,
context: Pick<CmsContext, "plugins">,
model: CmsModel,
operation: "toStorage" | "fromStorage",
entry: CmsEntry
Expand Down Expand Up @@ -75,7 +75,7 @@ const entryStorageTransform = async (
* A function that is used in crud to transform entry into the storage type.
*/
export const entryToStorageTransform = async (
context: CmsContext,
context: Pick<CmsContext, "plugins">,
model: CmsModel,
entry: CmsEntry
): Promise<CmsEntry> => {
Expand All @@ -86,15 +86,15 @@ export const entryToStorageTransform = async (
* A function that is used to transform the whole entry from storage into its native form.
*/
export const entryFromStorageTransform = async (
context: CmsContext,
context: Pick<CmsContext, "plugins">,
model: CmsModel,
entry: CmsEntry
): Promise<CmsEntry> => {
return entryStorageTransform(context, model, "fromStorage", entry);
};

interface EntryFieldFromStorageTransformParams {
context: CmsContext;
context: Pick<CmsContext, "plugins">;
model: CmsModel;
field: CmsModelField;
value: any;
Expand Down

0 comments on commit f58d4c1

Please sign in to comment.