Skip to content

Commit

Permalink
Make an onboarding contact field required for specified roles
Browse files Browse the repository at this point in the history
  • Loading branch information
ba1uev committed Feb 9, 2024
1 parent 855d1d3 commit ff49b86
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const UserRolesEditorModal: React.FC<{
}, [props.onChange, props.onClose, groupedRoleIds, unsupportedRoleIds])

return (
<Modal title="Role editor" size="normal" onClose={onCloseSafe}>
<Modal title="Roles editor" size="normal" onClose={onCloseSafe}>
<div className="mb-6">
<UserLabel user={user} />
</div>
Expand Down
14 changes: 10 additions & 4 deletions src/modules/users/client/components/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,19 @@ const Contacts: React.FC<{
onSubmit: (value: { contacts: Record<string, string> }) => void
onMoveBack: () => void
}> = ({ metadata, onSubmit, onMoveBack }) => {
const me = useStore(stores.me)
const metadataFields = Object.keys(metadata)
const [state, setState] = React.useState<Record<string, string>>({})
const [isValid, setIsValid] = React.useState(false)

const requiredFieldsIds: string[] = metadataFields.filter(
(contactId) => metadata[contactId].required
)
const requiredFieldsIds: string[] = metadataFields.filter((contactId) => {
const contactField = metadata[contactId]
const userRoles = me?.roles || []
return (
contactField.required ||
fp.hasIntersection(contactField.requiredForRoles, userRoles)
)
})

const [selectedFieldIds, setSelectedFieldIds] =
React.useState<string[]>(requiredFieldsIds)
Expand Down Expand Up @@ -462,7 +468,7 @@ const Contacts: React.FC<{
}
label={x.label}
containerClassName="w-full mb-4"
required={x.required}
required={requiredFieldsIds.includes(x.id)}
/>
)
})}
Expand Down
1 change: 1 addition & 0 deletions src/modules/users/metadata-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const contactFieldSchema = z.object({
label: z.string().optional(),
placeholder: z.string().optional(),
required: z.boolean().optional(),
requiredForRoles: z.array(z.string()).default([]),
prefix: z.string().optional(), // Optional because not all fields have it
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ module.exports = {
`,
{ transaction }
)
await queryInterface.changeColumn(
'users',
'role',
{
type: DataTypes.STRING,
allowNull: true,
},
{ transaction }
)
})
},
async down({ context: queryInterface, appConfig }) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/users/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ const adminRouter: FastifyPluginCallback = async function (fastify, opts) {
if (!roles.length) continue
const users = await fastify.db.User.findAll({
where: {
id: { [Op.not]: req.params.userId },
roles: {
[Op.overlap]: roles,
},
Expand Down
1 change: 1 addition & 0 deletions src/modules/users/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export type ProfileField = {
required: boolean
placeholder?: string
prefix?: string
requiredForRoles: string[]
}

export type ProfileFieldsMetadata = {
Expand Down

0 comments on commit ff49b86

Please sign in to comment.