@defra-fish/recurring-payments-job 1.61.0-rc.2 → 1.61.0-rc.4

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/recurring-payments-job",
3
- "version": "1.61.0-rc.2",
3
+ "version": "1.61.0-rc.4",
4
4
  "description": "Rod Licensing Recurring Payments Job",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,10 +36,10 @@
36
36
  "test": "echo \"Error: run tests from root\" && exit 1"
37
37
  },
38
38
  "dependencies": {
39
- "@defra-fish/business-rules-lib": "1.61.0-rc.2",
40
- "@defra-fish/connectors-lib": "1.61.0-rc.2",
39
+ "@defra-fish/business-rules-lib": "1.61.0-rc.4",
40
+ "@defra-fish/connectors-lib": "1.61.0-rc.4",
41
41
  "commander": "^7.2.0",
42
42
  "moment-timezone": "^0.5.34"
43
43
  },
44
- "gitHead": "4a762cba2fbe794d581e4f6c05af93016b6d8728"
44
+ "gitHead": "492a8abe5453ea44483fa565fc7bdec42e926993"
45
45
  }
@@ -1,6 +1,7 @@
1
1
  import { salesApi } from '@defra-fish/connectors-lib'
2
2
  import { processRecurringPayments } from '../recurring-payments-processor.js'
3
3
  import { getPaymentStatus, sendPayment } from '../services/govuk-pay-service.js'
4
+ import db from 'debug'
4
5
 
5
6
  jest.mock('@defra-fish/business-rules-lib')
