Skip to content

Commit

Permalink
fix: health delegation bug and move logic to service
Browse files Browse the repository at this point in the history
  • Loading branch information
disaerna committed Oct 1, 2024
1 parent 3a56c8c commit 3a4c4c9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 71 deletions.
2 changes: 1 addition & 1 deletion libs/api/domains/documents/src/lib/documentV2.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class DocumentResolverV2 {
@Args('input') input: DocumentsInput,
@CurrentUser() user: User,
): Promise<PaginatedDocuments> {
return this.documentServiceV2.listDocuments(user.nationalId, input)
return this.documentServiceV2.listDocuments(user, input)
}

@ResolveField('categories', () => [Category])
Expand Down
29 changes: 22 additions & 7 deletions libs/api/domains/documents/src/lib/documentV2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -75,30 +79,41 @@ export class DocumentServiceV2 {
}

async listDocuments(
nationalId: string,
user: User,
input: DocumentsInput,
): Promise<PaginatedDocuments> {
//If a delegated user is viewing the mailbox, do not return any health related data
//Category is now "1,2,3,...,n"
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,
)
}
}

const documents = await this.documentService.getDocumentList({
...restOfInput,
categoryId: mutableCategoryIds.join(),
nationalId,
nationalId: user.nationalId,
})

if (typeof documents?.totalCount !== 'number') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,4 @@ export class DocumentsInput {
@IsOptional()
@IsInt()
readonly pageSize?: number

@Field({ nullable: true })
@IsOptional()
@IsBoolean()
isLegalGuardian?: boolean
}
20 changes: 3 additions & 17 deletions libs/auth/react/src/lib/auth/Auth.state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { User } from '@island.is/shared/types'
import { getBirthday } from '../utils/getBirthday'

export type AuthState =
| 'logged-out'
Expand Down Expand Up @@ -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,
Expand All @@ -64,15 +49,16 @@ export const reducer = (
case ActionType.SIGNIN_SUCCESS:
return {
...state,
userInfo: formatUser(action.payload),
userInfo: action.payload,

authState: 'logged-in',
isAuthenticated: true,
}
case ActionType.USER_LOADED:
return state.isAuthenticated
? {
...state,
userInfo: formatUser(action.payload),
userInfo: action.payload,
}
: state
case ActionType.SIGNIN_FAILURE:
Expand Down
26 changes: 0 additions & 26 deletions libs/auth/react/src/lib/utils/getBirthday.spec.ts

This file was deleted.

15 changes: 0 additions & 15 deletions libs/service-portal/documents/src/hooks/useDocumentList.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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,
},
Expand Down

0 comments on commit 3a4c4c9

Please sign in to comment.