@memberjunction/server 2.20.1 → 2.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/config.d.ts +5 -1
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +2 -1
  4. package/dist/config.js.map +1 -1
  5. package/dist/generated/generated.d.ts +122 -57
  6. package/dist/generated/generated.d.ts.map +1 -1
  7. package/dist/generated/generated.js +1287 -694
  8. package/dist/generated/generated.js.map +1 -1
  9. package/dist/generic/ResolverBase.d.ts.map +1 -1
  10. package/dist/generic/ResolverBase.js +8 -4
  11. package/dist/generic/ResolverBase.js.map +1 -1
  12. package/dist/index.d.ts +7 -1
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +7 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/resolvers/AskSkipResolver.d.ts +14 -5
  17. package/dist/resolvers/AskSkipResolver.d.ts.map +1 -1
  18. package/dist/resolvers/AskSkipResolver.js +250 -61
  19. package/dist/resolvers/AskSkipResolver.js.map +1 -1
  20. package/dist/resolvers/GetDataResolver.d.ts +90 -0
  21. package/dist/resolvers/GetDataResolver.d.ts.map +1 -0
  22. package/dist/resolvers/GetDataResolver.js +294 -0
  23. package/dist/resolvers/GetDataResolver.js.map +1 -0
  24. package/dist/resolvers/SyncDataResolver.d.ts +47 -0
  25. package/dist/resolvers/SyncDataResolver.d.ts.map +1 -0
  26. package/dist/resolvers/SyncDataResolver.js +345 -0
  27. package/dist/resolvers/SyncDataResolver.js.map +1 -0
  28. package/dist/resolvers/SyncRolesUsersResolver.d.ts +46 -0
  29. package/dist/resolvers/SyncRolesUsersResolver.d.ts.map +1 -0
  30. package/dist/resolvers/SyncRolesUsersResolver.js +389 -0
  31. package/dist/resolvers/SyncRolesUsersResolver.js.map +1 -0
  32. package/package.json +23 -22
  33. package/src/config.ts +2 -0
  34. package/src/generated/generated.ts +899 -610
  35. package/src/generic/ResolverBase.ts +14 -5
  36. package/src/index.ts +12 -1
  37. package/src/resolvers/AskSkipResolver.ts +300 -66
  38. package/src/resolvers/GetDataResolver.ts +245 -0
  39. package/src/resolvers/SyncDataResolver.ts +337 -0
  40. package/src/resolvers/SyncRolesUsersResolver.ts +401 -0
