Skip to content

Commit

Permalink
Merge pull request #36 from vtex/feature/Add-create-affiliate-mutation
Browse files Browse the repository at this point in the history
Feature/add create affiliate mutation
  • Loading branch information
gabrielHosino authored Feb 10, 2022
2 parents c996a51 + 0d642cc commit dbd2e72
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- addAffiliate mutation

## [0.28.0] - 2022-02-08

### Added
Expand Down
1 change: 1 addition & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ type Query {
}

type Mutation {
addAffiliate(newAffiliate: NewAffiliateInput!): Affiliate!
setAffiliateOnOrderForm(affiliateId: String, orderFormId: String): OrderForm
}
31 changes: 31 additions & 0 deletions graphql/types/affiliates.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@ input AffliatesSortingInput {
order: SortingOrder!
}

input AddresInput {
city: String
country: String
neighborhood: String
number: String
postalCode: String
reference: String
street: String
state: String
}

input MarketingInput {
instagram: String
facebook: String
whatsapp: String
gtmId: String
}

input NewAffiliateInput {
slug: String!
name: String!
email: String!
isApproved: Boolean!
storeName: String
address: AddresInput
document: String
documentType: String
phone: String
marketing: MarketingInput
}

type Affiliate {
id: ID
name: String
Expand Down
8 changes: 7 additions & 1 deletion masterdata/affiliates/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"phone": {
"type": "string"
},
"refId": {
"type": "string"
},
"address": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -56,7 +59,7 @@
"isApproved": {
"type": "boolean"
},
"social": {
"marketing": {
"type": "object",
"properties": {
"instagram": {
Expand All @@ -67,6 +70,9 @@
},
"facebook": {
"type": "string"
},
"gtmId": {
"type": "string"
}
}
}
Expand Down
99 changes: 99 additions & 0 deletions node/__tests__/unit/resolvers/addAffiliate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { addAffiliate } from '../../../resolvers/addAffiliate'

describe('addAffiliate mutation', () => {
it('Should return error if slug is invalid', () => {
const newAffiliateParams = {
newAffiliate: {
slug: 'Invalid Slug',
},
}

const mockCtx = {
clients: {
affiliates: {
get: jest.fn(),
search: jest.fn(),
},
},
} as unknown as Context

return expect(
addAffiliate(null, newAffiliateParams, mockCtx)
).rejects.toThrow('Slug is not valid, must be alphanumeric')
})

it('Should return error if slug is already in use', () => {
const newAffiliateParams = {
newAffiliate: {
slug: 'validSlug',
},
}

const mockCtx = {
clients: {
affiliates: {
get: jest.fn().mockResolvedValueOnce({ id: 'validSlug' }),
search: jest.fn(),
},
},
} as unknown as Context

return expect(
addAffiliate(null, newAffiliateParams, mockCtx)
).rejects.toThrow('Affiliate already exists(slug is already in use)')
})

it('Should return error if email is already in use', () => {
const newAffiliateParams = {
newAffiliate: {
slug: 'validSlug',
email: '[email protected]',
},
}

const mockCtx = {
clients: {
affiliates: {
get: jest.fn().mockResolvedValueOnce(null),
search: jest.fn().mockResolvedValueOnce([{ id: 'validSlug' }]),
},
},
} as unknown as Context

return expect(
addAffiliate(null, newAffiliateParams, mockCtx)
).rejects.toThrow('Affiliate already exists(email is already in use)')
})

it('Should add a new affiliate if no errors were found', () => {
const newAffiliateParams = {
newAffiliate: {
slug: 'validSlug',
email: '[email protected]',
name: 'affiliate name',
isApproved: true,
},
}

const mdFields = {
id: 'validSlug',
email: '[email protected]',
name: 'affiliate name',
isApproved: true,
}

const mockCtx = {
clients: {
affiliates: {
get: jest.fn().mockResolvedValueOnce(null),
search: jest.fn().mockResolvedValueOnce([]),
save: jest.fn(),
},
},
} as unknown as Context

return addAffiliate(null, newAffiliateParams, mockCtx).then(() => {
expect(mockCtx.clients.affiliates.save).toHaveBeenCalledWith(mdFields)
})
})
})
2 changes: 2 additions & 0 deletions node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { getAffiliateLead } from './middlewares/getAffiliateLead'
import { verifyUserAffiliation } from './middlewares/verifyUserAffiliation'
import { authenticateRequest } from './middlewares/authenticateRequest'
import { getAffiliates } from './resolvers/getAffiliates'
import { addAffiliate } from './resolvers/addAffiliate'

const TIMEOUT_MS = 1000

Expand Down Expand Up @@ -105,6 +106,7 @@ export default new Service({
getAffiliates,
},
Mutation: {
addAffiliate,
setAffiliateOnOrderForm,
},
},
Expand Down
42 changes: 42 additions & 0 deletions node/resolvers/addAffiliate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Affiliates, MutationAddAffiliateArgs } from 'vtex.affiliates'

import { isSlugValid } from '../utils/shared'

export const addAffiliate = async (
_: unknown,
{ newAffiliate }: MutationAddAffiliateArgs,
{ clients: { affiliates } }: Context
) => {
const { slug, email } = newAffiliate

if (!isSlugValid(slug)) {
throw new Error('Slug is not valid, must be alphanumeric')
}

const affiliateInDbById = await affiliates.get(slug, ['_all'])

if (affiliateInDbById) {
throw new Error('Affiliate already exists(slug is already in use)')
}

const affiliateInDbByEmail = await affiliates.search(
{ page: 1, pageSize: 10 },
['_all'],
undefined,
`email=${email}`
)

if (affiliateInDbByEmail.length > 0) {
throw new Error('Affiliate already exists(email is already in use)')
}

const mdDocument = {
id: slug,
...newAffiliate,
} as Affiliates

delete mdDocument.slug
await affiliates.save(mdDocument)

return affiliates.get(slug, ['_all'])
}

0 comments on commit dbd2e72

Please sign in to comment.