From a53837816dfe223827db068a9041786f980739e8 Mon Sep 17 00:00:00 2001 From: John Shields Date: Fri, 15 Mar 2024 16:36:07 +0000 Subject: [PATCH] PP-11681 Create shared client object and refactor as per review comment. --- app/services/clients/adminusers.client.js | 187 +++++++++------------- 1 file changed, 76 insertions(+), 111 deletions(-) diff --git a/app/services/clients/adminusers.client.js b/app/services/clients/adminusers.client.js index e120dcf800..fe5bf26f76 100644 --- a/app/services/clients/adminusers.client.js +++ b/app/services/clients/adminusers.client.js @@ -26,6 +26,7 @@ module.exports = function (clientOptions = {}) { const forgottenPasswordResource = `/v1/api/forgotten-passwords` const resetPasswordResource = `/v1/api/reset-password` const serviceResource = `/v1/api/services` + const client = new Client(SERVICE_NAME) /** * Get a User by external id @@ -35,9 +36,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.get(url, 'find a user') return responseBodyToUserTransformer(response.data) } @@ -49,9 +49,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.get(url, 'find a user') return responseBodyToUserListTransformer(response.data) } @@ -62,9 +61,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { email: email, password: password }, 'find a user') return responseBodyToUserTransformer(response.data) } @@ -75,14 +73,13 @@ module.exports = function (clientOptions = {}) { */ async function incrementSessionVersionForUser (externalId) { const url = `${baseUrl}${userResource}/${externalId}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(client, url) const body = { op: 'append', path: 'sessionVersion', value: 1 } - const response = await this.client.patch(url, body, 'increment session version for a user') + const response = await client.patch(url, body, 'increment session version for a user') return response.data } @@ -93,9 +90,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { username: username }, 'create a forgotten password for a user') return response.data } @@ -106,9 +102,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.get(url, 'get a forgotten password') return response.data } @@ -120,9 +115,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { forgotten_password_code: token, new_password: newPassword }, 'update a password for a user') return response.data } @@ -134,9 +128,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { provisional }, 'post a second factor auth token to the user') return response.data } @@ -148,17 +141,15 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { code }, 'authenticate a second factor auth token entered by user') return responseBodyToUserTransformer(response.data) } 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') + configureClient(client, url) + const response = await client.get(url, 'get a services users') return responseBodyToUserListTransformer(response.data) } @@ -170,9 +161,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { service_external_id: serviceExternalId, role_name: roleName }, 'assigns user to a new service') return responseBodyToUserTransformer(response.data) } @@ -185,9 +175,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.put(url, { role_name: roleName }, 'update role of a service that currently belongs to a user') return responseBodyToUserTransformer(response.data) } @@ -201,15 +190,14 @@ module.exports = function (clientOptions = {}) { */ 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) + configureClient(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') + const response = await client.post(url, body, 'invite a user to join a service') return response.data } @@ -219,9 +207,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.get(url, 'get invited users for a service') return response.data } @@ -231,51 +218,46 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await 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) + configureClient(client, url) const body = [{ op: 'replace', path: 'password', value: password }] - const response = await this.client.patch(url, body, 'update the password for an invite') + const response = await 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) + configureClient(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') + const response = await 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') + configureClient(client, url) + const response = await 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') + configureClient(client, url) + const response = await client.post(url, 're-provision OTP key') return response.data } @@ -288,22 +270,20 @@ module.exports = function (clientOptions = {}) { */ async function completeInvite (inviteCode, secondFactorMethod) { const url = `${baseUrl}/v1/api/invites/${inviteCode}/complete` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(client, url) const body = secondFactorMethod ? { second_factor: secondFactorMethod } : {} - const response = await this.client.post(url, body, 'complete invite') + const response = await 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) + configureClient(client, url) const body = { code: inviteCode, otp: securityCode } - const response = await this.client.post(url, body, 'submit invite otp code') + const response = await client.post(url, body, 'submit invite otp code') return response.data } @@ -314,9 +294,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, { email: email }, 'create self-registration invite') return response.data } @@ -331,9 +310,8 @@ module.exports = function (clientOptions = {}) { } } 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) + configureClient(client, url) + const response = await client.delete(url, 'delete a user from a service', config) return response.data } @@ -347,8 +325,7 @@ module.exports = function (clientOptions = {}) { */ async function createService (serviceName, serviceNameCy) { const url = `${baseUrl}${serviceResource}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(client, url) const body = {} if (serviceName) { body.service_name = lodash.merge(body.service_name, { en: serviceName }) @@ -356,7 +333,7 @@ module.exports = function (clientOptions = {}) { if (serviceNameCy) { body.service_name = lodash.merge(body.service_name, { cy: serviceNameCy }) } - const response = await this.client.post(url, body, 'create service') + const response = await client.post(url, body, 'create service') return responseBodyToServiceTransformer(response.data) } @@ -369,9 +346,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.patch(url, body, 'update service') return responseBodyToServiceTransformer(response.data) } @@ -385,8 +361,7 @@ module.exports = function (clientOptions = {}) { */ async function updateServiceName (serviceExternalId, serviceName, serviceNameCy) { const url = `${baseUrl}${serviceResource}/${serviceExternalId}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(client, url) const body = [ { op: 'replace', @@ -399,7 +374,7 @@ module.exports = function (clientOptions = {}) { value: serviceNameCy || '' } ] - const response = await this.client.patch(url, body, 'update service name') + const response = await client.patch(url, body, 'update service name') return response.data } @@ -412,27 +387,25 @@ module.exports = function (clientOptions = {}) { */ async function updateCollectBillingAddress (serviceExternalId, collectBillingAddress) { const url = `${baseUrl}${serviceResource}/${serviceExternalId}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(client, url) const body = { op: 'replace', path: 'collect_billing_address', value: collectBillingAddress } - const response = await this.client.patch(url, body, 'update collect billing address') + const response = await 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 = { + configureClient(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') + const response = await client.patch(url, body, 'update default billing address country') return response.data } @@ -445,14 +418,13 @@ module.exports = function (clientOptions = {}) { */ async function addGatewayAccountsToService (serviceExternalId, gatewayAccountIds) { const url = `${baseUrl}${serviceResource}/${serviceExternalId}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) - const body = { + configureClient(client, url) + const body = { op: 'add', path: 'gateway_account_ids', value: gatewayAccountIds } - const response = await this.client.patch(url, body, 'update service name') + const response = await client.patch(url, body, 'update service name') return responseBodyToServiceTransformer(response.data) } @@ -463,9 +435,8 @@ module.exports = function (clientOptions = {}) { */ 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') + configureClient(client, url) + const response = await client.post(url, 'create a new 2FA provisional OTP key') return responseBodyToUserTransformer(response.data) } @@ -478,55 +449,50 @@ module.exports = function (clientOptions = {}) { */ 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 = { + configureClient(client, url) + const body = { code: code, second_factor: secondFactor } - const response = await this.client.post(url, body, 'configure a new OTP key and method') + const response = await 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) + configureClient(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') + const response = await 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) + configureClient(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') + const response = await client.patch(url, body, 'update PSP test account stage') return responseBodyToServiceTransformer(response.data) } 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') + configureClient(client, url) + const response = await client.post(url, { ip_address: ipAddress }, 'post the ip address of the user who agreed to stripe terms') return response.data } 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') + configureClient(client, url) + const response = await client.post(url, { user_external_id: userExternalId }, 'post the external id of the user who agreed to GovUk Pay terms') return response.data } @@ -538,14 +504,13 @@ module.exports = function (clientOptions = {}) { */ async function updatePhoneNumberForUser (externalId, newPhoneNumber) { const url = `${baseUrl}${userResource}/${externalId}` - this.client = new Client(SERVICE_NAME) - configureClient(this.client, url) + configureClient(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') + const response = await client.patch(url, body, 'update a phone number for a user') return response.data }