@defra-fish/sales-api-service 1.62.0-rc.0 → 1.62.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra-fish/sales-api-service",
3
- "version": "1.62.0-rc.0",
3
+ "version": "1.62.0-rc.2",
4
4
  "description": "Rod Licensing Sales API",
5
5
  "type": "module",
6
6
  "engines": {
@@ -35,9 +35,9 @@
35
35
  "test": "echo \"Error: run tests from root\" && exit 1"
36
36
  },
37
37
  "dependencies": {
38
- "@defra-fish/business-rules-lib": "1.62.0-rc.0",
39
- "@defra-fish/connectors-lib": "1.62.0-rc.0",
40
- "@defra-fish/dynamics-lib": "1.62.0-rc.0",
38
+ "@defra-fish/business-rules-lib": "1.62.0-rc.2",
39
+ "@defra-fish/connectors-lib": "1.62.0-rc.2",
40
+ "@defra-fish/dynamics-lib": "1.62.0-rc.2",
41
41
  "@hapi/boom": "^9.1.2",
42
42
  "@hapi/hapi": "^20.1.3",
43
43
  "@hapi/inert": "^6.0.3",
@@ -52,5 +52,5 @@
52
52
  "moment-timezone": "^0.5.34",
53
53
  "uuid": "^8.3.2"
54
54
  },
55
- "gitHead": "d56e1a0d0f4b22f1dded800c0ed511b737cb91a7"
55
+ "gitHead": "6a3a2c6a81067424144382557214c49c27559cdf"
56
56
  }
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  dueRecurringPaymentsRequestParamsSchema,
3
3
  dueRecurringPaymentsResponseSchema,
4
- processRPResultRequestParamsSchema
4
+ processRPResultRequestParamsSchema,
5
+ cancelRecurringPaymentRequestParamsSchema
5
6
  } from '../recurring-payments.schema.js'
6
7
 
