-
Notifications
You must be signed in to change notification settings - Fork 0
/
Corrigo.js
95 lines (85 loc) · 3.09 KB
/
Corrigo.js
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
// Get Corrigo Enterprise Company Token using API
function getCorrigoToken(clientID, clientSecret) {
var url = "https://oauth-pro-v2.corrigo.com/OAuth/token";
var options = {
'method': 'post',
'payload': {
'grant_type': 'client_credentials',
'client_id': clientID,
'client_secret': clientSecret
},
'contentType': 'application/x-www-form-urlencoded'
};
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response).access_token;
}
// Get Corrigo Enterprise Company Hostname using API
function getCorrigoHostname(companyName, token) {
var url = "https://am-apilocator.corrigo.com/api/v1/cmd/GetCompanyWsdkUrlCommand";
var options = {
'method': 'post',
'headers': {
"CompanyName": companyName,
"Authorization": "Bearer " + token
},
'contentType': "application/json",
'payload': JSON.stringify({
"Command": {
"ApiType": "REST",
"CompanyName": companyName,
"Protocol": "HTTPS"
}
})
};
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText()).CommandResult.Url;
}
// Create Corrigo user using child functions below
function createCorrigoUser(fName, lName, email, location) {
var sp = PropertiesService.getScriptProperties();
var corrigoName = sp.getProperty('CORRIGO_COMPANY_NAME');
// Get token/company hostname using Corrigo Authentication API
var token = getCorrigoToken(sp.getProperty('CORRIGO_CLIENT_ID'), sp.getProperty('CORRIGO_CLIENT_SECRET'));
var hostname = getCorrigoHostname(corrigoName, token);
// PDX Team ID = 3, SEA Team ID = 4
var team = location == "PDX" ? 3 : 4;
// Pass values to postCorrigoUser to create the user using the Corrigo API
var user = postCorrigoUser(hostname, corrigoName, token, fName, lName, email, 7, team);
return user ? user.EntitySpecifier.Id : null;
}
// Create new user in Corrigo using Corrigo Enterprise API
function postCorrigoUser(hostname, companyName, token, fName, lName, email, role, team) {
var url = `${hostname}api/v1/base/Employee`;
var options = {
'method': 'post',
'headers': {
"CompanyName": companyName,
"Authorization": `Bearer ${token}`
},
'contentType': "application/json",
'payload': JSON.stringify({
"Entity": {
"FirstName": fName,
"LastName": lName,
"DisplayAs": fName + " " + lName,
"Role":{"Id":role},
"ActorTypeId": "Employee",
"Username": email,
"Password": Math.random().toString(36).substring(2,12),
"Teams": [ {"TeamId": team}]
}
})
};
try {
var response = UrlFetchApp.fetch(url, options);
Logger.log(`${email}:\tCorrigo accout created`);
return JSON.parse(response.getContentText());
} catch (error) {
let err = error.toString();
if (err.startsWith(`Exception: Request failed for https://am-ce910a.corrigo.com returned code 400. Truncated server response: {"ErrorMessage":"{!{User}!} with {!{User}!} ID '${email}' already exists`)) {
Logger.log(`${email}: Corrigo account already exists`);
} else {
throw new Error(error);
}
}
}