generated from vtex-apps/service-example
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from vtex/feature/Add-create-affiliate-mutation
Feature/add create affiliate mutation
- Loading branch information
Showing
7 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
} |