7
8
  jest.mock('../validators/validators.js', () => ({
@@ -149,3 +150,19 @@ describe('processRPResultRequestParamsSchema', () => {
149
150
  expect(() => processRPResultRequestParamsSchema.validateAsync(sampleData).rejects.toThrow())
150
151
  })
151
152
  })
153
+
154
+ describe('cancelRecurringPaymentRequestParamsSchema', () => {
155
+ it('validates expected object', async () => {
156
+ const sampleData = { id: 'abc123' }
157
+ expect(() => cancelRecurringPaymentRequestParamsSchema.validateAsync(sampleData)).not.toThrow()
158
+ })
159
+
160
+ it('throws an error if id missing', async () => {
161
+ expect(() => cancelRecurringPaymentRequestParamsSchema.validateAsync({}).rejects.toThrow())
162
+ })
163
+
164
+ it('throws an error if id is not the correct type', async () => {
165
+ const sampleData = { id: 99 }
166
+ expect(() => cancelRecurringPaymentRequestParamsSchema.validateAsync(sampleData).rejects.toThrow())
167
+ })
168
+ })
@@ -29,3 +29,7 @@ export const processRPResultRequestParamsSchema = Joi.object({
29
29
  paymentId: Joi.string().required(),
30
30
  createdDate: Joi.string().isoDate().required()
31
31
  })
32
+
33
+ export const cancelRecurringPaymentRequestParamsSchema = Joi.object({
34
+ id: Joi.string().required()
35
+ })
@@ -1,6 +1,10 @@
1
1
  import recurringPayments from '../recurring-payments.js'
2
- import { getRecurringPayments, processRPResult } from '../../../services/recurring-payments.service.js'
3
- import { dueRecurringPaymentsRequestParamsSchema, processRPResultRequestParamsSchema } from '../../../schema/recurring-payments.schema.js'
2
+ import { getRecurringPayments, processRPResult, cancelRecurringPayment } from '../../../services/recurring-payments.service.js'
3
+ import {
4
+ dueRecurringPaymentsRequestParamsSchema,
5
+ processRPResultRequestParamsSchema,
6
+ cancelRecurringPaymentRequestParamsSchema
7
+ } from '../../../schema/recurring-payments.schema.js'
4
8
 
5
9
  const [
6
10
  {
@@ -8,17 +12,22 @@ const [
8
12
  },
9
13
  {
10
14
  options: { handler: prpHandler }
15
+ },
16
+ {
17
+ options: { handler: crpHandler }
11
18
  }
12
19
  ] = recurringPayments
13
20
 
14
21
  jest.mock('../../../services/recurring-payments.service.js', () => ({
15
22
  getRecurringPayments: jest.fn(),
16
- processRPResult: jest.fn()
23
+ processRPResult: jest.fn(),
24
+ cancelRecurringPayment: jest.fn()
17
25
  }))
18
26
 
19
27
  jest.mock('../../../schema/recurring-payments.schema.js', () => ({
20
28
  dueRecurringPaymentsRequestParamsSchema: jest.fn(),
21
- processRPResultRequestParamsSchema: jest.fn()
29
+ processRPResultRequestParamsSchema: jest.fn(),
30
+ cancelRecurringPaymentRequestParamsSchema: jest.fn()
22
31
  }))
23
32
 
24
33
  const getMockRequest = ({
@@ -27,9 +36,10 @@ const getMockRequest = ({
27
36
  paymentId = 'payment-id',
28
37
  createdDate = 'created-date',
29
38
  existingRecurringPaymentId = 'existing-recurring-payment-id',
30
- agreementId = 'agreement-id'
39
+ agreementId = 'agreement-id',
40
+ id = 'abc123'
31
41
  }) => ({
32
- params: { date, transactionId, paymentId, createdDate, existingRecurringPaymentId, agreementId }
42
+ params: { date, transactionId, paymentId, createdDate, existingRecurringPaymentId, agreementId, id }
33
43
  })
34
44
 
35
45
  const getMockResponseToolkit = () => ({
@@ -86,4 +96,26 @@ describe('recurring payments', () => {
86
96
  expect(recurringPayments[1].options.validate.params).toBe(processRPResultRequestParamsSchema)
87
97
  })
88
98
  })
99
+
100
+ describe('cancelRecurringPayment', () => {
101
+ it('handler should return continue response', async () => {
102
+ const request = getMockRequest({})
103
+ const responseToolkit = getMockResponseToolkit()
104
+ expect(await crpHandler(request, responseToolkit)).toEqual(responseToolkit.continue)
105
+ })
106
+
107
+ it('should call cancelRecurringPayment with id', async () => {
108
+ const id = Symbol('recurring-payment-id')
109
+ const request = getMockRequest({ id })
110
+ await crpHandler(request, getMockResponseToolkit())
111
+ expect(cancelRecurringPayment).toHaveBeenCalledWith(id)
112
+ })
113
+
114
+ it('should validate with cancelRecurringPaymentRequestParamsSchema', async () => {
115
+ const id = Symbol('recurring-payment-id')
116
+ const request = getMockRequest({ id })
117
+ await crpHandler(request, getMockResponseToolkit())
118
+ expect(recurringPayments[2].options.validate.params).toBe(cancelRecurringPaymentRequestParamsSchema)
119
+ })
120
+ })
89
121
  })
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  dueRecurringPaymentsRequestParamsSchema,
3
3
  dueRecurringPaymentsResponseSchema,
4
- processRPResultRequestParamsSchema
4
+ processRPResultRequestParamsSchema,
5
+ cancelRecurringPaymentRequestParamsSchema
5
6
  } from '../../schema/recurring-payments.schema.js'
6
- import { getRecurringPayments, processRPResult } from '../../services/recurring-payments.service.js'
7
+ import { getRecurringPayments, processRPResult, cancelRecurringPayment } from '../../services/recurring-payments.service.js'
7
8
 
8
9
  const SWAGGER_TAGS = ['api', 'recurring-payments']
9
10
 
@@ -55,5 +56,29 @@ export default [
55
56
  }
56
57
  }
57
58
  }
