/
import-employees-from-alibeez.ts
92 lines (85 loc) · 2.34 KB
/
import-employees-from-alibeez.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
import * as firebase from "firebase-admin";
import fetch from "node-fetch";
import { AlibeezConfig } from "./config";
interface AlibeezEmployee {
fullName: string;
zenikaEmail: string;
operationalManagerShortUsername: string;
geographicalAgency: string;
}
export interface Employee {
fullName: string;
email: string;
managerEmail: string;
agency: string;
}
const obfuscateKey = (key: string) => {
if (key) {
if (key.length > 8) {
return `${key.substr(0, 4)}***`;
} else {
return "***";
}
} else {
return null;
}
};
const hasValidEmail = (employee: AlibeezEmployee) =>
employee.zenikaEmail && employee.zenikaEmail.endsWith("@zenika.com");
const hasNoValidEmail = (employee: AlibeezEmployee) => !hasValidEmail(employee);
export const importEmployeesFromAlibeez = async (config: AlibeezConfig) => {
const requestRef = await firebase
.firestore()
.collection("alibeez-requests")
.add({
at: new Date().toISOString(),
url: config.url,
key: obfuscateKey(config.key)
});
const response = await fetch(config.url, {
headers: {
Authorization: config.key
}
});
const responseCollection = firebase
.firestore()
.collection("alibeez-responses");
await responseCollection.add({
request: requestRef,
at: new Date().toISOString(),
ok: response.ok,
status: response.status
});
if (!response.ok) {
return;
}
const employees: AlibeezEmployee[] = await response.json();
const employeesWithValidEmail = employees.filter(hasValidEmail);
const employeesWithNoValidEmail = employees.filter(hasNoValidEmail);
employeesWithNoValidEmail.forEach(employee => {
console.info("employee with no valid email: " + employee.fullName);
});
const batch = firebase.firestore().batch();
employeesWithValidEmail
.map(
employee =>
({
fullName: employee.fullName,
email: employee.zenikaEmail,
managerEmail: employee.operationalManagerShortUsername
? employee.operationalManagerShortUsername + "@zenika.com"
: null,
agency: employee.geographicalAgency
} as Employee)
)
.forEach(employee =>
batch.set(
firebase
.firestore()
.collection("employees")
.doc(employee.email),
employee
)
);
await batch.commit();
};