Skip to content

Commit

Permalink
chore(application-systems): Switching shared user profile service to …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
Ballioli and kodiakhq[bot] authored Jun 24, 2024
1 parent bc4b637 commit 1a2734e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand All @@ -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<typeof IdsClientConfig>,
Expand All @@ -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<UserProfileParameters>): Promise<UserProfile> {
// 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<string | undefined> {
return this.islyklarApi
.islyklarGet({ ssn: auth.nationalId })
.then((results) => {
return results?.bankInfo
})
.catch((error) => {
if (isRunningOnEnvironment('local')) {
return '0000-11-222222'
}
throw error
})
Expand Down

0 comments on commit 1a2734e

Please sign in to comment.