From 00e17525e7080939c06b123fc0c1a638f4651024 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Wed, 6 Nov 2024 11:37:51 +0800 Subject: [PATCH] feat(schemas): add account center table (#6761) --- .../next-1730689363-add-account-center.ts | 31 +++++++++++++++++++ .../jsonb-types/account-centers.ts | 28 +++++++++++++++++ .../src/foundations/jsonb-types/index.ts | 1 + packages/schemas/tables/account_centers.sql | 10 ++++++ 4 files changed, 70 insertions(+) create mode 100644 packages/schemas/alterations/next-1730689363-add-account-center.ts create mode 100644 packages/schemas/src/foundations/jsonb-types/account-centers.ts create mode 100644 packages/schemas/tables/account_centers.sql diff --git a/packages/schemas/alterations/next-1730689363-add-account-center.ts b/packages/schemas/alterations/next-1730689363-add-account-center.ts new file mode 100644 index 00000000000..0e913a1a1fa --- /dev/null +++ b/packages/schemas/alterations/next-1730689363-add-account-center.ts @@ -0,0 +1,31 @@ +import { sql } from '@silverhand/slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + create table account_centers ( + tenant_id varchar(21) not null + references tenants (id) on update cascade on delete cascade, + id varchar(21) not null, + /** The whole feature can be disabled */ + enabled boolean not null default false, + /** Control each fields */ + fields jsonb /* @use AccountCenterFieldControl */ not null default '{}'::jsonb, + primary key (tenant_id, id) + ); + `); + await applyTableRls(pool, 'account_centers'); + }, + down: async (pool) => { + await dropTableRls(pool, 'account_centers'); + await pool.query(sql` + drop table account_centers; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/src/foundations/jsonb-types/account-centers.ts b/packages/schemas/src/foundations/jsonb-types/account-centers.ts new file mode 100644 index 00000000000..23474b0a6b1 --- /dev/null +++ b/packages/schemas/src/foundations/jsonb-types/account-centers.ts @@ -0,0 +1,28 @@ +import { z } from 'zod'; + +export enum AccountCenterControlValue { + Off = 'Off', + ReadOnly = 'ReadOnly', + Edit = 'Edit', +} + +/** + * Control list of each field in the account center (profile API) + * all fields are optional, if not set, the default value is `Off` + * this can make the alteration of the field control easier + */ +export const accountCenterFieldControlGuard = z + .object({ + name: z.nativeEnum(AccountCenterControlValue), + avatar: z.nativeEnum(AccountCenterControlValue), + profile: z.nativeEnum(AccountCenterControlValue), + email: z.nativeEnum(AccountCenterControlValue), + phone: z.nativeEnum(AccountCenterControlValue), + password: z.nativeEnum(AccountCenterControlValue), + username: z.nativeEnum(AccountCenterControlValue), + social: z.nativeEnum(AccountCenterControlValue), + customData: z.nativeEnum(AccountCenterControlValue), + }) + .partial(); + +export type AccountCenterFieldControl = z.infer; diff --git a/packages/schemas/src/foundations/jsonb-types/index.ts b/packages/schemas/src/foundations/jsonb-types/index.ts index aad607656af..4cbc95dc99e 100644 --- a/packages/schemas/src/foundations/jsonb-types/index.ts +++ b/packages/schemas/src/foundations/jsonb-types/index.ts @@ -9,6 +9,7 @@ export * from './users.js'; export * from './sso-connector.js'; export * from './applications.js'; export * from './verification-records.js'; +export * from './account-centers.js'; export { configurableConnectorMetadataGuard, diff --git a/packages/schemas/tables/account_centers.sql b/packages/schemas/tables/account_centers.sql new file mode 100644 index 00000000000..5c1ecbeb265 --- /dev/null +++ b/packages/schemas/tables/account_centers.sql @@ -0,0 +1,10 @@ +create table account_centers ( + tenant_id varchar(21) not null + references tenants (id) on update cascade on delete cascade, + id varchar(21) not null, + /** The whole feature can be disabled */ + enabled boolean not null default false, + /** Control each fields */ + fields jsonb /* @use AccountCenterFieldControl */ not null default '{}'::jsonb, + primary key (tenant_id, id) +);