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

fix: fix form builder settings after switching to new locale #3546

Open
wants to merge 9 commits into
base: next
Choose a base branch
from
4 changes: 3 additions & 1 deletion packages/api-form-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createCruds from "./plugins/crud";
import graphql from "./plugins/graphql";
import triggerHandlers from "./plugins/triggers";
import settings from "./plugins/settings";
import validators from "./plugins/validators";
import formsGraphQL from "./plugins/graphql/form";
import formSettingsGraphQL from "./plugins/graphql/formSettings";
Expand All @@ -19,6 +20,7 @@ export const createFormBuilder = (params: CreateFormBuilderParams) => {
validators,
formsGraphQL,
formSettingsGraphQL,
formBuilderPrerenderingPlugins()
formBuilderPrerenderingPlugins(),
settings
];
};
17 changes: 11 additions & 6 deletions packages/api-form-builder/src/plugins/crud/settings.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
onSettingsBeforeDelete,
onSettingsAfterDelete,
async getSettings(this: FormBuilder, params) {
const { auth, throwOnNotFound } = params || {};
const { auth, throwOnNotFound, locale } = params || {};

if (auth !== false) {
await settingsPermissions.ensure();
Expand All @@ -70,7 +70,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
try {
settings = await this.storageOperations.getSettings({
tenant: getTenant().id,
locale: getLocale().code
locale: locale || getLocale().code
});
} catch (ex) {
throw new WebinyError(
Expand All @@ -89,7 +89,11 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR

const data = await formBuilderSettings.toJSON();

const original = await this.getSettings({ auth: false });
const original = await this.getSettings({
auth: false,
locale: input.locale
});

if (original) {
throw new WebinyError(
`"Form Builder" settings already exist.`,
Expand All @@ -106,7 +110,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
domain: data.domain,
reCaptcha: data.reCaptcha,
tenant: getTenant().id,
locale: getLocale().code
locale: input.locale || getLocale().code
};
try {
await onSettingsBeforeCreate.publish({
Expand Down Expand Up @@ -186,10 +190,11 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
);
}
},
async deleteSettings(this: FormBuilder) {
async deleteSettings(this: FormBuilder, params) {
const { locale } = params || {};
await settingsPermissions.ensure();

const settings = await this.getSettings();
const settings = await this.getSettings({ locale });
if (!settings) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ContextPlugin } from "@webiny/api";
import { FormBuilderContext } from "~/types";

export const createSettingsForNewLocale = new ContextPlugin<FormBuilderContext>(context => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once a locale has been deleted, let's also delete settings.

context.i18n.locales.onLocaleAfterCreate.subscribe(async ({ locale }) => {
const currentSettings = await context.formBuilder.getSettings({
auth: false
});

await context.formBuilder.createSettings({ ...currentSettings, locale: locale.code });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ContextPlugin } from "@webiny/api";
import { FormBuilderContext } from "~/types";

export const deleteSettingsForDeletedLocale = new ContextPlugin<FormBuilderContext>(context => {
context.i18n.locales.onLocaleAfterDelete.subscribe(async ({ locale }) => {
await context.formBuilder.deleteSettings({ locale: locale.code });
});
});
4 changes: 4 additions & 0 deletions packages/api-form-builder/src/plugins/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createSettingsForNewLocale } from "./createSettingsForNewLocale";
import { deleteSettingsForDeletedLocale } from "./deleteSettingsForDeletedLocale";

export default [createSettingsForNewLocale, deleteSettingsForDeletedLocale];
7 changes: 6 additions & 1 deletion packages/api-form-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ export interface Settings {
export interface SettingsCRUDGetParams {
auth?: boolean;
throwOnNotFound?: boolean;
locale?: string;
}

export interface SettingsCRUDDeleteParams {
locale?: string;
}

/**
Expand Down Expand Up @@ -415,7 +420,7 @@ export interface SettingsCRUD {
getSettings(params?: SettingsCRUDGetParams): Promise<Settings | null>;
createSettings(data: Partial<Settings>): Promise<Settings>;
updateSettings(data: Partial<Settings>): Promise<Settings>;
deleteSettings(): Promise<void>;
deleteSettings(params?: SettingsCRUDDeleteParams): Promise<void>;
/**
* Lifecycle Events
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
SettingsStorageOperations,
SettingsStorageOperationsCreateParams,
SettingsStorageOperationsGetParams,
SettingsStorageOperationsUpdateParams
SettingsStorageOperationsUpdateParams,
SettingsStorageOperationsDeleteParams
} from "@webiny/api-page-builder/types";
import { Entity } from "@webiny/db-dynamodb/toolbox";
import { getClean } from "@webiny/db-dynamodb/utils/get";
Expand All @@ -17,8 +18,8 @@ import { put } from "@webiny/db-dynamodb";
* it in consideration and create the partition key for the global settings.
*/
interface PartitionKeyParams {
tenant: string | boolean | undefined;
locale: string | boolean | undefined;
tenant?: string | boolean;
locale?: string | boolean;
}

const createPartitionKey = (params: PartitionKeyParams): string => {
Expand Down Expand Up @@ -165,6 +166,25 @@ export const createSettingsStorageOperations = ({
);
}
};

const deleteSettings = async (params: SettingsStorageOperationsDeleteParams) => {
const { settings } = params;
const keys = {
PK: createPartitionKey(settings),
SK: "A"
};
try {
await entity.delete(keys);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not delete the settings record by given keys.",
ex.code || "DELETE_SETTINGS_ERROR",
{
keys
}
);
}
};
/**
* We can simply return the partition key for this storage operations.
*/
Expand All @@ -177,6 +197,7 @@ export const createSettingsStorageOperations = ({
getDefaults,
create,
update,
delete: deleteSettings,
createCacheKey
};
};
27 changes: 24 additions & 3 deletions packages/api-page-builder-so-ddb/src/operations/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
SettingsStorageOperations,
SettingsStorageOperationsCreateParams,
SettingsStorageOperationsGetParams,
SettingsStorageOperationsUpdateParams
SettingsStorageOperationsUpdateParams,
SettingsStorageOperationsDeleteParams
} from "@webiny/api-page-builder/types";
import { Entity } from "@webiny/db-dynamodb/toolbox";
import { getClean } from "@webiny/db-dynamodb/utils/get";
Expand All @@ -17,8 +18,8 @@ import { put } from "@webiny/db-dynamodb";
* it in consideration and create the partition key for the global settings.
*/
interface PartitionKeyParams {
tenant: string | boolean | undefined;
locale: string | boolean | undefined;
tenant?: string | boolean;
locale?: string | boolean;
}

const createPartitionKey = (params: PartitionKeyParams): string => {
Expand Down Expand Up @@ -162,6 +163,25 @@ export const createSettingsStorageOperations = ({
);
}
};

const deleteSettings = async (params: SettingsStorageOperationsDeleteParams) => {
const { settings } = params;
const keys = {
PK: createPartitionKey(settings),
SK: "A"
};
try {
await entity.delete(keys);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not delete the settings record by given keys.",
ex.code || "DELETE_SETTINGS_ERROR",
{
keys
}
);
}
};
/**
* We can simply return the partition key for this storage operations.
*/
Expand All @@ -174,6 +194,7 @@ export const createSettingsStorageOperations = ({
getDefaults,
create,
update,
delete: deleteSettings,
createCacheKey
};
};
7 changes: 7 additions & 0 deletions packages/api-page-builder/src/graphql/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ const setup = (params: CreateCrudParams) => {
fullAccessPermissionName: "pb.*"
});

const settingsPermissions = new PageTemplatesPermissions({
getIdentity: context.security.getIdentity,
getPermissions: () => context.security.getPermissions("pb.settings"),
fullAccessPermissionName: "pb.*"
});

const system = await createSystemCrud({
context,
storageOperations,
Expand All @@ -129,6 +135,7 @@ const setup = (params: CreateCrudParams) => {
const settings = createSettingsCrud({
context,
storageOperations,
settingsPermissions,
getTenantId,
getLocaleCode
});
Expand Down
9 changes: 5 additions & 4 deletions packages/api-page-builder/src/graphql/crud/categories.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ export const createCategoriesCrud = (params: CreateCategoriesCrudParams): Catego
* This method should return category or null. No error throwing on not found.
*/
async getCategory(slug, options = { auth: true }) {
const { auth } = options;
const { auth, locale } = options;

const params: CategoryStorageOperationsGetParams = {
where: {
slug,
tenant: getTenantId(),
locale: getLocaleCode()
locale: locale || getLocaleCode()
}
};

Expand Down Expand Up @@ -177,7 +177,8 @@ export const createCategoriesCrud = (params: CreateCategoriesCrudParams): Catego
}

const existingCategory = await this.getCategory(input.slug, {
auth: false
auth: false,
locale: input.locale
});
if (existingCategory) {
throw new NotFoundError(`Category with slug "${input.slug}" already exists.`);
Expand All @@ -196,7 +197,7 @@ export const createCategoriesCrud = (params: CreateCategoriesCrudParams): Catego
displayName: identity.displayName
},
tenant: getTenantId(),
locale: getLocaleCode()
locale: input.locale || getLocaleCode()
};

try {
Expand Down
Loading
Loading