-
Notifications
You must be signed in to change notification settings - Fork 2
FEC-237: to migrate product selection to new model and add product of selection submodel #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4b250e9
f9e5176
3ace724
a156df4
7adf4ff
1353ea3
f647280
0458970
b1b8717
1673925
e5d8f99
c3289e0
b66650e
ee19e18
447a6ee
04e04a8
91b46f5
af1c82e
e77e39b
5fc0861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| '@commercetools/composable-commerce-test-data': patch | ||
| --- | ||
|
|
||
| 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: | ||
|
CarlosCortizasCT marked this conversation as resolved.
|
||
|
|
||
| - `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. | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { TProductSelectionGraphql, TProductSelectionRest } from './types'; | ||
| import { | ||
| ProductSelection, | ||
|
rajrdk marked this conversation as resolved.
|
||
| ProductSelectionGraphql, | ||
| ProductSelectionRest, | ||
| } from './index'; | ||
|
|
||
| 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.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({ | ||
|
Rombelirk marked this conversation as resolved.
|
||
| 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', | ||
| }), | ||
| createdBy: expect.objectContaining({ | ||
| __typename: 'Initiator', | ||
| }), | ||
| nameAllLocales: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| __typename: 'LocalizedString', | ||
| }), | ||
| ]), | ||
| name: expect.any(String), | ||
|
Rombelirk marked this conversation as resolved.
|
||
| key: expect.any(String), | ||
| productCount: expect.any(Number), | ||
| mode: expect.any(String), | ||
| custom: expect.any(Object), | ||
| productRefs: expect.objectContaining({ | ||
| __typename: 'SelectionOfProductQueryResult', | ||
| results: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| __typename: 'ProductOfSelection', | ||
| }), | ||
| ]), | ||
| }), | ||
| __typename: 'ProductSelection', | ||
| }); | ||
| }; | ||
|
|
||
| describe('ProductSelection model builders', () => { | ||
| it('builds a REST model', () => { | ||
| const restModel = ProductSelectionRest.random().build(); | ||
|
|
||
| validateRestModel(restModel); | ||
| }); | ||
|
|
||
| it('builds a GraphQL model', () => { | ||
| const graphqlModel = ProductSelectionGraphql.random().build(); | ||
|
|
||
| validateGraphqlModel(graphqlModel); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ProductSelection model compatibility builders', () => { | ||
| it('builds a default (REST) model', () => { | ||
| const restModel = ProductSelection.random().build(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in this case. |
||
|
|
||
| validateRestModel(restModel); | ||
| }); | ||
|
|
||
| it('builds a REST model', () => { | ||
| const restModel = ProductSelection.random().buildRest(); | ||
|
|
||
| validateRestModel(restModel); | ||
| }); | ||
|
|
||
| it('builds a GraphQL model', () => { | ||
| const graphqlModel = | ||
| ProductSelection.random().buildGraphql<TProductSelectionGraphql>(); | ||
|
|
||
| validateGraphqlModel(graphqlModel); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<TProductSelectionModel>({ | ||
| name: 'ProductSelectionCompatBuilder', | ||
|
|
||
| modelFieldsConfig: { | ||
| rest: restFieldsConfig as TModelFieldsConfig<TProductSelectionModel>, | ||
| graphql: | ||
| graphqlFieldsConfig as TModelFieldsConfig<TProductSelectionModel>, | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const productSelectionMode = { | ||
| Individual: 'Individual', | ||
| IndividualExclusion: 'IndividualExclusion', | ||
| } as const; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { | ||
| 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(); | ||
|
|
||
| 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(...Object.values(productSelectionMode)), | ||
| custom: null, | ||
| createdAt: fake(getOlderDate), | ||
| createdBy: fake(() => ClientLogging.random()), | ||
| lastModifiedAt: fake(getNewerDate), | ||
| lastModifiedBy: fake(() => ClientLogging.random()), | ||
| }; | ||
|
|
||
| export const restFieldsConfig: TModelFieldsConfig<TProductSelectionRest> = { | ||
| fields: { | ||
| ...commonFieldsConfig, | ||
| }, | ||
| }; | ||
|
|
||
| export const graphqlFieldsConfig: TModelFieldsConfig<TProductSelectionGraphql> = | ||
| { | ||
| fields: { | ||
| ...commonFieldsConfig, | ||
| __typename: 'ProductSelection', | ||
| nameAllLocales: fake(() => LocalizedString.random()), | ||
| productRefs: fake(() => | ||
| buildGraphqlList([ProductOfSelectionGraphql.random()], { | ||
| __typename: 'SelectionOfProductQueryResult', | ||
| }) | ||
| ), | ||
| }, | ||
| postBuild: (model) => { | ||
| const name = model.nameAllLocales | ||
| ? LocalizedString.resolveGraphqlDefaultLocaleValue(model.nameAllLocales) | ||
| : undefined; | ||
|
|
||
| return { | ||
| name, | ||
| }; | ||
| }, | ||
| }; |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.