From 21e21913faf1724670c990f07b1a8250acf7a367 Mon Sep 17 00:00:00 2001 From: Sokratis Vidros Date: Mon, 23 Dec 2024 11:22:06 +0200 Subject: [PATCH] chore(api): Apply minor refactoring to empty JSON Schema generation --- .../build-payload-schema.usecase.ts | 14 +++----------- .../build-available-variable-schema.usecase.ts | 9 ++------- apps/api/src/app/workflows-v2/util/jsonToSchema.ts | 12 ++++++++---- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.usecase.ts b/apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.usecase.ts index 5cd80c861e0..33f841a8f0f 100644 --- a/apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.usecase.ts +++ b/apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.usecase.ts @@ -5,7 +5,7 @@ import { Instrument, InstrumentUsecase } from '@novu/application-generic'; import { flattenObjectValues } from '../../util/utils'; import { pathsToObject } from '../../util/path-to-object'; import { extractLiquidTemplateVariables } from '../../util/template-parser/liquid-parser'; -import { convertJsonToSchemaWithDefaults } from '../../util/jsonToSchema'; +import { convertJsonToSchemaWithDefaults, emptyJsonSchema } from '../../util/jsonToSchema'; import { BuildPayloadSchemaCommand } from './build-payload-schema.command'; import { transformMailyContentToLiquid } from '../generate-preview/transform-maily-content-to-liquid'; import { isStringTipTapNode } from '../../util/tip-tap.util'; @@ -19,20 +19,12 @@ export class BuildPayloadSchema { const controlValues = await this.buildControlValues(command); if (!controlValues.length) { - return { - type: 'object', - properties: {}, - additionalProperties: true, - }; + return emptyJsonSchema(); } const templateVars = await this.processControlValues(controlValues); if (templateVars.length === 0) { - return { - type: 'object', - properties: {}, - additionalProperties: true, - }; + return emptyJsonSchema(); } const variablesExample = pathsToObject(templateVars, { diff --git a/apps/api/src/app/workflows-v2/usecases/build-variable-schema/build-available-variable-schema.usecase.ts b/apps/api/src/app/workflows-v2/usecases/build-variable-schema/build-available-variable-schema.usecase.ts index aeaef56a4c5..2beb08b37eb 100644 --- a/apps/api/src/app/workflows-v2/usecases/build-variable-schema/build-available-variable-schema.usecase.ts +++ b/apps/api/src/app/workflows-v2/usecases/build-variable-schema/build-available-variable-schema.usecase.ts @@ -7,6 +7,7 @@ import { BuildAvailableVariableSchemaCommand } from './build-available-variable- import { parsePayloadSchema } from '../../shared/parse-payload-schema'; import { BuildPayloadSchemaCommand } from '../build-payload-schema/build-payload-schema.command'; import { BuildPayloadSchema } from '../build-payload-schema/build-payload-schema.usecase'; +import { emptyJsonSchema } from '../../util/jsonToSchema'; @Injectable() export class BuildAvailableVariableSchemaUsecase { @@ -62,13 +63,7 @@ export class BuildAvailableVariableSchemaUsecase { command: BuildAvailableVariableSchemaCommand ): Promise { if (workflow.payloadSchema) { - return ( - parsePayloadSchema(workflow.payloadSchema, { safe: true }) || { - type: 'object', - properties: {}, - additionalProperties: true, - } - ); + return parsePayloadSchema(workflow.payloadSchema, { safe: true }) || emptyJsonSchema(); } return this.buildPayloadSchema.execute( diff --git a/apps/api/src/app/workflows-v2/util/jsonToSchema.ts b/apps/api/src/app/workflows-v2/util/jsonToSchema.ts index ed9a7b0264b..d3617cfd730 100644 --- a/apps/api/src/app/workflows-v2/util/jsonToSchema.ts +++ b/apps/api/src/app/workflows-v2/util/jsonToSchema.ts @@ -1,5 +1,13 @@ import { JSONSchemaDefinition, JSONSchemaDto } from '@novu/shared'; +export function emptyJsonSchema(): JSONSchemaDto { + return { + type: 'object', + properties: {}, + additionalProperties: true, + }; +} + export function convertJsonToSchemaWithDefaults(unknownObject?: Record) { if (!unknownObject) { return {}; @@ -8,10 +16,6 @@ export function convertJsonToSchemaWithDefaults(unknownObject?: Record): JSONSchemaDto { const schema: JSONSchemaDto = { type: 'object',