Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPMMA-2550 Point Groups API #13517

Merged
merged 12 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/bundles/LeadBundle/Translations/en_US/messages.ini
Expand Up @@ -85,6 +85,9 @@ mautic.lead.field.utmmedium="Medium"
mautic.lead.field.umtsource="Source"
mautic.lead.field.utmterm="Term"
mautic.lead.event.api="API"
mautic.lead.event.api.operation.not.allowed="The requested operation is not permitted."
mautic.lead.event.api.lead.not.found="Contact could not be found."
mautic.lead.event.api.point.group.not.found="The specified point group was not found."
mautic.lead.event.apiadded="Added through API"
mautic.lead.field.form.choose="Select a field"
mautic.lead.field.form.confirmbatchdelete="Delete the selected custom fields? WARNING: this will also delete any values of these custom fields that are associated with contacts."
Expand Down Expand Up @@ -415,6 +418,7 @@ mautic.lead.lead.submitaction.operator_divide="divide (/)"
mautic.lead.lead.submitaction.operator_minus="subtract (-)"
mautic.lead.lead.submitaction.operator_plus="add (+)"
mautic.lead.lead.submitaction.operator_times="multiply (x)"
mautic.lead.lead.submitaction.operator_set="set"
mautic.lead.lead.submitaction.points="Amount to change points by"
mautic.lead.lead.tab.history="History"
mautic.lead.lead.tab.notes="Notes"
Expand Down
19 changes: 19 additions & 0 deletions app/bundles/PointBundle/Config/config.php
Expand Up @@ -63,6 +63,25 @@
'controller' => 'Mautic\PointBundle\Controller\Api\PointApiController::adjustPointsAction',
'method' => 'POST',
],
'mautic_api_pointgroupsstandard' => [
'standard_entity' => true,
'name' => 'pointGroups',
'path' => '/points/groups',
'controller' => \Mautic\PointBundle\Controller\Api\PointGroupsApiController::class,
patrykgruszka marked this conversation as resolved.
Show resolved Hide resolved
],
'mautic_api_getcontactpointgroups' => [
'path' => '/contacts/{contactId}/points/groups',
'controller' => 'Mautic\PointBundle\Controller\Api\PointGroupsApiController::getContactPointGroupsAction',
],
'mautic_api_getcontactpointgroup' => [
'path' => '/contacts/{contactId}/points/groups/{groupId}',
'controller' => 'Mautic\PointBundle\Controller\Api\PointGroupsApiController::getContactPointGroupAction',
],
'mautic_api_adjustcontactgrouppoints' => [
'path' => '/contacts/{contactId}/points/groups/{groupId}/{operator}/{value}',
'controller' => 'Mautic\PointBundle\Controller\Api\PointGroupsApiController::adjustGroupPointsAction',
'method' => 'POST',
],
],
],

Expand Down
150 changes: 150 additions & 0 deletions app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php
@@ -0,0 +1,150 @@
<?php

patrykgruszka marked this conversation as resolved.
Show resolved Hide resolved
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;
patrykgruszka marked this conversation as resolved.
Show resolved Hide resolved

/** @phpstan-ignore-next-line */
patrykgruszka marked this conversation as resolved.
Show resolved Hide resolved
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'));

Check warning on line 53 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L53

Added line #L53 was not covered by tests
}

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

Check warning on line 57 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L57

Added line #L57 was not covered by tests
}

$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'));

Check warning on line 80 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L80

Added line #L80 was not covered by tests
}

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

Check warning on line 84 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L84

Added line #L84 was not covered by tests
}

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

Check warning on line 89 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L89

Added line #L89 was not covered by tests
}

$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'));

Check warning on line 111 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L111

Added line #L111 was not covered by tests
}

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

Check warning on line 115 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L115

Added line #L115 was not covered by tests
}

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

Check warning on line 120 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L120

Added line #L120 was not covered by tests
}

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

Check warning on line 124 in app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php

View check run for this annotation

Codecov / codecov/patch

app/bundles/PointBundle/Controller/Api/PointGroupsApiController.php#L124

Added line #L124 was not covered by tests
}

$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);
}
}
21 changes: 21 additions & 0 deletions app/bundles/PointBundle/Entity/Group.php
Expand Up @@ -3,6 +3,7 @@
namespace Mautic\PointBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -38,6 +39,26 @@ public static function loadValidatorMetadata(ClassMetadata $metadata): void
]));
}

public static function loadApiMetadata(ApiMetadataDriver $metadata): void
{
$metadata->setGroupPrefix('pointGroup')
->addListProperties(
[
'id',
'name',
'description',
]
)
->addProperties(
[
'id',
'name',
'description',
]
)
->build();
}

public function getId(): ?int
{
return $this->id;
Expand Down
19 changes: 19 additions & 0 deletions app/bundles/PointBundle/Entity/GroupContactScore.php
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\CommonEntity;
use Mautic\LeadBundle\Entity\Lead;
Expand Down Expand Up @@ -48,6 +49,24 @@ public static function loadMetadata(ORM\ClassMetadata $metadata): void
->build();
}

public static function loadApiMetadata(ApiMetadataDriver $metadata): void
{
$metadata->setGroupPrefix('groupContactScore')
->addListProperties(
[
'score',
'group',
]
)
->addProperties(
[
'score',
'group',
]
)
->build();
}

public function getContact(): Lead
{
return $this->contact;
Expand Down
8 changes: 8 additions & 0 deletions app/bundles/PointBundle/Entity/GroupRepository.php
Expand Up @@ -15,4 +15,12 @@ public function getTableAlias(): string
{
return 'pl';
}

public function getEntities(array $args = [])
{
// Without qb it returns entities indexed by id instead of array indexes
$args['qb'] = $this->createQueryBuilder($this->getTableAlias());

return parent::getEntities($args);
}
}
5 changes: 5 additions & 0 deletions app/bundles/PointBundle/Model/PointGroupModel.php
Expand Up @@ -136,4 +136,9 @@ public function adjustPoints(Lead $contact, Group $group, int $points, string $o

return $contact;
}

public static function isAllowedPointOperation(string $operator): bool
{
return in_array($operator, [Lead::POINTS_ADD, Lead::POINTS_SUBTRACT, Lead::POINTS_MULTIPLY, Lead::POINTS_DIVIDE, Lead::POINTS_SET]);
}
}