0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00
logto/packages/schemas/alterations/1.0.0-1678259693-remove-branding-style-config.ts
Gao Sun 28e9fa4290
release: core v1.0.0 (#3554)
* chore: move changesets

* release: core v1.0.0

* chore: update workflow
2023-03-20 23:57:04 +08:00

112 lines
2.6 KiB
TypeScript

import type { DatabaseTransactionConnection } from 'slonik';
import { sql } from 'slonik';
import type { AlterationScript } from '../lib/types/alteration.js';
enum DeprecatedBrandingStyle {
Logo = 'Logo',
Logo_Slogan = 'Logo_Slogan',
}
const deprecatedDefaultBrandingStyle = DeprecatedBrandingStyle.Logo_Slogan;
const deprecatedDefaultSlogan = 'admin_console.welcome.title';
type DeprecatedBranding = {
style?: DeprecatedBrandingStyle;
logoUrl?: string;
darkLogoUrl?: string;
favicon?: string;
slogan?: string;
};
type DeprecatedSignInExperience = {
id: string;
tenantId: string;
branding: DeprecatedBranding;
};
type Branding = {
logoUrl?: string;
darkLogoUrl?: string;
favicon?: string;
};
type SignInExperience = {
id: string;
tenantId: string;
branding: Branding;
};
const alterBranding = async (
signInExperience: DeprecatedSignInExperience,
pool: DatabaseTransactionConnection
) => {
const { id, tenantId, branding: originBranding } = signInExperience;
const {
style, // Extract to remove from branding
slogan, // Extract to remove from branding
logoUrl,
darkLogoUrl,
favicon,
} = originBranding;
const branding: Branding = { logoUrl, darkLogoUrl, favicon };
await pool.query(
sql`update sign_in_experiences set branding = ${JSON.stringify(
branding
)} where id = ${id} and tenant_id = ${tenantId}`
);
};
const rollbackBranding = async (
signInExperience: SignInExperience,
pool: DatabaseTransactionConnection
) => {
const {
id,
tenantId,
branding: { logoUrl, darkLogoUrl, favicon },
} = signInExperience;
const adminBranding: DeprecatedBranding = {
style: DeprecatedBrandingStyle.Logo_Slogan,
slogan: 'admin_console.welcome.title',
logoUrl,
darkLogoUrl,
favicon,
};
const defaultBranding: DeprecatedBranding = {
style: DeprecatedBrandingStyle.Logo,
logoUrl,
darkLogoUrl,
favicon,
};
const branding: DeprecatedBranding = tenantId === 'admin' ? adminBranding : defaultBranding;
await pool.query(
sql`update sign_in_experiences set branding = ${JSON.stringify(
branding
)} where id = ${id} and tenant_id = ${tenantId}`
);
};
const alteration: AlterationScript = {
up: async (pool) => {
const rows = await pool.many<DeprecatedSignInExperience>(
sql`select * from sign_in_experiences`
);
await Promise.all(rows.map(async (row) => alterBranding(row, pool)));
},
down: async (pool) => {
const rows = await pool.many<SignInExperience>(sql`select * from sign_in_experiences`);
await Promise.all(rows.map(async (row) => rollbackBranding(row, pool)));
},
};
export default alteration;