6
7
  jest.mock('@defra-fish/connectors-lib', () => ({
@@ -16,12 +17,13 @@ jest.mock('@defra-fish/connectors-lib', () => ({
16
17
  processRPResult: jest.fn()
17
18
  }
18
19
  }))
19
-
20
20
  jest.mock('../services/govuk-pay-service.js', () => ({
21
21
  sendPayment: jest.fn(),
22
22
  getPaymentStatus: jest.fn()
23
23
  }))
24
+ jest.mock('debug', () => jest.fn(() => jest.fn()))
24
25
 
26
+ const debugMock = db.mock.results[0].value
25
27
  const PAYMENT_STATUS_DELAY = 60000
26
28
  const getPaymentStatusSuccess = () => ({ state: { status: 'success' } })
27
29
  const getMockPaymentRequestResponse = () => [
@@ -44,21 +46,18 @@ describe('recurring-payments-processor', () => {
44
46
  global.setTimeout = jest.fn((cb, ms) => cb())
45
47
  })
46
48
 
47
- it('console log displays "Recurring Payments job disabled" when env is false', async () => {
49
+ it('debug displays "Recurring Payments job disabled" when env is false', async () => {
48
50
  process.env.RUN_RECURRING_PAYMENTS = 'false'
49
- const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
50
51
 
51
52
  await processRecurringPayments()
52
53
 
53
- expect(consoleLogSpy).toHaveBeenCalledWith('Recurring Payments job disabled')
54
+ expect(debugMock).toHaveBeenCalledWith('Recurring Payments job disabled')
54
55
  })
55
56
 
56
- it('console log displays "Recurring Payments job enabled" when env is true', async () => {
57
- const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
58
-
57
+ it('debug displays "Recurring Payments job enabled" when env is true', async () => {
59
58
  await processRecurringPayments()
60
59
 
61
- expect(consoleLogSpy).toHaveBeenCalledWith('Recurring Payments job enabled')
60
+ expect(debugMock).toHaveBeenCalledWith('Recurring Payments job enabled')
62
61
  })
63
62
 
64
63
  it('get recurring payments is called when env is true', async () => {
@@ -69,12 +68,10 @@ describe('recurring-payments-processor', () => {
69
68
  expect(salesApi.getDueRecurringPayments).toHaveBeenCalledWith(date)
70
69
  })
71
70
 
72
- it('console log displays "Recurring Payments found: " when env is true', async () => {
73
- const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
74
-
71
+ it('debug displays "Recurring Payments found: " when env is true', async () => {
75
72
  await processRecurringPayments()
76
73
 
77
- expect(consoleLogSpy).toHaveBeenCalledWith('Recurring Payments found: ', [])
74
+ expect(debugMock).toHaveBeenCalledWith('Recurring Payments found: ', [])
78
75
  })
79
76
 
80
77
  it('prepares the data for found recurring payments', async () => {
@@ -289,8 +286,7 @@ describe('recurring-payments-processor', () => {
289
286
  expect(getPaymentStatus).toHaveBeenCalledWith('test-payment-id')
290
287
  })
291
288
 
292
- it('should log payment status for recurring payment', async () => {
293
- const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
289
+ it('debug should log payment status for recurring payment', async () => {
294
290
  const mockPaymentId = 'test-payment-id'
295
291
  const mockResponse = [
296
292
  {
@@ -304,17 +300,48 @@ describe('recurring-payments-processor', () => {
304
300
  }
305
301
  }
306
302
  ]
303
+ const mockStatus = getPaymentStatusSuccess()
307
304
  salesApi.getDueRecurringPayments.mockResolvedValueOnce(mockResponse)
308
305
  salesApi.createTransaction.mockResolvedValueOnce({
309
306
  id: mockPaymentId
310
307
  })
311
308
  const mockPaymentResponse = { payment_id: mockPaymentId, agreementId: 'agreement-1' }
312
309
  sendPayment.mockResolvedValueOnce(mockPaymentResponse)
310
+ getPaymentStatus.mockResolvedValueOnce(mockStatus)
311
+
312
+ await processRecurringPayments()
313
+
314
+ expect(debugMock).toHaveBeenCalledWith(`Payment status for ${mockPaymentId}: ${mockStatus.state.status}`)
315
+ })
316
+
317
+ it('debug should inform that the recurring payment has been processed', async () => {
318
+ const mockTransactionId = 'test-transaction-id'
319
+ salesApi.createTransaction.mockReturnValueOnce({
320
+ cost: 50
321
+ })
322
+ const mockResponse = [
323
+ {
324
+ entity: { agreementId: 'agreement-1' },
325
+ expanded: {
326
+ activePermission: {
327
+ entity: {
328
+ referenceNumber: 'ref-1'
329
+ }
330
+ }
331
+ }
332
+ }
333
+ ]
334
+ salesApi.getDueRecurringPayments.mockResolvedValueOnce(mockResponse)
335
+ salesApi.createTransaction.mockResolvedValueOnce({
336
+ id: mockTransactionId
337
+ })
338
+ const mockPaymentResponse = { payment_id: 'payment-id', agreementId: 'agreement-1' }
339
+ sendPayment.mockResolvedValueOnce(mockPaymentResponse)
313
340
  getPaymentStatus.mockResolvedValueOnce(getPaymentStatusSuccess())
314
341
 
315
342
  await processRecurringPayments()
316
343
 
317
- expect(consoleLogSpy).toHaveBeenCalledWith(`Payment status for ${mockPaymentId}: success`)
344
+ expect(debugMock).toHaveBeenCalledWith(`Processed Recurring Payment for ${mockTransactionId}`)
318
345
  })
319
346
 
320
347
  it('should call setTimeout with correct delay when there are recurring payments', async () => {
@@ -484,7 +511,7 @@ describe('recurring-payments-processor', () => {
484
511
 
485
512
  const permits = []
486
513
  for (let i = 0; i < count; i++) {
487
- permits.push(Symbol(`permit${i}`))
514
+ permits.push(`permit${i}`)
488
515
  }
489
516
 
490
517
  permits.forEach((permit, i) => {
@@ -2,6 +2,8 @@ import moment from 'moment-timezone'
2
2
  import { SERVICE_LOCAL_TIME } from '@defra-fish/business-rules-lib'
3
3
  import { salesApi } from '@defra-fish/connectors-lib'
4
4
  import { getPaymentStatus, sendPayment } from './services/govuk-pay-service.js'
5
+ import db from 'debug'
6
+ const debug = db('recurring-payments:processor')
5
7
 
6
8
  const PAYMENT_STATUS_DELAY = 60000
7
9
  const payments = []
@@ -9,17 +11,17 @@ const PAYMENT_STATUS_SUCCESS = 'success'
9
11
 
10
12
  export const processRecurringPayments = async () => {
11
13
  if (process.env.RUN_RECURRING_PAYMENTS?.toLowerCase() === 'true') {
12
- console.log('Recurring Payments job enabled')
14
+ debug('Recurring Payments job enabled')
13
15
  const date = new Date().toISOString().split('T')[0]
14
16
  const response = await salesApi.getDueRecurringPayments(date)
15
- console.log('Recurring Payments found: ', response)
17
+ debug('Recurring Payments found: ', response)
16
18
  await Promise.all(response.map(record => processRecurringPayment(record)))
17
19
  if (response.length > 0) {
18
20
  await new Promise(resolve => setTimeout(resolve, PAYMENT_STATUS_DELAY))
19
21
  await Promise.all(response.map(record => processRecurringPaymentStatus(record)))
20
22
  }
21
23
  } else {
22
- console.log('Recurring Payments job disabled')
24
+ debug('Recurring Payments job disabled')
23
25
  }
24
26
  }
25
27
 
@@ -32,20 +34,20 @@ const processRecurringPayment = async record => {
32
34
 
33
35
  const createNewTransaction = async (referenceNumber, agreementId) => {
34
36
  const transactionData = await processPermissionData(referenceNumber, agreementId)
35
- console.log('Creating new transaction based on', referenceNumber, 'with agreementId', agreementId)
37
+ debug('Creating new transaction based on: ', referenceNumber, 'with agreementId: ', agreementId)
36
38
  try {
37
39
  const response = await salesApi.createTransaction(transactionData)
38
- console.log('New transaction created:', response)
40
+ debug('New transaction created:', response)
39
41
  return response
40
42
  } catch (e) {
41
- console.log('Error creating transaction', JSON.stringify(transactionData))
43
+ console.error('Error creating transaction', JSON.stringify(transactionData))
42
44
  throw e
43
45
  }
44
46
  }
45
47
 
46
48
  const takeRecurringPayment = async (agreementId, transaction) => {
47
49
  const preparedPayment = preparePayment(agreementId, transaction)
48
- console.log('Requesting payment:', preparedPayment)
50
+ debug('Requesting payment:', preparedPayment)
49
51
  const payment = await sendPayment(preparedPayment)
50
52
  payments.push({
51
53
  agreementId,
@@ -56,7 +58,7 @@ const takeRecurringPayment = async (agreementId, transaction) => {
56
58
  }
57
59
 
58
60
  const processPermissionData = async (referenceNumber, agreementId) => {
59
- console.log('Preparing data based on', referenceNumber, 'with agreementId', agreementId)
61
+ debug('Preparing data based on', referenceNumber, 'with agreementId', agreementId)
60
62
  const data = await salesApi.preparePermissionDataForRenewal(referenceNumber)
61
63
  const licenseeWithoutCountryCode = Object.assign((({ countryCode: _countryCode, ...l }) => l)(data.licensee))
62
64
  return {
@@ -104,10 +106,11 @@ const processRecurringPaymentStatus = async record => {
104
106
  const {
105
107
  state: { status }
106
108
  } = await getPaymentStatus(paymentId)
107
- console.log(`Payment status for ${paymentId}: ${status}`)
109
+ debug(`Payment status for ${paymentId}: ${status}`)
108
110
  if (status === PAYMENT_STATUS_SUCCESS) {
109
111
  const payment = payments.find(p => p.paymentId === paymentId)
110
112
  await salesApi.processRPResult(payment.transaction.id, paymentId, payment.created_date)
113
+ debug(`Processed Recurring Payment for ${payment.transaction.id}`)
111
114
  }
112
115
  }
113
116