/
role.py
162 lines (128 loc) · 5.42 KB
/
role.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
#
# Copyright (c) Magenta ApS
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
'''
Roles
-----
This section describes how to interact with employee roles.
'''
import uuid
from . import handlers
from . import org
from .validation import validator
from .. import common
from .. import exceptions
from .. import lora
from .. import mapping
from .. import util
class RoleRequestHandler(handlers.OrgFunkRequestHandler):
role_type = 'role'
function_key = mapping.ROLE_KEY
def prepare_create(self, req):
org_unit = util.checked_get(req, mapping.ORG_UNIT,
{}, required=True)
org_unit_uuid = util.get_uuid(org_unit, required=True)
employee = util.checked_get(req, mapping.PERSON, {}, required=True)
employee_uuid = util.get_uuid(employee, required=True)
valid_from, valid_to = util.get_validities(req)
# Validation
validator.is_date_range_in_org_unit_range(org_unit, valid_from,
valid_to)
validator.is_date_range_in_employee_range(employee, valid_from,
valid_to)
org_uuid = org.get_configured_organisation(
util.get_mapping_uuid(req, mapping.ORG, required=False))["uuid"]
role_type_uuid = util.get_mapping_uuid(req, mapping.ROLE_TYPE,
required=True)
func_id = util.get_uuid(req, required=False) or str(uuid.uuid4())
bvn = util.checked_get(req, mapping.USER_KEY, func_id)
role = common.create_organisationsfunktion_payload(
funktionsnavn=mapping.ROLE_KEY,
valid_from=valid_from,
valid_to=valid_to,
brugervendtnoegle=bvn,
tilknyttedebrugere=[employee_uuid],
tilknyttedeorganisationer=[org_uuid],
tilknyttedeenheder=[org_unit_uuid],
funktionstype=role_type_uuid,
integration_data=req.get(mapping.INTEGRATION_DATA),
)
self.payload = role
self.uuid = func_id
self.employee_uuid = employee_uuid
self.org_unit_uuid = org_unit_uuid
def prepare_edit(self, req: dict):
role_uuid = req.get('uuid')
# Get the current org-funktion which the user wants to change
c = lora.Connector(virkningfra='-infinity', virkningtil='infinity')
original = c.organisationfunktion.get(uuid=role_uuid)
if not original:
exceptions.ErrorCodes.E_NOT_FOUND(uuid=role_uuid)
data = req.get('data')
new_from, new_to = util.get_validities(data)
validator.is_edit_from_date_before_today(new_from)
# Get org unit uuid for validation purposes
org_unit = mapping.ASSOCIATED_ORG_UNIT_FIELD(original)[0]
payload = dict()
payload['note'] = 'Rediger rolle'
original_data = req.get('original')
if original_data:
# We are performing an update
old_from, old_to = util.get_validities(original_data)
payload = common.inactivate_old_interval(
old_from, old_to, new_from, new_to, payload,
('tilstande', 'organisationfunktiongyldighed')
)
update_fields = list()
# Always update gyldighed
update_fields.append((
mapping.ORG_FUNK_GYLDIGHED_FIELD,
{'gyldighed': "Aktiv"}
))
if mapping.USER_KEY in data:
update_fields.append((
mapping.ORG_FUNK_EGENSKABER_FIELD,
{'brugervendtnoegle': data[mapping.USER_KEY]},
))
if mapping.ROLE_TYPE in data:
update_fields.append((
mapping.ORG_FUNK_TYPE_FIELD,
{'uuid': data.get(mapping.ROLE_TYPE).get('uuid')},
))
if mapping.ORG_UNIT in data:
update_fields.append((
mapping.ASSOCIATED_ORG_UNIT_FIELD,
{'uuid': (util.get_mapping_uuid(data, mapping.ORG_UNIT))},
))
if mapping.PERSON in data:
employee = data.get(mapping.PERSON)
employee_uuid = employee.get('uuid')
update_fields.append((
mapping.USER_FIELD,
{'uuid': employee_uuid},
))
else:
employee = util.get_obj_value(
original, mapping.USER_FIELD.path)[-1]
payload = common.update_payload(new_from, new_to, update_fields,
original,
payload)
bounds_fields = list(
mapping.ROLE_FIELDS.difference({x[0] for x in update_fields}))
payload = common.ensure_bounds(new_from, new_to, bounds_fields,
original, payload)
validator.is_date_range_in_org_unit_range(org_unit, new_from,
new_to)
validator.is_date_range_in_employee_range(employee, new_from,
new_to)
self.payload = payload
self.uuid = role_uuid
self.org_unit_uuid = util.get_uuid(org_unit, required=False)
self.employee_uuid = (
util.get_mapping_uuid(data, mapping.PERSON) or
mapping.USER_FIELD.get_uuid(original)
)