DormantAccountEmailNotifier.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services\DormantAction;
  3. use App\Services\CallApiService;
  4. use App\Services\Msg\MailTemplateService;
  5. use Carbon\Carbon;
  6. class DormantAccountEmailNotifier
  7. {
  8. private $callApiService;
  9. private $mailTemplateService;
  10. public function __construct(CallApiService $callApiService, MailTemplateService $mailTemplateService)
  11. {
  12. $this->callApiService = $callApiService;
  13. $this->mailTemplateService = $mailTemplateService;
  14. }
  15. public function sendNotificationEmail($gateToken, $setup): bool
  16. {
  17. $formDt = Carbon::now()->startOfDay();
  18. $toDt = Carbon::now()->endOfDay();
  19. $toDt->subMonths($setup['ActiveMonth'])->addDays($setup['ActiveNoticeDays']);
  20. $formDt->subMonths($setup['ActiveMonth']);
  21. $memberPage = $this->callApiService->callApi([
  22. 'url' => 'member-page',
  23. 'data' => [
  24. 'PageVars' => [
  25. 'Query' => "((last_login_on between $formDt->timestamp and $toDt->timestamp) and dormant_mail_on = 0) or dormant_mail_on = -1",
  26. 'Asc' => 'last_login_on',
  27. 'Limit' => 999999
  28. ]
  29. ],
  30. 'headers' => [
  31. 'GateToken' => $gateToken
  32. ]
  33. ]);
  34. if ($this->callApiService->verifyApiError($memberPage)) {
  35. return false;
  36. }
  37. $dormantList = $memberPage['Page'] ?? [];
  38. $result = [];
  39. foreach ($dormantList as $member) {
  40. $result[] = $this->mailTemplateService->send('msg.dabory.pro.ko_KR.email.auth.dormant-inform-1', [ 'C11' => $member['Email'], 'C12' => Carbon::createFromTimestamp($member['LastLoginOn'])->addMonths($setup['ActiveMonth'])->format('Y-m-d'), 'C13' => route('member-login') ],
  41. $member['Email'], sprintf('[%s] 휴먼 계정 전환 안내입니다.', config('app.name')), $gateToken);
  42. }
  43. $mailSendPage = collect($dormantList)->map(function ($member, $i) use ($result) {
  44. $dormantMailOn = $result[$i] ? Carbon::now()->timestamp : -1;
  45. return ['Id' => $member['Id'], 'DormantMailOn' => $dormantMailOn];
  46. })->toArray();
  47. if (count($mailSendPage) <= 0) {
  48. return true;
  49. }
  50. $memberAct = $this->callApiService->callApi([
  51. 'url' => 'member-act',
  52. 'data' => [
  53. 'Page' => $mailSendPage
  54. ],
  55. 'headers' => [
  56. 'GateToken' => $gateToken
  57. ]
  58. ]);
  59. if ($this->callApiService->verifyApiError($memberAct)) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. }