File size: 6,161 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Mautic\PointBundle\Controller\Api;

use Doctrine\Persistence\ManagerRegistry;
use Mautic\ApiBundle\Controller\CommonApiController;
use Mautic\ApiBundle\Helper\EntityResultHelper;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\CoreBundle\Helper\AppVersion;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\PointBundle\Entity\Group;
use Mautic\PointBundle\Model\PointGroupModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;

/**
 * @extends CommonApiController<Group>
 */
class PointGroupsApiController extends CommonApiController
{
    /**
     * @var PointGroupModel
     */
    protected $model;

    /** @phpstan-ignore-next-line the parent class uses the deprecated MauticFactory */
    public function __construct(CorePermissions $security, Translator $translator, EntityResultHelper $entityResultHelper, RouterInterface $router, FormFactoryInterface $formFactory, AppVersion $appVersion, RequestStack $requestStack, ManagerRegistry $doctrine, ModelFactory $modelFactory, EventDispatcherInterface $dispatcher, CoreParametersHelper $coreParametersHelper, MauticFactory $factory, PointGroupModel $pointGroupModel, private LeadModel $leadModel)
    {
        $this->model            = $pointGroupModel;
        $this->entityClass      = Group::class;
        $this->entityNameOne    = 'pointGroup';
        $this->entityNameMulti  = 'pointGroups';
        $this->serializerGroups = ['pointGroupDetails', 'pointGroupList', 'publishDetails'];

        parent::__construct($security, $translator, $entityResultHelper, $router, $formFactory, $appVersion, $requestStack, $doctrine, $modelFactory, $dispatcher, $coreParametersHelper, $factory);
    }

    public function getContactPointGroupsAction(int $contactId): Response
    {
        $contact = $this->leadModel->getEntity($contactId);

        if (null === $contact) {
            return $this->notFound($this->translator->trans('mautic.lead.event.api.lead.not.found'));
        }

        if (!$this->checkEntityAccess($contact)) {
            return $this->accessDenied();
        }

        $groupScores = $contact->getGroupScores();
        $view        = $this->view(
            [
                'total'       => count($groupScores),
                'groupScores' => $groupScores,
            ],
            Response::HTTP_OK
        );

        $context = $view->getContext()->setGroups(['groupContactScoreDetails', 'pointGroupDetails']);
        $view->setContext($context);

        return $this->handleView($view);
    }

    public function getContactPointGroupAction(int $contactId, int $groupId): Response
    {
        $contact = $this->leadModel->getEntity($contactId);

        if (null === $contact) {
            return $this->notFound($this->translator->trans('mautic.lead.event.api.lead.not.found'));
        }

        if (!$this->checkEntityAccess($contact)) {
            return $this->accessDenied();
        }

        $pointGroup = $this->model->getEntity($groupId);
        if (null === $pointGroup) {
            return $this->notFound($this->translator->trans('mautic.lead.event.api.point.group.not.found'));
        }

        $groupScore  = $contact->getGroupScore($pointGroup);
        $view        = $this->view(
            [
                'groupScore' => $groupScore,
            ],
            Response::HTTP_OK
        );

        $context = $view->getContext()->setGroups(['groupContactScoreDetails', 'pointGroupDetails']);
        $view->setContext($context);

        return $this->handleView($view);
    }

    public function adjustGroupPointsAction(Request $request, IpLookupHelper $ipLookupHelper, int $contactId, int $groupId, string $operator, int $value): Response
    {
        $contact = $this->leadModel->getEntity($contactId);

        if (null === $contact) {
            return $this->notFound($this->translator->trans('mautic.lead.event.api.lead.not.found'));
        }

        if (!$this->checkEntityAccess($contact)) {
            return $this->accessDenied();
        }

        $pointGroup = $this->model->getEntity($groupId);
        if (null === $pointGroup) {
            return $this->notFound($this->translator->trans('mautic.lead.event.api.point.group.not.found'));
        }

        if (!PointGroupModel::isAllowedPointOperation($operator)) {
            return $this->badRequest($this->translator->trans('mautic.lead.event.api.operation.not.allowed'));
        }

        $oldScore    = $contact->getGroupScore($pointGroup)?->getScore();
        $contact     = $this->model->adjustPoints($contact, $pointGroup, $value, $operator);
        $newScore    = $contact->getGroupScore($pointGroup)->getScore();
        $delta       = $newScore - ($oldScore ?? 0);

        $eventName  = InputHelper::clean($request->request->get('eventName', $this->translator->trans('mautic.point.event.manual_change')));
        $actionName = InputHelper::clean($request->request->get('actionName', $this->translator->trans('mautic.lead.event.api')));
        $contact->addPointsChangeLogEntry(
            type: 'API',
            name: $eventName,
            action: $actionName,
            pointChanges: $delta,
            ip: $ipLookupHelper->getIpAddress(),
            group: $pointGroup
        );
        $this->leadModel->saveEntity($contact, false);

        $view    = $this->view(['groupScore' => $contact->getGroupScore($pointGroup)], Response::HTTP_OK);
        $context = $view->getContext()->setGroups(['groupContactScoreDetails', 'pointGroupDetails']);
        $view->setContext($context);

        return $this->handleView($view);
    }
}