diff --git a/CHANGELOG.md b/CHANGELOG.md index 72187c70..76a92ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add initial admin development +## [0.20.0] - 2022-01-17 + +### Updated + +- setAffiliateOnOrderForm mutation to respond with the mutated orderform +- AffiliateValidator component to update OrderFormContext with customData after mutation + ## [0.19.0] - 2022-01-14 ### Added diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 9726e8cc..f756284e 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -4,5 +4,5 @@ type Query { } type Mutation { - setAffiliateOnOrderForm(affiliateId: String, orderFormId: String): Boolean + setAffiliateOnOrderForm(affiliateId: String, orderFormId: String): OrderForm } diff --git a/graphql/types/OrderForm.graphql b/graphql/types/OrderForm.graphql new file mode 100644 index 00000000..5531fc73 --- /dev/null +++ b/graphql/types/OrderForm.graphql @@ -0,0 +1,15 @@ +type OrderForm { + customData: CustomData +} + +type CustomData { + customApps: [CustomApp!]! +} + +scalar CustomFieldsObject + +type CustomApp { + id: ID! + major: Int! + fields: CustomFieldsObject! +} diff --git a/manifest.json b/manifest.json index 31594340..20969929 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "affiliates", "vendor": "vtex", - "version": "0.19.0", + "version": "0.20.0", "title": "Affiliates", "description": "App that adds support for affiliates on IO stores", "mustUpdateAt": "2018-01-04", diff --git a/node/__tests__/unit/resolvers/setAffiliateOnOrderForm.spec.ts b/node/__tests__/unit/resolvers/setAffiliateOnOrderForm.spec.ts index 7d4a4130..83d75ff8 100644 --- a/node/__tests__/unit/resolvers/setAffiliateOnOrderForm.spec.ts +++ b/node/__tests__/unit/resolvers/setAffiliateOnOrderForm.spec.ts @@ -96,7 +96,9 @@ describe('setAffiliateOnOrderForm resolver', () => { checkout: { setSingleCustomData: jest .fn() - .mockRejectedValueOnce(new Error('error')), + .mockRejectedValueOnce( + new Error('Error setting affiliate on order form') + ), }, }, vtex: { diff --git a/node/package.json b/node/package.json index 82f27788..de9d75b1 100644 --- a/node/package.json +++ b/node/package.json @@ -30,5 +30,5 @@ "lint": "tsc --noEmit --pretty", "test": "vtex-test-tools test --collectCoverage" }, - "version": "0.19.0" + "version": "0.20.0" } diff --git a/node/resolvers/setAffiliateOnOrderForm.ts b/node/resolvers/setAffiliateOnOrderForm.ts index 672d0dd9..b19f7777 100644 --- a/node/resolvers/setAffiliateOnOrderForm.ts +++ b/node/resolvers/setAffiliateOnOrderForm.ts @@ -22,7 +22,7 @@ export const setAffiliateOnOrderForm = async ( } try { - await checkout.setSingleCustomData( + return checkout.setSingleCustomData( orderFormId, { appId: APP_CUSTOM_DATA.id, @@ -31,8 +31,6 @@ export const setAffiliateOnOrderForm = async ( }, 'AUTH_TOKEN' ) - - return true } catch (err) { logger.error({ metric: 'set-affiliate-on-orderform', diff --git a/react/AffiliateValidator.tsx b/react/AffiliateValidator.tsx index 3f405a8e..5a7ab9a0 100644 --- a/react/AffiliateValidator.tsx +++ b/react/AffiliateValidator.tsx @@ -21,6 +21,7 @@ type Props = { const AffiliateValidator: FC = ({ Invalid, Valid }) => { const { orderForm: { id: orderFormId }, + setOrderForm, } = useOrderForm() const slug = useMemo(() => { @@ -28,7 +29,9 @@ const AffiliateValidator: FC = ({ Invalid, Valid }) => { }, []) const [setAffiliateOnOrderForm, { called: mutationHasBeenCalled }] = - useMutation(SET_ON_ORDER_FORM_MUTATION) + useMutation(SET_ON_ORDER_FORM_MUTATION, { + onCompleted: (data) => setOrderForm(data.setAffiliateOnOrderForm), + }) const { data, error } = useQuery( IS_AFFILIATE_VALID_QUERY, diff --git a/react/__mocks__/setOnOrderFormMutation.tsx b/react/__mocks__/setOnOrderFormMutation.tsx index 7f0339cf..72495191 100644 --- a/react/__mocks__/setOnOrderFormMutation.tsx +++ b/react/__mocks__/setOnOrderFormMutation.tsx @@ -11,7 +11,20 @@ export const mocks = [ }, result: jest.fn(() => ({ data: { - setAffiliateOnOrderForm: true, + setAffiliateOnOrderForm: { + id: 'mockedOrderFormId', + customData: { + customApps: [ + { + fields: { + affiliateId: 'validId', + }, + id: 'mockedAppId', + major: 1, + }, + ], + }, + }, }, })), }, diff --git a/react/__mocks__/vtex.order-manager/OrderForm.ts b/react/__mocks__/vtex.order-manager/OrderForm.ts index 2a1eba56..101c5c0f 100644 --- a/react/__mocks__/vtex.order-manager/OrderForm.ts +++ b/react/__mocks__/vtex.order-manager/OrderForm.ts @@ -1,5 +1,8 @@ +export const setOrderFormSpy = jest.fn() + export const useOrderForm = () => { return { orderForm: { id: 'mockedOrderFormId' }, + setOrderForm: setOrderFormSpy, } } diff --git a/react/__tests__/AffiliateValidator.spec.tsx b/react/__tests__/AffiliateValidator.spec.tsx index cf3fa9e4..f312edf8 100644 --- a/react/__tests__/AffiliateValidator.spec.tsx +++ b/react/__tests__/AffiliateValidator.spec.tsx @@ -5,6 +5,7 @@ import { MockedProvider, wait } from '@apollo/react-testing' import { mocks as queryMocks } from '../__mocks__/isAffiliateValidQuery' import { mocks as mutationMocks } from '../__mocks__/setOnOrderFormMutation' +import { setOrderFormSpy } from '../__mocks__/vtex.order-manager/OrderForm' import AffiliateValidator from '../AffiliateValidator' describe('Affiliate Validator', () => { @@ -64,4 +65,13 @@ describe('Affiliate Validator', () => { expect(mutationSpy).toHaveBeenCalled() }) + + it('Should update OrderFormContext after sucessfully calling the mutation', async () => { + await renderTest('/validId') + + const mutationMockedResult = + mutationMocks[0].result().data.setAffiliateOnOrderForm + + expect(setOrderFormSpy).toHaveBeenCalledWith(mutationMockedResult) + }) }) diff --git a/react/graphql/setAffiliateOnOrderForm.graphql b/react/graphql/setAffiliateOnOrderForm.graphql index b08c84e7..83446a68 100644 --- a/react/graphql/setAffiliateOnOrderForm.graphql +++ b/react/graphql/setAffiliateOnOrderForm.graphql @@ -1,3 +1,14 @@ mutation setAffiliateOnOrderForm($affiliateId: String, $orderFormId: String) { - setAffiliateOnOrderForm(affiliateId: $affiliateId, orderFormId: $orderFormId) + setAffiliateOnOrderForm( + affiliateId: $affiliateId + orderFormId: $orderFormId + ) { + customData { + customApps { + fields + id + major + } + } + } } diff --git a/react/package.json b/react/package.json index 2b2c3eee..f2bd519c 100644 --- a/react/package.json +++ b/react/package.json @@ -1,8 +1,8 @@ { "name": "vtex.affiliates", - "version": "0.19.0", + "version": "0.20.0", "scripts": { - "test": "vtex-test-tools test" + "test": "vtex-test-tools test --collectCoverage" }, "dependencies": { "@vtex/admin-ui": "^0.117.1-next.1",