Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2544 from riiniii/fix/2540
Browse files Browse the repository at this point in the history
fix(ImagingRequest): imaging table requestedBy show current usr
  • Loading branch information
matteovivona committed Jan 13, 2021
2 parents 7cbdf11 + 2c18c5d commit 45647d3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@
"git add ."
]
}
}
}
15 changes: 12 additions & 3 deletions src/__tests__/imagings/hooks/useRequestImaging.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ImagingRequestError } from '../../../imagings/util/validate-imaging-req
import * as imagingRequestValidator from '../../../imagings/util/validate-imaging-request'
import ImagingRepository from '../../../shared/db/ImagingRepository'
import Imaging from '../../../shared/model/Imaging'
import { UserState, LoginError } from '../../../user/user-slice'
import executeMutation from '../../test-utils/use-mutation.util'

describe('useReportIncident', () => {
Expand All @@ -13,6 +14,13 @@ describe('useReportIncident', () => {
console.error = jest.fn()
})

const user = {
fullName: 'test',
id: 'test-hospitalrun',
permissions: [],
loginError: {} as LoginError,
} as UserState

it('should save the imaging request with correct data', async () => {
const expectedDate = new Date(Date.now())
Date.now = jest.fn().mockReturnValue(expectedDate)
Expand All @@ -28,11 +36,12 @@ describe('useReportIncident', () => {
const expectedImagingRequest = {
...givenImagingRequest,
requestedOn: expectedDate.toISOString(),
requestedBy: 'test',
requestedBy: 'test-hospitalrun',
requestedByFullName: 'test',
} as Imaging
jest.spyOn(ImagingRepository, 'save').mockResolvedValue(expectedImagingRequest)

await executeMutation(() => useRequestImaging(), givenImagingRequest)
await executeMutation(() => useRequestImaging(user), givenImagingRequest)
expect(ImagingRepository.save).toHaveBeenCalledTimes(1)
expect(ImagingRepository.save).toBeCalledWith(expectedImagingRequest)
})
Expand All @@ -46,7 +55,7 @@ describe('useReportIncident', () => {
jest.spyOn(ImagingRepository, 'save').mockResolvedValue({} as Imaging)

try {
await executeMutation(() => useRequestImaging(), {})
await executeMutation(() => useRequestImaging(user), {})
} catch (e) {
expect(e).toEqual(expectedImagingRequestError)
expect(ImagingRepository.save).not.toHaveBeenCalled()
Expand Down
9 changes: 8 additions & 1 deletion src/__tests__/imagings/requests/NewImagingRequest.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ImagingRepository from '../../../shared/db/ImagingRepository'
import Imaging from '../../../shared/model/Imaging'
import Patient from '../../../shared/model/Patient'
import { RootState } from '../../../shared/store'
import { UserState, LoginError } from '../../../user/user-slice'

const mockStore = createMockStore<RootState, any>([thunk])

Expand All @@ -35,7 +36,13 @@ describe('New Imaging Request', () => {

history = createMemoryHistory()
history.push(`/imaging/new`)
const store = mockStore({} as any)
const store = mockStore({
user: {
fullName: 'test',
permissions: [],
loginError: {} as LoginError,
} as UserState,
} as any)

let wrapper: any
await act(async () => {
Expand Down
27 changes: 15 additions & 12 deletions src/imagings/hooks/useRequestImaging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ export interface ImagingRequest {
type: string
}

async function requestImaging(request: ImagingRequest): Promise<void> {
const error = validateImagingRequest(request)
function requestImagingWrapper(user: any) {
return async function requestImaging(request: ImagingRequest): Promise<void> {
const error = validateImagingRequest(request)

if (!isEmpty(error)) {
throw error
}
if (!isEmpty(error)) {
throw error
}

await ImagingRepository.save({
...request,
requestedBy: 'test',
requestedOn: new Date(Date.now()).toISOString(),
} as Imaging)
await ImagingRepository.save({
...request,
requestedBy: user?.id || '',
requestedByFullName: user?.fullName || '',
requestedOn: new Date(Date.now()).toISOString(),
} as Imaging)
}
}

export default function useRequestImaging() {
return useMutation(requestImaging, {
export default function useRequestImaging(user: any) {
return useMutation(requestImagingWrapper(user), {
onSuccess: async () => {
await queryCache.invalidateQueries('imagings')
},
Expand Down
6 changes: 5 additions & 1 deletion src/imagings/requests/NewImagingRequest.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Typeahead, Label, Button, Alert, Column, Row } from '@hospitalrun/components'
import format from 'date-fns/format'
import React, { useState, useEffect } from 'react'
import { useSelector } from 'react-redux'
import { useHistory } from 'react-router-dom'

import useAddBreadcrumbs from '../../page-header/breadcrumbs/useAddBreadcrumbs'
Expand All @@ -13,17 +14,20 @@ import TextInputWithLabelFormGroup from '../../shared/components/input/TextInput
import PatientRepository from '../../shared/db/PatientRepository'
import useTranslator from '../../shared/hooks/useTranslator'
import Patient from '../../shared/model/Patient'
import { RootState } from '../../shared/store'
import useRequestImaging, { ImagingRequest } from '../hooks/useRequestImaging'
import { ImagingRequestError } from '../util/validate-imaging-request'

const NewImagingRequest = () => {
const { t } = useTranslator()
const history = useHistory()
const { user } = useSelector((state: RootState) => state.user)

const updateTitle = useUpdateTitle()
useEffect(() => {
updateTitle(t('imagings.requests.new'))
})
const [mutate] = useRequestImaging()
const [mutate] = useRequestImaging(user)
const [error, setError] = useState<ImagingRequestError>()
const [visitOption, setVisitOption] = useState([] as Option[])

Expand Down
2 changes: 1 addition & 1 deletion src/imagings/search/ImagingRequestTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ImagingRequestTable = (props: Props) => {
{
label: t('imagings.imaging.requestedBy'),
key: 'requestedBy',
formatter: (row) => extractUsername(row.requestedBy),
formatter: (row) => extractUsername(row.requestedByFullName || ''),
},
{ label: t('imagings.imaging.status'), key: 'status' },
]}
Expand Down
1 change: 1 addition & 0 deletions src/shared/model/Imaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default interface Imaging extends AbstractDBModel {
visitId: string
requestedOn: string
requestedBy: string // will be the currently logged in user's id
requestedByFullName?: string
completedOn?: string
canceledOn?: string
notes?: string
Expand Down
4 changes: 2 additions & 2 deletions src/user/user-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import Permissions from '../shared/model/Permissions'
import User from '../shared/model/User'
import { AppThunk } from '../shared/store'

interface LoginError {
export interface LoginError {
message?: string
username?: string
password?: string
}

interface UserState {
export interface UserState {
permissions: (Permissions | null)[]
user?: User
loginError?: LoginError
Expand Down

1 comment on commit 45647d3

@vercel
Copy link

@vercel vercel bot commented on 45647d3 Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.