/
add-or-invite-users-to-teams.ts
80 lines (79 loc) · 2.61 KB
/
add-or-invite-users-to-teams.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
import {
ArcGISRequestError,
IAuthenticationManager,
} from "@esri/arcgis-rest-request";
import { addOrInviteUsersToTeam } from "./add-or-invite-users-to-team";
import {
IAddOrInviteEmail,
IAddOrInviteToTeamResult,
IUserWithOrgType,
} from "./types";
/**
* addOrInviteUsersToTeams adds/invites N users to N teams
* Initial entry point function for add/invite members flow
* when dealing with multiple teams.
* Responses from each group are then consolidated into the final returned object.
*
* @export
* @param {string[]} groupIds array of groups we are adding users to
* @param {IUserWithOrgType[]} users array of users to add to those teams
* @param {IAuthenticationManager} primaryRO primary requestOptions
* @param {boolean} [canAutoAddUser=false] Can we automatically add a user to the team?
* @param {boolean} [addUserAsGroupAdmin=false] Can the user be added to a team as an administrator of that team?
* @param {IAddOrInviteEmail} [email] Email object contains auth for the email && the email object itself
* @return {*} {Promise<{
* notAdded: string[];
* notInvited: string[];
* notEmailed: string[];
* errors: ArcGISRequestError[];
* responses: IAddOrInviteToTeamResult[];
* }>} Results object
*/
export async function addOrInviteUsersToTeams(
groupIds: string[],
users: IUserWithOrgType[],
primaryRO: IAuthenticationManager,
canAutoAddUser: boolean = false,
addUserAsGroupAdmin: boolean = false,
email?: IAddOrInviteEmail
): Promise<{
notAdded: string[];
notInvited: string[];
notEmailed: string[];
errors: ArcGISRequestError[];
responses: IAddOrInviteToTeamResult[];
}> {
let notAdded: string[] = [];
let notInvited: string[] = [];
let notEmailed: string[] = [];
let errors: ArcGISRequestError[] = [];
const responses: IAddOrInviteToTeamResult[] = [];
// need to for..of loop this as a reduce will overwrite promises during execution
// this way we get an object of each group id nicely.
for (const groupId of groupIds) {
// For each group we'll add the users to them.
const result = await addOrInviteUsersToTeam(
groupId,
users,
primaryRO,
canAutoAddUser,
addUserAsGroupAdmin,
email
);
// attach each groups results
responses.push(result);
// surface results to the top of the stack...
notAdded = notAdded.concat(result.notAdded);
errors = errors.concat(result.errors);
notInvited = notInvited.concat(result.notInvited);
notEmailed = notEmailed.concat(result.notEmailed);
}
// Return built up result object.
return {
notAdded,
notInvited,
notEmailed,
errors,
responses,
};
}