OrganizerController.php 2.61 KB
Newer Older
Korko's avatar
Korko committed
1
2
3
4
<?php

namespace App\Http\Controllers;

Korko's avatar
Korko committed
5
use App\Draw;
Korko's avatar
Korko committed
6
7
use App\Http\Requests\OrganizerChangeEmailRequest;
use App\Http\Requests\OrganizerResendEmailRequest;
Korko's avatar
Korko committed
8
use App\Jobs\SendMail;
Korko's avatar
CS    
Korko committed
9
10
use App\Mail as MailModel;
use App\Mail\TargetDrawn;
Korko's avatar
Korko committed
11
12
use App\Participant;
use Metrics;
Korko's avatar
Korko committed
13
14
15
16
17
18

class OrganizerController extends Controller
{
    public function view(Draw $draw)
    {
        return view('organizer', [
19
            'draw' => $draw->hash,
Korko's avatar
Korko committed
20
21
22
23
24
        ]);
    }

    public function fetch(Draw $draw)
    {
Korko's avatar
Korko committed
25
        return response()->json([
26
            'draw' => $draw->hash,
Korko's avatar
Korko committed
27
            'participants' => $draw->participants->mapWithKeys(function ($participant) {
28
                return [$participant->id => $participant->only([
29
                    'id', 'name', 'email', 'mail',
30
                ])];
31
            }),
Korko's avatar
Korko committed
32
        ]);
Korko's avatar
Korko committed
33
34
    }

35
36
37
    public function fetchState(Draw $draw)
    {
        return response()->json([
Korko's avatar
Korko committed
38
            'participants' => $draw->participants->mapWithKeys(function ($participant) {
39
                return [$participant->id => $participant->only(['id', 'mail'])];
Korko's avatar
PHPCS    
Korko committed
40
            }),
41
42
43
        ]);
    }

Korko's avatar
Korko committed
44
45
    public function changeEmail(OrganizerChangeEmailRequest $request, Draw $draw, Participant $participant)
    {
46
        $participant->email = $request->input('email');
Korko's avatar
Korko committed
47
        $participant->save();
Korko's avatar
Korko committed
48

Korko's avatar
Korko committed
49
        $participant->mail->updateDeliveryStatus(MailModel::CREATED);
50

51
        $this->doResendEmail($participant);
Korko's avatar
Korko committed
52

53
        $message = trans('organizer.up_and_sent');
Korko's avatar
Korko committed
54

Korko's avatar
Korko committed
55
        return $request->ajax() ?
56
            response()->json([
57
                'message' => $message, 'participant' => $participant->only(['id', 'mail']),
58
            ]) :
Korko's avatar
Korko committed
59
60
            redirect('/')->with('message', $message);
    }
Korko's avatar
Korko committed
61

Korko's avatar
Korko committed
62
63
    public function resendEmail(OrganizerResendEmailRequest $request, Draw $draw, Participant $participant)
    {
Korko's avatar
Korko committed
64
        $participant->mail->updateDeliveryStatus(MailModel::CREATED);
Korko's avatar
Korko committed
65

66
67
68
        $this->doResendEmail($participant);

        $message = trans('message.sent');
Korko's avatar
Korko committed
69

70
        return $request->ajax() ?
71
            response()->json(['message' => $message, 'status' => $participant->mail->delivery_status]) :
72
            redirect('/')->with('message', $message);
Korko's avatar
Korko committed
73
74
    }

75
    protected function doResendEmail(Participant $participant)
Korko's avatar
Korko committed
76
    {
77
        Metrics::increment('email');
Korko's avatar
Korko committed
78

Korko's avatar
Korko committed
79
        SendMail::dispatch($participant, new TargetDrawn($participant));
Korko's avatar
Korko committed
80
    }
Korko's avatar
Korko committed
81
82
83
84
85
86
87
88
89
90
91
92
93

    public function delete(Draw $draw)
    {
        $draw->delete();

        $message = trans('organizer.deleted');

        return $request->ajax() ?
            response()->json([
                'message' => $message,
            ]) :
            redirect('/')->with('message', $message);
    }
Korko's avatar
Korko committed
94
}