From 4b250e9a1732771638c0ae24737a425b4af5bd6a Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Mon, 12 May 2025 15:47:30 +0200 Subject: [PATCH 01/20] chore: to migrate product selection to new model --- .../models/product-selection/builder.spec.ts | 94 --------- .../src/models/product-selection/builder.ts | 15 -- .../models/product-selection/builders.spec.ts | 60 ++++++ .../src/models/product-selection/builders.ts | 44 +++++ .../models/product-selection/fields-config.ts | 55 ++++++ .../src/models/product-selection/generator.ts | 26 --- .../src/models/product-selection/index.ts | 28 ++- .../models/product-selection/presets/index.ts | 6 +- .../product-selection-draft/builder.spec.ts | 67 ------- .../product-selection-draft/builder.ts | 15 -- .../product-selection-draft/builders.spec.ts | 55 ++++++ .../product-selection-draft/builders.ts | 43 +++++ .../product-selection-draft/fields-config.ts | 25 +++ .../product-selection-draft/generator.ts | 16 -- .../product-selection-draft/index.ts | 26 ++- .../presets/empty.spec.ts | 43 ++++- .../product-selection-draft/presets/empty.ts | 32 +++- .../product-selection-draft/presets/index.ts | 29 ++- .../default-product-selection.spec.ts | 148 +++++++-------- .../default-product-selection.ts | 60 +++++- .../presets/sample-data-b2b/index.ts | 4 +- .../us-medium-customers-catalog.spec.ts | 178 ++++++++++-------- .../us-medium-customers-catalog.ts | 60 +++++- .../product-selection-draft/transformers.ts | 26 --- .../models/product-selection/transformers.ts | 24 --- .../src/models/product-selection/types.ts | 45 +++-- 26 files changed, 726 insertions(+), 498 deletions(-) delete mode 100644 standalone/src/models/product-selection/builder.spec.ts delete mode 100644 standalone/src/models/product-selection/builder.ts create mode 100644 standalone/src/models/product-selection/builders.spec.ts create mode 100644 standalone/src/models/product-selection/builders.ts create mode 100644 standalone/src/models/product-selection/fields-config.ts delete mode 100644 standalone/src/models/product-selection/generator.ts delete mode 100644 standalone/src/models/product-selection/product-selection-draft/builder.spec.ts delete mode 100644 standalone/src/models/product-selection/product-selection-draft/builder.ts create mode 100644 standalone/src/models/product-selection/product-selection-draft/builders.spec.ts create mode 100644 standalone/src/models/product-selection/product-selection-draft/builders.ts create mode 100644 standalone/src/models/product-selection/product-selection-draft/fields-config.ts delete mode 100644 standalone/src/models/product-selection/product-selection-draft/generator.ts delete mode 100644 standalone/src/models/product-selection/product-selection-draft/transformers.ts delete mode 100644 standalone/src/models/product-selection/transformers.ts diff --git a/standalone/src/models/product-selection/builder.spec.ts b/standalone/src/models/product-selection/builder.spec.ts deleted file mode 100644 index 001d8b2551..0000000000 --- a/standalone/src/models/product-selection/builder.spec.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* eslint-disable jest/no-disabled-tests */ -/* eslint-disable jest/valid-title */ -import { createBuilderSpec } from '@/core/test-utils'; -import type { TProductSelection, TProductSelectionGraphql } from './types'; -import * as ProductSelection from './index'; - -describe('builder', () => { - it( - ...createBuilderSpec( - 'default', - ProductSelection.random(), - expect.objectContaining({ - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - productCount: expect.any(Number), - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - }) - ) - ); - it( - ...createBuilderSpec( - 'rest', - ProductSelection.random(), - expect.objectContaining({ - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - productCount: expect.any(Number), - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - }) - ) - ); - - it( - ...createBuilderSpec( - 'graphql', - ProductSelection.random(), - expect.objectContaining({ - __typename: 'ProductSelection', - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - nameAllLocales: expect.arrayContaining([ - expect.objectContaining({ - __typename: 'LocalizedString', - }), - ]), - productCount: expect.any(Number), - productRefs: null, - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customerRef: expect.objectContaining({ typeId: 'customer' }), - userRef: expect.objectContaining({ typeId: 'user' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customerRef: expect.objectContaining({ typeId: 'customer' }), - userRef: expect.objectContaining({ typeId: 'user' }), - }), - }) - ) - ); -}); diff --git a/standalone/src/models/product-selection/builder.ts b/standalone/src/models/product-selection/builder.ts deleted file mode 100644 index 2e8519c018..0000000000 --- a/standalone/src/models/product-selection/builder.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Builder } from '@/core'; -import generator from './generator'; -import transformers from './transformers'; -import type { - TCreateProductSelectionBuilder, - TProductSelection, -} from './types'; - -const Model: TCreateProductSelectionBuilder = () => - Builder({ - generator, - transformers, - }); - -export default Model; diff --git a/standalone/src/models/product-selection/builders.spec.ts b/standalone/src/models/product-selection/builders.spec.ts new file mode 100644 index 0000000000..773c6cb776 --- /dev/null +++ b/standalone/src/models/product-selection/builders.spec.ts @@ -0,0 +1,60 @@ +import { TProductSelectionGraphql, TProductSelectionRest } from './types'; +import { + ProductSelection, + ProductSelectionGraphql, + ProductSelectionRest, +} from './index'; + +const validateModel = ( + model: TProductSelectionGraphql | TProductSelectionRest +): void => { + expect(model).toMatchObject({ + version: expect.any(Number), + id: expect.any(String), + createdAt: expect.any(String), + lastModifiedAt: expect.any(String), + lastModifiedBy: expect.any(Object), + createdBy: expect.any(Object), + key: expect.any(String), + productCount: expect.any(Number), + mode: expect.any(String), + custom: expect.any(Object), + }); +}; + +describe('ProductSelection model builders', () => { + it('builds a REST model', () => { + const restModel = ProductSelectionRest.random().build(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionGraphql.random().build(); + + validateModel(graphqlModel); + expect(graphqlModel.__typename).toEqual('ProductSelection'); + }); +}); + +describe('ProductSelection model compatibility builders', () => { + it('builds a default (REST) model', () => { + const restModel = ProductSelection.random().build(); + + validateModel(restModel); + }); + + it('builds a REST model', () => { + const restModel = ProductSelection.random().buildRest(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = + ProductSelection.random().buildGraphql(); + + validateModel(graphqlModel); + expect(graphqlModel.__typename).toEqual('ProductSelection'); + }); +}); diff --git a/standalone/src/models/product-selection/builders.ts b/standalone/src/models/product-selection/builders.ts new file mode 100644 index 0000000000..63cc4b4434 --- /dev/null +++ b/standalone/src/models/product-selection/builders.ts @@ -0,0 +1,44 @@ +import { + createCompatibilityBuilder, + createSpecializedBuilder, + TModelFieldsConfig, +} from '../../core'; +import { restFieldsConfig, graphqlFieldsConfig } from './fields-config'; +import type { + TCreateProductSelectionBuilder, + TProductSelectionGraphql, + TProductSelectionRest, +} from './types'; + +export const RestModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionRest +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionRestBuilder', + type: 'rest', + modelFieldsConfig: restFieldsConfig, + }); + +export const GraphqlModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); + +export const CompatModelBuilder = < + TProductSelectionModel extends + | TProductSelectionRest + | TProductSelectionGraphql = TProductSelectionRest, +>() => + createCompatibilityBuilder({ + name: 'ProductSelectionCompatBuilder', + + modelFieldsConfig: { + rest: restFieldsConfig as TModelFieldsConfig, + graphql: + graphqlFieldsConfig as TModelFieldsConfig, + }, + }); diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts new file mode 100644 index 0000000000..ed4fe5c441 --- /dev/null +++ b/standalone/src/models/product-selection/fields-config.ts @@ -0,0 +1,55 @@ +import { + ClientLogging, + LocalizedString, + // ReferenceGraphql, +} from '../../commons'; +import { fake, oneOf, sequence, TModelFieldsConfig } from '../../core'; +import { createRelatedDates } from '../../utils'; +import { TProductSelectionRest, TProductSelectionGraphql } from './types'; + +const [getOlderDate, getNewerDate] = createRelatedDates(); + +const commonFieldsConfig = { + id: fake((f) => f.string.uuid()), + version: sequence(), + key: fake((f) => f.lorem.slug(2)), + name: fake(() => LocalizedString.random()), + productCount: fake((f) => f.number.int()), + mode: oneOf('Individual', 'IndividualExclusion'), + custom: null, + createdAt: fake(getOlderDate), + createdBy: fake(() => ClientLogging.random()), + lastModifiedAt: fake(getNewerDate), + lastModifiedBy: fake(() => ClientLogging.random()), +}; + +export const restFieldsConfig: TModelFieldsConfig = { + fields: { + ...commonFieldsConfig, + }, +}; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + ...commonFieldsConfig, + __typename: 'ProductSelection', + nameAllLocales: fake(() => LocalizedString.random()), + name: null, + productRefs: null, + }, + postBuild: (model) => { + const name = model.nameAllLocales + ? LocalizedString.resolveGraphqlDefaultLocaleValue(model.nameAllLocales) + : undefined; + + // const productRefs = ReferenceGraphql.presets + // .productReference() + // .buildGraphql(); + + return { + name, + // productRefs, + }; + }, + }; diff --git a/standalone/src/models/product-selection/generator.ts b/standalone/src/models/product-selection/generator.ts deleted file mode 100644 index a7231fb86b..0000000000 --- a/standalone/src/models/product-selection/generator.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Generator, fake, oneOf, sequence } from '@/core'; -import { ClientLogging, LocalizedString } from '@/models/commons'; -import { createRelatedDates } from '@/utils'; -import { TProductSelection } from './types'; - -const [getOlderDate, getNewerDate] = createRelatedDates(); - -// https://docs.commercetools.com/api/projects/product-selections#productselection - -const generator = Generator({ - fields: { - id: fake((f) => f.string.uuid()), - version: sequence(), - key: fake((f) => f.lorem.slug(2)), - name: fake(() => LocalizedString.random()), - productCount: fake((f) => f.number.int()), - mode: oneOf('Individual', 'IndividualExclusion'), - custom: null, - createdAt: fake(getOlderDate), - createdBy: fake(() => ClientLogging.random()), - lastModifiedAt: fake(getNewerDate), - lastModifiedBy: fake(() => ClientLogging.random()), - }, -}); - -export default generator; diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index ac78772c7a..8de32f22df 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -1,6 +1,24 @@ -export * as ProductSelectionDraft from './product-selection-draft'; -export * as ProductSelection from '.'; +import { + RestModelBuilder, + GraphqlModelBuilder, + CompatModelBuilder, +} from './builders'; +import * as modelPresets from './presets'; -export { default as random } from './builder'; -export { default as presets } from './presets'; -export * from './types'; +export const ProductSelectionRest = { + random: RestModelBuilder, + presets: modelPresets.restPresets, +}; + +export const ProductSelectionGraphql = { + random: GraphqlModelBuilder, + presets: modelPresets.graphqlPresets, +}; + +/** + * @deprecated Use `ProductSelectionRest` or `ProductSelectionGraphql` exported models instead of `ProductSelection`. + */ +export const ProductSelection = { + random: CompatModelBuilder, + presets: modelPresets.compatPresets, +}; diff --git a/standalone/src/models/product-selection/presets/index.ts b/standalone/src/models/product-selection/presets/index.ts index 763e57fe0a..ec0bad6e96 100644 --- a/standalone/src/models/product-selection/presets/index.ts +++ b/standalone/src/models/product-selection/presets/index.ts @@ -1,3 +1,3 @@ -const presets = {}; - -export default presets; +export const restPresets = {}; +export const graphqlPresets = {}; +export const compatPresets = {}; diff --git a/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts deleted file mode 100644 index da959218ea..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* eslint-disable jest/no-disabled-tests */ -/* eslint-disable jest/valid-title */ -import { createBuilderSpec } from '@/core/test-utils'; -import type { - TProductSelectionDraft, - TProductSelectionDraftGraphql, -} from '../types'; -import * as ProductSelectionDraft from './index'; - -describe('builder', () => { - it( - ...createBuilderSpec( - 'default', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - mode: expect.any(String), - custom: null, - }) - ) - ); - it( - ...createBuilderSpec( - 'rest', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - mode: expect.any(String), - custom: null, - }) - ) - ); - it( - ...createBuilderSpec( - 'graphql', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - mode: expect.any(String), - name: expect.arrayContaining([ - expect.objectContaining({ - locale: 'en', - value: expect.any(String), - }), - expect.objectContaining({ - locale: 'fr', - value: expect.any(String), - }), - expect.objectContaining({ - locale: 'de', - value: expect.any(String), - }), - ]), - }) - ) - ); -}); diff --git a/standalone/src/models/product-selection/product-selection-draft/builder.ts b/standalone/src/models/product-selection/product-selection-draft/builder.ts deleted file mode 100644 index 8ada0fcf63..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/builder.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Builder } from '@/core'; -import type { - TCreateProductSelectionDraftBuilder, - TProductSelectionDraft, -} from '../types'; -import generator from './generator'; -import transformers from './transformers'; - -const Model: TCreateProductSelectionDraftBuilder = () => - Builder({ - generator, - transformers, - }); - -export default Model; diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts new file mode 100644 index 0000000000..95f8dc173d --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts @@ -0,0 +1,55 @@ +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; +import { + ProductSelectionDraft, + ProductSelectionDraftGraphql, + ProductSelectionDraftRest, +} from './index'; + +const validateModel = ( + model: TProductSelectionDraftGraphql | TProductSelectionDraftRest +): void => { + expect(model).toEqual( + expect.objectContaining({ + key: expect.any(String), + name: expect.any(String), + mode: expect.any(String), + custom: expect.any(Object), + }) + ); +}; + +describe('ProductSelection model builders', () => { + it('builds a REST model', () => { + const restModel = ProductSelectionDraftRest.random().build(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionDraftGraphql.random().build(); + validateModel(graphqlModel); + }); +}); + +describe('ProductSelection model compatibility builders', () => { + it('builds a default (REST) model', () => { + const restModel = ProductSelectionDraft.random().build(); + + validateModel(restModel); + }); + + it('builds a REST model', () => { + const restModel = ProductSelectionDraft.random().buildRest(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionDraft.random().buildGraphql(); + + validateModel(graphqlModel); + }); +}); diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.ts b/standalone/src/models/product-selection/product-selection-draft/builders.ts new file mode 100644 index 0000000000..c446edcefa --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/builders.ts @@ -0,0 +1,43 @@ +import { + createCompatibilityBuilder, + createSpecializedBuilder, + TModelFieldsConfig, +} from '../../../core'; +import type { + TCreateProductSelectionBuilder, + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; +import { restFieldsConfig, graphqlFieldsConfig } from './fields-config'; + +export const RestModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionDraftRest +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionDraftRestBuilder', + type: 'rest', + modelFieldsConfig: restFieldsConfig, + }); + +export const GraphqlModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionDraftGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionDraftGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); + +export const CompatModelBuilder = < + TProductSelectionModel extends + | TProductSelectionDraftRest + | TProductSelectionDraftGraphql = TProductSelectionDraftRest, +>() => + createCompatibilityBuilder({ + name: 'ProductSelectionCompatBuilder', + modelFieldsConfig: { + rest: restFieldsConfig as TModelFieldsConfig, + graphql: + graphqlFieldsConfig as TModelFieldsConfig, + }, + }); diff --git a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts new file mode 100644 index 0000000000..32992d39e9 --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts @@ -0,0 +1,25 @@ +import { fake, TModelFieldsConfig } from '../../../core'; +import { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; + +export const restFieldsConfig: TModelFieldsConfig = + { + fields: { + name: fake((f) => f.lorem.word()), + key: fake((f) => f.lorem.slug()), + custom: null, + mode: 'Individual', + }, + }; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + name: fake((f) => f.lorem.word()), + key: fake((f) => f.lorem.slug()), + custom: null, + mode: 'Individual', + }, + }; diff --git a/standalone/src/models/product-selection/product-selection-draft/generator.ts b/standalone/src/models/product-selection/product-selection-draft/generator.ts deleted file mode 100644 index b93f6521cc..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/generator.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fake, Generator, oneOf } from '@/core'; -import { LocalizedStringDraft } from '@/models/commons'; -import { TProductSelectionDraft } from '../types'; - -//https://docs.commercetools.com/api/projects/product-selections#productselectiondraft - -const generator = Generator({ - fields: { - key: fake((f) => f.lorem.slug(2)), - name: fake(() => LocalizedStringDraft.random()), - mode: oneOf('Individual', 'IndividualExclusion'), - custom: null, - }, -}); - -export default generator; diff --git a/standalone/src/models/product-selection/product-selection-draft/index.ts b/standalone/src/models/product-selection/product-selection-draft/index.ts index 96e2519e1a..77df47a126 100644 --- a/standalone/src/models/product-selection/product-selection-draft/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/index.ts @@ -1,2 +1,24 @@ -export { default as random } from './builder'; -export { default as presets } from './presets'; +import { + RestModelBuilder, + GraphqlModelBuilder, + CompatModelBuilder, +} from './builders'; +import * as modelPresets from './presets'; + +export const ProductSelectionDraftRest = { + random: RestModelBuilder, + presets: modelPresets.restPresets, +}; + +export const ProductSelectionDraftGraphql = { + random: GraphqlModelBuilder, + presets: modelPresets.graphqlPresets, +}; + +/** + * @deprecated Use `ProductSelectionDraftRest` or `ProductSelectionDraftGraphql` exported models instead of `ProductSelectionDraft`. + */ +export const ProductSelectionDraft = { + random: CompatModelBuilder, + presets: modelPresets.compatPresets, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts index 9317451438..67d466c211 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts @@ -1,11 +1,44 @@ -import { TProductSelectionDraft } from '../../types'; -import empty from './empty'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../types'; +import { restPreset, graphqlPreset, compatPreset } from './empty'; -it(`should set all specified fields to undefined`, () => { - const emptyDraft = empty().build(); - expect(emptyDraft).toMatchObject({ +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ key: undefined, mode: undefined, custom: undefined, }); +}; + +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + key: undefined, + mode: undefined, + custom: undefined, + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); + }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts index 09855126e6..9cd684970c 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts @@ -1,7 +1,29 @@ -import type { TProductSelectionDraftBuilder } from '../../types'; -import ProductSelectionDraft from '../builder'; +import type { TBuilder } from '../../../../core'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../index'; -const empty = (): TProductSelectionDraftBuilder => - ProductSelectionDraft().key(undefined).mode(undefined).custom(undefined); +export const restPreset = (): TBuilder => + ProductSelectionDraftRest.random() + .key(undefined) + .mode(undefined) + .custom(undefined); -export default empty; +export const graphqlPreset = (): TBuilder => + ProductSelectionDraftGraphql.random() + .key(undefined) + .mode(undefined) + .custom(undefined); + +export const compatPreset = (): TBuilder => + ProductSelectionDraft.random() + .key(undefined) + .mode(undefined) + .custom(undefined); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts index ce328b9dd3..b639cf04b0 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts @@ -1,6 +1,27 @@ -import empty from './empty'; +import * as empty from './empty'; import sampleDataB2B from './sample-data-b2b'; -const presets = { empty, sampleDataB2B }; - -export default presets; +export const restPresets = { + empty: empty.restPreset, + sampleDataB2B: { + usMediumCustomersCatalog: sampleDataB2B.usMediumCustomersCatalog.restPreset, + defaultProductSelection: sampleDataB2B.defaultProductSelection.restPreset, + }, +}; +export const graphqlPresets = { + empty: empty.graphqlPreset, + sampleDataB2B: { + usMediumCustomersCatalog: + sampleDataB2B.usMediumCustomersCatalog.graphqlPreset, + defaultProductSelection: + sampleDataB2B.defaultProductSelection.graphqlPreset, + }, +}; +export const compatPresets = { + empty: empty.compatPreset, + sampleDataB2B: { + usMediumCustomersCatalog: + sampleDataB2B.usMediumCustomersCatalog.compatPreset, + defaultProductSelection: sampleDataB2B.defaultProductSelection.compatPreset, + }, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts index 1a9a94c852..df5b3d7d75 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts @@ -1,85 +1,73 @@ -import type { TProductSelectionDraft } from '../../../types'; -import defaultProductSelection from './default-product-selection'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + restPreset, + graphqlPreset, + compatPreset, +} from './default-product-selection'; -describe(`with defaultProductSelection preset`, () => { - it(`should return a defaultProductSelection preset`, () => { - const defaultProductSelectionPreset = - defaultProductSelection().build(); - expect(defaultProductSelectionPreset).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "default-product-selection", - "mode": "Individual", - "name": { - "de": undefined, - "de-DE": "Standard", - "en": undefined, - "en-AU": "Default", - "en-GB": "Default", - "en-NZ": "Default", - "en-US": "Default", - "es-ES": "Predeterminado", - "fr": undefined, - "fr-FR": "Défaut", - "it-IT": "Predefinito", - "nl-NL": "Standaard", - "pt-PT": "Padrão", - }, - } - `); +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'default-product-selection', + mode: 'Individual', + name: { + de: undefined, + 'de-DE': 'Standard', + en: undefined, + 'en-AU': 'Default', + 'en-GB': 'Default', + 'en-NZ': 'Default', + 'en-US': 'Default', + 'es-ES': 'Predeterminado', + fr: undefined, + 'fr-FR': 'Défaut', + 'it-IT': 'Predefinito', + 'nl-NL': 'Standaard', + 'pt-PT': 'Padrão', + }, }); +}; - it(`should return a defaultProductSelection preset when built for graphql`, () => { - const defaultProductSelectionPresetGraphql = - defaultProductSelection().buildGraphql(); - expect(defaultProductSelectionPresetGraphql).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "default-product-selection", - "mode": "Individual", - "name": [ - { - "locale": "de-DE", - "value": "Standard", - }, - { - "locale": "it-IT", - "value": "Predefinito", - }, - { - "locale": "nl-NL", - "value": "Standaard", - }, - { - "locale": "fr-FR", - "value": "Défaut", - }, - { - "locale": "en-AU", - "value": "Default", - }, - { - "locale": "es-ES", - "value": "Predeterminado", - }, - { - "locale": "en-GB", - "value": "Default", - }, - { - "locale": "en-NZ", - "value": "Default", - }, - { - "locale": "pt-PT", - "value": "Padrão", - }, - { - "locale": "en-US", - "value": "Default", - }, - ], - } - `); +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + key: 'default-product-selection', + name: [ + { locale: 'de-DE', value: 'Standard' }, + { locale: 'it-IT', value: 'Predefinito' }, + { locale: 'nl-NL', value: 'Standaard' }, + { locale: 'fr-FR', value: 'Défaut' }, + { locale: 'en-AU', value: 'Default' }, + { locale: 'es-ES', value: 'Predeterminado' }, + { locale: 'en-GB', value: 'Default' }, + { locale: 'en-NZ', value: 'Default' }, + { locale: 'pt-PT', value: 'Padrão' }, + { locale: 'en-US', value: 'Default' }, + ], + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts index b87bfa19bd..540be6e533 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,9 +1,39 @@ import { LocalizedStringDraft } from '../../../../commons'; -import type { TProductSelectionDraftBuilder } from '../../../types'; -import * as ProductSelectionDraft from '../../index'; +import type { TBuilder } from '../../../../../core'; +import { TCtpProductSelectionMode } from '../../../../../graphql-types'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../../index'; -const defaultProductSelection = (): TProductSelectionDraftBuilder => - ProductSelectionDraft.presets +export const restPreset = (): TBuilder => + ProductSelectionDraftRest.presets + .empty() + .key('default-product-selection') + .name( + LocalizedStringDraft.presets + .empty() + ['de-DE']('Standard') + ['it-IT']('Predefinito') + ['nl-NL']('Standaard') + ['fr-FR']('Défaut') + ['en-AU']('Default') + ['es-ES']('Predeterminado') + ['en-GB']('Default') + ['en-NZ']('Default') + ['pt-PT']('Padrão') + ['en-US']('Default') + ) + .mode(TCtpProductSelectionMode.Individual); + +export const graphqlPreset = (): TBuilder => + ProductSelectionDraftGraphql.presets .empty() .key('default-product-selection') .name( @@ -20,6 +50,24 @@ const defaultProductSelection = (): TProductSelectionDraftBuilder => ['pt-PT']('Padrão') ['en-US']('Default') ) - .mode('Individual'); + .mode(TCtpProductSelectionMode.Individual); -export default defaultProductSelection; +export const compatPreset = (): TBuilder => + ProductSelectionDraft.presets + .empty() + .key('default-product-selection') + .name( + LocalizedStringDraft.presets + .empty() + ['de-DE']('Standard') + ['it-IT']('Predefinito') + ['nl-NL']('Standaard') + ['fr-FR']('Défaut') + ['en-AU']('Default') + ['es-ES']('Predeterminado') + ['en-GB']('Default') + ['en-NZ']('Default') + ['pt-PT']('Padrão') + ['en-US']('Default') + ) + .mode(TCtpProductSelectionMode.Individual); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts index d3a9c6b1a0..b2ad0bee55 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts @@ -1,5 +1,5 @@ -import defaultProductSelection from './default-product-selection'; -import usMediumCustomersCatalog from './us-medium-customers-catalog'; +import * as defaultProductSelection from './default-product-selection'; +import * as usMediumCustomersCatalog from './us-medium-customers-catalog'; const presets = { usMediumCustomersCatalog, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts index f004f90e40..c7d5eed878 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts @@ -1,85 +1,105 @@ -import type { TProductSelectionDraft } from '../../../types'; -import usMediumCustomersCatalog from './us-medium-customers-catalog'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + restPreset, + graphqlPreset, + compatPreset, +} from './us-medium-customers-catalog'; -describe(`with usMediumCustomersCatalog preset`, () => { - it(`should return a usMediumCustomersCatalog preset`, () => { - const usMediumCustomersCatalogPreset = - usMediumCustomersCatalog().build(); - expect(usMediumCustomersCatalogPreset).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "us-medium-customers-catalog", - "mode": "Individual", - "name": { - "de": undefined, - "de-DE": "US Mittel Kundenkatalog", - "en": undefined, - "en-AU": "US Medium Customers Catalog", - "en-GB": "US Medium Customers Catalog", - "en-NZ": "US Medium Customers Catalog", - "en-US": "US Medium Customers Catalog", - "es-ES": "Catálogo de Clientes Medianos de EE. UU.", - "fr": undefined, - "fr-FR": "Catalogue Clients Moyens des États-Unis", - "it-IT": "Catalogo Clienti Medio USA", - "nl-NL": "Catalogus voor Middelgrote Klanten in de VS", - "pt-PT": "Catálogo de Clientes Médios dos EUA", - }, - } - `); +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'us-medium-customers-catalog', + mode: 'Individual', + name: { + de: undefined, + 'de-DE': 'US Mittel Kundenkatalog', + en: undefined, + 'en-AU': 'US Medium Customers Catalog', + 'en-GB': 'US Medium Customers Catalog', + 'en-NZ': 'US Medium Customers Catalog', + 'en-US': 'US Medium Customers Catalog', + 'es-ES': 'Catálogo de Clientes Medianos de EE. UU.', + fr: undefined, + 'fr-FR': 'Catalogue Clients Moyens des États-Unis', + 'it-IT': 'Catalogo Clienti Medio USA', + 'nl-NL': 'Catalogus voor Middelgrote Klanten in de VS', + 'pt-PT': 'Catálogo de Clientes Médios dos EUA', + }, }); +}; - it(`should return a usMediumCustomersCatalog preset when built for graphql`, () => { - const usMediumCustomersCatalogPresetGraphql = - usMediumCustomersCatalog().buildGraphql(); - expect(usMediumCustomersCatalogPresetGraphql).toMatchInlineSnapshot(` +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'us-medium-customers-catalog', + mode: 'Individual', + name: [ + { + locale: 'de-DE', + value: 'US Mittel Kundenkatalog', + }, + { + locale: 'it-IT', + value: 'Catalogo Clienti Medio USA', + }, + { + locale: 'nl-NL', + value: 'Catalogus voor Middelgrote Klanten in de VS', + }, + { + locale: 'fr-FR', + value: 'Catalogue Clients Moyens des États-Unis', + }, + { + locale: 'en-AU', + value: 'US Medium Customers Catalog', + }, + { + locale: 'es-ES', + value: 'Catálogo de Clientes Medianos de EE. UU.', + }, + { + locale: 'en-GB', + value: 'US Medium Customers Catalog', + }, { - "custom": undefined, - "key": "us-medium-customers-catalog", - "mode": "Individual", - "name": [ - { - "locale": "de-DE", - "value": "US Mittel Kundenkatalog", - }, - { - "locale": "it-IT", - "value": "Catalogo Clienti Medio USA", - }, - { - "locale": "nl-NL", - "value": "Catalogus voor Middelgrote Klanten in de VS", - }, - { - "locale": "fr-FR", - "value": "Catalogue Clients Moyens des États-Unis", - }, - { - "locale": "en-AU", - "value": "US Medium Customers Catalog", - }, - { - "locale": "es-ES", - "value": "Catálogo de Clientes Medianos de EE. UU.", - }, - { - "locale": "en-GB", - "value": "US Medium Customers Catalog", - }, - { - "locale": "en-NZ", - "value": "US Medium Customers Catalog", - }, - { - "locale": "pt-PT", - "value": "Catálogo de Clientes Médios dos EUA", - }, - { - "locale": "en-US", - "value": "US Medium Customers Catalog", - }, - ], - } - `); + locale: 'en-NZ', + value: 'US Medium Customers Catalog', + }, + { + locale: 'pt-PT', + value: 'Catálogo de Clientes Médios dos EUA', + }, + { + locale: 'en-US', + value: 'US Medium Customers Catalog', + }, + ], + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts index 6bb6f96654..68d9b5ae12 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts @@ -1,9 +1,39 @@ import { LocalizedStringDraft } from '../../../../commons'; -import type { TProductSelectionDraftBuilder } from '../../../types'; -import * as ProductSelectionDraft from '../../index'; +import type { TBuilder } from '../../../../../core'; +import { TCtpProductSelectionMode } from '../../../../../graphql-types'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../../index'; -const usMediumCustomersCatalog = (): TProductSelectionDraftBuilder => - ProductSelectionDraft.presets +export const restPreset = (): TBuilder => + ProductSelectionDraftRest.presets + .empty() + .key('us-medium-customers-catalog') + .name( + LocalizedStringDraft.presets + .empty() + ['de-DE']('US Mittel Kundenkatalog') + ['it-IT']('Catalogo Clienti Medio USA') + ['nl-NL']('Catalogus voor Middelgrote Klanten in de VS') + ['fr-FR']('Catalogue Clients Moyens des États-Unis') + ['en-AU']('US Medium Customers Catalog') + ['es-ES']('Catálogo de Clientes Medianos de EE. UU.') + ['en-GB']('US Medium Customers Catalog') + ['en-NZ']('US Medium Customers Catalog') + ['pt-PT']('Catálogo de Clientes Médios dos EUA') + ['en-US']('US Medium Customers Catalog') + ) + .mode(TCtpProductSelectionMode.Individual); + +export const graphqlPreset = (): TBuilder => + ProductSelectionDraftGraphql.presets .empty() .key('us-medium-customers-catalog') .name( @@ -20,6 +50,24 @@ const usMediumCustomersCatalog = (): TProductSelectionDraftBuilder => ['pt-PT']('Catálogo de Clientes Médios dos EUA') ['en-US']('US Medium Customers Catalog') ) - .mode('Individual'); + .mode(TCtpProductSelectionMode.Individual); -export default usMediumCustomersCatalog; +export const compatPreset = (): TBuilder => + ProductSelectionDraft.presets + .empty() + .key('us-medium-customers-catalog') + .name( + LocalizedStringDraft.presets + .empty() + ['de-DE']('US Mittel Kundenkatalog') + ['it-IT']('Catalogo Clienti Medio USA') + ['nl-NL']('Catalogus voor Middelgrote Klanten in de VS') + ['fr-FR']('Catalogue Clients Moyens des États-Unis') + ['en-AU']('US Medium Customers Catalog') + ['es-ES']('Catálogo de Clientes Medianos de EE. UU.') + ['en-GB']('US Medium Customers Catalog') + ['en-NZ']('US Medium Customers Catalog') + ['pt-PT']('Catálogo de Clientes Médios dos EUA') + ['en-US']('US Medium Customers Catalog') + ) + .mode(TCtpProductSelectionMode.Individual); diff --git a/standalone/src/models/product-selection/product-selection-draft/transformers.ts b/standalone/src/models/product-selection/product-selection-draft/transformers.ts deleted file mode 100644 index 234aa5c2f6..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/transformers.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Transformer } from '@/core'; -import type { - TProductSelectionDraft, - TProductSelectionDraftGraphql, -} from '../types'; - -const transformers = { - default: Transformer( - 'default', - { - buildFields: ['name'], - } - ), - rest: Transformer('rest', { - buildFields: ['name'], - }), - - graphql: Transformer( - 'graphql', - { - buildFields: ['name'], - } - ), -}; - -export default transformers; diff --git a/standalone/src/models/product-selection/transformers.ts b/standalone/src/models/product-selection/transformers.ts deleted file mode 100644 index b3e3456354..0000000000 --- a/standalone/src/models/product-selection/transformers.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Transformer } from '@/core'; -import { LocalizedString } from '@/models/commons'; -import type { TProductSelection, TProductSelectionGraphql } from './types'; - -const transformers = { - default: Transformer('default', { - buildFields: ['createdBy', 'lastModifiedBy', 'name'], - }), - rest: Transformer('rest', { - buildFields: ['createdBy', 'lastModifiedBy', 'name'], - }), - - graphql: Transformer('graphql', { - buildFields: ['createdBy', 'lastModifiedBy'], - removeFields: ['name'], - addFields: ({ fields }) => ({ - nameAllLocales: LocalizedString.toLocalizedField(fields.name), - productRefs: null, - __typename: 'ProductSelection', - }), - }), -}; - -export default transformers; diff --git a/standalone/src/models/product-selection/types.ts b/standalone/src/models/product-selection/types.ts index a93f7a2f3b..7a6c3f6654 100644 --- a/standalone/src/models/product-selection/types.ts +++ b/standalone/src/models/product-selection/types.ts @@ -3,24 +3,33 @@ import type { ProductSelectionDraft, } from '@commercetools/platform-sdk'; import type { TBuilder } from '@/core'; -import { TLocalizedStringGraphql } from '@/models/commons'; +import { + TCtpProductSelection, + TCtpCreateProductSelectionDraft, +} from '@/graphql-types'; -//ProductSelectionDraft +/** + * @deprecated use `TProductSelectionDraftRest` or `TProductSelectionDraftGraphql` instead + */ export type TProductSelectionDraft = ProductSelectionDraft; -export type TProductSelectionDraftBuilder = TBuilder; -export type TCreateProductSelectionDraftBuilder = - () => TProductSelectionDraftBuilder; -export type TProductSelectionDraftGraphql = Omit< - TProductSelectionDraft, - 'name' -> & { - name: TLocalizedStringGraphql; -}; - -//ProductSelection +/** + * @deprecated use `TProductSelectionRest` or `TProductSelectionGraphql` instead + */ export type TProductSelection = ProductSelection; -export type TProductSelectionBuilder = TBuilder; -export type TCreateProductSelectionBuilder = () => TProductSelectionBuilder; -export type TProductSelectionGraphql = TProductSelection & { - __typename: 'ProductSelection'; -}; + +// REST types +export type TProductSelectionDraftRest = ProductSelectionDraft; +export type TProductSelectionRest = ProductSelection; + +// GraphQL types +export type TProductSelectionDraftGraphql = TCtpCreateProductSelectionDraft; +export type TProductSelectionGraphql = TCtpProductSelection; + +// Builders types +export type TCreateProductSelectionBuilder< + TProductSelectionModel extends + | TProductSelectionRest + | TProductSelectionGraphql + | TProductSelectionDraftRest + | TProductSelectionDraftGraphql, +> = () => TBuilder; From f9e51762bae19557b0058bab32c9584c7badfd53 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Tue, 13 May 2025 14:40:16 +0200 Subject: [PATCH 02/20] chore: to export draft model in the index file --- standalone/src/models/product-selection/index.ts | 6 ++++++ .../presets/sample-data-b2b/default-product-selection.ts | 6 ++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index 8de32f22df..63b7d257f2 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -5,6 +5,12 @@ import { } from './builders'; import * as modelPresets from './presets'; +export { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from './product-selection-draft'; + export const ProductSelectionRest = { random: RestModelBuilder, presets: modelPresets.restPresets, diff --git a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts index 655e272a76..6c4f7b7fd9 100644 --- a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,8 +1,6 @@ import { KeyReferenceDraft } from '../../../../../commons'; -import { - ProductSelectionDraft, - type TProductSelectionDraft, -} from '../../../../../product-selection'; +import { ProductSelectionDraft } from '../../../../../product-selection'; +import type { TProductSelectionDraft } from '../../../../../product-selection/types'; import type { TProductSelectionSettingDraftBuilder } from '../../../types'; import ProductSelectionSettingDraft from '../../builder'; From 3ace724b708e420d160de542153cb7e632e2b0a6 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Tue, 13 May 2025 14:52:36 +0200 Subject: [PATCH 03/20] chore: to fix type import --- .../us-medium-customers-catalog-product-selection.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts index f3a37d700f..ca1ddedca8 100644 --- a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts +++ b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts @@ -1,8 +1,6 @@ import { KeyReferenceDraft } from '../../../../../commons'; -import { - ProductSelectionDraft, - type TProductSelectionDraft, -} from '../../../../../product-selection'; +import { ProductSelectionDraft } from '../../../../../product-selection'; +import type { TProductSelectionDraft } from '../../../../../product-selection/types'; import type { TProductSelectionSettingDraftBuilder } from '../../../types'; import ProductSelectionSettingDraft from '../../builder'; From a156df4e4bf78b802a38c0054258667f1752f123 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Tue, 13 May 2025 15:15:36 +0200 Subject: [PATCH 04/20] chore: to remove name from graphql config --- standalone/src/models/product-selection/fields-config.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts index ed4fe5c441..e08dfee293 100644 --- a/standalone/src/models/product-selection/fields-config.ts +++ b/standalone/src/models/product-selection/fields-config.ts @@ -35,7 +35,6 @@ export const graphqlFieldsConfig: TModelFieldsConfig = ...commonFieldsConfig, __typename: 'ProductSelection', nameAllLocales: fake(() => LocalizedString.random()), - name: null, productRefs: null, }, postBuild: (model) => { From 7adf4ff3f60b9726d059af77cf794caa7f75c2ff Mon Sep 17 00:00:00 2001 From: Carlos Cortizas Date: Tue, 13 May 2025 18:14:16 +0200 Subject: [PATCH 05/20] refactor(product-selection): refactor draft preset --- .../product-selection-draft/presets/index.ts | 20 ++------ .../default-product-selection.ts | 2 +- .../presets/sample-data-b2b/index.ts | 16 ++++-- .../us-medium-customers-catalog.spec.ts | 1 - .../us-medium-customers-catalog.ts | 51 ++++--------------- 5 files changed, 28 insertions(+), 62 deletions(-) diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts index b639cf04b0..1d552e4069 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts @@ -1,27 +1,15 @@ import * as empty from './empty'; -import sampleDataB2B from './sample-data-b2b'; +import * as sampleDataB2B from './sample-data-b2b'; export const restPresets = { empty: empty.restPreset, - sampleDataB2B: { - usMediumCustomersCatalog: sampleDataB2B.usMediumCustomersCatalog.restPreset, - defaultProductSelection: sampleDataB2B.defaultProductSelection.restPreset, - }, + sampleDataB2B: sampleDataB2B.restPresets, }; export const graphqlPresets = { empty: empty.graphqlPreset, - sampleDataB2B: { - usMediumCustomersCatalog: - sampleDataB2B.usMediumCustomersCatalog.graphqlPreset, - defaultProductSelection: - sampleDataB2B.defaultProductSelection.graphqlPreset, - }, + sampleDataB2B: sampleDataB2B.graphqlPresets, }; export const compatPresets = { empty: empty.compatPreset, - sampleDataB2B: { - usMediumCustomersCatalog: - sampleDataB2B.usMediumCustomersCatalog.compatPreset, - defaultProductSelection: sampleDataB2B.defaultProductSelection.compatPreset, - }, + sampleDataB2B: sampleDataB2B.compatPresets, }; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts index 540be6e533..d663b553d9 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,6 +1,6 @@ -import { LocalizedStringDraft } from '../../../../commons'; import type { TBuilder } from '../../../../../core'; import { TCtpProductSelectionMode } from '../../../../../graphql-types'; +import { LocalizedStringDraft } from '../../../../commons'; import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts index b2ad0bee55..bd2aa147ac 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts @@ -1,9 +1,17 @@ import * as defaultProductSelection from './default-product-selection'; import * as usMediumCustomersCatalog from './us-medium-customers-catalog'; -const presets = { - usMediumCustomersCatalog, - defaultProductSelection, +export const restPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.restPreset, + defaultProductSelection: defaultProductSelection.restPreset, }; -export default presets; +export const graphqlPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.graphqlPreset, + defaultProductSelection: defaultProductSelection.graphqlPreset, +}; + +export const compatPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.compatPreset, + defaultProductSelection: defaultProductSelection.compatPreset, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts index c7d5eed878..8d0aa4d14d 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts @@ -1,7 +1,6 @@ import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, - TProductSelectionDraft, } from '../../../types'; import { restPreset, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts index 68d9b5ae12..735ebfb2b3 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts @@ -1,6 +1,6 @@ -import { LocalizedStringDraft } from '../../../../commons'; import type { TBuilder } from '../../../../../core'; import { TCtpProductSelectionMode } from '../../../../../graphql-types'; +import { LocalizedStringDraft } from '../../../../commons'; import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, @@ -12,9 +12,10 @@ import { ProductSelectionDraft, } from '../../index'; -export const restPreset = (): TBuilder => - ProductSelectionDraftRest.presets - .empty() +function populateBuilder< + TModel extends TProductSelectionDraftRest | TProductSelectionDraftGraphql, +>(builder: TBuilder): TBuilder { + return builder .key('us-medium-customers-catalog') .name( LocalizedStringDraft.presets @@ -31,43 +32,13 @@ export const restPreset = (): TBuilder => ['en-US']('US Medium Customers Catalog') ) .mode(TCtpProductSelectionMode.Individual); +} + +export const restPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftRest.presets.empty()); export const graphqlPreset = (): TBuilder => - ProductSelectionDraftGraphql.presets - .empty() - .key('us-medium-customers-catalog') - .name( - LocalizedStringDraft.presets - .empty() - ['de-DE']('US Mittel Kundenkatalog') - ['it-IT']('Catalogo Clienti Medio USA') - ['nl-NL']('Catalogus voor Middelgrote Klanten in de VS') - ['fr-FR']('Catalogue Clients Moyens des États-Unis') - ['en-AU']('US Medium Customers Catalog') - ['es-ES']('Catálogo de Clientes Medianos de EE. UU.') - ['en-GB']('US Medium Customers Catalog') - ['en-NZ']('US Medium Customers Catalog') - ['pt-PT']('Catálogo de Clientes Médios dos EUA') - ['en-US']('US Medium Customers Catalog') - ) - .mode(TCtpProductSelectionMode.Individual); + populateBuilder(ProductSelectionDraftGraphql.presets.empty()); export const compatPreset = (): TBuilder => - ProductSelectionDraft.presets - .empty() - .key('us-medium-customers-catalog') - .name( - LocalizedStringDraft.presets - .empty() - ['de-DE']('US Mittel Kundenkatalog') - ['it-IT']('Catalogo Clienti Medio USA') - ['nl-NL']('Catalogus voor Middelgrote Klanten in de VS') - ['fr-FR']('Catalogue Clients Moyens des États-Unis') - ['en-AU']('US Medium Customers Catalog') - ['es-ES']('Catálogo de Clientes Medianos de EE. UU.') - ['en-GB']('US Medium Customers Catalog') - ['en-NZ']('US Medium Customers Catalog') - ['pt-PT']('Catálogo de Clientes Médios dos EUA') - ['en-US']('US Medium Customers Catalog') - ) - .mode(TCtpProductSelectionMode.Individual); + populateBuilder(ProductSelectionDraft.presets.empty()); From 1353ea35b11216a78ec8c26cc2f613ff0e6bc69b Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 10:46:14 +0200 Subject: [PATCH 06/20] fix: to fix imports --- standalone/src/models/product-selection/index.ts | 7 ++----- .../presets/sample-data-b2b/default-product-selection.ts | 6 ++++-- .../us-medium-customers-catalog-product-selection.ts | 6 ++++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index 63b7d257f2..064a4d8812 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -5,11 +5,8 @@ import { } from './builders'; import * as modelPresets from './presets'; -export { - ProductSelectionDraftRest, - ProductSelectionDraftGraphql, - ProductSelectionDraft, -} from './product-selection-draft'; +export * from './types'; +export * from './product-selection-draft'; export const ProductSelectionRest = { random: RestModelBuilder, diff --git a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts index 6c4f7b7fd9..655e272a76 100644 --- a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,6 +1,8 @@ import { KeyReferenceDraft } from '../../../../../commons'; -import { ProductSelectionDraft } from '../../../../../product-selection'; -import type { TProductSelectionDraft } from '../../../../../product-selection/types'; +import { + ProductSelectionDraft, + type TProductSelectionDraft, +} from '../../../../../product-selection'; import type { TProductSelectionSettingDraftBuilder } from '../../../types'; import ProductSelectionSettingDraft from '../../builder'; diff --git a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts index ca1ddedca8..f3a37d700f 100644 --- a/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts +++ b/standalone/src/models/store/product-selection-setting/product-selection-setting-draft/presets/sample-data-b2b/us-medium-customers-catalog-product-selection.ts @@ -1,6 +1,8 @@ import { KeyReferenceDraft } from '../../../../../commons'; -import { ProductSelectionDraft } from '../../../../../product-selection'; -import type { TProductSelectionDraft } from '../../../../../product-selection/types'; +import { + ProductSelectionDraft, + type TProductSelectionDraft, +} from '../../../../../product-selection'; import type { TProductSelectionSettingDraftBuilder } from '../../../types'; import ProductSelectionSettingDraft from '../../builder'; From f647280bb444aa2bbddd7967af68c8347abd28fb Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 11:22:30 +0200 Subject: [PATCH 07/20] chore: to refactor second preset --- .../default-product-selection.ts | 49 ++++--------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts index d663b553d9..023a5a47c9 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts @@ -12,9 +12,10 @@ import { ProductSelectionDraft, } from '../../index'; -export const restPreset = (): TBuilder => - ProductSelectionDraftRest.presets - .empty() +function populateBuilder< + TModel extends TProductSelectionDraftRest | TProductSelectionDraftGraphql, +>(builder: TBuilder): TBuilder { + return builder .key('default-product-selection') .name( LocalizedStringDraft.presets @@ -31,43 +32,13 @@ export const restPreset = (): TBuilder => ['en-US']('Default') ) .mode(TCtpProductSelectionMode.Individual); +} + +export const restPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftRest.presets.empty()); export const graphqlPreset = (): TBuilder => - ProductSelectionDraftGraphql.presets - .empty() - .key('default-product-selection') - .name( - LocalizedStringDraft.presets - .empty() - ['de-DE']('Standard') - ['it-IT']('Predefinito') - ['nl-NL']('Standaard') - ['fr-FR']('Défaut') - ['en-AU']('Default') - ['es-ES']('Predeterminado') - ['en-GB']('Default') - ['en-NZ']('Default') - ['pt-PT']('Padrão') - ['en-US']('Default') - ) - .mode(TCtpProductSelectionMode.Individual); + populateBuilder(ProductSelectionDraftGraphql.presets.empty()); export const compatPreset = (): TBuilder => - ProductSelectionDraft.presets - .empty() - .key('default-product-selection') - .name( - LocalizedStringDraft.presets - .empty() - ['de-DE']('Standard') - ['it-IT']('Predefinito') - ['nl-NL']('Standaard') - ['fr-FR']('Défaut') - ['en-AU']('Default') - ['es-ES']('Predeterminado') - ['en-GB']('Default') - ['en-NZ']('Default') - ['pt-PT']('Padrão') - ['en-US']('Default') - ) - .mode(TCtpProductSelectionMode.Individual); + populateBuilder(ProductSelectionDraft.presets.empty()); From 045897009920ea595a555276c71e489f0bae9b52 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 12:15:11 +0200 Subject: [PATCH 08/20] fix: to fix drafts name field --- .../product-selection-draft/builders.spec.ts | 2 +- .../product-selection-draft/fields-config.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts index 95f8dc173d..f59d6be8c3 100644 --- a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts @@ -14,7 +14,7 @@ const validateModel = ( expect(model).toEqual( expect.objectContaining({ key: expect.any(String), - name: expect.any(String), + name: expect.any(Object), mode: expect.any(String), custom: expect.any(Object), }) diff --git a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts index 32992d39e9..d8453363c2 100644 --- a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts +++ b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts @@ -1,4 +1,5 @@ import { fake, TModelFieldsConfig } from '../../../core'; +import { LocalizedString } from '../../commons'; import { TProductSelectionDraftGraphql, TProductSelectionDraftRest, @@ -7,7 +8,7 @@ import { export const restFieldsConfig: TModelFieldsConfig = { fields: { - name: fake((f) => f.lorem.word()), + name: fake(() => LocalizedString.random()), key: fake((f) => f.lorem.slug()), custom: null, mode: 'Individual', @@ -17,7 +18,7 @@ export const restFieldsConfig: TModelFieldsConfig = export const graphqlFieldsConfig: TModelFieldsConfig = { fields: { - name: fake((f) => f.lorem.word()), + name: fake((f) => LocalizedString.random()), key: fake((f) => f.lorem.slug()), custom: null, mode: 'Individual', From b1b8717dd70eed5086f236e3c78f37312c5d31bc Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 12:54:12 +0200 Subject: [PATCH 09/20] chore: to use different validate helpers in the test --- .../models/product-selection/builders.spec.ts | 89 ++++++++++++++++--- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/standalone/src/models/product-selection/builders.spec.ts b/standalone/src/models/product-selection/builders.spec.ts index 773c6cb776..36a3a47acb 100644 --- a/standalone/src/models/product-selection/builders.spec.ts +++ b/standalone/src/models/product-selection/builders.spec.ts @@ -5,20 +5,87 @@ import { ProductSelectionRest, } from './index'; -const validateModel = ( - model: TProductSelectionGraphql | TProductSelectionRest -): void => { +const validateRestModel = (model: TProductSelectionRest): void => { expect(model).toMatchObject({ version: expect.any(Number), id: expect.any(String), createdAt: expect.any(String), lastModifiedAt: expect.any(String), - lastModifiedBy: expect.any(Object), - createdBy: expect.any(Object), + lastModifiedBy: expect.objectContaining({ + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customer: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + obj: expect.objectContaining({ + id: expect.any(String), + }), + }), + }), + createdBy: expect.objectContaining({ + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customer: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + obj: expect.objectContaining({ + id: expect.any(String), + }), + }), + }), key: expect.any(String), productCount: expect.any(Number), mode: expect.any(String), custom: expect.any(Object), + name: expect.objectContaining({ + en: expect.any(String), + }), + }); +}; + +const validateGraphqlModel = (model: TProductSelectionGraphql): void => { + expect(model).toMatchObject({ + version: expect.any(Number), + id: expect.any(String), + createdAt: expect.any(String), + lastModifiedAt: expect.any(String), + lastModifiedBy: expect.objectContaining({ + __typename: 'Initiator', + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customerRef: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + __typename: 'Reference', + }), + }), + createdBy: expect.objectContaining({ + __typename: 'Initiator', + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customerRef: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + __typename: 'Reference', + }), + }), + nameAllLocales: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'LocalizedString', + value: expect.any(String), + locale: expect.any(String), + }), + ]), + name: expect.any(String), + key: expect.any(String), + productCount: expect.any(Number), + mode: expect.any(String), + custom: expect.any(Object), + __typename: 'ProductSelection', }); }; @@ -26,14 +93,13 @@ describe('ProductSelection model builders', () => { it('builds a REST model', () => { const restModel = ProductSelectionRest.random().build(); - validateModel(restModel); + validateRestModel(restModel); }); it('builds a GraphQL model', () => { const graphqlModel = ProductSelectionGraphql.random().build(); - validateModel(graphqlModel); - expect(graphqlModel.__typename).toEqual('ProductSelection'); + validateGraphqlModel(graphqlModel); }); }); @@ -41,20 +107,19 @@ describe('ProductSelection model compatibility builders', () => { it('builds a default (REST) model', () => { const restModel = ProductSelection.random().build(); - validateModel(restModel); + validateRestModel(restModel); }); it('builds a REST model', () => { const restModel = ProductSelection.random().buildRest(); - validateModel(restModel); + validateRestModel(restModel); }); it('builds a GraphQL model', () => { const graphqlModel = ProductSelection.random().buildGraphql(); - validateModel(graphqlModel); - expect(graphqlModel.__typename).toEqual('ProductSelection'); + validateGraphqlModel(graphqlModel); }); }); From 16739258e1086de5dce2c20f7742bb2870a8d773 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 14:12:12 +0200 Subject: [PATCH 10/20] chore: to use constants --- standalone/src/models/product-selection/constants.ts | 4 ++++ standalone/src/models/product-selection/fields-config.ts | 3 ++- standalone/src/models/product-selection/index.ts | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 standalone/src/models/product-selection/constants.ts diff --git a/standalone/src/models/product-selection/constants.ts b/standalone/src/models/product-selection/constants.ts new file mode 100644 index 0000000000..a5f22b7d51 --- /dev/null +++ b/standalone/src/models/product-selection/constants.ts @@ -0,0 +1,4 @@ +export const productSelectionMode = { + Individual: 'Individual', + IndividualExclusion: 'IndividualExclusion', +} as const; diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts index e08dfee293..43b8c69f43 100644 --- a/standalone/src/models/product-selection/fields-config.ts +++ b/standalone/src/models/product-selection/fields-config.ts @@ -5,6 +5,7 @@ import { } from '../../commons'; import { fake, oneOf, sequence, TModelFieldsConfig } from '../../core'; import { createRelatedDates } from '../../utils'; +import { productSelectionMode } from './constants'; import { TProductSelectionRest, TProductSelectionGraphql } from './types'; const [getOlderDate, getNewerDate] = createRelatedDates(); @@ -15,7 +16,7 @@ const commonFieldsConfig = { key: fake((f) => f.lorem.slug(2)), name: fake(() => LocalizedString.random()), productCount: fake((f) => f.number.int()), - mode: oneOf('Individual', 'IndividualExclusion'), + mode: oneOf(...Object.values(productSelectionMode)), custom: null, createdAt: fake(getOlderDate), createdBy: fake(() => ClientLogging.random()), diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index 064a4d8812..28ab17356c 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -7,6 +7,7 @@ import * as modelPresets from './presets'; export * from './types'; export * from './product-selection-draft'; +export * from './constants'; export const ProductSelectionRest = { random: RestModelBuilder, From e5d8f9971ab11a8662a20030bbae9ec098887221 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Wed, 14 May 2025 15:42:32 +0200 Subject: [PATCH 11/20] chore: to use aliases --- standalone/src/models/product-selection/builders.ts | 2 +- standalone/src/models/product-selection/fields-config.ts | 6 +++--- .../product-selection/product-selection-draft/builders.ts | 2 +- .../product-selection-draft/fields-config.ts | 4 ++-- .../product-selection-draft/presets/empty.ts | 2 +- .../presets/sample-data-b2b/default-product-selection.ts | 6 +++--- .../presets/sample-data-b2b/us-medium-customers-catalog.ts | 6 +++--- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/standalone/src/models/product-selection/builders.ts b/standalone/src/models/product-selection/builders.ts index 63cc4b4434..6a97398d82 100644 --- a/standalone/src/models/product-selection/builders.ts +++ b/standalone/src/models/product-selection/builders.ts @@ -2,7 +2,7 @@ import { createCompatibilityBuilder, createSpecializedBuilder, TModelFieldsConfig, -} from '../../core'; +} from '@/core'; import { restFieldsConfig, graphqlFieldsConfig } from './fields-config'; import type { TCreateProductSelectionBuilder, diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts index 43b8c69f43..466a879234 100644 --- a/standalone/src/models/product-selection/fields-config.ts +++ b/standalone/src/models/product-selection/fields-config.ts @@ -1,10 +1,10 @@ +import { fake, oneOf, sequence, TModelFieldsConfig } from '@/core'; import { ClientLogging, LocalizedString, // ReferenceGraphql, -} from '../../commons'; -import { fake, oneOf, sequence, TModelFieldsConfig } from '../../core'; -import { createRelatedDates } from '../../utils'; +} from '@/models/commons'; +import { createRelatedDates } from '@/utils'; import { productSelectionMode } from './constants'; import { TProductSelectionRest, TProductSelectionGraphql } from './types'; diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.ts b/standalone/src/models/product-selection/product-selection-draft/builders.ts index c446edcefa..26c4d21ba8 100644 --- a/standalone/src/models/product-selection/product-selection-draft/builders.ts +++ b/standalone/src/models/product-selection/product-selection-draft/builders.ts @@ -2,7 +2,7 @@ import { createCompatibilityBuilder, createSpecializedBuilder, TModelFieldsConfig, -} from '../../../core'; +} from '@/core'; import type { TCreateProductSelectionBuilder, TProductSelectionDraftGraphql, diff --git a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts index d8453363c2..fc50baf3b8 100644 --- a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts +++ b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts @@ -1,5 +1,5 @@ -import { fake, TModelFieldsConfig } from '../../../core'; -import { LocalizedString } from '../../commons'; +import { fake, TModelFieldsConfig } from '@/core'; +import { LocalizedString } from '@/models/commons'; import { TProductSelectionDraftGraphql, TProductSelectionDraftRest, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts index 9cd684970c..90402a9734 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts @@ -1,4 +1,4 @@ -import type { TBuilder } from '../../../../core'; +import type { TBuilder } from '@/core'; import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts index 023a5a47c9..6460211691 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,6 +1,6 @@ -import type { TBuilder } from '../../../../../core'; -import { TCtpProductSelectionMode } from '../../../../../graphql-types'; -import { LocalizedStringDraft } from '../../../../commons'; +import type { TBuilder } from '@/core'; +import { TCtpProductSelectionMode } from '@/graphql-types'; +import { LocalizedStringDraft } from '@/models/commons'; import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts index 735ebfb2b3..51058c650e 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts @@ -1,6 +1,6 @@ -import type { TBuilder } from '../../../../../core'; -import { TCtpProductSelectionMode } from '../../../../../graphql-types'; -import { LocalizedStringDraft } from '../../../../commons'; +import type { TBuilder } from '@/core'; +import { TCtpProductSelectionMode } from '@/graphql-types'; +import { LocalizedStringDraft } from '@/models/commons'; import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, From c3289e089beb4f77e80556d36bf207b410793950 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 13:14:24 +0200 Subject: [PATCH 12/20] chore: to add product of selection model --- .../models/product-selection/builders.spec.ts | 19 +++++++++++++++ .../models/product-selection/fields-config.ts | 24 ++++++++++--------- .../product-of-selection/builders.spec.ts | 21 ++++++++++++++++ .../product-of-selection/builders.ts | 15 ++++++++++++ .../product-of-selection/fields-config.ts | 18 ++++++++++++++ .../product-of-selection/index.ts | 8 +++++++ .../product-of-selection/presets/index.ts | 1 + .../src/models/product-selection/types.ts | 5 +++- 8 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 standalone/src/models/product-selection/product-of-selection/builders.spec.ts create mode 100644 standalone/src/models/product-selection/product-of-selection/builders.ts create mode 100644 standalone/src/models/product-selection/product-of-selection/fields-config.ts create mode 100644 standalone/src/models/product-selection/product-of-selection/index.ts create mode 100644 standalone/src/models/product-selection/product-of-selection/presets/index.ts diff --git a/standalone/src/models/product-selection/builders.spec.ts b/standalone/src/models/product-selection/builders.spec.ts index 36a3a47acb..d86f92740e 100644 --- a/standalone/src/models/product-selection/builders.spec.ts +++ b/standalone/src/models/product-selection/builders.spec.ts @@ -85,6 +85,25 @@ const validateGraphqlModel = (model: TProductSelectionGraphql): void => { productCount: expect.any(Number), mode: expect.any(String), custom: expect.any(Object), + productRefs: expect.objectContaining({ + __typename: 'SelectionOfProductQueryResult', + count: expect.any(Number), + offset: expect.any(Number), + total: expect.any(Number), + results: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'ProductOfSelection', + product: expect.any(Object), + variantExclusion: expect.any(Object), + variantSelection: expect.any(Object), + productRef: expect.objectContaining({ + __typename: 'Reference', + id: expect.any(String), + typeId: 'product', + }), + }), + ]), + }), __typename: 'ProductSelection', }); }; diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts index 466a879234..4341e638a6 100644 --- a/standalone/src/models/product-selection/fields-config.ts +++ b/standalone/src/models/product-selection/fields-config.ts @@ -1,11 +1,14 @@ -import { fake, oneOf, sequence, TModelFieldsConfig } from '@/core'; import { - ClientLogging, - LocalizedString, - // ReferenceGraphql, -} from '@/models/commons'; + fake, + oneOf, + sequence, + TModelFieldsConfig, + buildGraphqlList, +} from '@/core'; +import { ClientLogging, LocalizedString } from '@/models/commons'; import { createRelatedDates } from '@/utils'; import { productSelectionMode } from './constants'; +import { ProductOfSelectionGraphql } from './product-of-selection'; import { TProductSelectionRest, TProductSelectionGraphql } from './types'; const [getOlderDate, getNewerDate] = createRelatedDates(); @@ -36,20 +39,19 @@ export const graphqlFieldsConfig: TModelFieldsConfig = ...commonFieldsConfig, __typename: 'ProductSelection', nameAllLocales: fake(() => LocalizedString.random()), - productRefs: null, + productRefs: fake(() => + buildGraphqlList([ProductOfSelectionGraphql.random()], { + __typename: 'SelectionOfProductQueryResult', + }) + ), }, postBuild: (model) => { const name = model.nameAllLocales ? LocalizedString.resolveGraphqlDefaultLocaleValue(model.nameAllLocales) : undefined; - // const productRefs = ReferenceGraphql.presets - // .productReference() - // .buildGraphql(); - return { name, - // productRefs, }; }, }; diff --git a/standalone/src/models/product-selection/product-of-selection/builders.spec.ts b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts new file mode 100644 index 0000000000..9d5905e0ea --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts @@ -0,0 +1,21 @@ +import { ProductOfSelectionGraphql } from './index'; + +describe('ProductOfSelection Builder', () => { + it('should build properties for the GraphQL representation', () => { + const graphqlModel = ProductOfSelectionGraphql.random().build(); + + expect(graphqlModel).toEqual( + expect.objectContaining({ + productRef: expect.objectContaining({ + __typename: 'Reference', + typeId: 'product', + id: expect.any(String), + }), + product: expect.any(Object), + variantExclusion: expect.any(Object), + variantSelection: expect.any(Object), + __typename: 'ProductOfSelection', + }) + ); + }); +}); diff --git a/standalone/src/models/product-selection/product-of-selection/builders.ts b/standalone/src/models/product-selection/product-of-selection/builders.ts new file mode 100644 index 0000000000..fd0d633462 --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/builders.ts @@ -0,0 +1,15 @@ +import { createSpecializedBuilder } from '@/core'; +import type { + TCreateProductSelectionBuilder, + TProductOfSelectionGraphql, +} from '../types'; +import { graphqlFieldsConfig } from './fields-config'; + +export const GraphqlModelBuilder: TCreateProductSelectionBuilder< + TProductOfSelectionGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductOfSelectionGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); diff --git a/standalone/src/models/product-selection/product-of-selection/fields-config.ts b/standalone/src/models/product-selection/product-of-selection/fields-config.ts new file mode 100644 index 0000000000..bfe0ca3aa0 --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/fields-config.ts @@ -0,0 +1,18 @@ +import { type TModelFieldsConfig } from '@/core'; +import { ReferenceGraphql } from '@/models/commons'; +import type { TProductOfSelectionGraphql } from '../types'; + +const commonFieldsConfig = { + productRef: ReferenceGraphql.presets.productReference().buildGraphql(), + product: null, + variantSelection: null, + variantExclusion: null, +}; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + ...commonFieldsConfig, + __typename: 'ProductOfSelection', + }, + }; diff --git a/standalone/src/models/product-selection/product-of-selection/index.ts b/standalone/src/models/product-selection/product-of-selection/index.ts new file mode 100644 index 0000000000..4f3d30283a --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/index.ts @@ -0,0 +1,8 @@ +import { GraphqlModelBuilder } from './builders'; +import * as ProductOfSelectionPresets from './presets'; +export * from '../types'; + +export const ProductOfSelectionGraphql = { + random: GraphqlModelBuilder, + presets: ProductOfSelectionPresets.graphqlPresets, +}; diff --git a/standalone/src/models/product-selection/product-of-selection/presets/index.ts b/standalone/src/models/product-selection/product-of-selection/presets/index.ts new file mode 100644 index 0000000000..16cb57eefa --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/presets/index.ts @@ -0,0 +1 @@ +export const graphqlPresets = {}; diff --git a/standalone/src/models/product-selection/types.ts b/standalone/src/models/product-selection/types.ts index 7a6c3f6654..23b3b8105b 100644 --- a/standalone/src/models/product-selection/types.ts +++ b/standalone/src/models/product-selection/types.ts @@ -6,6 +6,7 @@ import type { TBuilder } from '@/core'; import { TCtpProductSelection, TCtpCreateProductSelectionDraft, + TCtpProductOfSelection, } from '@/graphql-types'; /** @@ -24,6 +25,7 @@ export type TProductSelectionRest = ProductSelection; // GraphQL types export type TProductSelectionDraftGraphql = TCtpCreateProductSelectionDraft; export type TProductSelectionGraphql = TCtpProductSelection; +export type TProductOfSelectionGraphql = TCtpProductOfSelection; // Builders types export type TCreateProductSelectionBuilder< @@ -31,5 +33,6 @@ export type TCreateProductSelectionBuilder< | TProductSelectionRest | TProductSelectionGraphql | TProductSelectionDraftRest - | TProductSelectionDraftGraphql, + | TProductSelectionDraftGraphql + | TProductOfSelectionGraphql, > = () => TBuilder; From b66650e4bee45fb81bbbd4661ae5bfd5108e95b8 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 13:28:28 +0200 Subject: [PATCH 13/20] chore: to fix the test --- .../product-selection-setting/builder.spec.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/standalone/src/models/store/product-selection-setting/builder.spec.ts b/standalone/src/models/store/product-selection-setting/builder.spec.ts index e227f86fc0..0d8a199544 100644 --- a/standalone/src/models/store/product-selection-setting/builder.spec.ts +++ b/standalone/src/models/store/product-selection-setting/builder.spec.ts @@ -75,7 +75,25 @@ describe('builder', () => { }), ]), productCount: expect.any(Number), - productRefs: null, + productRefs: expect.objectContaining({ + __typename: 'SelectionOfProductQueryResult', + count: expect.any(Number), + offset: expect.any(Number), + total: expect.any(Number), + results: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'ProductOfSelection', + product: expect.any(Object), + variantExclusion: expect.any(Object), + variantSelection: expect.any(Object), + productRef: expect.objectContaining({ + __typename: 'Reference', + id: expect.any(String), + typeId: 'product', + }), + }), + ]), + }), mode: expect.any(String), custom: null, createdAt: expect.any(String), From ee19e182d3b60f80e4b78731d8bd8d83b4cbefab Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 13:45:25 +0200 Subject: [PATCH 14/20] chore: to export product of selection from the index file --- standalone/src/models/product-selection/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index 28ab17356c..fb3e77cb06 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -7,6 +7,8 @@ import * as modelPresets from './presets'; export * from './types'; export * from './product-selection-draft'; +export * from './product-of-selection'; + export * from './constants'; export const ProductSelectionRest = { From 447a6eeb8f20a7fb295249d4e868857e10ea592a Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 14:51:41 +0200 Subject: [PATCH 15/20] Create spicy-numbers-deny.md --- .changeset/spicy-numbers-deny.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .changeset/spicy-numbers-deny.md diff --git a/.changeset/spicy-numbers-deny.md b/.changeset/spicy-numbers-deny.md new file mode 100644 index 0000000000..024b12ceed --- /dev/null +++ b/.changeset/spicy-numbers-deny.md @@ -0,0 +1,20 @@ +--- +"@commercetools/composable-commerce-test-data": patch +--- + +Added new submodel to the ProductSelection model: + +- `ProductOfSelection` + +You can use it like this: + +```ts +import { + ProductOfSelectionGraphql, +} from '@commercetools/composable-commerce-test-data/product-selection'; + +const productOfSelectionModel = ProductOfSelectionGraphql.random().build(); +``` + +This submodel is now used to populate the required `productRefs` field of the ProductSelection GraphQL model, which was previously `null` by default. + From 04e04a81b3bb5434893eda2801f2d12676949fc3 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 17:22:50 +0200 Subject: [PATCH 16/20] chore: to address review comments --- .../models/product-selection/builders.spec.ts | 29 ------------------- .../src/models/product-selection/index.ts | 2 ++ .../product-of-selection/builders.ts | 8 ++--- .../product-of-selection/fields-config.ts | 14 ++++----- .../product-of-selection/index.ts | 1 + .../product-of-selection/types.ts | 8 +++++ .../src/models/product-selection/types.ts | 5 +--- 7 files changed, 21 insertions(+), 46 deletions(-) create mode 100644 standalone/src/models/product-selection/product-of-selection/types.ts diff --git a/standalone/src/models/product-selection/builders.spec.ts b/standalone/src/models/product-selection/builders.spec.ts index d86f92740e..6f3d4054d3 100644 --- a/standalone/src/models/product-selection/builders.spec.ts +++ b/standalone/src/models/product-selection/builders.spec.ts @@ -53,31 +53,13 @@ const validateGraphqlModel = (model: TProductSelectionGraphql): void => { lastModifiedAt: expect.any(String), lastModifiedBy: expect.objectContaining({ __typename: 'Initiator', - externalUserId: expect.any(String), - anonymousId: expect.any(String), - clientId: expect.any(String), - customerRef: expect.objectContaining({ - id: expect.any(String), - typeId: 'customer', - __typename: 'Reference', - }), }), createdBy: expect.objectContaining({ __typename: 'Initiator', - externalUserId: expect.any(String), - anonymousId: expect.any(String), - clientId: expect.any(String), - customerRef: expect.objectContaining({ - id: expect.any(String), - typeId: 'customer', - __typename: 'Reference', - }), }), nameAllLocales: expect.arrayContaining([ expect.objectContaining({ __typename: 'LocalizedString', - value: expect.any(String), - locale: expect.any(String), }), ]), name: expect.any(String), @@ -87,20 +69,9 @@ const validateGraphqlModel = (model: TProductSelectionGraphql): void => { custom: expect.any(Object), productRefs: expect.objectContaining({ __typename: 'SelectionOfProductQueryResult', - count: expect.any(Number), - offset: expect.any(Number), - total: expect.any(Number), results: expect.arrayContaining([ expect.objectContaining({ __typename: 'ProductOfSelection', - product: expect.any(Object), - variantExclusion: expect.any(Object), - variantSelection: expect.any(Object), - productRef: expect.objectContaining({ - __typename: 'Reference', - id: expect.any(String), - typeId: 'product', - }), }), ]), }), diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index fb3e77cb06..7770c3c74e 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -6,6 +6,8 @@ import { import * as modelPresets from './presets'; export * from './types'; +export * from './product-of-selection/types'; + export * from './product-selection-draft'; export * from './product-of-selection'; diff --git a/standalone/src/models/product-selection/product-of-selection/builders.ts b/standalone/src/models/product-selection/product-of-selection/builders.ts index fd0d633462..14191d72db 100644 --- a/standalone/src/models/product-selection/product-of-selection/builders.ts +++ b/standalone/src/models/product-selection/product-of-selection/builders.ts @@ -1,11 +1,11 @@ import { createSpecializedBuilder } from '@/core'; +import { graphqlFieldsConfig } from './fields-config'; import type { - TCreateProductSelectionBuilder, + TCreateProductOfSelectionBuilder, TProductOfSelectionGraphql, -} from '../types'; -import { graphqlFieldsConfig } from './fields-config'; +} from './types'; -export const GraphqlModelBuilder: TCreateProductSelectionBuilder< +export const GraphqlModelBuilder: TCreateProductOfSelectionBuilder< TProductOfSelectionGraphql > = () => createSpecializedBuilder({ diff --git a/standalone/src/models/product-selection/product-of-selection/fields-config.ts b/standalone/src/models/product-selection/product-of-selection/fields-config.ts index bfe0ca3aa0..8f24cced14 100644 --- a/standalone/src/models/product-selection/product-of-selection/fields-config.ts +++ b/standalone/src/models/product-selection/product-of-selection/fields-config.ts @@ -1,18 +1,14 @@ import { type TModelFieldsConfig } from '@/core'; import { ReferenceGraphql } from '@/models/commons'; -import type { TProductOfSelectionGraphql } from '../types'; - -const commonFieldsConfig = { - productRef: ReferenceGraphql.presets.productReference().buildGraphql(), - product: null, - variantSelection: null, - variantExclusion: null, -}; +import type { TProductOfSelectionGraphql } from './types'; export const graphqlFieldsConfig: TModelFieldsConfig = { fields: { - ...commonFieldsConfig, + productRef: ReferenceGraphql.presets.productReference().buildGraphql(), + product: null, + variantSelection: null, + variantExclusion: null, __typename: 'ProductOfSelection', }, }; diff --git a/standalone/src/models/product-selection/product-of-selection/index.ts b/standalone/src/models/product-selection/product-of-selection/index.ts index 4f3d30283a..cae0055cd2 100644 --- a/standalone/src/models/product-selection/product-of-selection/index.ts +++ b/standalone/src/models/product-selection/product-of-selection/index.ts @@ -2,6 +2,7 @@ import { GraphqlModelBuilder } from './builders'; import * as ProductOfSelectionPresets from './presets'; export * from '../types'; +// We only export the GraphQL model for product-of-selection, as the REST model is not used in the current implementation. export const ProductOfSelectionGraphql = { random: GraphqlModelBuilder, presets: ProductOfSelectionPresets.graphqlPresets, diff --git a/standalone/src/models/product-selection/product-of-selection/types.ts b/standalone/src/models/product-selection/product-of-selection/types.ts new file mode 100644 index 0000000000..508ad049bb --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/types.ts @@ -0,0 +1,8 @@ +import type { TBuilder } from '@/core'; +import { TCtpProductOfSelection } from '@/graphql-types'; + +export type TProductOfSelectionGraphql = TCtpProductOfSelection; + +export type TCreateProductOfSelectionBuilder< + TProductOfSelectionModel extends TCtpProductOfSelection, +> = () => TBuilder; diff --git a/standalone/src/models/product-selection/types.ts b/standalone/src/models/product-selection/types.ts index 23b3b8105b..7a6c3f6654 100644 --- a/standalone/src/models/product-selection/types.ts +++ b/standalone/src/models/product-selection/types.ts @@ -6,7 +6,6 @@ import type { TBuilder } from '@/core'; import { TCtpProductSelection, TCtpCreateProductSelectionDraft, - TCtpProductOfSelection, } from '@/graphql-types'; /** @@ -25,7 +24,6 @@ export type TProductSelectionRest = ProductSelection; // GraphQL types export type TProductSelectionDraftGraphql = TCtpCreateProductSelectionDraft; export type TProductSelectionGraphql = TCtpProductSelection; -export type TProductOfSelectionGraphql = TCtpProductOfSelection; // Builders types export type TCreateProductSelectionBuilder< @@ -33,6 +31,5 @@ export type TCreateProductSelectionBuilder< | TProductSelectionRest | TProductSelectionGraphql | TProductSelectionDraftRest - | TProductSelectionDraftGraphql - | TProductOfSelectionGraphql, + | TProductSelectionDraftGraphql, > = () => TBuilder; From 91b46f5102a336b18e87926d085c84948759402a Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Thu, 15 May 2025 17:35:58 +0200 Subject: [PATCH 17/20] chore: to remove extra fields from the test --- .../store/product-selection-setting/builder.spec.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/standalone/src/models/store/product-selection-setting/builder.spec.ts b/standalone/src/models/store/product-selection-setting/builder.spec.ts index 0d8a199544..298b4c425b 100644 --- a/standalone/src/models/store/product-selection-setting/builder.spec.ts +++ b/standalone/src/models/store/product-selection-setting/builder.spec.ts @@ -77,20 +77,9 @@ describe('builder', () => { productCount: expect.any(Number), productRefs: expect.objectContaining({ __typename: 'SelectionOfProductQueryResult', - count: expect.any(Number), - offset: expect.any(Number), - total: expect.any(Number), results: expect.arrayContaining([ expect.objectContaining({ __typename: 'ProductOfSelection', - product: expect.any(Object), - variantExclusion: expect.any(Object), - variantSelection: expect.any(Object), - productRef: expect.objectContaining({ - __typename: 'Reference', - id: expect.any(String), - typeId: 'product', - }), }), ]), }), From af1c82eeec1326333269592be61239078ce2fb10 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Fri, 16 May 2025 11:35:37 +0200 Subject: [PATCH 18/20] chore: to address the review comments --- .changeset/spicy-numbers-deny.md | 8 +++----- .../product-of-selection/builders.spec.ts | 2 -- .../product-selection/product-of-selection/index.ts | 4 ++-- .../product-selection-draft/builders.spec.ts | 4 ++-- .../product-selection-draft/presets/empty.spec.ts | 1 - 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/.changeset/spicy-numbers-deny.md b/.changeset/spicy-numbers-deny.md index 024b12ceed..6207c067dc 100644 --- a/.changeset/spicy-numbers-deny.md +++ b/.changeset/spicy-numbers-deny.md @@ -1,7 +1,8 @@ --- -"@commercetools/composable-commerce-test-data": patch +'@commercetools/composable-commerce-test-data': patch --- +ProductSelection is migrated to the new model. Added new submodel to the ProductSelection model: - `ProductOfSelection` @@ -9,12 +10,9 @@ Added new submodel to the ProductSelection model: You can use it like this: ```ts -import { - ProductOfSelectionGraphql, -} from '@commercetools/composable-commerce-test-data/product-selection'; +import { ProductOfSelectionGraphql } from '@commercetools/composable-commerce-test-data/product-selection'; const productOfSelectionModel = ProductOfSelectionGraphql.random().build(); ``` This submodel is now used to populate the required `productRefs` field of the ProductSelection GraphQL model, which was previously `null` by default. - diff --git a/standalone/src/models/product-selection/product-of-selection/builders.spec.ts b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts index 9d5905e0ea..810754f2ca 100644 --- a/standalone/src/models/product-selection/product-of-selection/builders.spec.ts +++ b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts @@ -8,8 +8,6 @@ describe('ProductOfSelection Builder', () => { expect.objectContaining({ productRef: expect.objectContaining({ __typename: 'Reference', - typeId: 'product', - id: expect.any(String), }), product: expect.any(Object), variantExclusion: expect.any(Object), diff --git a/standalone/src/models/product-selection/product-of-selection/index.ts b/standalone/src/models/product-selection/product-of-selection/index.ts index cae0055cd2..c163b7109d 100644 --- a/standalone/src/models/product-selection/product-of-selection/index.ts +++ b/standalone/src/models/product-selection/product-of-selection/index.ts @@ -1,8 +1,8 @@ import { GraphqlModelBuilder } from './builders'; import * as ProductOfSelectionPresets from './presets'; -export * from '../types'; +export * from './types'; -// We only export the GraphQL model for product-of-selection, as the REST model is not used in the current implementation. +// This model only exists in the GrahpQL API and that's why there's no REST version of it. export const ProductOfSelectionGraphql = { random: GraphqlModelBuilder, presets: ProductOfSelectionPresets.graphqlPresets, diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts index f59d6be8c3..e328438d42 100644 --- a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts @@ -21,7 +21,7 @@ const validateModel = ( ); }; -describe('ProductSelection model builders', () => { +describe('ProductSelectionDraft model builders', () => { it('builds a REST model', () => { const restModel = ProductSelectionDraftRest.random().build(); @@ -34,7 +34,7 @@ describe('ProductSelection model builders', () => { }); }); -describe('ProductSelection model compatibility builders', () => { +describe('ProductSelectionDraft model compatibility builders', () => { it('builds a default (REST) model', () => { const restModel = ProductSelectionDraft.random().build(); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts index 67d466c211..7bf0e60f72 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts @@ -1,7 +1,6 @@ import type { TProductSelectionDraftGraphql, TProductSelectionDraftRest, - TProductSelectionDraft, } from '../../types'; import { restPreset, graphqlPreset, compatPreset } from './empty'; From e77e39b53871d8bb3a0fc79f007c1b248da746cc Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Fri, 16 May 2025 11:47:22 +0200 Subject: [PATCH 19/20] chore: to use constants --- .../product-selection-draft/fields-config.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts index fc50baf3b8..75bb76fcfb 100644 --- a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts +++ b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts @@ -1,5 +1,6 @@ import { fake, TModelFieldsConfig } from '@/core'; import { LocalizedString } from '@/models/commons'; +import { productSelectionMode } from '../constants'; import { TProductSelectionDraftGraphql, TProductSelectionDraftRest, @@ -11,7 +12,7 @@ export const restFieldsConfig: TModelFieldsConfig = name: fake(() => LocalizedString.random()), key: fake((f) => f.lorem.slug()), custom: null, - mode: 'Individual', + mode: productSelectionMode.Individual, }, }; @@ -21,6 +22,6 @@ export const graphqlFieldsConfig: TModelFieldsConfig LocalizedString.random()), key: fake((f) => f.lorem.slug()), custom: null, - mode: 'Individual', + mode: productSelectionMode.Individual, }, }; From 5fc0861949776056756ea792b1e7a12dae297862 Mon Sep 17 00:00:00 2001 From: Roman Beloborodov Date: Fri, 16 May 2025 13:33:52 +0200 Subject: [PATCH 20/20] Update .changeset/spicy-numbers-deny.md Co-authored-by: Carlos Cortizas <97907068+CarlosCortizasCT@users.noreply.github.com> --- .changeset/spicy-numbers-deny.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/spicy-numbers-deny.md b/.changeset/spicy-numbers-deny.md index 6207c067dc..0300d9d006 100644 --- a/.changeset/spicy-numbers-deny.md +++ b/.changeset/spicy-numbers-deny.md @@ -2,7 +2,7 @@ '@commercetools/composable-commerce-test-data': patch --- -ProductSelection is migrated to the new model. +The `ProductSelection` and `ProductSelectionDraft` models have been refactored to use the new implementation patterns but that does not affect consumers.. Added new submodel to the ProductSelection model: - `ProductOfSelection`