diff --git a/processor/package.json b/processor/package.json index 7ad9ab6..8bfcc2b 100644 --- a/processor/package.json +++ b/processor/package.json @@ -11,8 +11,8 @@ "build": "rimraf ./dist && tsc", "watch": "nodemon -q --ignore '**/*.spec.ts' src/index.ts", "lint": "eslint . --ext .ts", - "prettier": "prettier --write '**/*.{js,ts}'", "prettier:check": "prettier --check '**/*.{js,ts}'", + "fix": "eslint . --ext .ts --fix && prettier --write '**/*.{js,ts}'", "test": "jest --detectOpenHandles --clearMocks --colors --config jest.config.cjs --ci --ci --reporters=default --reporters=jest-junit --coverage", "test:watch": "jest --watch --clearMocks --detectOpenHandles --colors --config jest.config.cjs", "preinstall": "npx npm-force-resolutions", diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index 7278675..749e48f 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -1,6 +1,7 @@ import { describe, test, expect, jest, beforeEach, afterEach, it } from '@jest/globals'; import { CustomFields, Payment } from '@commercetools/platform-sdk'; import { + handlePaymentWebhook, getCreatePaymentUpdateAction, getPaymentCancelRefundActions, handleCreatePayment, @@ -15,19 +16,20 @@ import { } from '../../src/utils/constant.utils'; import { PaymentStatus, Payment as molliePayment } from '@mollie/api-client'; import { CTTransactionState } from '../../src/types/commercetools.types'; -import { - cancelPaymentRefund, - createMolliePayment, - getPaymentById, - listPaymentMethods, -} from '../../src/mollie/payment.mollie'; +import { listPaymentMethods, getPaymentById, createMolliePayment, cancelPaymentRefund } from '../../src/mollie/payment.mollie'; import CustomError from '../../src/errors/custom.error'; import { logger } from '../../src/utils/logger.utils'; +import { getPaymentByMolliePaymentId, updatePayment } from '../../src/commercetools/payment.commercetools'; const uuid = '5c8b0375-305a-4f19-ae8e-07806b101999'; jest.mock('uuid', () => ({ v4: () => uuid, })); +jest.mock('../../src/commercetools/payment.commercetools', () => ({ + getPaymentByMolliePaymentId: jest.fn(), + updatePayment: jest.fn(), +})); + jest.mock('../../src/service/payment.service.ts', () => ({ ...(jest.requireActual('../../src/service/payment.service.ts') as object), getCreatePaymentUpdateAction: jest.fn(), @@ -734,3 +736,89 @@ describe('Test handlePaymentCancelRefund', () => { }); }); }); + +describe('Test handlePaymentWebhook', () => { + it('should handle with no action', async () => { + const fakePaymentId = 'tr_XXXX'; + (getPaymentById as jest.Mock).mockReturnValue({ + id: fakePaymentId, + status: 'open', + amount: { + currency: 'EUR', + value: '10.00', + }, + }); + (getPaymentByMolliePaymentId as jest.Mock).mockReturnValue({ + transactions: [ + { + interactionId: fakePaymentId, + state: 'Initial', + }, + ], + }); + await handlePaymentWebhook(fakePaymentId); + expect(logger.debug).toBeCalledTimes(2); + expect(logger.debug).toBeCalledWith(`SCTM - handlePaymentWebhook - paymentId:${fakePaymentId}`); + expect(logger.debug).toBeCalledWith(`handlePaymentWebhook - No actions needed`); + }); + + it('should handle with add action', async () => { + const fakePaymentId = 'tr_XXXX'; + (getPaymentById as jest.Mock).mockReturnValue({ + id: fakePaymentId, + status: 'open', + amount: { + currency: 'EUR', + value: '10.00', + }, + }); + const ctPayment = { + transactions: [], + }; + (getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment); + await handlePaymentWebhook(fakePaymentId); + expect(updatePayment as jest.Mock).toBeCalledTimes(1); + expect(updatePayment as jest.Mock).toBeCalledWith(ctPayment, [ + { + action: 'addTransaction', + transaction: { + amount: { centAmount: 1000, currencyCode: 'EUR', fractionDigits: 2, type: 'centPrecision' }, + interactionId: 'tr_XXXX', + state: 'Initial', + type: 'Charge', + }, + }, + ]); + }); + + it('should handle with update action', async () => { + const fakePaymentId = 'tr_XXXX'; + (getPaymentById as jest.Mock).mockReturnValue({ + id: fakePaymentId, + status: 'paid', + amount: { + currency: 'EUR', + value: '10.00', + }, + }); + const ctPayment = { + transactions: [ + { + id: 'test', + interactionId: fakePaymentId, + state: 'Initial', + }, + ], + }; + (getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment); + await handlePaymentWebhook(fakePaymentId); + expect(updatePayment as jest.Mock).toBeCalledTimes(1); + expect(updatePayment as jest.Mock).toBeCalledWith(ctPayment, [ + { + action: 'changeTransactionState', + transactionId: 'test', + state: 'Success', + }, + ]); + }); +});