From b2524781c1ebada1c8673098c8ee79402c8ef0e5 Mon Sep 17 00:00:00 2001 From: John Shields Date: Wed, 21 Feb 2024 16:41:47 +0000 Subject: [PATCH] =?UTF-8?q?PP-11681=20Replace=20=E2=80=98base-client?= =?UTF-8?q?=E2=80=99=20with=20=E2=80=98axios-base-client=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change ‘adminusers.client’ methods to use ‘axios-base-client’. - Refactor call parameters initialisation. - Use ‘async/await’ syntax. - Extend ‘instanceof RESTClientError’ test in registration controller to include a test for constructor name. This is order to identify an error thrown by the new 'axios-base-client'. Possibly needs refactoring. --- .../registration/registration.controller.js | 2 +- app/services/clients/adminusers.client.js | 817 +++++++----------- 2 files changed, 312 insertions(+), 507 deletions(-) diff --git a/app/controllers/registration/registration.controller.js b/app/controllers/registration/registration.controller.js index 7e3d2d4337..8351269636 100644 --- a/app/controllers/registration/registration.controller.js +++ b/app/controllers/registration/registration.controller.js @@ -53,7 +53,7 @@ async function submitEmailPage (req, res, next) { res.redirect(paths.register.checkEmail) } catch (err) { - if (err instanceof RESTClientError) { + if (err instanceof RESTClientError | err.constructor.name === 'RESTClientError') { if (err.errorCode === 403) { errors[EMAIL_INPUT_FIELD_NAME] = validationErrors.notPublicSectorEmail return res.render('registration/email', { errors, email }) diff --git a/app/services/clients/adminusers.client.js b/app/services/clients/adminusers.client.js index 7b788228e0..e120dcf800 100644 --- a/app/services/clients/adminusers.client.js +++ b/app/services/clients/adminusers.client.js @@ -2,7 +2,8 @@ const lodash = require('lodash') -const baseClient = require('./base-client/base.client') +const { Client } = require('@govuk-pay/pay-js-commons/lib/utils/axios-base-client/axios-base-client') +const { configureClient } = require('./base/config') const User = require('../../models/User.class') const Service = require('../../models/Service.class') @@ -32,17 +33,12 @@ module.exports = function (clientOptions = {}) { * @param {string} externalId * @return {Promise} A promise of a User */ - function getUserByExternalId (externalId) { - return baseClient.get( - { - baseUrl, - url: `${userResource}/${externalId}`, - json: true, - description: 'find a user', - service: SERVICE_NAME, - transform: responseBodyToUserTransformer - } - ) + async function getUserByExternalId (externalId) { + const url = `${baseUrl}${userResource}/${externalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'find a user') + return responseBodyToUserTransformer(response.data) } /** @@ -51,20 +47,12 @@ module.exports = function (clientOptions = {}) { * @return {Promise} A promise of a User * @param externalIds */ - function getUsersByExternalIds (externalIds = []) { - return baseClient.get( - { - baseUrl, - url: `${userResource}`, - qs: { - ids: externalIds.join() - }, - json: true, - description: 'find a user', - service: SERVICE_NAME, - transform: responseBodyToUserListTransformer - } - ) + async function getUsersByExternalIds (externalIds = []) { + const url = `${baseUrl}${userResource}?ids=${externalIds.join()}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'find a user') + return responseBodyToUserListTransformer(response.data) } /** @@ -72,21 +60,12 @@ module.exports = function (clientOptions = {}) { * @param password * @returns {Promise} */ - function authenticateUser (email, password) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/authenticate`, - json: true, - body: { - email: email, - password: password - }, - description: 'authenticate a user', - service: SERVICE_NAME, - transform: responseBodyToUserTransformer - } - ) + async function authenticateUser (email, password) { + const url = `${baseUrl}${userResource}/authenticate` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { email: email, password: password }, 'find a user') + return responseBodyToUserTransformer(response.data) } /** @@ -94,21 +73,17 @@ module.exports = function (clientOptions = {}) { * @param externalId * @returns {Promise} */ - function incrementSessionVersionForUser (externalId) { - return baseClient.patch( - { - baseUrl, - url: `${userResource}/${externalId}`, - json: true, - body: { - op: 'append', - path: 'sessionVersion', - value: 1 - }, - description: 'increment session version for a user', - service: SERVICE_NAME - } - ) + async function incrementSessionVersionForUser (externalId) { + const url = `${baseUrl}${userResource}/${externalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'append', + path: 'sessionVersion', + value: 1 + } + const response = await this.client.patch(url, body, 'increment session version for a user') + return response.data } /** @@ -116,19 +91,12 @@ module.exports = function (clientOptions = {}) { * @param username * @returns {Promise} */ - function createForgottenPassword (username) { - return baseClient.post( - { - baseUrl, - url: `${forgottenPasswordResource}`, - json: true, - body: { - username: username - }, - description: 'create a forgotten password for a user', - service: SERVICE_NAME - } - ) + async function createForgottenPassword (username) { + const url = `${baseUrl}${forgottenPasswordResource}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { username: username }, 'create a forgotten password for a user') + return response.data } /** @@ -136,16 +104,12 @@ module.exports = function (clientOptions = {}) { * @param code * @returns {Promise} */ - function getForgottenPassword (code) { - return baseClient.get( - { - baseUrl, - url: `${forgottenPasswordResource}/${code}`, - json: true, - description: 'get a forgotten password', - service: SERVICE_NAME - } - ) + async function getForgottenPassword (code) { + const url = `${baseUrl}${forgottenPasswordResource}/${code}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'get a forgotten password') + return response.data } /** @@ -154,20 +118,12 @@ module.exports = function (clientOptions = {}) { * @param newPassword * @returns {Promise} */ - function updatePasswordForUser (token, newPassword) { - return baseClient.post( - { - baseUrl, - url: `${resetPasswordResource}`, - json: true, - body: { - forgotten_password_code: token, - new_password: newPassword - }, - description: 'update a password for a user', - service: SERVICE_NAME - } - ) + async function updatePasswordForUser (token, newPassword) { + const url = `${baseUrl}${resetPasswordResource}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { forgotten_password_code: token, new_password: newPassword }, 'update a password for a user') + return response.data } /** @@ -176,17 +132,12 @@ module.exports = function (clientOptions = {}) { * @param provisional * @returns {Promise} */ - function sendSecondFactor (externalId, provisional) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/${externalId}/second-factor`, - json: true, - body: { provisional }, - description: 'post a second factor auth token to the user', - service: SERVICE_NAME - } - ) + async function sendSecondFactor (externalId, provisional) { + const url = `${baseUrl}${userResource}/${externalId}/second-factor` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { provisional }, 'post a second factor auth token to the user') + return response.data } /** @@ -195,31 +146,20 @@ module.exports = function (clientOptions = {}) { * @param code * @returns {Promise} */ - function authenticateSecondFactor (externalId, code) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/${externalId}/second-factor/authenticate`, - json: true, - body: { code }, - description: 'authenticate a second factor auth token entered by user', - service: SERVICE_NAME, - transform: responseBodyToUserTransformer - } - ) + async function authenticateSecondFactor (externalId, code) { + const url = `${baseUrl}${userResource}/${externalId}/second-factor/authenticate` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { code }, 'authenticate a second factor auth token entered by user') + return responseBodyToUserTransformer(response.data) } - function getServiceUsers (serviceExternalId) { - return baseClient.get( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}/users`, - json: true, - description: 'get a services users', - service: SERVICE_NAME, - transform: responseBodyToUserListTransformer - } - ) + async function getServiceUsers (serviceExternalId) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}/users` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'get a services users') + return responseBodyToUserListTransformer(response.data) } /** @@ -228,21 +168,12 @@ module.exports = function (clientOptions = {}) { * @param serviceExternalId * @param roleName */ - function assignServiceRole (userExternalId, serviceExternalId, roleName) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/${userExternalId}/services`, - json: true, - body: { - service_external_id: serviceExternalId, - role_name: roleName - }, - description: 'assigns user to a new service', - service: SERVICE_NAME, - transform: responseBodyToUserTransformer - } - ) + async function assignServiceRole (userExternalId, serviceExternalId, roleName) { + const url = `${baseUrl}${userResource}/${userExternalId}/services` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { service_external_id: serviceExternalId, role_name: roleName }, 'assigns user to a new service') + return responseBodyToUserTransformer(response.data) } /** @@ -252,20 +183,12 @@ module.exports = function (clientOptions = {}) { * @param roleName * @returns {Promise} */ - function updateServiceRole (externalId, serviceExternalId, roleName) { - return baseClient.put( - { - baseUrl, - url: `${userResource}/${externalId}/services/${serviceExternalId}`, - json: true, - body: { - role_name: roleName - }, - description: 'update role of a service that currently belongs to a user', - service: SERVICE_NAME, - transform: responseBodyToUserTransformer - } - ) + async function updateServiceRole (externalId, serviceExternalId, roleName) { + const url = `${baseUrl}${userResource}/${externalId}/services/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.put(url, { role_name: roleName }, 'update role of a service that currently belongs to a user') + return responseBodyToUserTransformer(response.data) } /** @@ -276,116 +199,84 @@ module.exports = function (clientOptions = {}) { * @param roleName * @returns {Promise} */ - function createInviteToJoinService (invitee, senderId, serviceExternalId, roleName) { - return baseClient.post( - { - baseUrl, - url: `/v1/api/invites/create-invite-to-join-service`, - json: true, - body: { - email: invitee, - sender: senderId, - service_external_id: serviceExternalId, - role_name: roleName - }, - description: 'invite a user to join a service', - service: SERVICE_NAME - } - ) + async function createInviteToJoinService (invitee, senderId, serviceExternalId, roleName) { + const url = `${baseUrl}/v1/api/invites/create-invite-to-join-service` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + email: invitee, + sender: senderId, + service_external_id: serviceExternalId, + role_name: roleName + } + const response = await this.client.post(url, body, 'invite a user to join a service') + return response.data } /** * Get a invited users for a given service * @param serviceExternalId */ - function getInvitedUsersList (serviceExternalId) { - return baseClient.get( - { - baseUrl, - url: `/v1/api/invites`, - qs: { - serviceId: serviceExternalId - }, - json: true, - method: 'GET', - description: 'get invited users for a service', - service: SERVICE_NAME - } - ) + async function getInvitedUsersList (serviceExternalId) { + const url = `${baseUrl}/v1/api/invites?serviceId=${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'get invited users for a service') + return response.data } /** * Get a valid invite or error if it's expired * @param inviteCode */ - function getValidatedInvite (inviteCode) { - return baseClient.get( - { - baseUrl, - url: `/v1/api/invites/${inviteCode}`, - json: true, - description: 'find a validated invitation', - service: SERVICE_NAME - } - ) - } - - function updateInvitePassword (inviteCode, password) { - return baseClient.patch( - { - baseUrl, - url: `/v1/api/invites/${inviteCode}`, - json: true, - body: [{ - op: 'replace', - path: 'password', - value: password - }], - description: 'update the password for an invite', - service: SERVICE_NAME - } - ) - } - - function updateInvitePhoneNumber (inviteCode, phoneNumber) { - return baseClient.patch( - { - baseUrl, - url: `/v1/api/invites/${inviteCode}`, - json: true, - body: [{ - op: 'replace', - path: 'telephone_number', - value: phoneNumber - }], - description: 'update the phone number for an invite', - service: SERVICE_NAME - } - ) - } - - function sendOtp (inviteCode) { - return baseClient.post( - { - baseUrl, - url: `/v1/api/invites/${inviteCode}/send-otp`, - json: true, - description: 'send OTP code', - service: SERVICE_NAME - } - ) - } - - function reprovisionOtp (inviteCode) { - return baseClient.post( - { - baseUrl, - url: `/v1/api/invites/${inviteCode}/reprovision-otp`, - json: true, - description: 're-provision OTP key', - service: SERVICE_NAME - } - ) + async function getValidatedInvite (inviteCode) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.get(url, 'find a validated invitation') + return response.data + } + + async function updateInvitePassword (inviteCode, password) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = [{ + op: 'replace', + path: 'password', + value: password + }] + const response = await this.client.patch(url, body, 'update the password for an invite') + return response.data + } + + async function updateInvitePhoneNumber (inviteCode, phoneNumber) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = [{ + op: 'replace', + path: 'telephone_number', + value: phoneNumber + }] + const response = await this.client.patch(url, body, 'update the phone number for an invite') + return response.data + } + + async function sendOtp (inviteCode) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}/send-otp` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, 'send OTP code') + return response.data + } + + async function reprovisionOtp (inviteCode) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}/reprovision-otp` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, 're-provision OTP key') + return response.data } /** @@ -395,37 +286,25 @@ module.exports = function (clientOptions = {}) { * @param secondFactorMethod * @returns {*|promise|Constructor} */ - function completeInvite (inviteCode, secondFactorMethod) { - const opts = { - baseUrl, - url: `/v1/api/invites/${inviteCode}/complete`, - json: true, - description: 'complete invite', - service: SERVICE_NAME + async function completeInvite (inviteCode, secondFactorMethod) { + const url = `${baseUrl}/v1/api/invites/${inviteCode}/complete` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = secondFactorMethod ? { second_factor: secondFactorMethod } : {} + const response = await this.client.post(url, body, 'complete invite') + return response.data + } + + async function verifyOtpForInvite (inviteCode, securityCode) { + const url = `${baseUrl}/v2/api/invites/otp/validate` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + code: inviteCode, + otp: securityCode } - if (secondFactorMethod) { - opts.body = { - second_factor: secondFactorMethod - } - } - - return baseClient.post(opts) - } - - function verifyOtpForInvite (inviteCode, securityCode) { - return baseClient.post( - { - baseUrl, - url: `/v2/api/invites/otp/validate`, - json: true, - body: { - code: inviteCode, - otp: securityCode - }, - description: 'submit invite otp code', - service: SERVICE_NAME - } - ) + const response = await this.client.post(url, body, 'submit invite otp code') + return response.data } /** @@ -433,40 +312,29 @@ module.exports = function (clientOptions = {}) { * * @param email */ - function createSelfSignupInvite (email) { - return baseClient.post( - { - baseUrl, - url: `/v1/api/invites/create-self-registration-invite`, - json: true, - body: { - email: email - }, - description: 'create self-registration invite', - service: SERVICE_NAME - } - ) + async function createSelfSignupInvite (email) { + const url = `${baseUrl}/v1/api/invites/create-self-registration-invite` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { email: email }, 'create self-registration invite') + return response.data } - function deleteUser (serviceExternalId, removerExternalId, userExternalId) { + async function deleteUser (serviceExternalId, removerExternalId, userExternalId) { let headers = {} headers[HEADER_USER_CONTEXT] = removerExternalId - - return baseClient.delete( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}/users/${userExternalId}`, - json: true, - body: { - headers: {} - }, - headers: headers, + const config = { + headers: headers, + data: { userDelete: userExternalId, - userRemover: removerExternalId, - description: 'delete a user from a service', - service: SERVICE_NAME + userRemover: removerExternalId } - ) + } + const url = `${baseUrl}${serviceResource}/${serviceExternalId}/users/${userExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.delete(url, 'delete a user from a service', config) + return response.data } /** @@ -477,26 +345,19 @@ module.exports = function (clientOptions = {}) { * @param gatewayAccountIds * @returns {*|promise|Constructor} */ - function createService (serviceName, serviceNameCy) { - let postBody = { - baseUrl, - url: `${serviceResource}`, - json: true, - body: {}, - description: 'create service', - transform: responseBodyToServiceTransformer, - service: SERVICE_NAME - } - + async function createService (serviceName, serviceNameCy) { + const url = `${baseUrl}${serviceResource}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = {} if (serviceName) { - postBody.body.service_name = lodash.merge(postBody.body.service_name, { en: serviceName }) + body.service_name = lodash.merge(body.service_name, { en: serviceName }) } if (serviceNameCy) { - postBody.body.service_name = lodash.merge(postBody.body.service_name, { cy: serviceNameCy }) + body.service_name = lodash.merge(body.service_name, { cy: serviceNameCy }) } - return baseClient.post( - postBody - ) + const response = await this.client.post(url, body, 'create service') + return responseBodyToServiceTransformer(response.data) } /** @@ -506,16 +367,12 @@ module.exports = function (clientOptions = {}) { * @param body * @returns {*|Constructor|promise} */ - function updateService (serviceExternalId, body) { - return baseClient.patch({ - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body, - description: 'update service', - transform: responseBodyToServiceTransformer, - service: SERVICE_NAME - }) + async function updateService (serviceExternalId, body) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.patch(url, body, 'update service') + return responseBodyToServiceTransformer(response.data) } /** @@ -526,28 +383,24 @@ module.exports = function (clientOptions = {}) { * @param serviceNameCy * @returns {*|Constructor|promise} */ - function updateServiceName (serviceExternalId, serviceName, serviceNameCy) { - return baseClient.patch( + async function updateServiceName (serviceExternalId, serviceName, serviceNameCy) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = [ + { + op: 'replace', + path: 'service_name/en', + value: serviceName || 'System Generated' + }, { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: [ - { - op: 'replace', - path: 'service_name/en', - value: serviceName || 'System Generated' - }, - { - op: 'replace', - path: 'service_name/cy', - value: serviceNameCy || '' - } - ], - description: 'update service name', - service: SERVICE_NAME + op: 'replace', + path: 'service_name/cy', + value: serviceNameCy || '' } - ) + ] + const response = await this.client.patch(url, body, 'update service name') + return response.data } /** @@ -557,40 +410,30 @@ module.exports = function (clientOptions = {}) { * @param collectBillingAddress * @returns {*|Constructor|promise} */ - function updateCollectBillingAddress (serviceExternalId, collectBillingAddress) { - return baseClient.patch( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: - { - op: 'replace', - path: 'collect_billing_address', - value: collectBillingAddress - }, - description: 'update collect billing address', - service: SERVICE_NAME - } - ) - } - - function updateDefaultBillingAddressCountry (serviceExternalId, countryCode) { - return baseClient.patch( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: - { - op: 'replace', - path: 'default_billing_address_country', - value: countryCode - }, - description: 'update default billing address country', - service: SERVICE_NAME - } - ) + async function updateCollectBillingAddress (serviceExternalId, collectBillingAddress) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'replace', + path: 'collect_billing_address', + value: collectBillingAddress + } + const response = await this.client.patch(url, body, 'update collect billing address') + return response.data + } + + async function updateDefaultBillingAddressCountry (serviceExternalId, countryCode) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'replace', + path: 'default_billing_address_country', + value: countryCode + } + const response = await this.client.patch(url, body, 'update default billing address country') + return response.data } /** @@ -600,22 +443,17 @@ module.exports = function (clientOptions = {}) { * @param gatewayAccountIds {String[]} a list of (unassigned) gateway account ids to add to the service * @returns {Promise} */ - function addGatewayAccountsToService (serviceExternalId, gatewayAccountIds) { - return baseClient.patch( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: { - op: 'add', - path: 'gateway_account_ids', - value: gatewayAccountIds - }, - description: 'update service name', - service: SERVICE_NAME, - transform: responseBodyToServiceTransformer - } - ) + async function addGatewayAccountsToService (serviceExternalId, gatewayAccountIds) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'add', + path: 'gateway_account_ids', + value: gatewayAccountIds + } + const response = await this.client.patch(url, body, 'update service name') + return responseBodyToServiceTransformer(response.data) } /** @@ -623,17 +461,12 @@ module.exports = function (clientOptions = {}) { * @param externalId * @returns {Promise} */ - function provisionNewOtpKey (externalId) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/${externalId}/second-factor/provision`, - json: true, - description: 'create a new 2FA provisional OTP key', - transform: responseBodyToUserTransformer, - service: SERVICE_NAME - } - ) + async function provisionNewOtpKey (externalId) { + const url = `${baseUrl}${userResource}/${externalId}/second-factor/provision` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, 'create a new 2FA provisional OTP key') + return responseBodyToUserTransformer(response.data) } /** @@ -643,82 +476,58 @@ module.exports = function (clientOptions = {}) { * @param secondFactor {String} 'SMS' or 'APP' * @returns {Promise} */ - function configureNewOtpKey (externalId, code, secondFactor) { - return baseClient.post( - { - baseUrl, - url: `${userResource}/${externalId}/second-factor/activate`, - json: true, - body: { - code: code, - second_factor: secondFactor - }, - description: 'configure a new OTP key and method', - service: SERVICE_NAME - } - ) - } - - function updateCurrentGoLiveStage (serviceExternalId, newStage) { - return baseClient.patch( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: { - op: 'replace', - path: 'current_go_live_stage', - value: newStage - }, - description: 'update current go live stage', - transform: responseBodyToServiceTransformer, - service: SERVICE_NAME - } - ) - } - - function updatePspTestAccountStage (serviceExternalId, newStage) { - return baseClient.patch( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}`, - json: true, - body: { - op: 'replace', - path: 'current_psp_test_account_stage', - value: newStage - }, - description: 'update PSP test account stage', - transform: responseBodyToServiceTransformer, - service: SERVICE_NAME - } - ) + async function configureNewOtpKey (externalId, code, secondFactor) { + const url = `${baseUrl}${userResource}/${externalId}/second-factor/activate` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + code: code, + second_factor: secondFactor + } + const response = await this.client.post(url, body, 'configure a new OTP key and method') + return response.data + } + + async function updateCurrentGoLiveStage (serviceExternalId, newStage) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'replace', + path: 'current_go_live_stage', + value: newStage + } + const response = await this.client.patch(url, body, 'update current go live stage') + return responseBodyToServiceTransformer(response.data) + } + + async function updatePspTestAccountStage (serviceExternalId, newStage) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'replace', + path: 'current_psp_test_account_stage', + value: newStage + } + const response = await this.client.patch(url, body, 'update PSP test account stage') + return responseBodyToServiceTransformer(response.data) } - function addStripeAgreementIpAddress (serviceExternalId, ipAddress) { - return baseClient.post( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}/stripe-agreement`, - json: true, - body: { ip_address: ipAddress }, - description: 'post the ip address of the user who agreed to stripe terms', - service: SERVICE_NAME - } - ) + async function addStripeAgreementIpAddress (serviceExternalId, ipAddress) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}/stripe-agreement` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { ip_address: ipAddress }, 'post the ip address of the user who agreed to stripe terms') + return response.data } - function addGovUkAgreementEmailAddress (serviceExternalId, userExternalId) { - return baseClient.post( - { - baseUrl, - url: `${serviceResource}/${serviceExternalId}/govuk-pay-agreement`, - json: true, - body: { user_external_id: userExternalId }, - description: 'post the external id of the user who agreed to GovUk Pay terms', - service: SERVICE_NAME - } - ) + async function addGovUkAgreementEmailAddress (serviceExternalId, userExternalId) { + const url = `${baseUrl}${serviceResource}/${serviceExternalId}/govuk-pay-agreement` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const response = await this.client.post(url, { user_external_id: userExternalId }, 'post the external id of the user who agreed to GovUk Pay terms') + return response.data } /** @@ -727,21 +536,17 @@ module.exports = function (clientOptions = {}) { * @param newPhoneNumber * @returns {Promise} */ - function updatePhoneNumberForUser (externalId, newPhoneNumber) { - return baseClient.patch( - { - baseUrl, - url: `${userResource}/${externalId}`, - json: true, - body: { - op: 'replace', - path: 'telephone_number', - value: newPhoneNumber - }, - description: 'update a phone number for a user', - service: SERVICE_NAME - } - ) + async function updatePhoneNumberForUser (externalId, newPhoneNumber) { + const url = `${baseUrl}${userResource}/${externalId}` + this.client = new Client(SERVICE_NAME) + configureClient(this.client, url) + const body = { + op: 'replace', + path: 'telephone_number', + value: newPhoneNumber + } + const response = await this.client.patch(url, body, 'update a phone number for a user') + return response.data } return {