-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_employee_script.py
174 lines (144 loc) · 8.08 KB
/
create_employee_script.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
import requests
import csv
from common import superuser_login, get_employee_types, get_employee_status, add_role_to_user, get_employees_by_phone, \
get_employees_by_id
from config import config
def main():
city = config.CITY_NAME
sheets = config.BASE_PPATH / "employees"
sheet_name = sheets / (city.lower() + ".csv")
tenant_id = "pb." + city.lower()
auth_token = superuser_login()["access_token"]
start_row = 1
employee_type_list = get_employee_types(tenant_id, auth_token)
permanent_employee_type_code = None
for etype in employee_type_list:
if etype["name"] == "Permanent":
permanent_employee_type_code = etype["id"]
break
employee_status_list = get_employee_status(tenant_id, auth_token)
employed_employee_status_code = None
for estatus in employee_status_list:
if estatus["code"] == "EMPLOYED":
employed_employee_status_code = estatus["id"]
break
if permanent_employee_type_code is None:
raise Exception("There is no code for Permanent Employee Type")
if employed_employee_status_code is None:
raise Exception("There is no Code for Employed Employee Status")
with open(sheet_name) as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
current_row = 0
for row in readCSV:
current_row += 1
if current_row < start_row:
continue
headers = {'Content-Type': 'application/json'}
details = []
roles = []
is_primary = True
departments = row[6]
role_codes = row[10]
role_names = row[11]
designation = row[7]
password = row[9]
username = row[8]
mobile_number = row[2]
name = row[0]
existing_employees = get_employees_by_id(auth_token, username, tenant_id)
roles_needed = set(map(str.strip, role_codes.split("|")))
if existing_employees:
# This employee already exists
existing_mobilenumber = existing_employees[0]['mobileNumber']
roles_currently = set(map(lambda role: role['code'], existing_employees[0]['roles']))
ask_for_role_update = False
if existing_mobilenumber != mobile_number:
print(
"The employee {} already exist with mobile number {}. You have specified a different mobile number {}".format(
username, existing_mobilenumber, mobile_number))
ask_for_role_update = True
else:
print("Employee", username, tenant_id, name, mobile_number, "already exists - ",
len(existing_employees))
if roles_needed.issubset(roles_currently):
print("The employee already has all required roles. Nothing needed")
else:
if ask_for_role_update:
username_update = input(
"Would you like to add " + role_codes + " to " + username + "[Use n for skip]? ")
if username_update.lower() == "n":
print("Skipping adding required roles to user - {}".format(username))
continue
else:
print("Adding required roles to user - {}".format(username))
add_role_to_user(auth_token, username, tenant_id, roles_needed - roles_currently)
continue
existing_employees = get_employees_by_phone(auth_token, mobile_number, tenant_id)
if existing_employees:
info = map(lambda emp: "(username={}, mob={}, name={}, roles={}".format(
emp["userName"],
emp["mobileNumber"],
emp["name"],
"|".join(map(lambda role: role["code"], emp["roles"])))
, existing_employees)
print("{} Employee(s) with mobile number {} already exists".format(len(existing_employees),
mobile_number), list(info))
if len(existing_employees) > 1:
username_update = input(
"Which user would you like to update with " + role_codes + " [Use n for skip]? ")
else:
username_update = input(
"Will you like to add the " + role_codes + " to user {} [Yn]? ".format(
existing_employees[0]["userName"]))
if username_update.strip().lower() == "n":
print("Skipping the user creation for {}".format(username))
continue
else:
username_update = existing_employees[0]["userName"]
if username_update.strip().lower() == "n":
print("Skipping the user creation for {}".format(username))
continue
else:
employee_found = list(filter(lambda emp: emp["userName"] == username_update, existing_employees))
if not employee_found:
print("Cannot find employee with username - " + username_update)
else:
roles_currently = set(map(lambda role: role['code'], employee_found[0]['roles']))
add_role_to_user(auth_token, username_update, tenant_id, roles_needed - roles_currently)
continue
for department in departments.split("|"):
details.append(
{"fromDate": row[4], "toDate": row[5], "department": department.strip(), "designation": designation,
"position": row[12],
"isPrimary": is_primary, "hod": []})
is_primary = False
for role, role_name in zip(role_codes.split("|"), role_names.split("|")):
roles.append({"code": role, "name": role_name, "tenantId": tenant_id})
post_data = {
"RequestInfo": {"authToken": auth_token},
"Employee": {"code": username, "dateOfAppointment": row[4],
"employeeStatus": employed_employee_status_code, "employeeType":
permanent_employee_type_code,
"assignments": details, "jurisdictions": ["100"], "physicallyDisabled": False,
"transferredEmployee": False, "medicalRep ortProduced": True, "languagesKnown": [],
"maritalStatus": "MARRIED", "ifscCode": "ffwe", "documents": [], "serviceHistory": [],
"probation": [], "regularisation": [], "technical": [], "education": [], "test": [],
"user": {"roles": roles,
"userName": username, "name": row[0], "gender": row[1],
"mobileNumber": mobile_number,
"emailId": "", "permanentAddress": city, "permanentCity": "Punjab",
"permanentPinCode": "", "correspondenceCity": city,
"correspondencePinCode": "", "correspondenceAddress": tenant_id,
"active": True, "dob": row[3], "locale": None, "signature": "",
"type": "EMPLOYEE", "password": password, "tenantId": tenant_id},
"tenantId": tenant_id}}
post_response = requests.post(url=config.HOST + '/hr-employee-v2/employees/_create', headers=headers,
json=post_data)
print("==================================================")
print(post_data)
print("--------")
print(post_response.json())
print("==================================================")
print("\n\n")
if __name__ == "__main__":
main()