Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update override delete-related updates #1599

Merged
merged 4 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated wording when creating an integration ([1572](https://github.com/grafana/oncall/pull/1572))
- Set FCM iOS/Android "message priority" to "high priority" for mobile app push notifications ([1612](https://github.com/grafana/oncall/pull/1612))

### Fixed

- Update override deletion changes to set its final duration

## v1.2.1 (2023-03-23)

### Changed
Expand Down
15 changes: 13 additions & 2 deletions engine/apps/schedules/models/custom_on_call_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,19 @@ def delete(self, *args, **kwargs):
force = kwargs.pop("force", False)
# do soft delete for started shifts that were created for web schedule
if self.schedule and self.event_is_started and not force:
self.until = timezone.now().replace(microsecond=0)
self.save(update_fields=["until"])
updated_until = timezone.now().replace(microsecond=0)
if self.until is not None and updated_until >= self.until:
# event is already finished
return
self.until = updated_until
update_fields = ["until"]
if self.type == self.TYPE_OVERRIDE:
# since it is a single-time event, update override duration
delta = self.until - self.start
if delta < self.duration:
self.duration = delta
update_fields += ["duration"]
self.save(update_fields=update_fields)
else:
super().delete(*args, **kwargs)

Expand Down
40 changes: 40 additions & 0 deletions engine/apps/schedules/tests/test_custom_on_call_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -1585,3 +1585,43 @@ def test_delete_shift(make_organization_and_user, make_schedule, make_on_call_sh
else:
on_call_shift.refresh_from_db()
assert on_call_shift.until is not None


@pytest.mark.django_db
@pytest.mark.parametrize(
"starting_day,duration,deleted",
[
(-1, 2, False),
(-2, 1, False),
(1, 1, True),
],
)
def test_delete_override(
make_organization_and_user, make_schedule, make_on_call_shift, starting_day, duration, deleted
):
organization, _ = make_organization_and_user()
schedule = make_schedule(organization, schedule_class=OnCallScheduleWeb)
start_date = (timezone.now() + timezone.timedelta(days=starting_day)).replace(microsecond=0)

data = {
"start": start_date,
"rotation_start": start_date,
"duration": timezone.timedelta(days=duration),
"schedule": schedule,
}
on_call_shift = make_on_call_shift(organization=organization, shift_type=CustomOnCallShift.TYPE_OVERRIDE, **data)
original_duration = on_call_shift.duration

on_call_shift.delete()

if deleted:
with pytest.raises(CustomOnCallShift.DoesNotExist):
on_call_shift.refresh_from_db()
else:
on_call_shift.refresh_from_db()
assert on_call_shift.until is not None
assert (
on_call_shift.duration == original_duration
if (starting_day + duration) < 0
else on_call_shift.duration < original_duration
)
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const RotationForm: FC<RotationFormProps> = observer((props) => {
}, []);

const isFormValid = useMemo(() => userGroups.some((group) => group.length), [userGroups]);
const disableAction = !endLess && rotationEnd.isBefore(dayjs().tz(currentTimezone));

const [focusElementName, setFocusElementName] = useState<undefined | string>(undefined);

Expand Down Expand Up @@ -419,7 +420,7 @@ const RotationForm: FC<RotationFormProps> = observer((props) => {
<Button variant="secondary" onClick={onHide}>
{shiftId === 'new' ? 'Cancel' : 'Close'}
</Button>
<Button variant="primary" onClick={handleCreate} disabled={!isFormValid}>
<Button variant="primary" onClick={handleCreate} disabled={!isFormValid || disableAction}>
{shiftId === 'new' ? 'Create' : 'Update'}
</Button>
</HorizontalGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ const ScheduleOverrideForm: FC<RotationFormProps> = (props) => {
const updatePreview = () => {
store.scheduleStore
.updateRotationPreview(scheduleId, shiftId, getFromString(startMoment), true, params)
.then(() => {
.finally(() => {
setIsOpen(true);
});
};

const handleChange = useDebouncedCallback(updatePreview, 200);

const isFormValid = useMemo(() => userGroups.some((group) => group.length), [userGroups]);
const disableAction = shiftEnd.isBefore(dayjs().tz(currentTimezone));

useEffect(handleChange, [params]);

Expand Down Expand Up @@ -244,7 +245,7 @@ const ScheduleOverrideForm: FC<RotationFormProps> = (props) => {
<Button variant="secondary" onClick={onHide}>
{shiftId === 'new' ? 'Cancel' : 'Close'}
</Button>
<Button variant="primary" onClick={handleCreate} disabled={!isFormValid}>
<Button variant="primary" onClick={handleCreate} disabled={!isFormValid || disableAction}>
{shiftId === 'new' ? 'Create' : 'Update'}
</Button>
</HorizontalGroup>
Expand Down