-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveHubAccess.py
242 lines (179 loc) · 7.97 KB
/
removeHubAccess.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sys
from utils import utils, associates, hubs, fleets
import grantHubAccess
def get_old_hub(env, orgId, retry=False):
if not retry:
print("TYPE THE HUB'S NAME")
hubName = input("> ").strip()
try:
hub = hubs.search_hub_by_name(env=env, orgId=orgId, hubName=hubName)[0]
print(f"Hub found ({hubName} -> {hub['id']})\n")
except Exception:
print("Hub not found, please try again:")
hub = get_old_hub(env, orgId, True)
return hub
def select_answer(question=None, answers=None):
if not answers:
answers = ["Y", "N"]
if not question:
question = f"Does the associate need to maintain access to all previous hubs? ({'/'.join(answers)})"
answer = ""
print(question)
while answer not in answers:
answer = str(input("> ")).upper().strip()
return answer
def associate_has_fleet(associate):
fleetId = ""
try:
fleetId = associate["fleetId"]
except Exception:
pass
return fleetId != ""
def get_associate_hubs_from_fleet(env, orgId, associate):
idsList = []
fleet = ""
if associate_has_fleet(associate):
fleet = associate["fleetId"]
print(f">> Associate has fleet {fleet}")
fleetHubs = fleets.get_hubs_from_fleet(env=env, orgId=orgId, fleetId=fleet)
# print(f">> Hubs in {fleet}: {fleetHubs}")
for hubId in fleetHubs:
idsList.append(hubId)
else:
idsList.append(associate["hubId"])
return idsList
def fill_hubs_list(env, orgId, hubIdsList):
hubsList = []
for hubId in hubIdsList:
hubsList.append(hubs.search_hub_by_id(env, orgId, hubId)[0])
return hubsList
def search_fleet_with_hubs(allFleets, hubIdsArray):
hubIdsArray.sort()
for fleet in allFleets:
try:
fleetHubs = fleet.get("hubIds")
if len(fleetHubs) == len(hubIdsArray):
found = True
fleetHubs.sort()
for hubid in hubIdsArray:
if hubid not in fleetHubs:
found = False
if found:
return fleet["fleetId"]
except Exception:
print(f"** No hubIds in {fleet.get('fleetId')}")
print("Fleet not found!")
return None
def update_associate(env, associate, userName):
response = associates.update_associate_data(env, associate, userName)
print(
f">> Updating associate data: {response}\n{response.text if response.status_code >= 400 else ''}"
)
def main():
userName = input("What is your name?\n> ")
env = utils.select_env()
orgId = utils.select_org(env)
print("TYPE THE ASSOCIATE ID")
associateId = input("> ")
associate = associates.get_associate_data(
env=env, orgId=orgId, associateId=associateId
)
if associate is not None:
print("Associate Found")
print()
accountType = associate["associateType"]
if accountType not in ["DRIVER", "SORTER"]:
oldHub = get_old_hub(env, orgId)
allFleets = fleets.search_fleet(env, orgId)
# print(f">> Loaded {len(allFleets)} fleets")
hubIdsList = get_associate_hubs_from_fleet(env, orgId, associate)
if oldHub["id"] in hubIdsList:
hubIdsList = [h for h in hubIdsList if h != oldHub.get("id")]
hubsList = fill_hubs_list(env, orgId, hubIdsList)
print(
f">> Searching for a fleet with {' '.join([hub['name'] for hub in hubsList])}"
)
fleet = search_fleet_with_hubs( # searching for a fleet with same hubs
allFleets=allFleets, hubIdsArray=hubIdsList
)
if fleet is not None: # if a fleet with the correct hubs already exists
print(f">> Fleet found! Updating associate data with {fleet}")
associate["fleetId"] = fleet
update_associate(env, associate, userName)
else:
if associate_has_fleet(
associate
): # if associate already have a fleetId
fleetId = associate["fleetId"]
print(">> Checking if other associates use the same fleet")
associatesWithSameFleet = associates.search_associate(
env=env,
org_id=orgId,
key_type_index=11, # fleetId (11)
search_key=fleetId,
)
if (
len(associatesWithSameFleet) == 1
and associatesWithSameFleet is not None
):
# if only this associate has this fleet id
# means we can just update his fleet instead of creating another one
print(">> No other associate use this fleetId")
print(
f">> Updating Fleet: {fleets.update_fleet_hubs(env, orgId, fleetId, hubsList)}"
)
else:
# In this case we need to create a new fleet
print(">> Someone else uses this fleetId as well")
fleet = fleets.search_fleet(env, orgId, fleetId)
companyName = grantHubAccess.get_company_name(
associate.get("companyId")
)
fleetId = grantHubAccess.create_new_fleet(
env, orgId, hubsList, companyName, fleet.get("logoUrl")
)
associate["fleetId"] = fleetId
update_associate(env, associate, userName)
else:
print(">> Associate doesn't have a fleet")
print(
f">> Creating new fleet with {[h.get('name') for h in hubsList]}"
)
companyName = grantHubAccess.get_company_name(
associate.get("companyId")
)
fleetId = grantHubAccess.create_new_fleet(
env, orgId, hubsList, companyName
)
associate["fleetId"] = fleetId
update_associate(env, associate, userName)
else:
st = f"{oldHub.get('name')} not present in associate's fleet"
if oldHub.get("id") == associate.get("hubId"):
st += ", but equals to the associate's HUB ID.\nWould you like to move the associate to another HUB?"
if select_answer(st) == "Y":
newHub = get_old_hub(env, orgId)
associate["hubId"] = newHub["id"]
associate["location"]["locationId"] = newHub["location"][
"locationId"
]
newAssociateData = {}
for header in associate.keys():
if header not in [
"fleetId",
"preferredVehicle",
"preferredRoute",
]:
newAssociateData[header] = associate[header]
update_associate(env, newAssociateData, userName)
else:
print(st)
sys.exit()
# quits the program if the hub is already on associate's fleet
else:
print(
"Sorry, this associate is a driver and we can't give drivers access to other hubs!"
)
print("Finishing script...")
sys.exit()
main()