package io.github.binout.soccer.application;

import io.github.binout.soccer.domain.event.FriendlyMatchPlanned;
import io.github.binout.soccer.domain.event.LeagueMatchPlanned;
import io.github.binout.soccer.infrastructure.mail.MailService;

import javax.enterprise.event.Observes;
import javax.inject.Inject;

public class NotificationService {

    @Inject
    MailService mailService;

    public void newLeagueMatchPlanned(@Observes LeagueMatchPlanned event){
        MailService.Mail mail = new MailService.Mail("admin@pes5.com", "New League match", "Date = " + event.date());
        event.players().forEach(mail::addRecipient);
        mailService.sendMail(mail);
    }

    public void newFriendlyMatchPlanned(@Observes FriendlyMatchPlanned event){
        MailService.Mail mail = new MailService.Mail("admin@pes5.com", "New Friendly match", "Date = " + event.date());
        event.players().forEach(mail::addRecipient);
        mailService.sendMail(mail);
    }

}

@@ -161,32 +161,148 @@
 ueMatchPlanned;%0A
+import io.github.binout.soccer.domain.player.Player;%0Aimport io.github.binout.soccer.domain.player.PlayerRepository;%0A
 import io.github
@@ -417,16 +417,75 @@
 .Inject;
+%0Aimport java.util.Optional;%0Aimport java.util.stream.Stream;
 %0A%0Apublic
@@ -556,16 +556,68 @@
 rvice;%0A%0A
+    @Inject%0A    PlayerRepository playerRepository;%0A%0A
     publ
@@ -813,86 +813,429 @@
     
-event.players().forEach(mail::addRecipient);%0A        mailService.sendMail(mail
+addRecipients(mail, event.players());%0A        mailService.sendMail(mail);%0A    %7D%0A%0A    private void addRecipients(MailService.Mail mail, Stream%3CString%3E players) %7B%0A        players.map(p -%3E playerRepository.byName(p))%0A                .filter(Optional::isPresent).map(Optional::get)%0A                .map(Player::email)%0A                .filter(Optional::isPresent).map(Optional::get)%0A                .forEach(mail::addRecipient
 );%0A 
@@ -1451,50 +1451,43 @@
     
-event.players().forEach(mail::addRecipient
+addRecipients(mail, event.players()
 );%0A 

fix recipients list