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 #322

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/Api/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,72 @@ public function subtractPoints($id, $points, array $parameters = [])
return $this->makeRequest('contacts/'.$id.'/points/minus/'.$points, $parameters, 'POST');
}

/**
* Get all point group scores associated with contact.
*/
public function getPointGroupScores(int $contactId): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups');
}

/**
* Get the contact score for a specified point group.
*/
public function getPointGroupScore(int $contactId, int $groupId): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId);
}

/**
* Increase the score of the contact point group.
*
* @param array $parameters 'eventName' and 'actionName'
*/
public function addPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/plus/'.$points, $parameters, 'POST');
}

/**
* Decrease the score of the contact point group.
*
* @param array $parameters 'eventName' and 'actionName'
*/
public function subtractPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/minus/'.$points, $parameters, 'POST');
}

/**
* Multiply the score of the contact point group.
*
* @param array $parameters 'eventName' and 'actionName'
*/
public function multiplyPointGroupScore(int $contactId, int $groupId, int $value, array $parameters = []): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/times/'.$value, $parameters, 'POST');
}

/**
* Divide the score of the contact point group.
*
* @param array $parameters 'eventName' and 'actionName'
*/
public function dividePointGroupScore(int $contactId, int $groupId, int $value, array $parameters = []): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/divide/'.$value, $parameters, 'POST');
}

/**
* Set the score of the contact point group.
*
* @param array $parameters 'eventName' and 'actionName'
*/
public function setPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array
{
return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/set/'.$points, $parameters, 'POST');
}

/**
* Adds Do Not Contact.
*
Expand Down
41 changes: 41 additions & 0 deletions lib/Api/PointGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @copyright 2014 Mautic, NP. All rights reserved.
* @author Mautic
*
* @see http://mautic.org
*
* @license MIT http://opensource.org/licenses/MIT
*/

namespace Mautic\Api;

/**
* Points Context.
*/
class PointGroups extends Api
{
/**
* {@inheritdoc}
*/
protected $endpoint = 'points/groups';

/**
* {@inheritdoc}
*/
protected $listName = 'pointGroups';

/**
* {@inheritdoc}
*/
protected $itemName = 'pointGroup';

/**
* {@inheritdoc}
*/
protected $searchCommands = [
'ids',
'is:published',
'is:unpublished',
];
}
137 changes: 137 additions & 0 deletions tests/Api/ContactsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,143 @@ public function testSubtractPoints()
$this->assertErrors($response);
}

public function testGetPointGroupScores(): void
{
$response = $this->api->create($this->testPayload);
$this->assertErrors($response);
$contact = $response[$this->api->itemName()];

// test empty group points list
$response = $this->api->getPointGroupScores($contact['id']);
$this->assertErrors($response);
$this->assertSame(0, $response['total']);
$this->assertIsArray($response['groupScores']);
$this->assertEmpty($response['groupScores']);

// add score
$pointsToAdd = 5;
$pointGroupApi = $this->getContext('pointGroups');
$response = $pointGroupApi->create(['name' => 'Group A']);
$pointGroup = $response[$pointGroupApi->itemName()];
$response = $this->api->addPointGroupScore($contact['id'], $pointGroup['id'], $pointsToAdd);
$this->assertErrors($response);
$this->assertNotEmpty($response['groupScore'], 'Adding point group score to a contact with ID ='.$contact['id'].' was not successful');

// test get point group scores list
$response = $this->api->getPointGroupScores($contact['id']);
$this->assertErrors($response);
$this->assertSame(1, $response['total']);
$this->assertIsArray($response['groupScores']);
$this->assertCount(1, $response['groupScores']);
$this->assertSame(5, $response['groupScores'][0]['score']);
$this->assertSame($pointGroup['id'], $response['groupScores'][0]['group']['id']);
$this->assertSame($pointGroup['name'], $response['groupScores'][0]['group']['name']);

$response = $this->api->delete($contact['id']);
$this->assertErrors($response);
}

