Added shift update event to send templated emails

This commit is contained in:
Igor Scheller 2023-12-20 23:31:59 +01:00 committed by msquare
parent 4bbeb93d64
commit 4429516a22
10 changed files with 162 additions and 107 deletions

View File

@ -83,5 +83,7 @@ return [
\Engelsystem\Events\Listener\Shift::class . '@deletedEntryCreateWorklog', \Engelsystem\Events\Listener\Shift::class . '@deletedEntryCreateWorklog',
\Engelsystem\Events\Listener\Shift::class . '@deletedEntrySendEmail', \Engelsystem\Events\Listener\Shift::class . '@deletedEntrySendEmail',
], ],
'shift.updating' => \Engelsystem\Events\Listener\Shift::class . '@updatedShiftSendEmail',
], ],
]; ];

View File

@ -160,7 +160,10 @@ function shift_edit_controller()
$shift->updatedBy()->associate(auth()->user()); $shift->updatedBy()->associate(auth()->user());
$shift->save(); $shift->save();
mail_shift_change($oldShift, $shift); event('shift.updating', [
'shift' => $shift,
'oldShift' => $oldShift,
]);
NeededAngelType::whereShiftId($shift_id)->delete(); NeededAngelType::whereShiftId($shift_id)->delete();
$needed_angel_types_info = []; $needed_angel_types_info = [];

View File

@ -6,8 +6,11 @@ use Carbon\Carbon;
use Engelsystem\Helpers\Shifts; use Engelsystem\Helpers\Shifts;
use Engelsystem\Mail\EngelsystemMailer; use Engelsystem\Mail\EngelsystemMailer;
use Engelsystem\Models\Location; use Engelsystem\Models\Location;
use Engelsystem\Models\Shifts\Shift as ShiftModel;
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\Models\Worklog; use Engelsystem\Models\Worklog;
use Illuminate\Database\Eloquent\Collection;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class Shift class Shift
@ -85,4 +88,48 @@ class Shift
] ]
); );
} }
public function updatedShiftSendEmail(
ShiftModel $shift,
ShiftModel $oldShift
): void {
// Only send e-mail on relevant changes
if (
$oldShift->shift_type_id == $shift->shift_type_id
&& $oldShift->title == $shift->title
&& $oldShift->start == $shift->start
&& $oldShift->end == $shift->end
&& $oldShift->location_id == $shift->location_id
) {
return;
}
$shift->load(['shiftType', 'location']);
$oldShift->load(['shiftType', 'location']);
/** @var ShiftEntry[]|Collection $shiftEntries */
$shiftEntries = $shift->shiftEntries()
->with(['angelType', 'user.settings'])
->get();
foreach ($shiftEntries as $shiftEntry) {
$user = $shiftEntry->user;
$angelType = $shiftEntry->angelType;
if (!$user->settings->email_shiftinfo || $shift->end < Carbon::now()) {
continue;
}
$this->mailer->sendViewTranslated(
$user,
'notification.shift.updated',
'emails/updated-shift',
[
'shift' => $shift,
'oldShift' => $oldShift,
'angelType' => $angelType,
'username' => $user->displayName,
]
);
}
}
} }

View File