59
+ },
60
+ {
61
+ method: 'GET',
62
+ path: '/cancelRecurringPayment/{id}',
63
+ options: {
64
+ handler: async (request, h) => {
65
+ const { id } = request.params
66
+ const result = await cancelRecurringPayment(id)
67
+ return h.response(result)
68
+ },
69
+ description: 'Cancel a recurring payment',
70
+ tags: SWAGGER_TAGS,
71
+ validate: {
72
+ params: cancelRecurringPaymentRequestParamsSchema
73
+ },
74
+ plugins: {
75
+ 'hapi-swagger': {
76
+ responses: {
77
+ 200: { description: 'Recurring payment cancelled' }
78
+ },
79
+ order: 3
80
+ }
81
+ }
82
+ }
58
83
  }
59
84
  ]
@@ -3,6 +3,7 @@ import {
3
3
  executeQuery,
4
4
  findDueRecurringPayments,
5
5
  findRecurringPaymentsByAgreementId,
6
+ findById,
6
7
  Permission,
7
8
  RecurringPayment
8
9
  } from '@defra-fish/dynamics-lib'
@@ -11,7 +12,8 @@ import {
11
12
  processRecurringPayment,
12
13
  generateRecurringPaymentRecord,
13
14
  processRPResult,
14
- findNewestExistingRecurringPaymentInCrm
15
+ findNewestExistingRecurringPaymentInCrm,
16
+ cancelRecurringPayment
15
17
  } from '../recurring-payments.service.js'
16
18
  import { calculateEndDate, generatePermissionNumber } from '../permissions.service.js'
17
19
  import { getObfuscatedDob } from '../contacts.service.js'
@@ -27,6 +29,7 @@ const { docClient, sqs } = AWS.mock.results[0].value
27
29
  jest.mock('@defra-fish/dynamics-lib', () => ({
28
30
  ...jest.requireActual('@defra-fish/dynamics-lib'),
29
31
  executeQuery: jest.fn(),
32
+ findById: jest.fn(),
30
33
  findDueRecurringPayments: jest.fn(),
31
34
  findRecurringPaymentsByAgreementId: jest.fn(() => ({ toRetrieveRequest: () => {} })),
32
35
  dynamicsClient: {
@@ -733,4 +736,31 @@ describe('recurring payments service', () => {
733
736
  expect(rcp).toBeFalsy()
734
737
  })
735
738
  })
739
+
740
+ describe('cancelRecurringPayment', () => {
741
+ it('should call findById with RecurringPayment and the provided id', async () => {
742
+ const id = 'abc123'
743
+ await cancelRecurringPayment(id)
744
+ expect(findById).toHaveBeenCalledWith(RecurringPayment, id)
745
+ })
746
+
747
+ it('should log a RecurringPayment record when there is one match', async () => {
748
+ const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
749
+ const recurringPayment = { entity: getMockRecurringPayment() }
750
+ findById.mockReturnValueOnce(recurringPayment)
751
+
752
+ await cancelRecurringPayment('id')
753
+
754
+ expect(consoleLogSpy).toHaveBeenCalledWith('RecurringPayment for cancellation: ', recurringPayment)
755
+ })
756
+
757
+ it('should log no matches when there are no matches', async () => {
758
+ const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
759
+ findById.mockReturnValueOnce(undefined)
760
+
761
+ await cancelRecurringPayment('id')
762
+
763
+ expect(consoleLogSpy).toHaveBeenCalledWith('No matches found for cancellation')
764
+ })
765
+ })
736
766
  })
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  executeQuery,
3
+ findById,
3
4
  findDueRecurringPayments,
4
5
  findRecurringPaymentsByAgreementId,
5
6
  RecurringPayment,
@@ -138,3 +139,12 @@ export const findNewestExistingRecurringPaymentInCrm = async agreementId => {
138
139
  }
139
140
  return false
140
141
  }
142
+
143
+ export const cancelRecurringPayment = async id => {
144
+ const recurringPayment = await findById(RecurringPayment, id)
145
+ if (recurringPayment) {
146
+ console.log('RecurringPayment for cancellation: ', recurringPayment)
147
+ } else {
148
+ console.log('No matches found for cancellation')
149
+ }
150
+ }