From d7dc73dd3149a9ab7f4651258d1447772c3ddef4 Mon Sep 17 00:00:00 2001 From: Stanislav Kiselev <151746694+stankis@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:10:01 +0300 Subject: [PATCH] Added branding handlers (#86) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added branding handlers * Changed getColorPalette connection to replica * Moved json validator * Revert "Moved json validator" This reverts commit 805c60df254ade100e7a970d7c969538085e5f6c. * Added branding migration with branding not null * Code review fixes --------- Co-authored-by: Stanislav Kiselev <“staskis@yandex-team.ru”> --- src/components/validation-schema-compiler.ts | 25 +++++++++++++++++++ src/const/common.ts | 1 + ..._branding_column_in_tenants_to_not_null.ts | 13 ++++++++++ src/db/models/new/entry/index.ts | 1 + src/db/models/new/tenant/index.ts | 2 ++ .../joined-migration-tenant/index.ts | 2 ++ .../color-palettes/get-color-palette.ts | 2 +- 7 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/db/migrations/20240306213719_change_branding_column_in_tenants_to_not_null.ts diff --git a/src/components/validation-schema-compiler.ts b/src/components/validation-schema-compiler.ts index 02433733..21026339 100644 --- a/src/components/validation-schema-compiler.ts +++ b/src/components/validation-schema-compiler.ts @@ -5,6 +5,7 @@ import { MAX_META_OBJECT_SYMBOLS, MAX_UNVERSIONED_DATA_OBJECT_SYMBOLS, MAX_PRESET_DATA_OBJECT_SYMBOLS, + MAX_BRANDING_OBJECT_SYMBOLS, } from '../const'; import US_ERRORS from '../const/us-error-constants'; import {AppError} from '@gravity-ui/nodekit'; @@ -115,6 +116,30 @@ ajv.addKeyword('restrictMetaSize', { errors: true, }); +ajv.addKeyword('restrictBrandingSize', { + validate: function validate(schema: object | string | boolean, data: any) { + if (data === null) { + return true; + } else { + const dataStringified = JSON.stringify(data); + + // @ts-ignore + validate.errors = [ + { + format: 'string', + message: `branding jsonb object can contain not greater ${MAX_BRANDING_OBJECT_SYMBOLS} symbols`, + params: { + keyword: 'restrictBrandingSize', + }, + }, + ]; + + return dataStringified.length <= MAX_BRANDING_OBJECT_SYMBOLS; + } + }, + errors: true, +}); + ajv.addKeyword('restrictUnversionedDataSize', { validate: function validate(schema: object | string | boolean, data: any) { if (data === null) { diff --git a/src/const/common.ts b/src/const/common.ts index acba2df9..a426fb48 100644 --- a/src/const/common.ts +++ b/src/const/common.ts @@ -146,6 +146,7 @@ export const CODING_BASE = '0123456789abcdefghijklmnopqrstuvwxyz'.split(''); export const MAX_META_OBJECT_SYMBOLS = 2000; export const MAX_UNVERSIONED_DATA_OBJECT_SYMBOLS = 5000; export const MAX_PRESET_DATA_OBJECT_SYMBOLS = 15000; +export const MAX_BRANDING_OBJECT_SYMBOLS = 15000; export const AJV_PATTERN_KEYS_NOT_OBJECT = { '.*': { diff --git a/src/db/migrations/20240306213719_change_branding_column_in_tenants_to_not_null.ts b/src/db/migrations/20240306213719_change_branding_column_in_tenants_to_not_null.ts new file mode 100644 index 00000000..5e9742d5 --- /dev/null +++ b/src/db/migrations/20240306213719_change_branding_column_in_tenants_to_not_null.ts @@ -0,0 +1,13 @@ +import type {Knex} from 'knex'; + +exports.up = function (knex: Knex): Promise { + return knex.raw(` + ALTER TABLE tenants ALTER COLUMN branding SET NOT NULL; + `); +}; + +exports.down = function (knex: Knex): Promise { + return knex.raw(` + ALTER TABLE tenants ALTER COLUMN branding DROP NOT NULL; + `); +}; diff --git a/src/db/models/new/entry/index.ts b/src/db/models/new/entry/index.ts index aea65b75..ba3938a9 100644 --- a/src/db/models/new/entry/index.ts +++ b/src/db/models/new/entry/index.ts @@ -27,6 +27,7 @@ export const EntryColumn = { UnversionedData: 'unversionedData', WorkbookId: 'workbookId', Mirrored: 'mirrored', + Branding: 'branding', } as const; export class Entry extends Model { diff --git a/src/db/models/new/tenant/index.ts b/src/db/models/new/tenant/index.ts index 40b56595..7e0db233 100644 --- a/src/db/models/new/tenant/index.ts +++ b/src/db/models/new/tenant/index.ts @@ -15,6 +15,7 @@ export const TenantColumn = { BillingAccountId: 'billingAccountId', BillingInstanceServiceId: 'billingInstanceServiceId', BillingStartedAt: 'billingStartedAt', + Branding: 'branding', } as const; export class Tenant extends Model { @@ -39,6 +40,7 @@ export class Tenant extends Model { [TenantColumn.BillingAccountId]!: Nullable; [TenantColumn.BillingInstanceServiceId]!: Nullable; [TenantColumn.BillingStartedAt]!: Nullable; + [TenantColumn.Branding]!: Record; } export {BillingRate}; diff --git a/src/db/presentations/joined-migration-tenant/index.ts b/src/db/presentations/joined-migration-tenant/index.ts index 05fed1f1..ed7d4c8d 100644 --- a/src/db/presentations/joined-migration-tenant/index.ts +++ b/src/db/presentations/joined-migration-tenant/index.ts @@ -17,6 +17,7 @@ const selectedTenantColumns = [ TenantColumn.BillingAccountId, TenantColumn.BillingInstanceServiceId, TenantColumn.BillingStartedAt, + TenantColumn.Branding, ] as const; const selectedMigrationTenantColumns = [ @@ -44,6 +45,7 @@ export const JoinedMigrationTenantColumn = { ToId: MigrationTenantColumn.ToId, Migrating: MigrationTenantColumn.Migrating, MigrationMeta: MigrationTenantColumn.MigrationMeta, + Branding: TenantColumn.Branding, } as const; export const selectedJoinedMigrationTenantColumns = [ diff --git a/src/services/color-palettes/get-color-palette.ts b/src/services/color-palettes/get-color-palette.ts index e5d3232f..e31ee373 100644 --- a/src/services/color-palettes/get-color-palette.ts +++ b/src/services/color-palettes/get-color-palette.ts @@ -14,7 +14,7 @@ export const getColorPalette = async ( colorPaletteId, }); - const colorPalette = await ColorPaletteModel.query(ColorPaletteModel.primary) + const colorPalette = await ColorPaletteModel.query(ColorPaletteModel.replica) .select() .where({ [ColorPaletteModelColumn.ColorPaletteId]: colorPaletteId,