@ -1,87 +1,7 @@
<?php <?php
use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Collection;
function mail_shift_change(Shift $old_shift, Shift $new_shift)
{
/** @var ShiftEntry[]|Collection $shiftEntries */
$shiftEntries = $old_shift->shiftEntries()
->with(['user', 'user.settings'])
->get();
$old_location = $old_shift->location;
$new_location = $new_shift->location;
$noticeable_changes = false;
$message = __('A Shift you are registered on has changed:');
$message .= "\n";
if ($old_shift->shift_type_id != $new_shift->shift_type_id) {
$message .= sprintf(
__('* Shift type changed from %s to %s'),
$old_shift->shiftType->name,
$new_shift->shiftType->name
) . "\n";
$noticeable_changes = true;
}
if ($old_shift->title != $new_shift->title) {
$message .= sprintf(__('* Shift title changed from %s to %s'), $old_shift->title, $new_shift->title) . "\n";
$noticeable_changes = true;
}
if ($old_shift->start->timestamp != $new_shift->start->timestamp) {
$message .= sprintf(
__('* Shift Start changed from %s to %s'),
$old_shift->start->format(__('general.datetime')),
$new_shift->start->format(__('general.datetime'))
) . "\n";
$noticeable_changes = true;
}
if ($old_shift->end->timestamp != $new_shift->end->timestamp) {
$message .= sprintf(
__('* Shift End changed from %s to %s'),
$old_shift->end->format(__('general.datetime')),
$new_shift->end->format(__('general.datetime'))
) . "\n";
$noticeable_changes = true;
}
if ($old_shift->location_id != $new_shift->location_id) {
$message .= sprintf(__('* Shift Location changed from %s to %s'), $old_location->name, $new_location->name) . "\n";
$noticeable_changes = true;
}
if (!$noticeable_changes) {
// There are no changes worth sending an E-Mail
return;
}
$message .= "\n";
$message .= __('The updated Shift:') . "\n";
$message .= $new_shift->shiftType->name . "\n";
$message .= $new_shift->title . "\n";
$message .= $new_shift->start->format(__('general.datetime')) . ' - ' . $new_shift->end->format(__('H:i')) . "\n";
$message .= $new_location->name . "\n\n";
$message .= url('/shifts', ['action' => 'view', 'shift_id' => $new_shift->id]) . "\n";
foreach ($shiftEntries as $shiftEntry) {
$user = $shiftEntry->user;
if ($user->settings->email_shiftinfo) {
engelsystem_email_to_user(
$user,
__('Your Shift has changed'),
$message,
true
);
}
}
}
function mail_shift_assign(User $user, Shift $shift) function mail_shift_assign(User $user, Shift $shift)
{ {

View File

@ -172,7 +172,6 @@ class ImportSchedule extends BaseController
'' ''
); );
$this->fireDeleteShiftEntryEvents($event, $schedule);
$this->deleteEvent($event, $schedule); $this->deleteEvent($event, $schedule);
} }
$schedule->delete(); $schedule->delete();
@ -280,7 +279,6 @@ class ImportSchedule extends BaseController
} }
foreach ($deleteEvents as $event) { foreach ($deleteEvents as $event) {
$this->fireDeleteShiftEntryEvents($event, $scheduleUrl);
$this->deleteEvent($event, $scheduleUrl); $this->deleteEvent($event, $scheduleUrl);
} }
@ -372,6 +370,7 @@ class ImportSchedule extends BaseController
/** @var ScheduleShift $scheduleShift */ /** @var ScheduleShift $scheduleShift */
$scheduleShift = ScheduleShift::whereGuid($event->getGuid())->where('schedule_id', $schedule->id)->first(); $scheduleShift = ScheduleShift::whereGuid($event->getGuid())->where('schedule_id', $schedule->id)->first();
$shift = $scheduleShift->shift; $shift = $scheduleShift->shift;
$oldShift = Shift::find($shift->id);
$shift->title = $event->getTitle(); $shift->title = $event->getTitle();
$shift->shift_type_id = $shiftTypeId; $shift->shift_type_id = $shiftTypeId;
$shift->start = $event->getDate()->copy()->timezone($eventTimeZone); $shift->start = $event->getDate()->copy()->timezone($eventTimeZone);
@ -381,6 +380,8 @@ class ImportSchedule extends BaseController
$shift->updatedBy()->associate($user); $shift->updatedBy()->associate($user);
$shift->save(); $shift->save();
$this->fireUpdateShiftUpdateEvent($oldShift, $shift);
$this->log( $this->log(
'Updated schedule shift "{shift}" in "{location}" ({from} {to}, {guid})', 'Updated schedule shift "{shift}" in "{location}" ({from} {to}, {guid})',
[ [
@ -400,6 +401,8 @@ class ImportSchedule extends BaseController
$shift = $scheduleShift->shift; $shift = $scheduleShift->shift;
$shift->delete(); $shift->delete();
$this->fireDeleteShiftEntryEvents($event, $schedule);
$this->log( $this->log(
'Deleted schedule shift "{shift}" in {location} ({from} {to}, {guid})', 'Deleted schedule shift "{shift}" in {location} ({from} {to}, {guid})',
[ [
@ -412,6 +415,14 @@ class ImportSchedule extends BaseController
); );
} }
protected function fireUpdateShiftUpdateEvent(Shift $oldShift, Shift $newShift): void
{
event('shift.updating', [
'shift' => $newShift,
'oldShift' => $oldShift,
]);
}
/** /**
* @param Request $request * @param Request $request
* @return Event[]|Room[]|Location[] * @return Event[]|Room[]|Location[]

View File

@ -254,6 +254,9 @@ msgstr ""
"Da die gelöschte Schicht bereits vergangen ist, " "Da die gelöschte Schicht bereits vergangen ist, "
"haben wir einen entsprechenden Arbeitseinsatz hinzugefügt." "haben wir einen entsprechenden Arbeitseinsatz hinzugefügt."
msgid "notification.shift.updated"
msgstr "Deine Schicht wurde aktualisiert"
msgid "notification.shift.no_next_found" msgid "notification.shift.no_next_found"
msgstr "Es wurde keine verfügbare Schicht gefunden." msgstr "Es wurde keine verfügbare Schicht gefunden."

View File

@ -432,30 +432,6 @@ msgstr ""
msgid "%s (%s as %s) in %s, %s - %s" msgid "%s (%s as %s) in %s, %s - %s"
msgstr "%s (%s als %s) in %s, %s - %s" msgstr "%s (%s als %s) in %s, %s - %s"
msgid "A Shift you are registered on has changed:"
msgstr "Eine deiner Schichten hat sich geändert:"
msgid "* Shift type changed from %s to %s"
msgstr "* Schichttyp von %s in %s geändert"
msgid "* Shift title changed from %s to %s"
msgstr "* Schicht Titel von %s nach %s geändert"
msgid "* Shift Start changed from %s to %s"
msgstr "* Schicht Beginn von %s nach %s geändert"
msgid "* Shift End changed from %s to %s"
msgstr "* Schicht Ende von %s nach %s geändert"
msgid "* Shift Location changed from %s to %s"
msgstr "* Schicht Ort von %s to %s geändert"
msgid "The updated Shift:"
msgstr "Die aktualisierte Schicht:"
msgid "Your Shift has changed"
msgstr "Deine Schicht hat sich geändert"
msgid "You have been assigned to a Shift:" msgid "You have been assigned to a Shift:"
msgstr "Du wurdest in eine Schicht eingetragen:" msgstr "Du wurdest in eine Schicht eingetragen:"
@ -2034,3 +2010,27 @@ msgstr "Wenn du dich für Schichten eintragen willst, komm gerne im Himmel vorbe
msgid "design.title" msgid "design.title"
msgstr "Design" msgstr "Design"
msgid "notification.shift.updated.introduction"
msgstr "Eine deiner Schichten wurde aktualisiert:"
msgid "notification.shift.updated.type"
msgstr "Schichttyp wurde von %s in %s geändert"
msgid "notification.shift.updated.title"
msgstr "Schicht Titel wurde von %s in %s geändert"
msgid "notification.shift.updated.description"
msgstr "Schicht Beschreibung wurde geändert"
msgid "notification.shift.updated.start"
msgstr "Schicht Start wurde von %s zu %s geändert"
msgid "notification.shift.updated.end"
msgstr "Schicht Ende wurde von %s zu %s geändert"
msgid "notification.shift.updated.location"
msgstr "Schicht Ort wurde von %s nach %s verschoben"
msgid "notification.shift.updated.shift"
msgstr "Die aktualisierte Schicht:"

View File

@ -253,6 +253,9 @@ msgstr ""
"Since the deleted shift was already done, " "Since the deleted shift was already done, "
"we added a worklog entry instead, to keep your work hours correct." "we added a worklog entry instead, to keep your work hours correct."
msgid "notification.shift.updated"
msgstr "Your shift was updated"
msgid "notification.shift.no_next_found" msgid "notification.shift.no_next_found"
msgstr "There is no available shift." msgstr "There is no available shift."

View File

@ -896,6 +896,30 @@ msgstr ""
msgid "form.recover" msgid "form.recover"
msgstr "Recover" msgstr "Recover"
msgid "notification.shift.updated.introduction"
msgstr "Your shift has changed:"
msgid "notification.shift.updated.type"
msgstr "Shift type changed from %s to %s"
msgid "notification.shift.updated.title"
msgstr "Shift title changed from %s to %s"
msgid "notification.shift.updated.description"
msgstr "Shift description changed"
msgid "notification.shift.updated.start"
msgstr "Shift start changed from %s to %s"
msgid "notification.shift.updated.end"
msgstr "Shift end changed from %s to %s"
msgid "notification.shift.updated.location"
msgstr "Shift location moved from %s to %s"
msgid "notification.shift.updated.shift"
msgstr "The updated Shift:"
msgid "general.actions" msgid "general.actions"
msgstr "Actions" msgstr "Actions"

View File

@ -0,0 +1,42 @@
{% extends "emails/mail.twig" %}
{% block introduction %}
{{ __('notification.shift.updated.introduction') }}
{% endblock %}
{% block message %}
{%- if oldShift.shift_type_id != shift.shift_type_id -%}
* {{ __('notification.shift.updated.type', [oldShift.shiftType.name, shift.shiftType.name]) }}
{% endif %}
{%- if oldShift.title != shift.title -%}
* {{ __('notification.shift.updated.title', [oldShift.title, shift.title]) }}
{% endif %}
{%- if oldShift.description != shift.description -%}
* {{ __('notification.shift.updated.description', [oldShift.description, shift.description]) }}
{% endif %}
{%- if oldShift.start != shift.start -%}
* {{ __(
'notification.shift.updated.start',
[oldShift.start.format(__('general.datetime')), shift.start.format(__('general.datetime'))]
) }}
{% endif %}
{%- if oldShift.end != shift.end -%}
* {{ __(
'notification.shift.updated.end',
[oldShift.end.format(__('general.datetime')), shift.end.format(__('general.datetime'))]
) }}
{% endif %}
{%- if oldShift.location_id != shift.location_id -%}
* {{ __('notification.shift.updated.location', [oldShift.location.name, shift.location.name]) }}
{% endif %}
{{ __('notification.shift.updated.shift') }}
{{ shift.shiftType.name }}
{{ shift.title }}
{{ shift.description }}
{{ shift.start.format(__('general.datetime')) }} - {{ shift.end.format(__('H:i')) }}
{{ shift.location.name }}
{{ url('/shifts', {'action': 'view', 'shift_id': shift.id})|raw }}
{% endblock %}