-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor some user invite code #514
Merged
alex-yau-ttd
merged 8 commits into
main
from
ajy-UID2-3878-Refactor-some-user-invite-code
Sep 4, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce66901
Refactor some user invite code
alex-yau-ttd f479ea9
Remove unused imports
alex-yau-ttd 5b776c4
Create wrapper method to assign keycloak user role
alex-yau-ttd 3f6273a
Delete userStore.ts
alex-yau-ttd 1ea6565
Rename and move const
alex-yau-ttd e9f26d5
Fix build
alex-yau-ttd 1d39485
Re-add trailing commas
alex-yau-ttd 8a0108e
Fix trailing comma for userController
alex-yau-ttd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import { | |
ParticipantDTO, | ||
ParticipantStatus, | ||
} from '../../entities/Participant'; | ||
import { UserDTO, UserJobFunction } from '../../entities/User'; | ||
import { UserDTO } from '../../entities/User'; | ||
import { siteIdNotSetError } from '../../helpers/errorHelpers'; | ||
import { getTraceId } from '../../helpers/loggingHelpers'; | ||
import { getKcAdminClient } from '../../keycloakAdminClient'; | ||
|
@@ -42,11 +42,7 @@ import { | |
constructAuditTrailObject, | ||
performAsyncOperationWithAuditTrail, | ||
} from '../../services/auditTrailService'; | ||
import { | ||
assignClientRoleToUser, | ||
createNewUser, | ||
sendInviteEmail, | ||
} from '../../services/kcUsersService'; | ||
import { assignApiParticipantMemberRole } from '../../services/kcUsersService'; | ||
import { | ||
addSharingParticipants, | ||
deleteSharingParticipants, | ||
|
@@ -60,18 +56,14 @@ import { | |
UserParticipantRequest, | ||
} from '../../services/participantsService'; | ||
import { getSignedParticipants } from '../../services/signedParticipantsService'; | ||
import { | ||
createUserInPortal, | ||
findUserByEmail, | ||
getAllUserFromParticipant, | ||
} from '../../services/usersService'; | ||
import { getAllUserFromParticipant } from '../../services/usersService'; | ||
import { createBusinessContactsRouter } from '../businessContactsRouter'; | ||
import { createParticipantUsersRouter } from '../participantUsersRouter'; | ||
import { getParticipantAppNames, setParticipantAppNames } from './participantsAppIds'; | ||
import { createParticipant, createParticipantFromRequest } from './participantsCreation'; | ||
import { createParticipantFromRequest, handleCreateParticipant } from './participantsCreation'; | ||
import { getParticipantDomainNames, setParticipantDomainNames } from './participantsDomainNames'; | ||
import { getParticipantKeyPairs } from './participantsKeyPairs'; | ||
import { getParticipantUsers } from './participantsUsers'; | ||
import { getParticipantUsers, handleInviteUserToParticipant } from './participantsUsers'; | ||
|
||
export type AvailableParticipantDTO = Required<Pick<ParticipantDTO, 'name' | 'siteId' | 'types'>>; | ||
|
||
|
@@ -175,7 +167,7 @@ export function createParticipantsRouter() { | |
await setSiteClientTypes(data); | ||
await Promise.all( | ||
usersFromParticipant.map((currentUser) => | ||
assignClientRoleToUser(kcAdminClient, currentUser.email, 'api-participant-member') | ||
assignApiParticipantMemberRole(kcAdminClient, currentUser.email) | ||
) | ||
); | ||
|
||
|
@@ -210,7 +202,7 @@ export function createParticipantsRouter() { | |
} | ||
); | ||
|
||
participantsRouter.put('/', createParticipant); | ||
participantsRouter.put('/', handleCreateParticipant); | ||
|
||
participantsRouter.use('/:participantId', verifyAndEnrichParticipant); | ||
|
||
|
@@ -220,62 +212,7 @@ export function createParticipantsRouter() { | |
return res.status(200).json(participant); | ||
}); | ||
|
||
const invitationParser = z.object({ | ||
firstName: z.string(), | ||
lastName: z.string(), | ||
email: z.string(), | ||
jobFunction: z.nativeEnum(UserJobFunction), | ||
}); | ||
|
||
participantsRouter.post( | ||
'/:participantId/invite', | ||
async (req: UserParticipantRequest, res: Response) => { | ||
try { | ||
const { participant, user } = req; | ||
const { firstName, lastName, email, jobFunction } = invitationParser.parse(req.body); | ||
const traceId = getTraceId(req); | ||
// TODO: UID2-3878 - support user belonging to multiple participants by not 400ing here if the user already exists. | ||
const existingUser = await findUserByEmail(email); | ||
if (existingUser) { | ||
return res.status(400).send('Error inviting user'); | ||
} | ||
const kcAdminClient = await getKcAdminClient(); | ||
const auditTrailInsertObject = constructAuditTrailObject( | ||
user!, | ||
AuditTrailEvents.ManageTeamMembers, | ||
{ | ||
action: AuditAction.Add, | ||
firstName, | ||
lastName, | ||
email, | ||
jobFunction, | ||
}, | ||
participant!.id | ||
); | ||
|
||
await performAsyncOperationWithAuditTrail(auditTrailInsertObject, traceId, async () => { | ||
const newUser = await createNewUser(kcAdminClient, firstName, lastName, email); | ||
await createUserInPortal( | ||
{ | ||
email, | ||
jobFunction, | ||
firstName, | ||
lastName, | ||
}, | ||
participant!.id | ||
); | ||
await sendInviteEmail(kcAdminClient, newUser); | ||
}); | ||
|
||
return res.sendStatus(201); | ||
} catch (err) { | ||
if (err instanceof z.ZodError) { | ||
return res.status(400).send(err.issues); | ||
} | ||
throw err; | ||
} | ||
} | ||
); | ||
participantsRouter.post('/:participantId/invite', handleInviteUserToParticipant); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extracted this out - functionality is the same (will be changed in a subsequent PR) |
||
|
||
participantsRouter.get( | ||
'/:participantId/sharingPermission', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,78 @@ | ||
import { Response } from 'express'; | ||
import { z } from 'zod'; | ||
|
||
import { ParticipantRequest } from '../../services/participantsService'; | ||
import { getAllUserFromParticipant } from '../../services/usersService'; | ||
import { AuditAction, AuditTrailEvents } from '../../entities/AuditTrail'; | ||
import { UserJobFunction } from '../../entities/User'; | ||
import { getTraceId } from '../../helpers/loggingHelpers'; | ||
import { getKcAdminClient } from '../../keycloakAdminClient'; | ||
import { | ||
constructAuditTrailObject, | ||
performAsyncOperationWithAuditTrail, | ||
} from '../../services/auditTrailService'; | ||
import { createNewUser, sendInviteEmailToNewUser } from '../../services/kcUsersService'; | ||
import { ParticipantRequest, UserParticipantRequest } from '../../services/participantsService'; | ||
import { | ||
createUserInPortal, | ||
findUserByEmail, | ||
getAllUserFromParticipant, | ||
} from '../../services/usersService'; | ||
|
||
export async function getParticipantUsers(req: ParticipantRequest, res: Response) { | ||
const { participant } = req; | ||
const users = await getAllUserFromParticipant(participant!); | ||
return res.status(200).json(users); | ||
} | ||
|
||
const invitationParser = z.object({ | ||
firstName: z.string(), | ||
lastName: z.string(), | ||
email: z.string(), | ||
jobFunction: z.nativeEnum(UserJobFunction), | ||
}); | ||
|
||
export async function handleInviteUserToParticipant(req: UserParticipantRequest, res: Response) { | ||
try { | ||
const { participant, user } = req; | ||
const { firstName, lastName, email, jobFunction } = invitationParser.parse(req.body); | ||
const traceId = getTraceId(req); | ||
// TODO: UID2-3878 - support user belonging to multiple participants by not 400ing here if the user already exists. | ||
const existingUser = await findUserByEmail(email); | ||
if (existingUser) { | ||
return res.status(400).send('Error inviting user'); | ||
} | ||
const kcAdminClient = await getKcAdminClient(); | ||
const auditTrailInsertObject = constructAuditTrailObject( | ||
user!, | ||
AuditTrailEvents.ManageTeamMembers, | ||
{ | ||
action: AuditAction.Add, | ||
firstName, | ||
lastName, | ||
email, | ||
jobFunction, | ||
}, | ||
participant!.id | ||
); | ||
|
||
await performAsyncOperationWithAuditTrail(auditTrailInsertObject, traceId, async () => { | ||
const newUser = await createNewUser(kcAdminClient, firstName, lastName, email); | ||
await createUserInPortal( | ||
{ | ||
email, | ||
jobFunction, | ||
firstName, | ||
lastName, | ||
}, | ||
participant!.id | ||
); | ||
await sendInviteEmailToNewUser(kcAdminClient, newUser); | ||
}); | ||
|
||
return res.sendStatus(201); | ||
} catch (err) { | ||
if (err instanceof z.ZodError) { | ||
return res.status(400).send(err.issues); | ||
} | ||
throw err; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed because I split the handler into two functions, one of which is called
createParticipant