File size: 7,734 Bytes
89a1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php

namespace App\Http\Controllers\Admin\Dispatch;

use App\Enums\ServiceType;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreEmailRequest;
use App\Service\Admin\Core\CustomerService;
use App\Service\Admin\Dispatch\EmailService;
use Illuminate\Http\Request;
use App\Models\EmailLog;
use App\Models\User;
use App\Models\Gateway;
use App\Jobs\ProcessEmail;
use App\Models\Group;
use App\Service\Admin\Dispatch\SmsService;

class ManageEmailController extends Controller
{
    public EmailService $emailService;
    public SmsService $smsService;
    public CustomerService $customerService;

    public function __construct(EmailService $emailService, CustomerService $customerService, SmsService $smsService) {

        $this->emailService    = $emailService;
        $this->customerService = $customerService;
        $this->smsService      = $smsService;
    }

    public function index() {

        $title     = "All Email History";
        $emailLogs = EmailLog::orderBy('id', 'DESC')->with('sender','user')->paginate(paginateNumber(site_settings("paginate_number")));
        return view('admin.email.index', compact('title', 'emailLogs'));
    }

    public function search(Request $request) {

        $search     = $request->input('search');
        $searchDate = $request->input('date');
        $status     = $request->input('status');

        if (empty($search) && empty($searchDate) && empty($status)) {

            $notify[] = ['error', 'Search data field empty'];
            return back()->withNotify($notify);
        }
        $emailLogs = $this->emailService->searchEmailLog($search, $searchDate);
        $emailLogs = $emailLogs->paginate(paginateNumber(site_settings("paginate_number")));
        $title     = 'Email History Search - ' . $search;
        return view('admin.email.index', compact('title', 'emailLogs', 'search', 'status', 'searchDate'));
    }

    public function emailStatusUpdate(Request $request) {
        
        $request->validate([
            'id'     => 'nullable|exists:email_logs,id',
            'status' => 'required|in:1,3,4',
        ]);

        $emailLogIds = !empty($emailLogIds) ? array_filter(explode(",",$request->input('email_log_id'))) : $request->input('id');
        $this->emailLogStatusUpdate((int) $request->status, (array) $emailLogIds);

        $notify[] = ['success', 'Email status has been updated'];
        return back()->withNotify($notify);
    }

    private function emailLogStatusUpdate(int $status, array $emailLogIds): void {

        foreach(array_reverse($emailLogIds) as $emailLogId) {
           
            $emailLog = EmailLog::find($emailLogId);
            if(!$emailLog) {
                continue;
            }
            if($status == 1) {

                if($emailLog->user_id) {

                    $user = User::find($emailLog->user_id);
                    if($user?->email_credit > 1) {

                        $this->customerService->deductCreditLog($user, 1, ServiceType::EMAIL->value);
                        $emailLog->status = $status;
                        $emailLog->update();
                        ProcessEmail::dispatch($emailLog);
                    }
                }else {
                    $emailLog->status = $status;
                    $emailLog->update();
                    ProcessEmail::dispatch($emailLog);
                }
            }else{
                $emailLog->status = $status;
                $emailLog->update();
            }
        }
    }

    public function emailSend($id)
    {
        
        $emailLog = EmailLog::where('id', $id)->first();
        $emailLog->status = 1;
        $emailLog->save();
        ProcessEmail::dispatch($emailLog);
        if($emailLog->user_id){
            $user = User::find($emailLog->user_id);
            if($user?->email_credit > 1) {
                $this->customerService->deductCreditLog($user, 1, ServiceType::EMAIL->value);
                $emailLog->status = EmailLog::PENDING;
                $emailLog->save();
            }
        } 

        $notify[] = ['success', 'Mail sent'];
        return back()->withNotify($notify);
    }

    public function create() {

        $title       = "Compose Email";
        $emailGroups = Group::whereNull('user_id')->get();
        $gateways    = Gateway::whereNotNull('mail_gateways')->whereNull('user_id')->where('status', 1)->get();
        $credentials = config('setting.gateway_credentials.email');
        $channel     = "email";
        return view('admin.email.create', compact('title', 'emailGroups', 'gateways', 'credentials', 'channel'));
    }

    public function store(StoreEmailRequest $request) {
        
        $defaultGateway = Gateway::whereNotNull('mail_gateways')->whereNull('user_id')->where('is_default', 1)->first();
        
        if($request->input('gateway_id')) {
            
            $emailMethod = Gateway::whereNotNull('mail_gateways')->whereNull('user_id')->where('id',$request->gateway_id)->firstOrFail();
        }

        else{
            if($defaultGateway) {
                $emailMethod = $defaultGateway;
            }
            else {
                $notify[] = ['error', 'No Available Default Mail Gateway'];
                return back()->withNotify($notify);
            }
        }

        if(!$emailMethod) {

            $notify[] = ['error', 'Invalid Email Gateway'];
            return back()->withNotify($notify);
        }
        if(!$request->input('email') && !$request->input('group_id') && !$request->has('file')) {

            $notify[] = ['error', 'Invalid email format'];
            return back()->withNotify($notify);
        }
        if($request->has('file')) {

            $extension    = strtolower($request->file('file')->getClientOriginalExtension());

            if (!in_array($extension, ['csv', 'xlsx'])) {
                
                $notify[] = ['error', 'Invalid file extension'];
                return back()->withNotify($notify);
            }
        }

        $numberGroupName  = []; 
        $allContactNumber = [];
        $this->emailService->processEmail($request,$allContactNumber);
        $this->smsService->processGroupId($request, $allContactNumber, $numberGroupName);
        $this->smsService->processFile($request, $allContactNumber, $numberGroupName);
       
        $emailAllNewArray = $this->emailService->flattenAndUnique($allContactNumber);
        $this->emailService->sendEmail($emailAllNewArray, $emailMethod, $request, $numberGroupName);

        $notify[]         = ['success', 'New Email request sent, please see in the Email history for final status'];
        return back()->withNotify($notify);
    }
    public function viewEmailBody($id) {

        $title     = "Details View";
        $emailLogs = EmailLog::where('id',$id)->orderBy('id', 'DESC')->limit(1)->first();
        return view('partials.email_view', compact('title', 'emailLogs'));
    }

    public function delete(Request $request) {

        $this->validate($request, [
            'id' => 'required'
        ]);

        try {
            $emailLog = EmailLog::findOrFail($request->id);
            $user     = User::find($emailLog->user_id);
            if ($emailLog->status==1 && $user) {

                $user->email_credit      += 1;
                $user->save();
                $this->customerService->deductCreditLog($user, 1, ServiceType::EMAIL->value);
            }

            $emailLog->delete();
            $notify[] = ['success', "Successfully email log deleted"];
        } catch (\Exception $e) {
            $notify[] = ['error', "Error occour in email delete time. Error is "+$e->getMessage()];
        }
        return back()->withNotify($notify);
    }
}