Skip to content

Commit

Permalink
Init commit for using v2 user profile endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballioli committed Jun 10, 2024
1 parent 8374e30 commit e1e3763
Showing 1 changed file with 34 additions and 43 deletions.
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 { 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,8 @@ import {
UserProfile,
UserProfileParameters,
} from '@island.is/application/types'
import { TemplateApiError } from '@island.is/nest/problem'
import { coreErrorMessages, getSlugFromType } from '@island.is/application/core'
import type { User } from '@island.is/auth-nest-tools'
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 +28,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 +38,48 @@ 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,
email,
bankInfo,
}
}
private async getBankInfoFromIslykill(auth: User) {
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 e1e3763

Please sign in to comment.