Skip to content

Commit 192e5a1

Browse files
authored
fix: use earliest role entry available when none found at the given point in time (#9308)
* fix: use closest role entry when none available Records generated for demo purposes include history entries dated before even a user was created in the system. Therefore, those history entries are unable to be matched against the role of the user in that particular time period. We are now using the first role entry available for that user in those cases. * test: add test cases for getUserRoleFromHistory * docs: add CHANGELOG entry * chore: grammatical soundness * docs: move CHANGELOG entry under 1.7.1
1 parent 248d961 commit 192e5a1

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Changelog
22

3-
## 1.7.0 Release candidate
3+
## 1.7.1 Release candidate
4+
5+
### Bug fixes
6+
7+
- Use the first role assigned to a user for record history entry if no role found at the point of time when the action was performed [#9300](https://github.com/opencrvs/opencrvs-core/issues/9300)
8+
9+
## [1.7.0](https://github.com/opencrvs/opencrvs-core/compare/v1.6.2...v1.7.0)
410

511
### Breaking changes
612

packages/commons/src/fhir/practitioner.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function getPractitionerContactDetails(practitioner: Practitioner) {
8686

8787
export const getUserRoleFromHistory = (
8888
practitionerRoleHistory: PractitionerRoleHistory[],
89-
lastModified: string
89+
timePoint: string
9090
) => {
9191
const practitionerRoleHistorySorted = practitionerRoleHistory.sort((a, b) => {
9292
if (a.meta?.lastUpdated === b.meta?.lastUpdated) {
@@ -104,12 +104,15 @@ export const getUserRoleFromHistory = (
104104
)
105105
})
106106

107-
const result = practitionerRoleHistorySorted.find(
108-
(it) =>
109-
it?.meta?.lastUpdated &&
110-
lastModified &&
111-
it?.meta?.lastUpdated <= lastModified!
112-
)
107+
/*
108+
* Find the the first history entry that was added before the
109+
* given point in time or take the earliest entry if none found
110+
*/
111+
const result =
112+
practitionerRoleHistorySorted.find(
113+
(it) =>
114+
it?.meta?.lastUpdated && timePoint && it?.meta?.lastUpdated <= timePoint
115+
) ?? practitionerRoleHistorySorted.at(-1)
113116

114117
const targetCode = result?.code?.find((element) => {
115118
return element.coding?.[0].system === 'http://opencrvs.org/specs/roles'
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* OpenCRVS is also distributed under the terms of the Civil Registration
7+
* & Healthcare Disclaimer located at http://opencrvs.org/license.
8+
*
9+
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
10+
*/
11+
import {
12+
getUserRoleFromHistory,
13+
PractitionerRoleHistory,
14+
ResourceIdentifier
15+
} from '@opencrvs/commons/types'
16+
17+
const history = [
18+
{
19+
resourceType: 'PractitionerRole',
20+
id: '1',
21+
meta: { versionId: '1', lastUpdated: '2020-01-01' },
22+
location: [{ reference: 'Location/1' as ResourceIdentifier }],
23+
code: [
24+
{
25+
coding: [
26+
{
27+
system: 'http://opencrvs.org/specs/roles',
28+
code: 'REGISTRATION_AGENT'
29+
}
30+
]
31+
}
32+
]
33+
},
34+
{
35+
resourceType: 'PractitionerRole',
36+
id: '2',
37+
location: [{ reference: 'Location/2' as ResourceIdentifier }],
38+
meta: { versionId: '2', lastUpdated: '2023-01-01' },
39+
code: [
40+
{
41+
coding: [
42+
{
43+
system: 'http://opencrvs.org/specs/roles',
44+
code: 'LOCAL_REGISTRAR'
45+
}
46+
]
47+
}
48+
]
49+
}
50+
] satisfies PractitionerRoleHistory[]
51+
52+
describe('getUserRoleFromHistory', () => {
53+
it('should find the user role from history at the given point in time', () => {
54+
expect(getUserRoleFromHistory(history, '2022-01-15')).toEqual(
55+
'REGISTRATION_AGENT'
56+
)
57+
expect(getUserRoleFromHistory(history, '2023-01-15')).toEqual(
58+
'LOCAL_REGISTRAR'
59+
)
60+
})
61+
62+
it('should find the first user role from history if there is no role entry present at the given point in time', () => {
63+
expect(getUserRoleFromHistory(history, '2019-01-15')).toEqual(
64+
'REGISTRATION_AGENT'
65+
)
66+
})
67+
})

0 commit comments

Comments
 (0)