-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCampaignRepository.php
More file actions
804 lines (681 loc) · 31.6 KB
/
CampaignRepository.php
File metadata and controls
804 lines (681 loc) · 31.6 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
<?php
declare(strict_types=1);
namespace MatchBot\Domain;
use DateTime;
use Doctrine\ORM\QueryBuilder;
use GuzzleHttp\Exception\ClientException;
use MatchBot\Application\Assertion;
use MatchBot\Application\AssertionFailedException;
use MatchBot\Client;
use MatchBot\Client\NotFoundException;
use MatchBot\Domain\DomainException\DomainCurrencyMustNotChangeException;
use Symfony\Component\Clock\ClockInterface;
use function is_string;
use function trim;
/**
* @psalm-import-type SFCampaignApiResponse from Client\Campaign
* @template-extends SalesforceReadProxyRepository<Campaign, Client\Campaign>
*
* @psalm-suppress MissingConstructor - setters must be called after repo is constructed by Doctrine
*/
class CampaignRepository extends SalesforceReadProxyRepository
{
private FundRepository $fundRepository; // @phpstan-ignore property.uninitialized
private ClockInterface $clock; // @phpstan-ignore property.uninitialized
/**
* Gets campaigns that it is particular important matchbot has up-to-date information about.
*
* More specifically gets those campaigns which are live now or recently closed (in the last week),
* based on their last known end time.
* This allows for campaigns to receive updates shortly after closure if a decision is made to reopen them soon after the end date,
* while keeping the number of API calls for regular update runs under control long-term.
* Future campaigns are also covered.
*
* Note that core account & campaign information important to MatchBot is now pushed, not pulled.
* This simplifies this query and dramatically reduces the maximum time we must look back for
* 'pulls' of funds.
*
* @return Campaign[]
*/
public function findCampaignsWhereFundsNeedToBeUpToDate(): array
{
$query = $this->getEntityManager()->createQuery(<<<DQL
SELECT c FROM MatchBot\Domain\Campaign c
INNER JOIN c.charity charity
WHERE c.endDate >= :lookBackDate
ORDER BY c.createdAt ASC
DQL
);
$query->setParameters([
'lookBackDate' => (new DateTime('now'))->sub(new \DateInterval('P7D')),
]);
/** @var Campaign[] $campaigns */
$campaigns = $query->getResult();
return $campaigns;
}
/**
* @param Salesforce18Id<Charity> $charitySfId
* @return list<Campaign>
*/
public function findUpdatableForCharity(Salesforce18Id $charitySfId): array
{
$query = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT campaign FROM MatchBot\Domain\Campaign campaign
JOIN campaign.charity charity
WHERE
charity.salesforceId = :charityId AND
campaign.endDate >= :eighteenMonthsAgo
DQL
);
$query->setParameters([
'charityId' => $charitySfId->value,
'eighteenMonthsAgo' => (new \DateTime('now'))->sub(new \DateInterval('P18M')),
]);
/** @var list<Campaign> $result */
$result = $query->getResult();
return $result;
}
/**
* @param Salesforce18Id<Campaign> $salesforceId
* @throws NotFoundException
* @throws ClientException
*/
public function pullNewFromSf(Salesforce18Id $salesforceId): Campaign
{
$campaignData = $this->getClient()->getById($salesforceId->value, true);
$charity = $this->pullCharity($campaignData);
$campaign = Campaign::fromSfCampaignData($campaignData, $salesforceId, $charity);
$campaign->setSalesforceLastPull(new \DateTime());
$this->getEntityManager()->persist($campaign);
$this->getEntityManager()->flush();
$this->logInfo('Done persisting new campiagn ' . $campaign->getSalesforceId());
return $campaign;
}
/**
* @param Salesforce18Id<Campaign> $salesforceId
*/
public function findOneBySalesforceId(Salesforce18Id $salesforceId, ?\DateTimeImmutable $mustBeUpdatedSince = null): ?Campaign
{
$sfIdString = $salesforceId->value;
if (\str_starts_with(\strtolower($sfIdString), 'xxx')) {
// SF ID was intentionally mangled for MAT-405 testing to simulate SF being down but
// the matchbot DB being up. We know that all our campaign IDs start with a05 so we fix it to query our DB.
$sfIdString = 'a05' . \substr($sfIdString, 3);
}
$campaign = $this->findOneBy(['salesforceId' => $sfIdString]);
$campaignUpdatedAt = $campaign?->getSalesforceLastPull();
if ($mustBeUpdatedSince && $campaignUpdatedAt < $mustBeUpdatedSince) {
$this->logWarning(
"Not returning stale campaign {$sfIdString}, last updated {$campaignUpdatedAt?->format('c')}, should have been since {$mustBeUpdatedSince->format('c')}"
);
return null;
}
return $campaign;
}
/**
* @return array{newFetchCount: int, updatedCount: int, campaigns: list<Campaign>}
*@throws NotFoundException
*
*/
public function fetchAllForMetaCampaign(MetaCampaignSlug $metaCampaginSlug): array
{
/** @var list<array{id: string}> $campaignList */
$campaignList = $this->getClient()->findCampaignsForMetaCampaign($metaCampaginSlug, limit: 10_000);
$campaignIds = array_map(function (array $campaign) {
return Salesforce18Id::ofCampaign($campaign['id']);
}, $campaignList);
$newFetchCount = 0;
$updatedCount = 0;
$count = count($campaignIds);
$i = 0;
$campaigns = [];
foreach ($campaignIds as $id) {
$i++;
$campaign = $this->findOneBySalesforceId($id);
if ($campaign) {
$this->updateFromSf($campaign, withCache: false, autoSave: true);
$updatedCount++;
} else {
$campaign = $this->pullNewFromSf($id);
$newFetchCount++;
}
$this->getLogger()->info("Fetched campaign $i of $count, '{$campaign->getCampaignName()}'\n");
$campaigns[] = $campaign;
}
return compact('newFetchCount', 'updatedCount', 'campaigns');
}
/**
* @param SFCampaignApiResponse $campaignData
* @throws AssertionFailedException if data in SF not fit in our charity model.
*/
public function pullCharity(array $campaignData): Charity
{
$charityData = $campaignData['charity'];
Assertion::notNull($charityData);
$id = $charityData['id'];
// getting some unexplained duplicate stripe ID errors, this may help explain?
$this->getLogger()->info("Pulling charity id: $id");
$this->getLogger()->info("Charity Data from SF: " . json_encode($charityData, flags: \JSON_THROW_ON_ERROR));
$charity = $this->getEntityManager()
->getRepository(Charity::class)
->findOneBy(['salesforceId' => $id]);
if (!$charity) {
$charity = $this->newCharityFromCampaignData($campaignData);
} else {
$this->updateCharityFromCampaignData($charity, $campaignData);
}
$this->getEntityManager()->persist($charity);
return $charity;
}
/**
* @param SFCampaignApiResponse $campaignData
*/
public function newCharityFromCampaignData(array $campaignData): Charity
{
$charityData = $campaignData['charity'];
Assertion::notNull($charityData, 'Charity must not be null for charity campaign');
$emailString = $charityData['emailAddress'] ?? null;
$emailAddress = is_string($emailString) && trim($emailString) !== '' ? EmailAddress::of($emailString) : null;
$psp = PaymentServiceProvider::from($charityData['psp'] ?? PaymentServiceProvider::Stripe->value);
return new Charity(
salesforceId: $charityData['id'],
charityName: $charityData['name'],
stripeAccountId: $charityData['stripeAccountId'],
ryftAccountId: is_string($charityData['ryftAccountId'] ?? null) ? RyftAccountId::of($charityData['ryftAccountId']) : null,
psp: $psp,
hmrcReferenceNumber: $charityData['hmrcReferenceNumber'],
giftAidOnboardingStatus: $charityData['giftAidOnboardingStatus'],
regulator: self::getRegulatorHMRCIdentifier($charityData['regulatorRegion']),
regulatorNumber: $charityData['regulatorNumber'],
time: new \DateTime('now'),
emailAddress: $emailAddress,
websiteUri: $charityData['website'],
logoUri: $charityData['logoUri'],
phoneNumber: $charityData['phoneNumber'] ?? null,
rawData: $charityData,
);
}
/**
* @param SFCampaignApiResponse $campaignData
* @throws AssertionFailedException if data given does not fit in our charity model.
*/
public function updateCharityFromCampaignData(Charity $charity, array $campaignData): void
{
$charityData = $campaignData['charity'];
Assertion::notNull($charityData, 'Charity date should not be null for charity campaign');
$emailString = $charityData['emailAddress'] ?? null;
$emailAddress = is_string($emailString) && trim($emailString) !== '' ? EmailAddress::of($emailString) : null;
$psp = PaymentServiceProvider::from($charityData['psp'] ?? PaymentServiceProvider::Stripe->value);
$charity->updateFromSfPull(
charityName: $charityData['name'],
websiteUri: $charityData['website'],
logoUri: $charityData['logoUri'],
stripeAccountId: $charityData['stripeAccountId'],
ryftAccountId: null,
psp: $psp,
hmrcReferenceNumber: $charityData['hmrcReferenceNumber'],
giftAidOnboardingStatus: $charityData['giftAidOnboardingStatus'],
regulator: self::getRegulatorHMRCIdentifier($charityData['regulatorRegion']),
regulatorNumber: $charityData['regulatorNumber'],
rawData: $charityData,
time: new \DateTime('now'),
phoneNumber: $charityData['phoneNumber'] ?? null,
emailAddress: $emailAddress,
);
}
/**
* Checks if the given campaign is already in the DB - if so does nothing, if not pulls it from Salesforce.
*
* For performance only does a count, doesn't load the campaign from the DB if it already exists.
*
* @param Salesforce18Id<Campaign> $campaignId
*/
public function pullFromSFIfNotPresent(Salesforce18Id $campaignId): void
{
$query = $this->getEntityManager()->createQuery(
'SELECT count(c.id) from MatchBot\Domain\Campaign c where c.salesforceId = :id'
);
$query->setParameter('id', $campaignId->value);
$count = $query->getSingleScalarResult();
if ($count === 0) {
$campaign = $this->pullNewFromSf($campaignId);
$this->fundRepository->pullForCampaign($campaign, $this->clock->now());
}
}
public function setFundRepository(FundRepository $fundRepository): void
{
$this->fundRepository = $fundRepository;
}
/**
* Returns the total of all the complete donations to this campaign, excluding matching and Gift Aid.
*/
public function totalCoreDonations(Campaign $campaign): Money
{
$donationQuery = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT donation.currencyCode, COALESCE(SUM(donation.amount), 0) as sum
FROM MatchBot\Domain\Donation donation
WHERE donation.campaign = :campaignId AND donation.donationStatus IN (:succcessStatus)
GROUP BY donation.currencyCode
DQL
);
$donationQuery->setParameters([
'campaignId' => $campaign->getId(),
'succcessStatus' => DonationStatus::SUCCESS_STATUSES,
]);
/** @var list<array{currencyCode: string, sum: numeric-string}> $donationResult */
$donationResult = $donationQuery->getResult();
if ($donationResult === []) {
return Money::zero($campaign->getCurrency());
}
Assertion::count(
$donationResult,
1,
"multiple currency donations found for same campaign, can't calculate sum"
);
$donationSumNumeric = $donationResult[0]['sum'];
return Money::fromNumericString($donationSumNumeric, Currency::fromIsoCode($donationResult[0]['currencyCode']));
}
public function totalMatchFundsUsed(int $campaignId): Money
{
// FW doesn't use Money value object or have its own currency code field yet, so we join to CampaignFunding
// solely to get that.
$query = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT COALESCE(SUM(fw.amount), 0) as sum, cf.currencyCode as currencyCode
FROM MatchBot\Domain\FundingWithdrawal fw
JOIN fw.donation donation
JOIN fw.campaignFunding cf
WHERE donation.campaign = :campaignId AND donation.donationStatus IN (:succcessStatus)
AND fw.releasedAt is null
GROUP BY cf.currencyCode
DQL
);
$query->setParameters([
'campaignId' => $campaignId,
'succcessStatus' => DonationStatus::SUCCESS_STATUSES,
]);
/** @var null|array{sum: numeric-string, currencyCode: string} $result */
$result = $query->getOneOrNullResult();
if ($result === null) {
return Money::zero();
}
Assertion::numeric($result['sum']);
return Money::fromNumericString($result['sum'], Currency::fromIsoCode($result['currencyCode']));
}
/**
* Returns a list of campaigns that should be displayed for the given Charity. Excludes any ended campaigns that
* had zero donations.
*
* Consider optimising by only selecting fields needed for 'campaign' summaries - maybe not so much
* for this but for a similar functions we have to get campaigns for a meta campaign or a search
*
* @return list<Campaign>
*/
public function findCampaignsForCharityPage(Charity $charity, \DateTimeImmutable $at): array
{
$query = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT campaign FROM MatchBot\Domain\Campaign campaign
JOIN campaign.campaignStatistics statistics
WHERE
campaign.charity = :charity
AND campaign.status IN ('Active', 'Preview', 'Expired')
AND (statistics.donationSum.amountInPence > 0 OR campaign.endDate > :at OR campaign.endDate IS NULL)
ORDER BY campaign.status ASC, campaign.endDate ASC
DQL
);
$query->setParameters([
'charity' => $charity,
'at' => $at,
]);
/** @var list<Campaign> $result */
$result = $query->getResult();
return $result;
}
public function countCampaignsInMetaCampaign(MetaCampaign $metaCampaign): int
{
// query copied from SOQL query in Salesforce function CampaignService.campaignSfToApi
$query = $this->getEntityManager()->createQuery(<<<'DQL'
SELECT COUNT(c.id)
FROM MatchBot\Domain\Campaign c
WHERE c.metaCampaignSlug = :slug
AND c.status IN ('Active', 'Preview', 'Expired')
AND c.relatedApplicationStatus = 'Approved'
AND c.relatedApplicationCharityResponseToOffer = 'Accepted'
DQL
);
$query->setParameter('slug', $metaCampaign->getSlug()->slug);
$count = (int)$query->getSingleScalarResult();
\assert($count >= 0);
return $count;
}
public function setClock(ClockInterface $clock): void
{
$this->clock = $clock;
}
/**
* @return list<Campaign> Each campaign with a donation updated recently.
* It's more DB-efficient to check for any update than for recent collection & recent refunds.
*
* Performance note: We rely on Donation.updatedAt reflecting any FundingWithdrawal changes.
* FundingWithdrawal has lifecycle callbacks that call $donation->updatedNow() on persist/update.
* This allows us to avoid extra joins here and keep this query fast for the once-per-minute poller.
*/
public function findWithDonationChangesSince(\DateTimeImmutable $updatedAfter): array
{
$query = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT DISTINCT campaign
FROM MatchBot\Domain\Campaign campaign
JOIN MatchBot\Domain\Donation donation WITH donation.campaign = campaign.id
WHERE donation.updatedAt >= :donationUpdatedAfter
DQL
);
$query->setParameter('donationUpdatedAfter', $updatedAfter);
/** @var list<Campaign> $result */
$result = $query->getResult();
return $result;
}
/**
* @return list<Campaign> Each campaign with no stats since $oldestExpected.
*/
public function findCampaignsWithNoRecentStats(\DateTimeImmutable $oldestExpected): array
{
$query = $this->getEntityManager()->createQuery(
<<<'DQL'
SELECT campaign FROM MatchBot\Domain\Campaign campaign
LEFT OUTER JOIN campaign.campaignStatistics stats
WHERE stats.lastCheck IS NULL OR stats.lastCheck < :oldestExpected
ORDER BY campaign.createdAt ASC
DQL
);
$query->setParameter('oldestExpected', $oldestExpected);
/** @var list<Campaign> $result */
$result = $query->getResult();
return $result;
}
/**
* @param Campaign $campaign
* @param SFCampaignApiResponse $campaignData
* @throws AssertionFailedException if data given does not fit in our charity model.
*/
public function updateCampaignFromSFData(Campaign $campaign, array $campaignData, bool $alsoUpdateCharity = true): void
{
$startDateString = $campaignData['startDate'];
$endDateString = $campaignData['endDate'];
$title = $campaignData['title'];
// dates may be null for a non-launched early stage preview campaign, but not for a campaign that we're pulling
// into the matchbot DB via an update.
Assertion::notNull($startDateString, "Null start date supplied when attempting to update campaign {$campaign->getSalesforceId()}");
Assertion::notNull($endDateString, "Null end date supplied when attempting to update campaign {$campaign->getSalesforceId()}");
Assertion::notNull($title, "Null title supplied when attempting to updated campaign {$campaign->getSalesforceId()}");
if ($alsoUpdateCharity) {
$this->updateCharityFromCampaignData($campaign->getCharity(), $campaignData);
}
if ($campaign->hasBeenPersisted() && $campaign->getCurrencyCode() !== $campaignData['currencyCode']) {
$this->logWarning(sprintf(
'Refusing to update campaign currency to %s for SF ID %s',
$campaignData['currencyCode'],
$campaignData['id'],
));
throw new DomainCurrencyMustNotChangeException();
}
if ($campaignData['status'] === null) {
$this->getLogger()->debug("null status from SF for campaign " . $campaignData['id']);
}
$regularGivingCollectionEnd = $campaignData['regularGivingCollectionEnd'] ?? null;
$regularGivingCollectionObject = $regularGivingCollectionEnd === null ?
null : new \DateTimeImmutable($regularGivingCollectionEnd);
$currency = Currency::fromIsoCode($campaignData['currencyCode']);
$relatedApplicationStatusString = $campaignData['relatedApplicationStatus'] ?? null;
$relatedApplicationCharityResponseToOfferString = $campaignData['relatedApplicationCharityResponseToOffer'] ?? null;
$campaign->updateFromSfPull(
currencyCode: $currency->isoCode(),
status: $campaignData['status'],
pinPosition: $campaignData['pinPosition'] ?? null,
championPagePinPosition: $campaignData['championPagePinPosition'] ?? null,
relatedApplicationStatus: is_string($relatedApplicationStatusString) ? ApplicationStatus::from($relatedApplicationStatusString) : null,
relatedApplicationCharityResponseToOffer: is_string($relatedApplicationCharityResponseToOfferString) ? CharityResponseToOffer::from($relatedApplicationCharityResponseToOfferString) : null,
endDate: new \DateTimeImmutable($endDateString),
isMatched: $campaignData['isMatched'],
name: $title,
summary: $campaignData['summary'],
metaCampaignSlug: $campaignData['parentRef'],
startDate: new \DateTimeImmutable($startDateString),
ready: $campaignData['ready'],
isRegularGiving: $campaignData['isRegularGiving'] ?? false,
regularGivingCollectionEnd: $regularGivingCollectionObject,
thankYouMessage: $campaignData['thankYouMessage'],
hidden: $campaignData['hidden'] ?? false,
totalFundraisingTarget: Money::fromPence((int)(100.0 * ($campaignData['totalFundraisingTarget'] ?? 0.0)), $currency),
sfData: $campaignData,
);
}
/**
* @param QueryBuilder $qb Builder with its select etc. already set up.
* @param array<string, string> $jsonMatchInListConditions
*
* @return list<int>| null campaign IDs sorted by relavence if doing a full text search, otherwise null.
*/
private function filterForSearch(
QueryBuilder $qb,
?string $status,
?string $metaCampaignSlug,
?string $fundSlug,
array $jsonMatchInListConditions,
bool $filterOutTargetMet,
?string $term,
): array|null {
$qb->andWhere($qb->expr()->eq('campaign.hidden', '0'));
$qb->andWhere($qb->expr()->eq('campaign.isMatched', '1'));
if ($status !== null) {
$qb->andWhere($qb->expr()->eq('campaign.status', ':status'));
$qb->setParameter('status', $status);
} elseif ($metaCampaignSlug === null) {
$qb->andWhere('campaign.status IN (:nonExpired)');
// we can't rely on the status being updated when the campaign expires, so also check that the end-date is not past:
$qb->andWhere($qb->expr()->gt('campaign.endDate', ':now'));
$qb->setParameter('nonExpired', ['Active', 'Preview']);
$qb->setParameter('now', $this->clock->now());
} else {
$qb->andWhere('campaign.status IS NOT NULL');
}
$qb->andWhere(<<<DQL
campaign.metaCampaignSlug IS NULL OR
(
campaign.relatedApplicationStatus = 'Approved' AND
campaign.relatedApplicationCharityResponseToOffer = 'Accepted'
)
DQL
);
if ($metaCampaignSlug !== null) {
$qb->andWhere($qb->expr()->eq('campaign.metaCampaignSlug', ':metaCampaignSlug'));
$qb->setParameter('metaCampaignSlug', $metaCampaignSlug);
}
if ($fundSlug !== null) {
$qb->andWhere($qb->expr()->eq('fund.slug', ':fundSlug'))
->andWhere($qb->expr()->gt('campaignFunding.amount', 0));
$qb->setParameter('fundSlug', $fundSlug);
}
foreach ($jsonMatchInListConditions as $field => $value) {
$qb->andWhere($qb->expr()->like("JSON_EXTRACT(campaign.salesforceData, '$.$field')", ':jsonMatchInList_' . $field));
$qb->setParameter('jsonMatchInList_' . $field, '%' . $value . '%');
}
$ids = null;
if (is_string($term)) {
// we use the same regex in the database to define the generated columns Campaign.normalisedName and
// Charity.normalisedName
$termWithoutApostrophes = preg_replace("/['`‘’]+/u", '', $term);
/** @var list<int> $ids */
$ids = $this->getEntityManager()->getConnection()->fetchFirstColumn(
'SELECT Campaign.id,
MATCH(Campaign.normalisedName) AGAINST(:term_normalised IN NATURAL LANGUAGE MODE) * 5 +
MATCH(Charity.normalisedName) AGAINST(:term_normalised IN NATURAL LANGUAGE MODE) * 3 +
MATCH(Campaign.searchable_text) AGAINST(:term_normalised IN NATURAL LANGUAGE MODE) * 1 +
MATCH(Charity.searchable_text) AGAINST(:term_normalised IN NATURAL LANGUAGE MODE) * 1 as score
FROM Campaign LEFT JOIN Charity ON Campaign.charity_id = Charity.id
WHERE ((MATCH (Campaign.searchable_text) AGAINST (:term_normalised IN NATURAL LANGUAGE MODE)) OR
( MATCH (Charity.searchable_text) AGAINST (:term_normalised IN NATURAL LANGUAGE MODE))
)
ORDER BY score DESC',
[
'term_normalised' => $termWithoutApostrophes,
]
);
$qb->andWhere('campaign.id IN (:ids)');
$qb->setParameter('ids', $ids);
}
if ($filterOutTargetMet) {
$qb->andWhere($qb->expr()->neq('campaignStatistics.distanceToTarget.amountInPence', 0));
}
Assertion::nullOrIsArray($ids);
return $ids;
}
/**
* @param list<int>|null $idsOrderedByRelavence
* @param QueryBuilder $qb Builder with its select etc. already set up.
* @param non-empty-string $safeSortField
*/
private function sortForSearch(
QueryBuilder $qb,
bool $applyPinSort,
string $safeSortField,
string $sortDirection,
array|null $idsOrderedByRelavence
): void {
// Active, Expired, Preview in that order; status sort takes highest precedence.
$qb->addOrderBy('campaign.status', 'asc');
if ($applyPinSort) {
$qb->addOrderBy('pinPosition', 'asc');
}
if ($safeSortField === 'relevance') {
Assertion::isArray($idsOrderedByRelavence, 'idsOrderedByRelavence must be an array when sorting by relevance');
$qb->addOrderBy('FIELD(campaign.id, :orderedIds)', 'ASC');
$qb->setParameter('orderedIds', $idsOrderedByRelavence);
} else {
$qb->addOrderBy($safeSortField, ($sortDirection === 'asc') ? 'asc' : 'desc');
}
// order additionally by id to make sure we have a total order in case other fields are equal.
// Total order is required so we don't lose items in pagination, even with the (still required)
// assumption that the user pages through campaigns faster than the data in the db changes.
// This means campaigns that were set up earlier will show first, which seems reasonable.
$qb->addOrderBy('campaign.id');
}
/**
* @param 'asc'|'desc' $sortDirection
* @param array<string, string> $jsonMatchInListConditions Keyed on plural JSON key name. Value must exactly match
* one of the items in the JSON array with the same key.
*
* Warning - keys in $jsonMatchInListConditionsmust be literal strings otherwise there will be SQL injection vuulnerabilities.
*
* @return list<Campaign>
*/
public function search(
string $sortField,
string $sortDirection,
int $offset,
int $limit,
?string $status,
?string $metaCampaignSlug,
?string $fundSlug,
array $jsonMatchInListConditions,
?string $term,
): array {
$qb = $this->getEntityManager()->createQueryBuilder();
$safeSortField = match ($sortField) {
'amountRaised' => 'campaignStatistics.amountRaised.amountInPence',
'distanceToTarget' => 'campaignStatistics.distanceToTarget.amountInPence',
'matchFundsRemaining' => 'campaignStatistics.matchFundsRemaining.amountInPence',
'matchFundsUsed' => 'campaignStatistics.matchFundsUsed.amountInPence',
'relevance' => 'relevance',
default => throw new \InvalidArgumentException("Invalid sort field '$sortField'"),
};
if ($term === null && $safeSortField === 'relevance') {
throw new \InvalidArgumentException('Please provide a term to sort by relevance');
}
$qb->select(<<<SELECT
campaign,
COALESCE(campaign.pinPosition, 999999999) AS HIDDEN pinPosition
SELECT)
->from(Campaign::class, 'campaign')
->join('campaign.charity', 'charity')
// Campaigns must have a stats record to be searched. Probably OK and worth it to keep the
// join & sorting simple.
->join('campaign.campaignStatistics', 'campaignStatistics')
->leftJoin('campaign.campaignFundings', 'campaignFunding')
->leftJoin('campaignFunding.fund', 'fund')
->groupBy('campaign.id')
->setFirstResult($offset)
->setMaxResults($limit);
$filterOutTargetMet =
$safeSortField === 'campaignStatistics.distanceToTarget.amountInPence' &&
$sortDirection === 'asc';
$idsOrderedByRelavence = $this->filterForSearch(
qb: $qb,
status: $status,
metaCampaignSlug: $metaCampaignSlug,
fundSlug: $fundSlug,
jsonMatchInListConditions: $jsonMatchInListConditions,
filterOutTargetMet: $filterOutTargetMet,
term: $term,
);
$this->sortForSearch(
qb: $qb,
applyPinSort: $jsonMatchInListConditions === [],
safeSortField: $safeSortField,
sortDirection: $sortDirection,
idsOrderedByRelavence: $idsOrderedByRelavence,
);
$query = $qb->getQuery();
/** @var list<Campaign> $result */
$result = $query->getResult();
return $result;
}
public static function getRegulatorHMRCIdentifier(string $regulatorName): ?string
{
return match ($regulatorName) {
'England and Wales' => 'CCEW',
'Northern Ireland' => 'CCNI',
'Scotland' => 'OSCR',
default => null,
};
}
/**
* @throws Client\NotFoundException
* @throws AssertionFailedException if data in SF does not fit in our campaign or charity model.
*/
public function updateFromSf(
Campaign $campaign,
bool $withCache = true,
bool $autoSave = true,
): void {
// Make sure we update existing object if passed in a partial copy and we already have that Salesforce object
// persisted, otherwise we'll try to insert a duplicate and get an ORM crash.
$salesforceId = $campaign->getSalesforceId();
if (
$campaign->hasBeenPersisted() &&
($existingCampaign = $this->findOneBy(['salesforceId' => $salesforceId]))
) {
$this->logInfo('Updating campaign ' . $salesforceId . '...');
$campaign = $existingCampaign;
} else {
$this->logInfo('Creating campaign ' . $salesforceId);
}
$campaignData = $this->getClient()->getById($campaign->getSalesforceId(), $withCache);
$this->updateCampaignFromSFData($campaign, $campaignData);
try {
$this->getEntityManager()->flush();
} catch (\PDOException $e) {
$this->logger?->error("PDOException generated trying to update campaign SFID {$campaign->getSalesforceId()} (reg no {$campaign->getCharity()->getRegulatorNumber()}); {$e->getMessage()}");
throw $e;
}
$campaign->setSalesforceLastPull(new DateTime('now'));
$this->getEntityManager()->persist($campaign);
if ($autoSave) {
$this->getEntityManager()->flush();
}
$this->logInfo('Done persisting ' . get_class($campaign) . ' ' . $salesforceId);
}
}