From 3a4c4c969881a61158d2a47787b24fd1a15c1b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81sd=C3=ADs=20Erna=20Gu=C3=B0mundsd=C3=B3ttir?= Date: Tue, 1 Oct 2024 10:34:10 +0000 Subject: [PATCH] fix: health delegation bug and move logic to service --- .../documents/src/lib/documentV2.resolver.ts | 2 +- .../documents/src/lib/documentV2.service.ts | 29 ++++++++++++++----- .../documents/src/lib/helpers}/getBirthday.ts | 0 .../src/lib/models/v2/documents.input.ts | 5 ---- libs/auth/react/src/lib/auth/Auth.state.ts | 20 ++----------- .../react/src/lib/utils/getBirthday.spec.ts | 26 ----------------- .../documents/src/hooks/useDocumentList.ts | 15 ---------- 7 files changed, 26 insertions(+), 71 deletions(-) rename libs/{auth/react/src/lib/utils => api/domains/documents/src/lib/helpers}/getBirthday.ts (100%) delete mode 100644 libs/auth/react/src/lib/utils/getBirthday.spec.ts diff --git a/libs/api/domains/documents/src/lib/documentV2.resolver.ts b/libs/api/domains/documents/src/lib/documentV2.resolver.ts index 548c858f4ac5..61235c05eab4 100644 --- a/libs/api/domains/documents/src/lib/documentV2.resolver.ts +++ b/libs/api/domains/documents/src/lib/documentV2.resolver.ts @@ -75,7 +75,7 @@ export class DocumentResolverV2 { @Args('input') input: DocumentsInput, @CurrentUser() user: User, ): Promise { - return this.documentServiceV2.listDocuments(user.nationalId, input) + return this.documentServiceV2.listDocuments(user, input) } @ResolveField('categories', () => [Category]) diff --git a/libs/api/domains/documents/src/lib/documentV2.service.ts b/libs/api/domains/documents/src/lib/documentV2.service.ts index a55b1873eb34..89312473a96e 100644 --- a/libs/api/domains/documents/src/lib/documentV2.service.ts +++ b/libs/api/domains/documents/src/lib/documentV2.service.ts @@ -18,6 +18,10 @@ import { HEALTH_CATEGORY_ID } from './document.types' import { Type } from './models/v2/type.model' import { DownloadServiceConfig } from '@island.is/nest/config' import { DocumentV2MarkAllMailAsRead } from './models/v2/markAllMailAsRead.model' +import type { User } from '@island.is/auth-nest-tools' +import { AuthDelegationType } from '@island.is/shared/types' +import { getBirthday } from './helpers/getBirthday' +import differceInYears from 'date-fns/differenceInYears' const LOG_CATEGORY = 'documents-api-v2' @Injectable() @@ -75,7 +79,7 @@ export class DocumentServiceV2 { } async listDocuments( - nationalId: string, + user: User, input: DocumentsInput, ): Promise { //If a delegated user is viewing the mailbox, do not return any health related data @@ -83,14 +87,25 @@ export class DocumentServiceV2 { const { categoryIds, ...restOfInput } = input let mutableCategoryIds = categoryIds ?? [] - if (input.isLegalGuardian) { + const isLegalGuardian = user.delegationType?.includes( + AuthDelegationType.LegalGuardian, + ) + + const birthdate = getBirthday(user.nationalId) + let childAgeIsOver15 = false + if (birthdate) { + childAgeIsOver15 = differceInYears(new Date(), birthdate) > 15 + } + const hideHealthData = isLegalGuardian && childAgeIsOver15 + + if (hideHealthData) { if (!mutableCategoryIds.length) { - mutableCategoryIds = (await this.getCategories(nationalId, true)).map( - (c) => c.id, - ) + mutableCategoryIds = ( + await this.getCategories(user.nationalId, true) + ).map((c) => c.id) } else { mutableCategoryIds = mutableCategoryIds.filter( - (c) => c === HEALTH_CATEGORY_ID, + (c) => c !== HEALTH_CATEGORY_ID, ) } } @@ -98,7 +113,7 @@ export class DocumentServiceV2 { const documents = await this.documentService.getDocumentList({ ...restOfInput, categoryId: mutableCategoryIds.join(), - nationalId, + nationalId: user.nationalId, }) if (typeof documents?.totalCount !== 'number') { diff --git a/libs/auth/react/src/lib/utils/getBirthday.ts b/libs/api/domains/documents/src/lib/helpers/getBirthday.ts similarity index 100% rename from libs/auth/react/src/lib/utils/getBirthday.ts rename to libs/api/domains/documents/src/lib/helpers/getBirthday.ts diff --git a/libs/api/domains/documents/src/lib/models/v2/documents.input.ts b/libs/api/domains/documents/src/lib/models/v2/documents.input.ts index 2399bb3eadfe..f8615073aa99 100644 --- a/libs/api/domains/documents/src/lib/models/v2/documents.input.ts +++ b/libs/api/domains/documents/src/lib/models/v2/documents.input.ts @@ -101,9 +101,4 @@ export class DocumentsInput { @IsOptional() @IsInt() readonly pageSize?: number - - @Field({ nullable: true }) - @IsOptional() - @IsBoolean() - isLegalGuardian?: boolean } diff --git a/libs/auth/react/src/lib/auth/Auth.state.ts b/libs/auth/react/src/lib/auth/Auth.state.ts index 23ae5c0fa891..74a1b1f9b785 100644 --- a/libs/auth/react/src/lib/auth/Auth.state.ts +++ b/libs/auth/react/src/lib/auth/Auth.state.ts @@ -1,5 +1,4 @@ import { User } from '@island.is/shared/types' -import { getBirthday } from '../utils/getBirthday' export type AuthState = | 'logged-out' @@ -37,20 +36,6 @@ export const initialState: AuthReducerState = { isAuthenticated: false, } -// Add dateOfBirth Date object to user profile -const formatUser = (payload: User): User | null => { - const dateOfBirth = getBirthday(payload?.profile?.nationalId) - - return { - ...payload, - scopes: payload.scopes || [], - profile: { - ...payload.profile, - dateOfBirth, - }, - } -} - export const reducer = ( state: AuthReducerState, action: Action, @@ -64,7 +49,8 @@ export const reducer = ( case ActionType.SIGNIN_SUCCESS: return { ...state, - userInfo: formatUser(action.payload), + userInfo: action.payload, + authState: 'logged-in', isAuthenticated: true, } @@ -72,7 +58,7 @@ export const reducer = ( return state.isAuthenticated ? { ...state, - userInfo: formatUser(action.payload), + userInfo: action.payload, } : state case ActionType.SIGNIN_FAILURE: diff --git a/libs/auth/react/src/lib/utils/getBirthday.spec.ts b/libs/auth/react/src/lib/utils/getBirthday.spec.ts deleted file mode 100644 index 5f53f470fca5..000000000000 --- a/libs/auth/react/src/lib/utils/getBirthday.spec.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { getBirthday } from './getBirthday' - -describe('getBirthday', () => { - it('should return date of birth from kennitala born before 2000', () => { - const date = new Date('1980-01-01T00:00') - expect(getBirthday('010180-0009')).toEqual(date) - }) - - it('should return date of birth from kennitala born after 2000', () => { - const date = new Date('2020-02-02T00:00') - expect(getBirthday('020220-0000')).toEqual(date) - }) - - it('should return date of creation from kennitala for company', () => { - const date = new Date('2020-02-02T00:00') - expect(getBirthday('420220-0000')).toEqual(date) - }) - - it('should return undefined on bad string input', () => { - expect(getBirthday('NOT A NAT-ID!')).toEqual(undefined) - }) - - it('should return undefined on bad string input 2', () => { - expect(getBirthday('134')).toEqual(undefined) - }) -}) diff --git a/libs/service-portal/documents/src/hooks/useDocumentList.ts b/libs/service-portal/documents/src/hooks/useDocumentList.ts index 29f630344b8d..c70e089cc7b7 100644 --- a/libs/service-portal/documents/src/hooks/useDocumentList.ts +++ b/libs/service-portal/documents/src/hooks/useDocumentList.ts @@ -1,9 +1,6 @@ import { useEffect } from 'react' -import { AuthDelegationType } from '@island.is/api/schema' -import { useUserInfo } from '@island.is/auth/react' import { useDocumentContext } from '../screens/Overview/DocumentContext' import { useDocumentsV2Query } from '../screens/Overview/Overview.generated' -import differenceInYears from 'date-fns/differenceInYears' export const pageSize = 10 @@ -22,17 +19,6 @@ export const useDocumentList = (props?: UseDocumentListProps) => { setSendersAvailable, } = useDocumentContext() - const userInfo = useUserInfo() - const isLegal = userInfo.profile.delegationType?.includes( - AuthDelegationType.LegalGuardian, - ) - const dateOfBirth = userInfo?.profile.dateOfBirth - let isOver15 = false - if (dateOfBirth) { - isOver15 = differenceInYears(new Date(), dateOfBirth) > 15 - } - const hideHealthData = isOver15 && isLegal - const fetchObject = { input: { senderNationalId: filterValue.activeSenders, @@ -44,7 +30,6 @@ export const useDocumentList = (props?: UseDocumentListProps) => { opened: filterValue.showUnread ? false : null, page: page, pageSize: props?.defaultPageSize ?? pageSize, - isLegalGuardian: hideHealthData, archived: filterValue.archived, bookmarked: filterValue.bookmarked, },