From 1a2734e17620bc826eab2e19b0f0c6ce24e0f1b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baldur=20=C3=93li?= Date: Mon, 24 Jun 2024 09:10:42 +0000 Subject: [PATCH] chore(application-systems): Switching shared user profile service to use v2 users api (#15300) * Init commit for using v2 user profile endpoint * updating module file and imports in service * adding a null coalescing check incase of nulls --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../api/user-profile/user-profile.module.ts | 8 +- .../api/user-profile/user-profile.service.ts | 82 +++++++++---------- 2 files changed, 41 insertions(+), 49 deletions(-) diff --git a/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.module.ts b/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.module.ts index 02bb493cb07e..eb5b8460411c 100644 --- a/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.module.ts +++ b/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.module.ts @@ -2,8 +2,8 @@ import { DynamicModule } from '@nestjs/common' import { BaseTemplateAPIModuleConfig } from '../../../../types' import { UserProfileService } from './user-profile.service' -import { Configuration, UserProfileApi } from '@island.is/clients/user-profile' -import { IslyklarApi, IslykillApiModule } from '@island.is/clients/islykill' +import { Configuration, V2UsersApi } from '@island.is/clients/user-profile' +import { IslykillApiModule } from '@island.is/clients/islykill' export class UserProfileModule { static register(config: BaseTemplateAPIModuleConfig): DynamicModule { return { @@ -18,9 +18,9 @@ export class UserProfileModule { providers: [ UserProfileService, { - provide: UserProfileApi, + provide: V2UsersApi, useFactory: () => - new UserProfileApi( + new V2UsersApi( new Configuration({ fetchApi: fetch, basePath: config.userProfile.serviceBasePath, diff --git a/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.service.ts b/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.service.ts index 360ed29e8dc5..2ea872a220fc 100644 --- a/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.service.ts +++ b/libs/application/template-api-modules/src/lib/modules/shared/api/user-profile/user-profile.service.ts @@ -1,7 +1,10 @@ import { Injectable } from '@nestjs/common' -import { Auth, AuthMiddleware } from '@island.is/auth-nest-tools' +import { Auth, AuthMiddleware, User } from '@island.is/auth-nest-tools' import { IslyklarApi } from '@island.is/clients/islykill' -import { UserProfileApi } from '@island.is/clients/user-profile' +import { + UserProfileControllerFindUserProfileClientTypeEnum, + V2UsersApi, +} from '@island.is/clients/user-profile' import { isRunningOnEnvironment } from '@island.is/shared/utils' import { BaseTemplateAPIModuleConfig, @@ -13,8 +16,7 @@ import { UserProfile, UserProfileParameters, } from '@island.is/application/types' -import { TemplateApiError } from '@island.is/nest/problem' -import { coreErrorMessages, getSlugFromType } from '@island.is/application/core' +import { getSlugFromType } from '@island.is/application/core' import { IdsClientConfig } from '@island.is/nest/config' import { Inject } from '@nestjs/common' import { ConfigService, ConfigType } from '@nestjs/config' @@ -25,7 +27,7 @@ export const MAX_OUT_OF_DATE_MONTHS = 6 @Injectable() export class UserProfileService extends BaseTemplateApiService { constructor( - private readonly userProfileApi: UserProfileApi, + private readonly userProfileApi: V2UsersApi, private readonly islyklarApi: IslyklarApi, @Inject(IdsClientConfig.KEY) private idsClientConfig: ConfigType, @@ -35,60 +37,50 @@ export class UserProfileService extends BaseTemplateApiService { super('UserProfile') } - userProfileApiWithAuth(auth: Auth): UserProfileApi { + userProfileApiWithAuth(auth: Auth): V2UsersApi { return this.userProfileApi.withMiddleware(new AuthMiddleware(auth)) } async userProfile({ - application, auth, - params, }: TemplateApiModuleActionProps): Promise { - // Temporary solution while we still run the old user profile service. - return this.islyklarApi - .islyklarGet({ ssn: auth.nationalId }) - - .then((results) => { - if (params?.validateBankInformation && !results?.bankInfo) { - // If individual does not have a valid bank account, then we fail this check - throw new TemplateApiError( - { - title: coreErrorMessages.noBankAccountError, - summary: coreErrorMessages.noBankAccountError, - }, - 400, - ) - } - - if (params?.validateEmail && !results?.email) { - throw new TemplateApiError( - { - title: coreErrorMessages.noEmailFound, - summary: { - ...coreErrorMessages.noEmailFoundDescription, - values: { link: this.getIDSLink(application) }, - }, - }, - 500, - ) - } - - return { - mobilePhoneNumber: results?.mobile, - email: results?.email, - bankInfo: results?.bankInfo, - } + const { mobilePhoneNumber, email } = await this.userProfileApiWithAuth(auth) + .userProfileControllerFindUserProfile({ + xParamNationalId: auth.nationalId, + clientType: + UserProfileControllerFindUserProfileClientTypeEnum.FirstParty, }) .catch((error) => { if (isRunningOnEnvironment('local')) { return { email: 'mockEmail@island.is', mobilePhoneNumber: '9999999', - bankInfo: '0000-11-222222', } } - if (params?.catchMock) { - return {} + throw error + }) + /// Temporary dependency on íslykill for bank info retrieval via FJS API. + /// A refactor is planned to integrate bank info directly from FJS API to eliminate íslykill dependency. + const bankInfo = await this.getBankInfoFromIslykill(auth) + + return { + mobilePhoneNumber: mobilePhoneNumber ?? undefined, + email: email ?? undefined, + bankInfo, + } + } + + private async getBankInfoFromIslykill( + auth: User, + ): Promise { + return this.islyklarApi + .islyklarGet({ ssn: auth.nationalId }) + .then((results) => { + return results?.bankInfo + }) + .catch((error) => { + if (isRunningOnEnvironment('local')) { + return '0000-11-222222' } throw error })