-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Policy.js
212 lines (190 loc) · 6.78 KB
/
Policy.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
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
203
204
205
206
207
208
209
210
211
212
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import {
GetPolicySummaryList, GetPolicyList, Policy_Employees_Merge, Policy_Create, Policy_Employees_Remove,
} from '../API';
import ONYXKEYS from '../../ONYXKEYS';
import {formatPersonalDetails} from './PersonalDetails';
import Growl from '../Growl';
import CONST from '../../CONST';
import {translateLocal} from '../translate';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
const allPolicies = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (val && key) {
allPolicies[key] = {...allPolicies[key], ...val};
}
},
});
/**
* Takes a full policy summary that is returned from the policySummaryList and simplifies it so we are only storing
* the pieces of data that we need to in Onyx
*
* @param {Object} fullPolicy
* @param {String} fullPolicy.id
* @param {String} fullPolicy.name
* @param {String} fullPolicy.role
* @param {String} fullPolicy.type
* @returns {Object}
*/
function getSimplifiedPolicyObject(fullPolicy) {
return {
id: fullPolicy.id,
name: fullPolicy.name,
role: fullPolicy.role,
type: fullPolicy.type,
};
}
/**
* Simplifies the policyList response into an object containing an array of emails
*
* @param {Object} fullPolicy
* @returns {Object}
*/
function getSimplifiedEmployeeListObject(fullPolicy) {
const employeeListEmails = _.chain(fullPolicy.value.employeeList)
.pluck('email')
.flatten()
.unique()
.value();
return {
employeeList: employeeListEmails,
};
}
/**
* Fetches the policySummaryList from the API and saves a simplified version in Onyx
*/
function getPolicySummaries() {
GetPolicySummaryList()
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = _.reduce(data.policySummaryList, (memo, policy) => ({
...memo,
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy),
}), {});
Onyx.mergeCollection(ONYXKEYS.COLLECTION.POLICY, policyDataToStore);
}
});
}
/**
* Fetches the policyList from the API and saves a simplified version in Onyx
*/
function getPolicyList() {
GetPolicyList()
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = _.reduce(data.policyList, (memo, policy) => ({
...memo,
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedEmployeeListObject(policy),
}), {});
Onyx.mergeCollection(ONYXKEYS.COLLECTION.POLICY, policyDataToStore);
}
});
}
/**
* Remove the passed members from the policy employeeList
*
* @param {Array} members
* @param {String} policyID
*/
function removeMembers(members, policyID) {
// In case user selects only themselves (admin), their email will be filtered out and the members
// array passed will be empty, prevent the funtion from proceeding in that case as there is noone to remove
if (members.length === 0) {
return;
}
const key = `${ONYXKEYS.COLLECTION.POLICY}${policyID}`;
// Make a shallow copy to preserve original data and remove the members
const policy = _.clone(allPolicies[key]);
policy.employeeList = _.without(policy.employeeList, ...members);
// Optimistically remove the members from the policy
Onyx.set(key, policy);
// Make the API call to merge the login into the policy
Policy_Employees_Remove({
emailList: members,
policyID,
})
.then((data) => {
if (data.jsonCode === 200) {
return;
}
const policyDataWithMembersRemoved = _.clone(allPolicies[key]);
policyDataWithMembersRemoved.employeeList = [...policyDataWithMembersRemoved.employeeList, ...members];
Onyx.set(key, policyDataWithMembersRemoved);
// Show the user feedback that the removal failed
console.error(data.message);
Growl.show(translateLocal('workspace.people.genericFailureMessage'), CONST.GROWL.ERROR, 5000);
});
}
/**
* Merges the passed in login into the specified policy
*
* @param {String} login
* @param {String} welcomeNote
* @param {String} policyID
*/
function invite(login, welcomeNote, policyID) {
const key = `${ONYXKEYS.COLLECTION.POLICY}${policyID}`;
// Make a shallow copy to preserve original data, and concat the login
const policy = _.clone(allPolicies[key]);
policy.employeeList = [...policy.employeeList, login];
// Optimistically add the user to the policy
Onyx.set(key, policy);
// Make the API call to merge the login into the policy
Policy_Employees_Merge({
employees: JSON.stringify([{email: login}]),
welcomeNote,
policyID,
})
.then((data) => {
// Save the personalDetails for the invited user in Onyx
if (data.jsonCode === 200) {
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, formatPersonalDetails(data.personalDetails));
return;
}
// If the operation failed, undo the optimistic addition
const policyDataWithoutLogin = _.clone(allPolicies[key]);
policyDataWithoutLogin.employeeList = _.without(allPolicies[key].employeeList, login);
Onyx.set(key, policyDataWithoutLogin);
// Show the user feedback that the addition failed
let errorMessage = translateLocal('workspace.invite.genericFailureMessage');
if (data.jsonCode === 402) {
errorMessage += ` ${translateLocal('workspace.invite.pleaseEnterValidLogin')}`;
}
Growl.error(errorMessage, 5000);
});
}
/**
* Merges the passed in login into the specified policy
*
* @param {String} name
*/
function create(name) {
Policy_Create({type: CONST.POLICY.TYPE.FREE, policyName: name})
.then((response) => {
if (response.jsonCode !== 200) {
// Show the user feedback
const errorMessage = translateLocal('workspace.new.genericFailureMessage');
Growl.error(errorMessage, 5000);
return;
}
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${response.policyID}`, {
id: response.policyID,
type: response.policy.type,
name: response.policy.name,
role: CONST.POLICY.ROLE.ADMIN,
});
Navigation.dismissModal();
Navigation.navigate(ROUTES.getWorkspaceCardRoute(response.policyID));
});
}
export {
getPolicySummaries,
getPolicyList,
removeMembers,
invite,
create,
};