Skip to content

Commit

Permalink
feat: add strict typing to roles.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Zangetsu101 committed Dec 31, 2024
1 parent 0ad7b93 commit 5ece692
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
22 changes: 9 additions & 13 deletions src/data-seeding/roles/roles.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { SCOPES } from '@opencrvs/toolkit/scopes'
import { SCOPES, Scope } from '@opencrvs/toolkit/scopes'
import { MessageDescriptor } from 'react-intl'

export const roles = [
type Role = {
id: string
label: MessageDescriptor
scopes: Scope[]
}

export const roles: Role[] = [
{
id: 'FIELD_AGENT',
label: {
defaultMessage: 'Field Agent',
description: 'Name for user role Field Agent',
id: 'userRole.fieldAgent'
},
systemRole: 'FIELD_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand All @@ -27,7 +33,6 @@ export const roles = [
description: 'Name for user role Police Officer',
id: 'userRole.policeOfficer'
},
systemRole: 'FIELD_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand All @@ -46,7 +51,6 @@ export const roles = [
description: 'Name for user role Social Worker',
id: 'userRole.socialWorker'
},
systemRole: 'FIELD_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand All @@ -65,7 +69,6 @@ export const roles = [
description: 'Name for user role Healthcare Worker',
id: 'userRole.healthcareWorker'
},
systemRole: 'FIELD_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand All @@ -84,7 +87,6 @@ export const roles = [
description: 'Name for user role Local Leader',
id: 'userRole.localLeader'
},
systemRole: 'FIELD_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand All @@ -103,7 +105,6 @@ export const roles = [
description: 'Name for user role Registration Agent',
id: 'userRole.registrationAgent'
},
systemRole: 'REGISTRATION_AGENT',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand Down Expand Up @@ -132,7 +133,6 @@ export const roles = [
description: 'Name for user role Local Registrar',
id: 'userRole.localRegistrar'
},
systemRole: 'LOCAL_REGISTRAR',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand Down Expand Up @@ -166,7 +166,6 @@ export const roles = [
description: 'Name for user role Local System Admin',
id: 'userRole.localSystemAdmin'
},
systemRole: 'LOCAL_SYSTEM_ADMIN',
scopes: [
SCOPES.USER_READ_MY_OFFICE,
SCOPES.USER_CREATE_MY_JURISDICTION,
Expand All @@ -183,7 +182,6 @@ export const roles = [
description: 'Name for user role National System Admin',
id: 'userRole.nationalSystemAdmin'
},
systemRole: 'NATIONAL_SYSTEM_ADMIN',
scopes: [
SCOPES.USER_CREATE,
SCOPES.USER_READ,
Expand All @@ -202,7 +200,6 @@ export const roles = [
description: 'Name for user role Performance Manager',
id: 'userRole.performanceManager'
},
systemRole: 'PERFORMANCE_MANAGEMENT',
scopes: [
SCOPES.PERFORMANCE_READ,
SCOPES.PERFORMANCE_READ_DASHBOARDS,
Expand All @@ -216,7 +213,6 @@ export const roles = [
description: 'Name for user role National Registrar',
id: 'userRole.nationalRegistrar'
},
systemRole: 'NATIONAL_REGISTRAR',
scopes: [
SCOPES.RECORD_DECLARE_BIRTH,
SCOPES.RECORD_DECLARE_DEATH,
Expand Down
17 changes: 12 additions & 5 deletions src/upgrade-to-1_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ async function upgradeRolesDefinitions() {
* Create the new "roles.ts" file with the updated roles and new format
*/

const rolesWithoutOldLabels = rolesWithGeneratedIds.map((role) => {
const { oldLabels, ...rest } = role
const rolesWithoutDeprecatedFields = rolesWithGeneratedIds.map((role) => {
const { oldLabels, systemRole, ...rest } = role
return rest
})

const formattedRoles = rolesWithoutOldLabels.map((role) => {
const formattedRoles = rolesWithoutDeprecatedFields.map((role) => {
return {
...role,
scopes: new FormattedScopes(role.scopes)
Expand All @@ -311,9 +311,16 @@ async function upgradeRolesDefinitions() {
join(__dirname, './data-seeding/roles/roles.ts'),
await format(
`
import { SCOPES } from '@opencrvs/toolkit/scopes'
import { SCOPES, Scope } from '@opencrvs/toolkit/scopes'
import { MessageDescriptor } from 'react-intl'
export const roles = ${inspect(formattedRoles, { depth: null })}
type Role = {
id: string
label: MessageDescriptor
scopes: Scope[]
}
export const roles: Role[] = ${inspect(formattedRoles, { depth: null })}
`,
{
...(await resolveConfig(__dirname)),
Expand Down

0 comments on commit 5ece692

Please sign in to comment.