@@ -0,0 +1,401 @@
1
+ import { Arg, Ctx, Field, InputType, Mutation, ObjectType, registerEnumType } from 'type-graphql';
2
+ import { AppContext } from '../types.js';
3
+ import { LogError, Metadata, RunView, UserInfo } from '@memberjunction/core';
4
+ import { RequireSystemUser } from '../directives/RequireSystemUser.js';
5
+ import { RoleEntity, UserEntity, UserRoleEntity } from '@memberjunction/core-entities';
6
+
7
+ @ObjectType()
8
+ export class SyncRolesAndUsersResultType {
9
+ @Field(() => Boolean)
10
+ Success: boolean;
11
+ }
12
+
13
+
14
+ @InputType()
15
+ export class RoleInputType {
16
+ @Field(() => String)
17
+ ID: string;
18
+
19
+ @Field(() => String)
20
+ Name: string;
21
+
22
+ @Field(() => String, {nullable: true})
23
+ Description: string;
24
+ }
25
+
26
+
27
+ export enum UserType {
28
+ Owner = "Owner",
29
+ User = "User",
30
+ }
31
+
32
+ registerEnumType(UserType, {
33
+ name: "UserType", // GraphQL Enum Name
34
+ description: "Defines whether a user is an Owner or a User",
35
+ });
36
+
37
+ @InputType()
38
+ export class UserInputType {
39
+ @Field(() => String)
40
+ ID!: string;
41
+
42
+ @Field(() => String)
43
+ Name!: string;
44
+
45
+ @Field(() => String)
46
+ Email!: string;
47
+
48
+ // the next field needs to have GraphQL enum with only Owner or User being allowed
49
+ @Field(() => UserType)
50
+ Type!: UserType;
51
+
52
+ @Field(() => String, {nullable: true})
53
+ FirstName: string;
54
+
55
+ @Field(() => String, {nullable: true})
56
+ LastName: string;
57
+
58
+ @Field(() => String, {nullable: true})
59
+ Title: string;
60
+
61
+ @Field(() => [RoleInputType], {nullable: true})
62
+ Roles?: RoleInputType[];
63
+ }
64
+
65
+
66
+ @InputType()
67
+ export class RolesAndUsersInputType {
68
+ @Field(() => [UserInputType])
69
+ public Users: UserInputType[];
70
+
71
+ @Field(() => [RoleInputType])
72
+ public Roles: RoleInputType[];
73
+ }
74
+
75
+
76
+ export class SyncRolesAndUsersResolver {
77
+ /**
78
+ * This mutation will sync both the roles and the users, and the user/role relationships in the system with the data provided in the input.
79
+ * Roles are matched by the name (case insensitive) and users are matched by email
80
+ * @param data
81
+ */
82
+ @RequireSystemUser()
83
+ @Mutation(() => SyncRolesAndUsersResultType)
84
+ async SyncRolesAndUsers(
85
+ @Arg('data', () => RolesAndUsersInputType ) data: RolesAndUsersInputType,
86
+ @Ctx() context: AppContext
87
+ ) {
88
+ try {
89
+ // first we sync the roles, then the users
90
+ const roleResult = await this.SyncRoles(data.Roles, context);
91
+ if (roleResult.Success) {
92
+ return await this.SyncUsers(data.Users, context);
93
+ }
94
+ else {
95
+ return roleResult;
96
+ }
97
+ }
98
+ catch (err) {
99
+ LogError(err);
100
+ throw new Error('Error syncing roles and users\n\n' + err);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * This mutation will sync the roles in the system with the data provided in the input, using the role name for matching (case insensitive)
106
+ * @param data
107
+ */
108
+ @RequireSystemUser()
109
+ @Mutation(() => SyncRolesAndUsersResultType)
110
+ async SyncRoles(
111
+ @Arg('roles', () => [RoleInputType]) roles: RoleInputType[],
112
+ @Ctx() context: AppContext
113
+ ) : Promise<SyncRolesAndUsersResultType> {
114
+ try {
115
+ // we iterate through the provided roles and we remove roles that are not in the input and add roles that are new
116
+ // and update roles that already exist
117
+ const rv = new RunView();
118
+ const result = await rv.RunView<RoleEntity>({
119
+ EntityName: "Roles",
120
+ ResultType: 'entity_object'
121
+ }, context.userPayload.userRecord);
122
+
123
+ if (result && result.Success) {
124
+ const currentRoles = result.Results;
125
+ if (await this.DeleteRemovedRoles(currentRoles, roles, context.userPayload.userRecord)) {
126
+ if ( await this.AddNewRoles(currentRoles, roles, context.userPayload.userRecord)) {
127
+ return await this.UpdateExistingRoles(currentRoles, roles, context.userPayload.userRecord);
128
+ }
129
+ }
130
+ }
131
+
132
+ return { Success: false }; // if we get here, something went wrong
133
+ } catch (err) {
134
+ LogError(err);
135
+ throw new Error('Error syncing roles and users\n\n' + err);
136
+ }
137
+ }
138
+
139
+ protected async UpdateExistingRoles(currentRoles: RoleEntity[], futureRoles: RoleInputType[], user: UserInfo): Promise<SyncRolesAndUsersResultType> {
140
+ // go through the future roles and update any that are in the current roles
141
+ const md = new Metadata();
142
+ let ok: boolean = true;
143
+
144
+ for (const update of futureRoles) {
145
+ const currentRole = currentRoles.find(r => r.Name.trim().toLowerCase() === update.Name.trim().toLowerCase());
146
+ if (currentRole) {
147
+ currentRole.Description = update.Description;
148
+ ok = ok && await currentRole.Save(); // no await, we do that with submit below
149
+ }
150
+ }
151
+ return { Success: ok };
152
+ }
153
+
154
+ protected async AddNewRoles(currentRoles: RoleEntity[], futureRoles: RoleInputType[], user: UserInfo): Promise<boolean> {
155
+ // go through the future roles and add any that are not in the current roles
156
+ const md = new Metadata();
157
+ let ok: boolean = true;
158
+
159
+ for (const add of futureRoles) {
160
+ if (!currentRoles.find(r => r.Name.trim().toLowerCase() === add.Name.trim().toLowerCase())) {
161
+ const role = await md.GetEntityObject<RoleEntity>("Roles", user);
162
+ role.Name = add.Name;
163
+ role.Description = add.Description;
164
+ ok = ok && await role.Save(); // no await, we do that with submit below
165
+ }
166
+ }
167
+ return ok;
168
+ }
169
+
170
+
171
+ protected async DeleteRemovedRoles(currentRoles: RoleEntity[], futureRoles: RoleInputType[], user: UserInfo): Promise<boolean> {
172
+ const rv = new RunView();
173
+ let ok: boolean = true;
174
+
175
+ // iterate through the existing roles and remove any that are not in the input
176
+ for (const remove of currentRoles) {
177
+ if (!this.IsStandardRole(remove.Name)) {
178
+ if (!futureRoles.find(r => r.Name.trim().toLowerCase() === remove.Name.trim().toLowerCase())) {
179
+ ok = ok && await this.DeleteSingleRole(remove, rv, user);
180
+ }
181
+ }
182
+ }
183
+ return ok;
184
+ }
185
+
186
+ public get StandardRoles(): string[] {
187
+ return ['Developer', 'Integration', 'UI']
188
+ }
189
+ public IsStandardRole(roleName: string): boolean {
190
+ return this.StandardRoles.find(r => r.toLowerCase() === roleName.toLowerCase()) !== undefined;
191
+ }
192
+
193
+ protected async DeleteSingleRole(role: RoleEntity, rv: RunView, user: UserInfo): Promise<boolean> {
194
+ // first, remove all the UserRole records that match this role
195
+ let ok: boolean = true;
196
+ const r2 = await rv.RunView<UserRoleEntity>({
197
+ EntityName: "User Roles",
198
+ ExtraFilter: "RoleID = '" + role.ID + "'",
199
+ ResultType: 'entity_object'
200
+ }, user);
201
+ if (r2.Success) {
202
+ for (const ur of r2.Results) {
203
+ ok = ok && await ur.Delete(); // remove the user role
204
+ }
205
+ }
206
+
207
+ return ok && role.Delete(); // remove the role
208
+ }
209
+
210
+ /**
211
+ * This mutation will sync the just the users in the system with the data provided in the input, matches existing users by email
212
+ * @important This method will NOT work if the roles are not already in sync, meaning if User/Role relationships exist in the input data where the Role doesn't already exist in this system the sync will fail
213
+ * @param data
214
+ */
215
+ @RequireSystemUser()
216
+ @Mutation(() => SyncRolesAndUsersResultType)
217
+ async SyncUsers(
218
+ @Arg('users', () => [UserInputType]) users: UserInputType[],
219
+ @Ctx() context: AppContext
220
+ ) : Promise<SyncRolesAndUsersResultType> {
221
+ try {
222
+ // first, we sync up the users and then the user roles.
223
+ // for syncing users we first remove users that are no longer in the input, then we add new users and update existing users
224
+ const rv = new RunView();
225
+ const result = await rv.RunView<UserEntity>({
226
+ EntityName: "Users",
227
+ ResultType: 'entity_object'
228
+ }, context.userPayload.userRecord);
229
+ if (result && result.Success) {
230
+ // go through current users and remove those that are not in the input
231
+ const currentUsers = result.Results;
232
+ if (await this.DeleteRemovedUsers(currentUsers, users, context.userPayload.userRecord)) {
233
+ if (await this.AddNewUsers(currentUsers, users, context.userPayload.userRecord)) {
234
+ if (await this.UpdateExistingUsers(currentUsers, users, context.userPayload.userRecord)) {
235
+ if (await this.SyncUserRoles(users, context.userPayload.userRecord)) {
236
+ return { Success: true };
237
+ }
238
+ }
239
+ }
240
+ }
241
+ }
242
+
243
+ return { Success: false }; // if we get here, something went wrong
244
+ } catch (err) {
245
+ LogError(err);
246
+ throw new Error('Error syncing roles and users\n\n' + err);
247
+ }
248
+ }
249
+
250
+ protected async UpdateExistingUsers(currentUsers: UserEntity[], futureUsers: UserInputType[], u: UserInfo): Promise<boolean> {
251
+ // go through the future users and update any that are in the current users
252
+ let ok: boolean = true;
253
+ for (const update of futureUsers) {
254
+ const current = currentUsers.find(c => c.Email?.trim().toLowerCase() === update.Email?.trim().toLowerCase());
255
+ if (current) {
256
+ current.Name = update.Name;
257
+ current.Type = update.Type;
258
+ current.FirstName = update.FirstName;
259
+ current.LastName = update.LastName;
260
+ current.Title = update.Title;
261
+ ok = ok && await current.Save();
262
+ }
263
+ }
264
+ return ok;
265
+ }
266
+ protected async AddNewUsers(currentUsers: UserEntity[], futureUsers: UserInputType[], u: UserInfo): Promise<boolean> {
267
+ // add users that are not in the current users
268
+ const md = new Metadata();
269
+ let ok: boolean = true;
270
+
271
+ for (const add of futureUsers) {
272
+ const match = currentUsers.find(currentUser => currentUser.Email?.trim().toLowerCase() === add.Email?.trim().toLowerCase());
273
+ if (match) {
274
+ // make sure the IsActive bit is set to true
275
+ match.IsActive = true;
276
+ ok = ok && await match.Save();
277
+ }
278
+ else {
279
+ const user = await md.GetEntityObject<UserEntity>("Users", u);
280
+ user.Name = add.Name;
281
+ user.Type = add.Type;
282
+ user.Email = add.Email;
283
+ user.FirstName = add.FirstName;
284
+ user.LastName = add.LastName;
285
+ user.Title = add.Title;
286
+ user.IsActive = true;
287
+
288
+ ok = ok && await user.Save();
289
+ }
290
+ }
291
+ return ok;
292
+ }
293
+
294
+ protected async DeleteRemovedUsers(currentUsers: UserEntity[], futureUsers: UserInputType[], u: UserInfo): Promise<boolean> {
295
+ // remove users that are not in the future users
296
+ const rv = new RunView();
297
+ const md = new Metadata();
298
+
299
+ let ok: boolean = true;
300
+ //const tg = await md.CreateTransactionGroup(); HAVING PROBLEMS with this, so skipping for now, I think the entire thing is wrapped in a transaction and that's causing issues with two styles of trans wrappers
301
+ for (const remove of currentUsers) {
302
+ if (remove.Type.trim().toLowerCase() !== 'owner') {
303
+ if (!futureUsers.find(r => r.Email.trim().toLowerCase() === remove.Email.trim().toLowerCase())) {
304
+ ok = ok && await this.DeleteSingleUser(remove, rv, u);
305
+ }
306
+ }
307
+ }
308
+ return ok;
309
+ }
310
+
311
+ protected async DeleteSingleUser(user: UserEntity, rv: RunView, u: UserInfo): Promise<boolean> {
312
+ // first, remove all the UserRole records that match this user
313
+ let ok: boolean = true;
314
+ const r2 = await rv.RunView<UserRoleEntity>({
315
+ EntityName: "User Roles",
316
+ ExtraFilter: "UserID = '" + user.ID + "'",
317
+ ResultType: 'entity_object'
318
+ }, u);
319
+ if (r2.Success) {
320
+ for (const ur of r2.Results) {
321
+ //ur.TransactionGroup = tg;
322
+ ok = ok && await ur.Delete(); // remove the user role
323
+ }
324
+ }
325
+ if (await user.Delete()) {
326
+ return ok;
327
+ }
328
+ else {
329
+ // in some cases there are a lot of fkey constraints that prevent the user from being deleted, so we mark the user as inactive instead
330
+ user.IsActive = false;
331
+ return await user.Save() && ok;
332
+ }
333
+ }
334
+
335
+ protected async SyncUserRoles(users: UserInputType[], u: UserInfo): Promise<boolean> {
336
+ // for each user in the users array, make sure there is a User Role that matches. First, get a list of all DATABASE user and roels so we have that for fast lookup in memory
337
+ const rv = new RunView();
338
+ const md = new Metadata();
339
+
340
+ const p1 = rv.RunView<UserEntity>({
341
+ EntityName: "Users",
342
+ ResultType: 'entity_object'
343
+ }, u);
344
+ const p2 = rv.RunView<RoleEntity>({
345
+ EntityName: "Roles",
346
+ ResultType: 'entity_object'
347
+ }, u);
348
+ const p3 = rv.RunView<UserRoleEntity>({
349
+ EntityName: "User Roles",
350
+ ResultType: 'entity_object'
351
+ }, u);
352
+
353
+ // await both
354
+ const [uResult,rResult, urResult] = await Promise.all([p1, p2, p3]);
355
+
356
+ if (uResult.Success && rResult.Success && urResult.Success) {
357
+ // we have the DB users and roles, and user roles
358
+ const dbUsers = uResult.Results;
359
+ const dbRoles = rResult.Results;
360
+ const dbUserRoles = urResult.Results;
361
+ let ok: boolean = true;
362
+
363
+ // now, we can do lookups in memory from those DB roles and Users for their ID values
364
+ // now we will iterate through the users input type and for each role, make sure it is in there
365
+ //const tg = await md.CreateTransactionGroup();
366
+ for (const user of users) {
367
+ const dbUser = dbUsers.find(u => u.Email.trim().toLowerCase() === user.Email.trim().toLowerCase());
368
+ if (dbUser) {
369
+ for (const role of user.Roles) {
370
+ const dbRole = dbRoles.find(r => r.Name.trim().toLowerCase() === role.Name.trim().toLowerCase());
371
+ if (dbRole) {
372
+ // now we need to make sure there is a user role that matches this user and role
373
+ if (!dbUserRoles.find(ur => ur.UserID === dbUser.ID && ur.RoleID === dbRole.ID)) {
374
+ // we need to add a user role
375
+ const ur = await md.GetEntityObject<UserRoleEntity>("User Roles", u);
376
+ ur.UserID = dbUser.ID;
377
+ ur.RoleID = dbRole.ID;
378
+ ok = ok && await ur.Save(); // no await, we do that with submit below
379
+ }
380
+ }
381
+ }
382
+ // now, we check for DB user roles that are NOT in the user.Roles property as they are no longer part of the user's roles
383
+ const thisUserDBRoles = dbUserRoles.filter(ur => ur.UserID === dbUser.ID);
384
+ for (const dbUserRole of thisUserDBRoles) {
385
+ const role = user.Roles.find(r => r.Name.trim().toLowerCase() === dbRoles.find(rr => rr.ID === dbUserRole.RoleID)?.Name.trim().toLowerCase());
386
+ if (!role && !this.IsStandardRole(dbUserRole.Role)) {
387
+ // this user role is no longer in the user's roles, we need to remove it
388
+ //dbUserRole.TransactionGroup = tg;
389
+ ok = ok && await dbUserRole.Delete(); // remove the user role - we use await for the DELETE, not the save
390
+ }
391
+ }
392
+ }
393
+ }
394
+ return ok;
395
+ }
396
+ else {
397
+ return false;
398
+ }
399
+ }
400
+ }
401
+