Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-13096 Refactor Adminusers client to hardcode URLs with some URI encoding magic #4266

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 56 additions & 43 deletions app/services/clients/adminusers.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ const responseBodyToUserTransformer = body => new User(body)
const responseBodyToUserListTransformer = body => body.map(userData => new User(userData))
const responseBodyToServiceTransformer = body => new Service(body)

const encode = (pathStrings, baseURL, ...pathParameters) => {
console.log(pathStrings, baseURL, pathParameters)
if (pathStrings.length !== pathParameters.length + 2) {
throw new TypeError('')
}

let result = `${pathStrings[0]}${baseURL}`
for (let i = 0; i < pathParameters.length; i++) {
result += pathStrings[i + 1]
result += encodeURIComponent(pathParameters[i])
}
result += pathStrings[pathStrings.length - 1]
return result
}

module.exports = function (clientOptions = {}) {
const baseUrl = clientOptions.baseUrl || ADMINUSERS_URL
const userResource = `/v1/api/users`
const forgottenPasswordResource = `/v1/api/forgotten-passwords`
const resetPasswordResource = `/v1/api/reset-password`
const serviceResource = `/v1/api/services`
const client = new Client(SERVICE_NAME)

/**
Expand All @@ -35,7 +46,7 @@ module.exports = function (clientOptions = {}) {
* @return {Promise<User>} A promise of a User
*/
async function getUserByExternalId (externalId) {
const url = `${baseUrl}${userResource}/${externalId}`
const url = encode`${baseUrl}/v1/api/users/${externalId}`
configureClient(client, url)
const response = await client.get(url, 'find a user')
return responseBodyToUserTransformer(response.data)
Expand All @@ -48,9 +59,9 @@ module.exports = function (clientOptions = {}) {
* @param externalIds
*/
async function getUsersByExternalIds (externalIds = []) {
const url = `${baseUrl}${userResource}?ids=${externalIds.join()}`
const url = encode`${baseUrl}/v1/api/users`
configureClient(client, url)
const response = await client.get(url, 'find a user')
const response = await client.get(url, 'find a user', { params: { ids: externalIds.join() } })
return responseBodyToUserListTransformer(response.data)
}

Expand All @@ -60,7 +71,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise<User>}
*/
async function authenticateUser (email, password) {
const url = `${baseUrl}${userResource}/authenticate`
const url = encode`${baseUrl}/v1/api/users/authenticate`
configureClient(client, url)
const response = await client.post(url, { email: email, password: password }, 'find a user')
return responseBodyToUserTransformer(response.data)
Expand All @@ -72,7 +83,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function incrementSessionVersionForUser (externalId) {
const url = `${baseUrl}${userResource}/${externalId}`
const url = encode`${baseUrl}/v1/api/users/${externalId}`
configureClient(client, url)
const body = {
op: 'append',
Expand All @@ -89,7 +100,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise<ForgottenPassword>}
*/
async function createForgottenPassword (username) {
const url = `${baseUrl}${forgottenPasswordResource}`
const url = encode`${baseUrl}/v1/api/forgotten-passwords`
configureClient(client, url)
const response = await client.post(url, { username: username }, 'create a forgotten password for a user')
return response.data
Expand All @@ -101,7 +112,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise<ForgottenPassword>}
*/
async function getForgottenPassword (code) {
const url = `${baseUrl}${forgottenPasswordResource}/${code}`
const url = encode`${baseUrl}/v1/api/forgotten-passwords/${code}`
configureClient(client, url)
const response = await client.get(url, 'get a forgotten password')
return response.data
Expand All @@ -114,7 +125,8 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function updatePasswordForUser (token, newPassword) {
const url = `${baseUrl}${resetPasswordResource}`
const url = encode`${baseUrl}/v1/api/reset-password`
console.log(url)
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
Expand All @@ -127,7 +139,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function sendSecondFactor (externalId, provisional) {
const url = `${baseUrl}${userResource}/${externalId}/second-factor`
const url = encode`${baseUrl}/v1/api/users/${externalId}/second-factor`
configureClient(client, url)
const response = await client.post(url, { provisional }, 'post a second factor auth token to the user')
return response.data
Expand All @@ -140,14 +152,14 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function authenticateSecondFactor (externalId, code) {
const url = `${baseUrl}${userResource}/${externalId}/second-factor/authenticate`
const url = encode`${baseUrl}/v1/api/users/${externalId}/second-factor/authenticate`
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`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}/users`
configureClient(client, url)
const response = await client.get(url, 'get a services users')
return responseBodyToUserListTransformer(response.data)
Expand All @@ -160,7 +172,7 @@ module.exports = function (clientOptions = {}) {
* @param roleName
*/
async function assignServiceRole (userExternalId, serviceExternalId, roleName) {
const url = `${baseUrl}${userResource}/${userExternalId}/services`
const url = encode`${baseUrl}/v1/api/users/${userExternalId}/services`
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)
Expand All @@ -174,7 +186,8 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise<User>}
*/
async function updateServiceRole (externalId, serviceExternalId, roleName) {
const url = `${baseUrl}${userResource}/${externalId}/services/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/users/${externalId}/services/${serviceExternalId}`

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)
Expand All @@ -189,7 +202,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function createInviteToJoinService (invitee, senderId, serviceExternalId, roleName) {
const url = `${baseUrl}/v1/api/invites/create-invite-to-join-service`
const url = encode`${baseUrl}/v1/api/invites/create-invite-to-join-service`
configureClient(client, url)
const body = {
email: invitee,
Expand All @@ -206,9 +219,9 @@ module.exports = function (clientOptions = {}) {
* @param serviceExternalId
*/
async function getInvitedUsersList (serviceExternalId) {
const url = `${baseUrl}/v1/api/invites?serviceId=${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/invites`
configureClient(client, url)
const response = await client.get(url, 'get invited users for a service')
const response = await client.get(url, 'get invited users for a service', { params: { serviceId: serviceExternalId } })
return response.data
}

Expand All @@ -217,14 +230,14 @@ module.exports = function (clientOptions = {}) {
* @param inviteCode
*/
async function getValidatedInvite (inviteCode) {
const url = `${baseUrl}/v1/api/invites/${inviteCode}`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}`
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}`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}`
configureClient(client, url)
const body = [{
op: 'replace',
Expand All @@ -236,7 +249,7 @@ module.exports = function (clientOptions = {}) {
}

async function updateInvitePhoneNumber (inviteCode, phoneNumber) {
const url = `${baseUrl}/v1/api/invites/${inviteCode}`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}`
configureClient(client, url)
const body = [{
op: 'replace',
Expand All @@ -248,14 +261,14 @@ module.exports = function (clientOptions = {}) {
}

async function sendOtp (inviteCode) {
const url = `${baseUrl}/v1/api/invites/${inviteCode}/send-otp`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}/send-otp`
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`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}/reprovision-otp`
configureClient(client, url)
const response = await client.post(url, 're-provision OTP key')
return response.data
Expand All @@ -269,15 +282,15 @@ module.exports = function (clientOptions = {}) {
* @returns {*|promise|Constructor}
*/
async function completeInvite (inviteCode, secondFactorMethod) {
const url = `${baseUrl}/v1/api/invites/${inviteCode}/complete`
const url = encode`${baseUrl}/v1/api/invites/${inviteCode}/complete`
configureClient(client, url)
const body = secondFactorMethod ? { second_factor: secondFactorMethod } : {}
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`
const url = encode`${baseUrl}/v2/api/invites/otp/validate`
configureClient(client, url)
const body = {
code: inviteCode,
Expand All @@ -293,7 +306,7 @@ module.exports = function (clientOptions = {}) {
* @param email
*/
async function createSelfSignupInvite (email) {
const url = `${baseUrl}/v1/api/invites/create-self-registration-invite`
const url = encode`${baseUrl}/v1/api/invites/create-self-registration-invite`
configureClient(client, url)
const response = await client.post(url, { email: email }, 'create self-registration invite')
return response.data
Expand All @@ -309,7 +322,7 @@ module.exports = function (clientOptions = {}) {
userRemover: removerExternalId
}
}
const url = `${baseUrl}${serviceResource}/${serviceExternalId}/users/${userExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}/users/${userExternalId}`
configureClient(client, url)
const response = await client.delete(url, 'delete a user from a service', config)
return response.data
Expand All @@ -324,7 +337,7 @@ module.exports = function (clientOptions = {}) {
* @returns {*|promise|Constructor}
*/
async function createService (serviceName, serviceNameCy) {
const url = `${baseUrl}${serviceResource}`
const url = encode`${baseUrl}/v1/api/services`
configureClient(client, url)
const body = {}
if (serviceName) {
Expand All @@ -345,7 +358,7 @@ module.exports = function (clientOptions = {}) {
* @returns {*|Constructor|promise}
*/
async function updateService (serviceExternalId, body) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const response = await client.patch(url, body, 'update service')
return responseBodyToServiceTransformer(response.data)
Expand All @@ -360,7 +373,7 @@ module.exports = function (clientOptions = {}) {
* @returns {*|Constructor|promise}
*/
async function updateServiceName (serviceExternalId, serviceName, serviceNameCy) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = [
{
Expand All @@ -386,7 +399,7 @@ module.exports = function (clientOptions = {}) {
* @returns {*|Constructor|promise}
*/
async function updateCollectBillingAddress (serviceExternalId, collectBillingAddress) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand All @@ -398,7 +411,7 @@ module.exports = function (clientOptions = {}) {
}

async function updateDefaultBillingAddressCountry (serviceExternalId, countryCode) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand All @@ -417,7 +430,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise<Service|Error>}
*/
async function addGatewayAccountsToService (serviceExternalId, gatewayAccountIds) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = {
op: 'add',
Expand All @@ -434,7 +447,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function provisionNewOtpKey (externalId) {
const url = `${baseUrl}${userResource}/${externalId}/second-factor/provision`
const url = encode`${baseUrl}/v1/api/users/${externalId}/second-factor/provision`
configureClient(client, url)
const response = await client.post(url, 'create a new 2FA provisional OTP key')
return responseBodyToUserTransformer(response.data)
Expand All @@ -448,7 +461,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function configureNewOtpKey (externalId, code, secondFactor) {
const url = `${baseUrl}${userResource}/${externalId}/second-factor/activate`
const url = encode`${baseUrl}/v1/api/users/${externalId}/second-factor/activate`
configureClient(client, url)
const body = {
code: code,
Expand All @@ -459,7 +472,7 @@ module.exports = function (clientOptions = {}) {
}

async function updateCurrentGoLiveStage (serviceExternalId, newStage) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand All @@ -471,7 +484,7 @@ module.exports = function (clientOptions = {}) {
}

async function updatePspTestAccountStage (serviceExternalId, newStage) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand All @@ -483,14 +496,14 @@ module.exports = function (clientOptions = {}) {
}

async function addStripeAgreementIpAddress (serviceExternalId, ipAddress) {
const url = `${baseUrl}${serviceResource}/${serviceExternalId}/stripe-agreement`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}/stripe-agreement`
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`
const url = encode`${baseUrl}/v1/api/services/${serviceExternalId}/govuk-pay-agreement`
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
Expand All @@ -503,7 +516,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function updatePhoneNumberForUser (externalId, newPhoneNumber) {
const url = `${baseUrl}${userResource}/${externalId}`
const url = encode`${baseUrl}/v1/api/users/${externalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand All @@ -520,7 +533,7 @@ module.exports = function (clientOptions = {}) {
* @returns {Promise}
*/
async function updateFeaturesForUser (externalId, features) {
const url = `${baseUrl}${userResource}/${externalId}`
const url = encode`${baseUrl}/v1/api/users/${externalId}`
configureClient(client, url)
const body = {
op: 'replace',
Expand Down