Skip to content

Commit 6438891

Browse files
committed
[IMP] shift_change: prevent registering to a not available shift
1 parent 241a2bb commit 6438891

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

shift_change/models/shift_change.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ def _get_hour_limit_change(self):
5555
hour_limit_change = 0
5656
return hour_limit_change
5757

58+
@api.model
59+
def _get_available_new_shift_ids(self, worker_id):
60+
"""List new shifts available for the given worker"""
61+
next_templates = (
62+
self.env["shift.shift"]
63+
.search(
64+
[
65+
("worker_id", "=", worker_id.id),
66+
("start_time", ">=", datetime.now()),
67+
]
68+
)
69+
.mapped("task_template_id")
70+
)
71+
return self.env["shift.shift"].search(
72+
[
73+
("worker_id", "=", False),
74+
("start_time", ">=", datetime.now()),
75+
("task_template_id", "not in", next_templates.ids),
76+
]
77+
)
78+
5879
@api.model_create_multi
5980
def create(self, vals_list):
6081
"""
@@ -155,3 +176,7 @@ def _check_new_shift(self, new_shift_id):
155176
raise ValidationError(
156177
_("You can't subscribe to a shift so close in the futur.")
157178
)
179+
if new_shift_id not in self._get_available_new_shift_ids(self.worker_id):
180+
raise ValidationError(
181+
_("You can't subscribe to this shift, it’s not available for you.")
182+
)

shift_change/tests/test_shift_change.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ def setUp(self):
8888
"worker_id": False,
8989
}
9090
)
91+
self.shift_7 = self.shift_model.create(
92+
{
93+
"name": "shift_7",
94+
"task_template_id": self.task_template_1.id,
95+
"start_time": self.now + timedelta(days=4),
96+
"end_time": self.now + timedelta(days=4),
97+
"is_regular": True,
98+
"worker_id": self.worker_regular_1.id,
99+
}
100+
)
101+
self.shift_7_bis = self.shift_model.create(
102+
{
103+
"name": "shift_7_bis",
104+
"task_template_id": self.task_template_1.id,
105+
"start_time": self.now + timedelta(days=4),
106+
"end_time": self.now + timedelta(days=4),
107+
"worker_id": False,
108+
}
109+
)
91110

92111
# Set context to avoid shift generation in the past
93112
self.env.context = dict(self.env.context, visualize_date=date.today())
@@ -228,3 +247,17 @@ def test_shift_origin_empty(self):
228247
"new_shift_id": self.shift_3.id,
229248
}
230249
)
250+
251+
def test_new_shift_not_available(self):
252+
"""Test that fails if worker has already subscribed to a sibling
253+
of new_shift"""
254+
self.assertEqual(self.shift_1.worker_id, self.worker_regular_1)
255+
self.assertFalse(self.shift_7_bis.worker_id)
256+
with self.assertRaises(ValidationError):
257+
self.shift_change_model.create(
258+
{
259+
"worker_id": self.worker_regular_1.id,
260+
"old_shift_id": self.shift_1.id,
261+
"new_shift_id": self.shift_7_bis.id,
262+
}
263+
)

shift_change/wizard/shift_change_create_wizard.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,9 @@ def _on_change_worker_id(self):
4848
@api.depends("worker_id", "old_shift_id")
4949
def _compute_available_new_shift_ids(self):
5050
for rec in self:
51-
next_templates = (
52-
self.env["shift.shift"]
53-
.search(
54-
[
55-
("worker_id", "=", self.worker_id.id),
56-
("start_time", ">=", datetime.now()),
57-
]
58-
)
59-
.mapped("task_template_id")
60-
)
61-
rec.available_new_shift_ids = self.env["shift.shift"].search(
62-
[
63-
("worker_id", "=", False),
64-
("start_time", ">=", datetime.now()),
65-
("task_template_id", "not in", next_templates.ids),
66-
]
67-
)
51+
rec.available_new_shift_ids = self.env[
52+
"shift.change"
53+
]._get_available_new_shift_ids(rec.worker_id)
6854

6955
@api.model
7056
def _get_hour_limit_change(self):

0 commit comments

Comments
 (0)