feat: copy the house managers of the selected houses on assignment mailings
This commit is contained in:
1 parent
2c51ddf86f
commit
f22d1f1199
17 files changed
+293
-24
No files matched your search
+18
-1
@@ -5,9 +5,11 @@ namespace App\Controller\Administrative\AssignmentMailing;
|
||||
use App\Controller\Traits\AuthenticatedUserTrait;
|
||||
use App\Email\MailBodyRenderer;
|
||||
use App\Entity\Upload;
|
||||
use App\Entity\User;
|
||||
use App\Form\AssignmentMailingAddType;
|
||||
use App\Form\AssignmentMailingType;
|
||||
use App\Model\AssignmentMailingDto;
|
||||
use App\Model\TeamerMailingRecipientsDto;
|
||||
use App\Service\Teamer\AssignmentMailingDraftHandler;
|
||||
use App\Service\Teamer\AssignmentMailingService;
|
||||
use App\Service\Teamer\MailingAttachmentHandler;
|
||||
@@ -81,8 +83,23 @@ abstract class AbstractAssignmentMailingController extends AbstractController
|
||||
{
|
||||
return [
|
||||
'mailingDto' => $mailingDto,
|
||||
'recipients' => $this->mailingService->resolveRecipients($mailingDto),
|
||||
'addForm' => $this->createForm(AssignmentMailingAddType::class)->createView(),
|
||||
] + $this->recipientsContext($mailingDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* The recipients and the house managers in CC, named on the compose page and in the
|
||||
* confirmation.
|
||||
*
|
||||
* @return array{recipients: TeamerMailingRecipientsDto, houseManagers: User[]}
|
||||
*/
|
||||
protected function recipientsContext(AssignmentMailingDto $mailingDto): array
|
||||
{
|
||||
$recipients = $this->mailingService->resolveRecipients($mailingDto);
|
||||
|
||||
return [
|
||||
'recipients' => $recipients,
|
||||
'houseManagers' => array_merge(...array_values($this->mailingService->resolveHouseManagerCc($mailingDto, $recipients))),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ class ConfirmController extends AbstractAssignmentMailingController
|
||||
'valid' => $form->isSubmitted() && $form->isValid(),
|
||||
'attachmentError' => $this->mailingService->validateAttachments($mailingDto),
|
||||
'attachmentSize' => $this->mailingService->getAttachmentSize($mailingDto),
|
||||
'recipients' => $this->mailingService->resolveRecipients($mailingDto),
|
||||
] + $this->attachmentsContext($mailingDto));
|
||||
] + $this->recipientsContext($mailingDto) + $this->attachmentsContext($mailingDto));
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,11 @@ class Mailer
|
||||
$email->replyTo($config['reply_to']);
|
||||
}
|
||||
|
||||
// on every mail of a multi-recipient 'to', like the rest of the mail
|
||||
if ([] !== $config['cc']) {
|
||||
$email->cc(...$config['cc']);
|
||||
}
|
||||
|
||||
foreach ($config['attachments'] as $attachment) {
|
||||
/* @var EmailAttachmentInterface $attachment */
|
||||
$attachment->attachTo($email);
|
||||
@@ -117,13 +122,14 @@ class Mailer
|
||||
public function send(TemplatedEmail $email): void
|
||||
{
|
||||
$recipients = array_map(fn (Address $address) => $address->toString(), $email->getTo());
|
||||
$cc = array_map(fn (Address $address) => $address->toString(), $email->getCc());
|
||||
|
||||
try {
|
||||
$this->mailer->send($email);
|
||||
$this->logger->info('Send email', [
|
||||
'to' => $recipients,
|
||||
'subject' => $email->getSubject(),
|
||||
]);
|
||||
] + ([] !== $cc ? ['cc' => $cc] : []));
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error('Email could not be sent', [
|
||||
'to' => $recipients,
|
||||
@@ -146,6 +152,7 @@ class Mailer
|
||||
'transport' => null,
|
||||
'bus_transport' => null,
|
||||
'reply_to' => null,
|
||||
'cc' => [],
|
||||
])
|
||||
->setRequired([
|
||||
'template',
|
||||
@@ -159,6 +166,7 @@ class Mailer
|
||||
->setAllowedTypes('transport', ['null', 'string'])
|
||||
->setAllowedTypes('bus_transport', ['null', 'string'])
|
||||
->setAllowedTypes('reply_to', ['null', 'string'])
|
||||
->setAllowedTypes('cc', 'string[]')
|
||||
;
|
||||
|
||||
return $resolver->resolve($options);
|
||||
|
||||
@@ -19,12 +19,15 @@ use App\Model\TeamerMailingRecipientsDto;
|
||||
* With $listColleagues every mail ends with the names and addresses of the other recipients,
|
||||
* taken from the same snapshot. $replyTo is decided when the mailing is queued, so a deploy
|
||||
* that changes it does not reroute answers to a mailing already on its way.
|
||||
*
|
||||
* A recipient's 'cc' holds the addresses that get their mail in copy - the house managers of an
|
||||
* assignment mailing, each on one mail only, see AssignmentMailingService::resolveHouseManagerCc().
|
||||
*/
|
||||
class SendTeamerMailing
|
||||
{
|
||||
/**
|
||||
* @param array<int, array{email: string, firstName: string, lastName: string, fullName: string}> $recipients
|
||||
* @param array<int, array{path: string, name: string}> $attachments
|
||||
* @param array<int, array{email: string, firstName: string, lastName: string, fullName: string, cc?: list<string>}> $recipients
|
||||
* @param array<int, array{path: string, name: string}> $attachments
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $subject,
|
||||
@@ -38,6 +41,7 @@ class SendTeamerMailing
|
||||
|
||||
/**
|
||||
* @param array<int, array{path: string, name: string}> $attachments
|
||||
* @param array<int, list<string>> $cc teamer id => addresses
|
||||
*/
|
||||
public static function fromRecipients(
|
||||
string $subject,
|
||||
@@ -46,6 +50,7 @@ class SendTeamerMailing
|
||||
array $attachments = [],
|
||||
bool $listColleagues = false,
|
||||
?string $replyTo = null,
|
||||
array $cc = [],
|
||||
): self {
|
||||
$snapshot = [];
|
||||
|
||||
@@ -60,6 +65,7 @@ class SendTeamerMailing
|
||||
'firstName' => $recipient->getFirstName(),
|
||||
'lastName' => $recipient->getLastName(),
|
||||
'fullName' => $recipient->getFullName(),
|
||||
'cc' => $cc[$recipient->getId()] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -77,7 +83,7 @@ class SendTeamerMailing
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{email: string, firstName: string, lastName: string, fullName: string}>
|
||||
* @return array<int, array{email: string, firstName: string, lastName: string, fullName: string, cc?: list<string>}>
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
|
||||
@@ -122,13 +122,13 @@ class UserRepository extends ServiceEntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* The house managers the nightly reminder may write to: not deleted and not blocked - a blocked
|
||||
* account cannot log in to do what it is reminded of. Hotel codes and muted notifications are
|
||||
* the caller's to check.
|
||||
* The house managers still in office: not deleted and not blocked. The nightly reminder writes
|
||||
* to them (a blocked account cannot log in to do what it is reminded of) and an assignment
|
||||
* mailing puts them in CC. Hotel codes and muted notifications are the caller's to check.
|
||||
*
|
||||
* @return User[]
|
||||
*/
|
||||
public function findHouseManagersToRemind(): array
|
||||
public function findActiveHouseManagers(): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('user');
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class FeedbackReminderService
|
||||
$today = ($today ?? new \DateTimeImmutable('today'))->setTime(0, 0);
|
||||
$count = 0;
|
||||
|
||||
foreach ($this->userRepository->findHouseManagersToRemind() as $houseManager) {
|
||||
foreach ($this->userRepository->findActiveHouseManagers() as $houseManager) {
|
||||
if (true === $houseManager->isMuteNotifications() || [] === $houseManager->getHotelCodes()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Entity\User;
|
||||
use App\Message\SendTeamerMailing;
|
||||
use App\Model\AssignmentMailingDto;
|
||||
use App\Model\TeamerMailingRecipientsDto;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
@@ -15,8 +16,8 @@ use Symfony\Component\Messenger\MessageBusInterface;
|
||||
/**
|
||||
* Sends a mailing to the teamers confirmed on a set of assignments. Recipients, placeholders and
|
||||
* the fan-out in the worker are the teamer mailing's, see TeamerMailingService; what this adds is
|
||||
* the attachments, the list of colleagues at the end of each mail and the Mailing record of what
|
||||
* went out.
|
||||
* the attachments, the list of colleagues at the end of each mail, the house managers in CC and
|
||||
* the Mailing record of what went out.
|
||||
*/
|
||||
class AssignmentMailingService
|
||||
{
|
||||
@@ -26,6 +27,7 @@ class AssignmentMailingService
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly MessageBusInterface $messageBus,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly UserRepository $userRepository,
|
||||
private readonly string $assignmentMailingReplyTo,
|
||||
) {
|
||||
}
|
||||
@@ -35,6 +37,73 @@ class AssignmentMailingService
|
||||
return $this->teamerMailingService->resolveAssignmentRecipients($mailingDto->getAssignments());
|
||||
}
|
||||
|
||||
/**
|
||||
* The house managers who get the mailing in CC, by the teamer whose mail carries them.
|
||||
*
|
||||
* Every house manager of a house on the selected assignments (User::hasHotelCodeMatch(), the
|
||||
* rule that gives them access to the house's teamers) is in CC on exactly one mail: that of
|
||||
* the first recipient confirmed on an assignment of their house, in the recipients' order.
|
||||
* Muted notifications do not count, this is no notification. Left out is a house manager who
|
||||
* gets the mailing anyway, as one of the teamers or under one of their addresses.
|
||||
*
|
||||
* @return array<int, User[]> teamer id => house managers
|
||||
*/
|
||||
public function resolveHouseManagerCc(AssignmentMailingDto $mailingDto, TeamerMailingRecipientsDto $recipients): array
|
||||
{
|
||||
$recipientEmails = [];
|
||||
$hotelCodes = [];
|
||||
|
||||
foreach ($recipients->getEligible() as $recipient) {
|
||||
$recipientEmails[] = mb_strtolower((string) $recipient->getEmail());
|
||||
// in the recipients' order, which decides whose mail carries a house manager
|
||||
$hotelCodes[$recipient->getId()] = [];
|
||||
}
|
||||
|
||||
foreach ($mailingDto->getAssignments() as $assignment) {
|
||||
$hotelCode = (string) $assignment->getDestination()?->getHotelCode();
|
||||
|
||||
if ('' === $hotelCode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($assignment->getConfirmedDispositions() as $disposition) {
|
||||
$teamerId = $disposition->getTeamer()?->getId();
|
||||
|
||||
if (null !== $teamerId && isset($hotelCodes[$teamerId])) {
|
||||
$hotelCodes[$teamerId][] = $hotelCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$cc = [];
|
||||
|
||||
foreach ($this->userRepository->findActiveHouseManagers() as $houseManager) {
|
||||
$email = (string) $houseManager->getEmail();
|
||||
$teamerId = $houseManager->getTeamer()?->getId();
|
||||
|
||||
if (
|
||||
'' === trim($email)
|
||||
|| in_array(mb_strtolower($email), $recipientEmails, true)
|
||||
|| (null !== $teamerId && isset($hotelCodes[$teamerId]))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($hotelCodes as $recipientId => $codes) {
|
||||
foreach ($codes as $hotelCode) {
|
||||
if ($houseManager->hasHotelCodeMatch($hotelCode)) {
|
||||
$cc[$recipientId][] = $houseManager;
|
||||
|
||||
continue 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// in the recipients' order, as the compose page names them
|
||||
return array_filter(array_replace(array_fill_keys(array_keys($hotelCodes), []), $cc));
|
||||
}
|
||||
|
||||
/**
|
||||
* The template's files plus the ones dropped for this mailing, in bytes.
|
||||
*/
|
||||
@@ -118,6 +187,11 @@ class AssignmentMailingService
|
||||
$this->entityManager->persist($mailing);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$cc = array_map(
|
||||
fn (array $houseManagers) => array_map(fn (User $houseManager) => (string) $houseManager->getEmail(), $houseManagers),
|
||||
$this->resolveHouseManagerCc($mailingDto, $recipients)
|
||||
);
|
||||
|
||||
$this->messageBus->dispatch(SendTeamerMailing::fromRecipients(
|
||||
(string) $mailingDto->getSubject(),
|
||||
(string) $mailingDto->getMessage(),
|
||||
@@ -125,7 +199,8 @@ class AssignmentMailingService
|
||||
$this->attachmentHandler->toMessageAttachments($attachments),
|
||||
listColleagues: true,
|
||||
// answers go to the travel management inbox, not to the app's sender address
|
||||
replyTo: $this->assignmentMailingReplyTo
|
||||
replyTo: $this->assignmentMailingReplyTo,
|
||||
cc: $cc
|
||||
));
|
||||
|
||||
$this->logger->info('Send assignment mailing', [
|
||||
@@ -134,6 +209,7 @@ class AssignmentMailingService
|
||||
'assignment_ids' => $mailing->getAssignments()->map(fn ($assignment) => $assignment->getId())->toArray(),
|
||||
'attachments' => count($attachments),
|
||||
'recipients' => $mailing->getRecipientCount(),
|
||||
'house_managers_cc' => count($cc, COUNT_RECURSIVE) - count($cc),
|
||||
]);
|
||||
|
||||
return $mailing;
|
||||
|
||||
@@ -133,7 +133,9 @@ class TeamerMailingService
|
||||
self::TRANSPORT,
|
||||
$this->mailingBusTransport,
|
||||
$mailing->listsColleagues() ? self::colleaguesOf($recipients, $index) : [],
|
||||
$mailing->getReplyTo()
|
||||
$mailing->getReplyTo(),
|
||||
// a mailing queued before the CC has none
|
||||
$recipient['cc'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
@@ -254,6 +256,7 @@ class TeamerMailingService
|
||||
*
|
||||
* @param EmailAttachmentInterface[] $attachments
|
||||
* @param array<int, array{fullName: string, email: string}> $colleagues
|
||||
* @param list<string> $cc
|
||||
*/
|
||||
private function sendTo(
|
||||
string $email,
|
||||
@@ -264,6 +267,7 @@ class TeamerMailingService
|
||||
?string $busTransport = null,
|
||||
array $colleagues = [],
|
||||
?string $replyTo = null,
|
||||
array $cc = [],
|
||||
): void {
|
||||
// $bodyHtml is already rendered and escaped mail HTML, see MailBodyRenderer -
|
||||
// email/teamer_mailing.html.twig prints it with |raw. The colleagues are plain values,
|
||||
@@ -279,6 +283,7 @@ class TeamerMailingService
|
||||
'transport' => $transport,
|
||||
'bus_transport' => $busTransport,
|
||||
'reply_to' => $replyTo,
|
||||
'cc' => $cc,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user