@defra-fish/connectors-lib 1.64.0-rc.6 → 1.64.0-rc.8

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/connectors-lib",
3
- "version": "1.64.0-rc.6",
3
+ "version": "1.64.0-rc.8",
4
4
  "description": "Shared connectors",
5
5
  "type": "module",
6
6
  "engines": {
@@ -46,5 +46,5 @@
46
46
  "node-fetch": "2.7.0",
47
47
  "redlock": "4.2.0"
48
48
  },
49
- "gitHead": "4ca80d63c58c37122b6fc8903840681202983468"
49
+ "gitHead": "88a60cab95e119b46cec17336d784aec1aeabae8"
50
50
  }
@@ -821,4 +821,103 @@ describe('sales-api-connector', () => {
821
821
  await expect(salesApi.retrieveStagedTransaction('id')).rejects.toThrow('Internal Server Error')
822
822
  })
823
823
  })
824
+
825
+ describe('retrieveRecurringPaymentAgreement', () => {
826
+ describe.each([['id'], ['abc-123']])("Retrieving recurring payment agreement id '%s'", agreementId => {
827
+ beforeEach(() => {
828
+ fetch.mockReturnValueOnce({
829
+ ok: true,
830
+ status: 200,
831
+ statusText: 'OK',
832
+ text: async () => JSON.stringify({ agreementId })
833
+ })
834
+ })
835
+
836
+ it('calls the endpoint with the correct parameters', async () => {
837
+ await salesApi.retrieveRecurringPaymentAgreement(agreementId)
838
+
839
+ expect(fetch).toHaveBeenCalledWith(`http://0.0.0.0:4000/retrieveRecurringPaymentAgreement/${agreementId}`, {
840
+ method: 'get',
841
+ headers: expect.any(Object),
842
+ timeout: 20000
843
+ })
844
+ })
845
+
846
+ it('returns the expected response data', async () => {
847
+ const processedResult = await salesApi.retrieveRecurringPaymentAgreement(agreementId)
848
+
849
+ expect(processedResult).toEqual({ agreementId })
850
+ })
851
+ })
852
+
853
+ it('throws an error on non-2xx response', async () => {
854
+ fetch.mockReturnValueOnce({
855
+ ok: false,
856
+ status: 500,
857
+ statusText: 'Internal Server Error',
858
+ text: async () => 'Server Error'
859
+ })
860
+
861
+ await expect(salesApi.retrieveRecurringPaymentAgreement('agreementId')).rejects.toThrow('Internal Server Error')
862
+ })
863
+ })
864
+
865
+ describe('updateRecurringTransaction', () => {
866
+ it.each([['Debit card'], ['Credit card']])('updates the transaction with payment method "%s"', async method => {
867
+ const transactionId = 'transaction-id'
868
+ const payload = {
869
+ payment: {
870
+ source: 'Gov Pay',
871
+ method
872
+ }
873
+ }
874
+ const expectedResponse = { id: transactionId, ...payload }
875
+
876
+ fetch.mockReturnValueOnce({
877
+ ok: true,
878
+ status: 200,
879
+ statusText: 'OK',
880
+ text: async () => JSON.stringify(expectedResponse)
881
+ })
882
+
883
+ await expect(salesApi.updateRecurringTransaction(transactionId, payload)).resolves.toEqual(expectedResponse)
884
+
885
+ expect(fetch).toHaveBeenCalledWith(
886
+ `http://0.0.0.0:4000/update-recurring-transactions/${transactionId}`,
887
+ expect.objectContaining({
888
+ method: 'patch',
889
+ body: JSON.stringify(payload)
890
+ })
891
+ )
892
+ })
893
+
894
+ it('throws on a non-ok response', async () => {
895
+ const transactionId = 'transaction-id'
896
+ const payload = {
897
+ payment: {
898
+ source: 'Gov Pay',
899
+ method: 'Debit card'
900
+ }
901
+ }
902
+
903
+ fetch.mockReturnValueOnce({
904
+ ok: false,
905
+ status: 422,
906
+ statusText: 'Unprocessable Entity',
907
+ text: async () => JSON.stringify({ error: 'Description' })
908
+ })
909
+
910
+ await expect(salesApi.updateRecurringTransaction(transactionId, payload)).rejects.toThrow(
911
+ /Unexpected response from the Sales API:.*"status": 422.*"statusText": "Unprocessable Entity"/s
912
+ )
913
+
914
+ expect(fetch).toHaveBeenCalledWith(
915
+ `http://0.0.0.0:4000/update-recurring-transactions/${transactionId}`,
916
+ expect.objectContaining({
917
+ method: 'patch',
918
+ body: JSON.stringify(payload)
919
+ })
920
+ )
921
+ })
922
+ })
824
923
  })
@@ -1,5 +1,5 @@
1
1
  import fetch from 'node-fetch'
2
- import querystring from 'querystring'
2
+ import querystring from 'node:querystring'
3
3
  import db from 'debug'
4
4
  const SALES_API_URL_DEFAULT = 'http://0.0.0.0:4000'
5
5
  const SALES_API_TIMEOUT_MS_DEFAULT = 20000
@@ -27,7 +27,7 @@ export const call = async (url, method = 'get', payload = null) => {
27
27
  ok: response.ok,
28
28
  status: response.status,
29
29
  statusText: response.statusText,
30
- body: response.status !== 204 ? await parseResponseBody(response) : undefined
30
+ body: response.status === 204 ? undefined : await parseResponseBody(response)
31
31
  }
32
32
  debug(
33
33
  'Request sent (%s): %s %s with payload %o. Response received (%s): %o',
@@ -100,6 +100,17 @@ export const createTransactions = async transactionArr =>
100
100
  */
101
101
  export const finaliseTransaction = async (id, payload) => exec2xxOrThrow(call(new URL(`/transactions/${id}`, urlBase), 'patch', payload))
102
102
 
103
+ /**
104
+ * Update a transaction in the sales API
105
+ *
106
+ * @param id the transaction id to finalise
107
+ * @param payload the update-transaction payload to supply on the request
108
+ * @returns {Promise<*>}
109
+ * @throws on a non-2xx response
110
+ */
111
+ export const updateRecurringTransaction = async (id, payload) =>
112
+ exec2xxOrThrow(call(new URL(`/update-recurring-transactions/${id}`, urlBase), 'patch', payload))
113
+
103
114
  /**
104
115
  * Retrieve the details of a transaction file. Returns null if not found.
105
116
  *
@@ -330,3 +341,13 @@ export const cancelRecurringPayment = async id => {
330
341
  export const retrieveStagedTransaction = async id => {
331
342
  return exec2xxOrThrow(call(new URL(`/retrieveStagedTransaction/${id}`, urlBase), 'get'))
332
343
  }
344
+
345
+ /**
346
+ * Retrieve recurring payment agreement details
347
+ *
348
+ * @param {string} agreementId
349
+ * @returns {Promise<*>}
350
+ * @throws on a non-2xx response
351
+ */
352
+ export const retrieveRecurringPaymentAgreement = async agreementId =>
353
+ exec2xxOrThrow(call(new URL(`/retrieveRecurringPaymentAgreement/${agreementId}`, urlBase), 'get'))