Skip to content

Commit

Permalink
fix: optimistically update lastUpdatedBy on complete [TECH-1440] (#320)
Browse files Browse the repository at this point in the history
* fix: optimistically update lastUpdatedBy on complete [TECH-1440]

---------

Co-authored-by: Jan-Gerke Salomon <[email protected]>
  • Loading branch information
tomzemp and Mohammer5 authored Aug 15, 2023
1 parent 0668a62 commit 7df7ef5
Show file tree
Hide file tree
Showing 13 changed files with 837 additions and 510 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dhis2-verify-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
CYPRESS_dhis2BaseUrl: https://debug.dhis2.org/dev
CYPRESS_dhis2ApiVersion: 40
CYPRESS_dhis2ApiVersion: 41
CYPRESS_networkMode: stub
REPORTPORTAL_API_KEY: ${{ secrets.REPORTPORTAL_API_KEY }}
REPORTPORTAL_ENDPOINT: ${{ vars.REPORTPORTAL_ENDPOINT }}
Expand Down
2 changes: 1 addition & 1 deletion cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ module.exports = defineConfig({
env: {
dhis2DataTestPrefix: 'dhis2-dataentry',
networkMode: 'live',
dhis2ApiVersion: '40',
dhis2ApiVersion: '41',
},
})
144 changes: 104 additions & 40 deletions cypress/fixtures/network/41/a_data_set_can_be_selected.json

Large diffs are not rendered by default.

266 changes: 232 additions & 34 deletions cypress/fixtures/network/41/a_period_can_be_selected.json

Large diffs are not rendered by default.

232 changes: 51 additions & 181 deletions cypress/fixtures/network/41/a_section_filter_can_be_selected.json

Large diffs are not rendered by default.

176 changes: 55 additions & 121 deletions cypress/fixtures/network/41/an_org_unit_can_be_selected.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

286 changes: 209 additions & 77 deletions cypress/fixtures/network/41/static_resources.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cypress/fixtures/network/41/summary.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"count": 1139,
"totalResponseSize": 5375655,
"duplicates": 1045,
"nonDeterministicResponses": 53,
"count": 1420,
"totalResponseSize": 5850276,
"duplicates": 1316,
"nonDeterministicResponses": 103,
"apiVersion": "41",
"fixtureFiles": [
"static_resources.json",
Expand Down
3 changes: 3 additions & 0 deletions src/shared/completion/use-set-form-completion-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useAlert, useDataEngine } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { useQueryClient, useMutation } from '@tanstack/react-query'
import { useDataValueSetQueryKey } from '../use-data-value-set/index.js'
import { useUsername } from '../use-user-info/use-user-info.js'
import { mutationKeys } from './mutation-key-factory.js'
import useSetFormCompletionMutationKey from './use-set-form-completion-mutation-key.js'

Expand Down Expand Up @@ -49,6 +50,7 @@ export function useSetFormCompletionMutation() {

const dataValueSetQueryKey = useDataValueSetQueryKey()
const mutationKey = useSetFormCompletionMutationKey()
const { data: userInfo } = useUsername()

return useMutation(mutationFn, {
// retry: 0, // @TODO: Is this correct?
Expand All @@ -67,6 +69,7 @@ export function useSetFormCompletionMutation() {
completeStatus: {
...previousDataValueSet?.completeStatus,
complete,
lastUpdatedBy: userInfo?.username,
},
}))

Expand Down
18 changes: 17 additions & 1 deletion src/shared/completion/use-set-form-completion-mutation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { Wrapper } from '../../test-utils/index.js'
import { useDataValueSet } from '../use-data-value-set/index.js'
import { useSetFormCompletionMutation } from './use-set-form-completion-mutation.js'

jest.mock('../use-user-info/use-user-info.js', () => ({
useUsername: jest.fn(() => ({
data: {
username: 'some new user',
},
})),
useUserInfo: jest.fn(() => ({
data: {
authorities: ['ALL'],
},
})),
}))

jest.mock('../use-data-value-set/use-data-value-set-query-key.js', () => ({
__esModule: true,
default: jest.fn(() => ['dataValues']),
Expand Down Expand Up @@ -93,7 +106,7 @@ describe('useSetFormCompletionMutation', () => {
})
})

it('should optimistically update the completion state', async () => {
it('should optimistically update the completion state and lastUpdatedBy', async () => {
const queryCache = new QueryCache()

const { result: setFormCompletion, waitFor: waitForSetFormCompletion } =
Expand Down Expand Up @@ -125,6 +138,9 @@ describe('useSetFormCompletionMutation', () => {
const cachedDataValues = cachedDataValuesQuery.state.data

expect(cachedDataValues.completeStatus.complete).toBe(true)
expect(cachedDataValues.completeStatus.lastUpdatedBy).toBe(
'some new user'
)
})

it('should revert to the old cache state when an error occurs', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/use-user-info/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { useUserInfo } from './use-user-info.js'
export { useUserInfo, useUsername } from './use-user-info.js'
export { default as useCanUserEditFields } from './use-can-user-edit-fields.js'
export * as userInfoSelectors from './user-info-selectors.js'
12 changes: 12 additions & 0 deletions src/shared/use-user-info/use-user-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ export const useUserInfo = () => {
const userInfoQuery = useQuery(queryKey, queryOpts)
return userInfoQuery
}

const queryKeyUsername = [`/me?fields=username`]

const queryOptsUsername = {
refetchOnMount: false,
staleTime: 24 * 60 * 60 * 1000,
}

export const useUsername = () => {
const usernameQuery = useQuery(queryKeyUsername, queryOptsUsername)
return usernameQuery
}

1 comment on commit 7df7ef5

@dhis2-bot
Copy link
Contributor

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.