-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersService.ts
202 lines (176 loc) · 5.85 KB
/
usersService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { Request } from 'express';
import { Participant, ParticipantDTO } from '../entities/Participant';
import { User, UserDTO } from '../entities/User';
import { UserRole, UserRoleDTO } from '../entities/UserRole';
import { UserToParticipantRole } from '../entities/UserToParticipantRole';
import { SSP_WEB_BASE_URL } from '../envars';
import { getKcAdminClient } from '../keycloakAdminClient';
import { createEmailService } from './emailService';
import { EmailArgs } from './emailTypes';
import {
assignApiParticipantMemberRole,
createNewUser,
sendInviteEmailToNewUser,
} from './kcUsersService';
export interface UserRequest extends Request {
user?: User;
}
export interface SiteRequest extends Request {
siteId?: number;
}
export interface SelfResendInviteRequest extends Request {
email?: string;
}
export type UserWithParticipantRoles = UserDTO & {
isUid2Support: boolean;
isSuperUser?: boolean;
currentParticipantUserRoles?: UserRoleDTO[];
};
export type UserPartialDTO = Omit<UserDTO, 'id' | 'acceptedTerms'>;
const simplifyUserParticipantRoles = (user: User) => {
return (
user.participants?.map((participant) => {
const { participantToUserRoles, ...rest } = participant;
const currentUserRoleIds = participantToUserRoles
?.filter((mapping) => mapping.userId === user.id)
.map((role) => role.userRoleId);
return Participant.fromJson({
...rest,
currentUserRoleIds,
});
}) || []
);
};
const deduplicateParticipants = (participants: Participant[]) => {
const seenIds = new Set<number>();
return participants.filter((participant) => {
if (seenIds.has(participant.id)) {
return false;
}
seenIds.add(participant.id);
return true;
});
};
const simplifyUserParticipants = (user: User) => {
if (!user?.participants) {
return;
}
const simplifiedParticipants = simplifyUserParticipantRoles(user);
return deduplicateParticipants(simplifiedParticipants);
};
export const findUserByEmail = async (email: string) => {
const user = await User.query()
.findOne('email', email)
.where('deleted', 0)
.where('locked', 0)
.modify('withParticipants');
if (user?.participants) {
user.participants = simplifyUserParticipants(user);
}
return user;
};
const mapUsersWithParticipantRoles = async (users: User[], participantId: number) => {
const userRoles = await UserRole.query();
return users.map((user) => {
const { userToParticipantRoles, ...rest } = user;
return {
...rest,
currentParticipantUserRoles: user?.userToParticipantRoles
?.filter((x) => x.participantId === participantId)
.map((y) => {
const role = userRoles.find((r) => r.id === y.userRoleId);
return role ?? null;
}),
};
});
};
export const getAllUsersFromParticipantWithRoles = async (participant: Participant) => {
const participantUserIds = (
await UserToParticipantRole.query().where('participantId', participant.id)
).map((userToParticipantRole) => userToParticipantRole.userId);
const usersWithParticipants = await User.query()
.whereIn('id', participantUserIds)
.where('deleted', 0)
.where('locked', 0)
.withGraphFetched('userToParticipantRoles');
return mapUsersWithParticipantRoles(usersWithParticipants, participant.id);
};
export const getAllUsersFromParticipant = async (participant: Participant) => {
const participantUserIds = (
await UserToParticipantRole.query().where('participantId', participant.id)
).map((userToParticipantRole) => userToParticipantRole.userId);
return User.query().whereIn('id', participantUserIds).where('deleted', 0).where('locked', 0);
};
export const sendInviteEmailToExistingUser = (
participantName: string,
existingUser: UserDTO,
traceId: string
) => {
const emailService = createEmailService();
const emailArgs: EmailArgs = {
subject: `You have been invited to join ${participantName} in the UID2 Portal`,
templateData: {
participantName,
firstName: existingUser.firstName,
link: SSP_WEB_BASE_URL,
},
template: 'inviteExistingUserToParticipant',
to: existingUser.email,
};
emailService.sendEmail(emailArgs, traceId);
};
export const createAndInviteKeycloakUser = async (
firstName: string,
lastName: string,
email: string
) => {
const kcAdminClient = await getKcAdminClient();
const newUser = await createNewUser(kcAdminClient, firstName, lastName, email);
await assignApiParticipantMemberRole(kcAdminClient, email);
await sendInviteEmailToNewUser(kcAdminClient, newUser);
};
const addAndInviteUserToParticipant = async (
existingUser: UserDTO,
participant: ParticipantDTO,
userRoleId: number,
traceId: string
) => {
await UserToParticipantRole.query().insert({
userId: existingUser.id,
participantId: participant.id,
userRoleId,
});
sendInviteEmailToExistingUser(participant.name, existingUser, traceId);
};
const createUserInPortal = async (
user: UserPartialDTO,
participantId: number,
userRoleId: number
) => {
const newUser = await User.transaction(async (trx) => {
const createdUser = await User.query(trx).insert(user);
// Update the user/participant/role mapping
await UserToParticipantRole.query(trx).insert({
userId: createdUser?.id,
participantId,
userRoleId,
});
return createdUser;
});
return newUser;
};
export const inviteUserToParticipant = async (
userPartial: UserPartialDTO,
participant: ParticipantDTO,
userRoleId: number,
traceId: string
) => {
const existingUser = await findUserByEmail(userPartial.email);
if (existingUser) {
await addAndInviteUserToParticipant(existingUser, participant, userRoleId, traceId);
} else {
const { firstName, lastName, email } = userPartial;
await createAndInviteKeycloakUser(firstName, lastName, email);
await createUserInPortal(userPartial, participant!.id, userRoleId);
}
};