Skip to content

Commit

Permalink
feat(schemas): add account center table (#6761)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Nov 6, 2024
1 parent fe06860 commit 00e1752
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/schemas/alterations/next-1730689363-add-account-center.ts
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions packages/schemas/src/foundations/jsonb-types/account-centers.ts
Original file line number Diff line number Diff line change
@@ -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<typeof accountCenterFieldControlGuard>;
1 change: 1 addition & 0 deletions packages/schemas/src/foundations/jsonb-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/schemas/tables/account_centers.sql
Original file line number Diff line number Diff line change
@@ -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)
);

0 comments on commit 00e1752

Please sign in to comment.