public function testAddPointGroupScore(): void
{
$pointsToAdd = 5;
$pointGroupApi = $this->getContext('pointGroups');
$response = $pointGroupApi->create(['name' => 'Group A']);
$pointGroup = $response[$pointGroupApi->itemName()];

$response = $this->api->create($this->testPayload);
$this->assertErrors($response);
$contact = $response[$this->api->itemName()];

$response = $this->api->addPointGroupScore($contact['id'], $pointGroup['id'], $pointsToAdd);
$this->assertErrors($response);
$this->assertTrue(!empty($response['groupScore']), 'Adding point group score to a contact with ID ='.$contact['id'].' was not successful');

$response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']);
$this->assertErrors($response);
$this->assertSame($response['groupScore']['score'], $pointsToAdd, 'Point group score was not added accurately');

$response = $this->api->delete($contact['id']);
$this->assertErrors($response);
}

public function testSubtractPointGroupScore(): void
{
$pointsToSubtract = 3;
$pointGroupApi = $this->getContext('pointGroups');
$response = $pointGroupApi->create(['name' => 'Group B']);
$pointGroup = $response[$pointGroupApi->itemName()];

$response = $this->api->create($this->testPayload);
$this->assertErrors($response);
$contact = $response[$this->api->itemName()];

$response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 10);
$this->assertErrors($response);

$response = $this->api->subtractPointGroupScore($contact['id'], $pointGroup['id'], $pointsToSubtract);
$this->assertErrors($response);
$this->assertTrue(!empty($response['groupScore']), 'Subtracting point group score from a contact with ID ='.$contact['id'].' was not successful');

$response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']);
$this->assertErrors($response);
$this->assertSame($response['groupScore']['score'], 10 - $pointsToSubtract, 'Point group score was not subtracted accurately');

$response = $this->api->delete($contact['id']);
$this->assertErrors($response);
}

public function testMultiplyPointGroupScore(): void
{
$multiplier = 2;
$pointGroupApi = $this->getContext('pointGroups');
$response = $pointGroupApi->create(['name' => 'Group C']);
$pointGroup = $response[$pointGroupApi->itemName()];

$response = $this->api->create($this->testPayload);
$this->assertErrors($response);
$contact = $response[$this->api->itemName()];

$response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 5);
$this->assertErrors($response);

$response = $this->api->multiplyPointGroupScore($contact['id'], $pointGroup['id'], $multiplier);
$this->assertErrors($response);
$this->assertTrue(!empty($response['groupScore']), 'Multiplying point group score for a contact with ID ='.$contact['id'].' was not successful');

$response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']);
$this->assertErrors($response);
$this->assertSame($response['groupScore']['score'], 5 * $multiplier, 'Point group score was not multiplied accurately');

$response = $this->api->delete($contact['id']);
$this->assertErrors($response);
}

public function testDividePointGroupScore(): void
{
$divisor = 4;
$pointGroupApi = $this->getContext('pointGroups');
$response = $pointGroupApi->create(['name' => 'Group D']);
$pointGroup = $response[$pointGroupApi->itemName()];

$response = $this->api->create($this->testPayload);
$this->assertErrors($response);
$contact = $response[$this->api->itemName()];

$response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 20);
$this->assertErrors($response);

$response = $this->api->dividePointGroupScore($contact['id'], $pointGroup['id'], $divisor);
$this->assertErrors($response);
$this->assertTrue(!empty($response['groupScore']), 'Dividing point group score for a contact with ID ='.$contact['id'].' was not successful');

$response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']);
$this->assertErrors($response);
$this->assertSame($response['groupScore']['score'], 20 / $divisor, 'Point group score was not divided accurately');

$response = $this->api->delete($contact['id']);
$this->assertErrors($response);
}

public function testBatchEndpoints()
{
$contact1 = $this->testPayload;
Expand Down
50 changes: 50 additions & 0 deletions tests/Api/PointGroupsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Mautic\Tests\Api;

class PointGroupsTest extends MauticApiTestCase
{
public function setUp(): void
{
$this->api = $this->getContext('pointGroups');
$this->testPayload = [
'name' => 'New Point Group',
'description' => 'Description of the new point group',
];
}

public function testGetList(): void
{
$this->standardTestGetList();
}

public function testGetListOfSpecificIds(): void
{
$this->standardTestGetListOfSpecificIds();
}

public function testCreateGetAndDelete(): void
{
$this->standardTestCreateGetAndDelete();
}

public function testEditPatch(): void
{
$editTo = [
'name' => 'Updated Point Group Name',
];
$this->standardTestEditPatch($editTo);
}

public function testEditPut(): void
{
$this->standardTestEditPut();
}

public function testBatchEndpoints(): void
{
$this->standardTestBatchEndpoints();
}
}
Loading