-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add endpoint for campaign statistics
- Loading branch information
Showing
7 changed files
with
307 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Routes; | ||
|
||
use DateInterval; | ||
use DatePeriod; | ||
use DateTimeImmutable; | ||
use Give\API\RestRoute; | ||
use Give\Campaigns\CampaignDonationQuery; | ||
use Give\Campaigns\Models\Campaign; | ||
use Give\Framework\Support\Facades\DateTime\Temporal; | ||
|
||
class CampaignOverviewStatistics implements RestRoute | ||
{ | ||
/** @var string */ | ||
protected $endpoint = 'campaign-overview-statistics'; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function registerRoute() | ||
{ | ||
register_rest_route( | ||
'give-api/v2', | ||
$this->endpoint, | ||
[ | ||
[ | ||
'methods' => 'GET', | ||
'callback' => [$this, 'handleRequest'], | ||
'permission_callback' => function () { | ||
return current_user_can('manage_options'); | ||
}, | ||
], | ||
'args' => [ | ||
'campaignId' => [ | ||
'type' => 'integer', | ||
'required' => true, | ||
'validate_callback' => 'is_numeric', | ||
], | ||
'rangeInDays' => [ | ||
'type' => 'integer', | ||
'required' => false, | ||
'validate_callback' => 'is_numeric', | ||
'default' => 0, // Zero to mean "all time". | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function handleRequest($request) | ||
{ | ||
$campaign = Campaign::find($request->get_param('campaignId')); | ||
|
||
$query = new CampaignDonationQuery($campaign); | ||
|
||
if(!$request->get_param('rangeInDays')) { | ||
return [[ | ||
'amountRaised' => $query->sumIntendedAmount(), | ||
'donationCount' => $query->countDonations(), | ||
'donorCount' => $query->countDonors(), | ||
]]; | ||
} | ||
|
||
$days = $request->get_param('rangeInDays'); | ||
$date = new DateTimeImmutable('now', wp_timezone()); | ||
$interval = DateInterval::createFromDateString("-$days days"); | ||
$period = new DatePeriod($date, $interval, 1); | ||
|
||
return array_map(function($targetDate) use ($query, $interval) { | ||
|
||
$query = $query->between( | ||
Temporal::withStartOfDay($targetDate->add($interval)), | ||
Temporal::withEndOfDay($targetDate) | ||
); | ||
|
||
return [ | ||
'amountRaised' => $query->sumIntendedAmount(), | ||
'donationCount' => $query->countDonations(), | ||
'donorCount' => $query->countDonors(), | ||
]; | ||
}, iterator_to_array($period) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/Unit/Campaigns/Routes/CampaignOverviewStatisticsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\Campaigns\Routes; | ||
|
||
use DateTime; | ||
use Give\Campaigns\Models\Campaign; | ||
use Give\Campaigns\Routes\CampaignOverviewStatistics; | ||
use Give\DonationForms\Models\DonationForm; | ||
use Give\Donations\Models\Donation; | ||
use Give\Donations\ValueObjects\DonationStatus; | ||
use Give\Framework\Database\DB; | ||
use Give\Framework\Support\ValueObjects\Money; | ||
use Give\Tests\TestCase; | ||
use Give\Tests\TestTraits\RefreshDatabase; | ||
use WP_REST_Request; | ||
|
||
final class CampaignOverviewStatisticsTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testReturnsAllTimeDonationsStatistics() | ||
{ | ||
$campaign = Campaign::factory()->create(); | ||
$form = DonationForm::factory()->create(); | ||
|
||
$db = DB::table('give_campaign_forms'); | ||
$db->insert(['form_id' => $form->id, 'campaign_id' => $campaign->id]); | ||
|
||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('-35 days'), | ||
]); | ||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('-5 days'), | ||
]); | ||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('now'), | ||
]); | ||
|
||
$request = new WP_REST_Request('GET', '/give-api/v2/campaign-overview-statistics'); | ||
$request->set_param('campaignId', $campaign->id); | ||
|
||
$route = new CampaignOverviewStatistics; | ||
$response = $route->handleRequest($request); | ||
|
||
$this->assertEquals(3, $response[0]['donorCount']); | ||
$this->assertEquals(3, $response[0]['donationCount']); | ||
$this->assertEquals(30, $response[0]['amountRaised']); | ||
} | ||
|
||
public function testReturnsPeriodStatisticsWithPreviousPeriod() | ||
{ | ||
$campaign = Campaign::factory()->create(); | ||
$form = DonationForm::factory()->create(); | ||
|
||
$db = DB::table('give_campaign_forms'); | ||
$db->insert(['form_id' => $form->id, 'campaign_id' => $campaign->id]); | ||
|
||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('-35 days'), | ||
]); | ||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('-5 days'), | ||
]); | ||
Donation::factory()->create([ | ||
'formId' => $form->id, | ||
'status' => DonationStatus::COMPLETE(), | ||
'amount' => new Money(1000, 'USD'), | ||
'createdAt' => new DateTime('now'), | ||
]); | ||
|
||
$request = new WP_REST_Request('GET', '/give-api/v2/campaign-overview-statistics'); | ||
$request->set_param('campaignId', $campaign->id); | ||
$request->set_param('rangeInDays', 30); | ||
|
||
$route = new CampaignOverviewStatistics; | ||
$response = $route->handleRequest($request); | ||
|
||
$this->assertEquals(2, $response[0]['donorCount']); | ||
$this->assertEquals(2, $response[0]['donationCount']); | ||
$this->assertEquals(20, $response[0]['amountRaised']); | ||
|
||
$this->assertEquals(1, $response[1]['donorCount']); | ||
$this->assertEquals(1, $response[1]['donationCount']); | ||
$this->assertEquals(10, $response[1]['amountRaised']); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/Unit/Framework/Support/Facades/DateTime/TemporalFacadeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\Framework\Support\Facades\DateTime; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use Give\Framework\Support\Facades\DateTime\TemporalFacade; | ||
use Give\Tests\TestCase; | ||
|
||
final class TemporalFacadeTest extends TestCase | ||
{ | ||
public function testImmutableOrCloneReturnsCloneOfDateTimeObject() | ||
{ | ||
$dateTime = new DateTime; | ||
$temporal = new TemporalFacade; | ||
|
||
$newDateTime = $temporal->immutableOrClone($dateTime); | ||
|
||
$this->assertNotSame($dateTime, $newDateTime); | ||
$this->assertInstanceOf(DateTime::class, $newDateTime); | ||
} | ||
|
||
public function testImmutableOrCloneReturnsSameImmutableDateTimeObject() | ||
{ | ||
$dateTime = new DateTimeImmutable; | ||
$temporal = new TemporalFacade; | ||
|
||
$newDateTime = $temporal->immutableOrClone($dateTime); | ||
|
||
$this->assertSame($dateTime, $newDateTime); | ||
$this->assertInstanceOf(DateTimeImmutable::class, $newDateTime); | ||
} | ||
|
||
public function testImmutableStartOfDay() | ||
{ | ||
$dateTime = new DateTime('2020-01-01 12:34:56'); | ||
$temporal = new TemporalFacade; | ||
|
||
$newDateTime = $temporal->withStartOfDay($dateTime); | ||
|
||
$this->assertNotSame($dateTime, $newDateTime); | ||
$this->assertEquals('2020-01-01 00:00:00', $newDateTime->format('Y-m-d H:i:s')); | ||
} | ||
|
||
public function testImmutableEndOfDay() | ||
{ | ||
$dateTime = new DateTime('2020-01-01 12:34:56'); | ||
$temporal = new TemporalFacade; | ||
|
||
$newDateTime = $temporal->withEndOfDay($dateTime); | ||
|
||
$this->assertNotSame($dateTime, $newDateTime); | ||
$this->assertEquals('2020-01-01 23:59:59.999999', $newDateTime->format('Y-m-d H:i:s.u')); | ||
} | ||
} |