-
Notifications
You must be signed in to change notification settings - Fork 0
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 #82 from mollie/feature/MOL-477/PICT-257
MOL-448/PICT-258 Update processor logic
- Loading branch information
Showing
4 changed files
with
648 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createApiRoot } from '../client/create.client'; | ||
import CustomError from '../errors/custom.error'; | ||
import { logger } from '../utils/logger.utils'; | ||
|
||
export const getCartFromPayment = async (paymentId: string) => { | ||
const carts = await createApiRoot() | ||
.carts() | ||
.get({ | ||
queryArgs: { | ||
where: `paymentInfo(payments(id= "${paymentId}"))`, | ||
}, | ||
}) | ||
.execute(); | ||
|
||
const results = carts.body.results; | ||
if (results.length !== 1) { | ||
logger.error('There is no cart which attached this target payment'); | ||
|
||
throw new CustomError(400, 'There is no cart which attached this target payment'); | ||
} | ||
|
||
logger.info(`Found cart with id ${results[0].id}`); | ||
|
||
return results[0]; | ||
}; |
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,88 @@ | ||
import { getCartFromPayment } from './../../src/commercetools/cart.commercetools'; | ||
import { afterEach, describe, expect, jest, it } from '@jest/globals'; | ||
import { Cart } from '@commercetools/platform-sdk'; | ||
import { createApiRoot } from '../../src/client/create.client'; | ||
import { logger } from '../../src/utils/logger.utils'; | ||
import CustomError from '../../src/errors/custom.error'; | ||
|
||
const cart: Cart = { | ||
id: '5c8b0375-305a-4f19-ae8e-07806b101999', | ||
country: 'DE', | ||
} as Cart; | ||
|
||
jest.mock('../../src/client/create.client', () => ({ | ||
createApiRoot: jest.fn(), | ||
})); | ||
|
||
describe('Test getCartFromPayment', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the correct cart', async () => { | ||
const getCarts = jest.fn(); | ||
|
||
(createApiRoot as jest.Mock).mockReturnValue({ | ||
carts: jest.fn().mockReturnValue({ | ||
get: getCarts, | ||
}), | ||
}); | ||
|
||
getCarts.mockReturnValue({ | ||
execute: jest.fn().mockReturnValue({ | ||
body: { | ||
results: [cart], | ||
}, | ||
}), | ||
}); | ||
|
||
const paymentId = 'test-payment-id'; | ||
const result = await getCartFromPayment(paymentId); | ||
|
||
expect(getCarts).toHaveBeenCalledTimes(1); | ||
expect(getCarts).toHaveBeenCalledWith({ | ||
queryArgs: { | ||
where: `paymentInfo(payments(id= "${paymentId}"))`, | ||
}, | ||
}); | ||
expect(result).toStrictEqual(cart); | ||
expect(logger.info).toHaveBeenCalledTimes(1); | ||
expect(logger.info).toHaveBeenCalledWith(`Found cart with id ${cart.id}`); | ||
}); | ||
|
||
it('should throw exception', async () => { | ||
const getCarts = jest.fn(); | ||
|
||
(createApiRoot as jest.Mock).mockReturnValue({ | ||
carts: jest.fn().mockReturnValue({ | ||
get: getCarts, | ||
}), | ||
}); | ||
|
||
getCarts.mockReturnValue({ | ||
execute: jest.fn().mockReturnValue({ | ||
body: { | ||
results: [], | ||
}, | ||
}), | ||
}); | ||
|
||
const paymentId = 'test-payment-id'; | ||
|
||
try { | ||
await getCartFromPayment(paymentId); | ||
} catch (error: any) { | ||
expect(getCarts).toHaveBeenCalledTimes(1); | ||
expect(getCarts).toHaveBeenCalledWith({ | ||
queryArgs: { | ||
where: `paymentInfo(payments(id= "${paymentId}"))`, | ||
}, | ||
}); | ||
expect(error).toBeInstanceOf(CustomError); | ||
expect(error.statusCode).toBe(400); | ||
expect(error.message).toBe('There is no cart which attached this target payment'); | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
expect(logger.error).toHaveBeenCalledWith('There is no cart which attached this target payment'); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.