-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFundRepository.php
More file actions
376 lines (323 loc) · 14.7 KB
/
FundRepository.php
File metadata and controls
376 lines (323 loc) · 14.7 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
<?php
declare(strict_types=1);
namespace MatchBot\Domain;
use DateTime;
use DateTimeImmutable;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use MatchBot\Application\Matching;
use MatchBot\Client;
use MatchBot\Domain\DomainException\DisallowedFundTypeChange;
use MatchBot\Domain\DomainException\DomainCurrencyMustNotChangeException;
/**
* @psalm-import-type fundArray from Client\Fund
* @template-extends SalesforceReadProxyRepository<Fund, Client\Fund>
*/
class FundRepository extends SalesforceReadProxyRepository
{
private ?CampaignFundingRepository $campaignFundingRepository = null;
private ?Matching\Adapter $matchingAdapter = null;
public function setCampaignFundingRepository(CampaignFundingRepository $repository): void
{
$this->campaignFundingRepository = $repository;
}
/**
* @param Matching\Adapter $matchingAdapter
*/
public function setMatchingAdapter(Matching\Adapter $matchingAdapter): void
{
$this->matchingAdapter = $matchingAdapter;
}
/**
* @param Campaign $campaign
* @param DateTimeImmutable $at
* @throws Client\NotFoundException if Campaign not found on Salesforce
*/
public function pullForCampaign(Campaign $campaign, \DateTimeImmutable $at): void
{
$fundsData = $this->getClient()->getForCampaign($campaign->getSalesforceId());
foreach ($fundsData as $fundData) {
// For each fund linked to the campaign, look it up or create it if unknown
$fund = $this->findOneBy(['salesforceId' => $fundData['id']]);
if (!$fund) {
$fund = $this->getNewFund($fundData);
}
// Then whether new or existing, set its key info
try {
$this->setAnyFundData($fund, $fundData);
} catch (DomainCurrencyMustNotChangeException $exception) {
return; // No-op w.r.t matching if fund currency changed unexpectedly.
}
try {
$this->getEntityManager()->persist($fund);
$this->getEntityManager()->flush(); // Need the fund ID for the CampaignFunding find
} catch (UniqueConstraintViolationException $exception) {
// Somebody else made the fund with this SF ID during the previous operations.
$this->logError('Skipping fund create as unique constraint failed on SF ID ' . $fundData['id']);
$fund = $this->findOneBy(['salesforceId' => $fundData['id']]);
\assert($fund !== null); // since someone else made it it must now be in the db.
$fund = $this->setAnyFundData($fund, $fundData);
$this->getEntityManager()->persist($fund);
}
// If there's already a CampaignFunding for this fund, use that regardless of existing campaigns
// iff the fund is shareable. Otherwise look up only fundings for this campaign. In both cases,
// if the funding is new the lookup result is null and we must make a new funding.
if ($fundData['isShared']) {
$campaignFunding = $this->getCampaignFundingRepository()->getFunding($fund);
} else {
$campaignFunding = $this->getCampaignFundingRepository()->getFundingForCampaign($campaign, $fund);
}
// We must now support funds' totals changing over time, even after a campaign opens. This must play
// well with high volume real-time adapters, so we must first check for a likely change and then push the
// change to the matching adapter when needed.
/** @psalm-var numeric-string $amountForCampaign */
$amountForCampaign = $fundData['amountForCampaign'] === null
? '0.00'
: (string) $fundData['amountForCampaign'];
if ($campaignFunding) {
$this->applyFundBalanceChange(
amountForCampaign: $amountForCampaign,
campaignFunding: $campaignFunding,
campaign: $campaign,
at: $at,
fundId: $fundData['id'],
campaignSFId: $campaign->getSalesforceId()
);
} else {
// Not a previously existing campaign -> create one and set balances without checking for existing ones.
$campaignFunding = new CampaignFunding(
fund: $fund,
amount: $amountForCampaign,
amountAvailable: $amountForCampaign,
);
}
// Make the CampaignFunding available to the Campaign. This method is immutable and won't add duplicates
// if a campaign is already among those linked to the CampaignFunding.
$campaignFunding->addCampaign($campaign);
try {
$this->getEntityManager()->persist($campaignFunding);
$this->getEntityManager()->flush();
} catch (UniqueConstraintViolationException $exception) {
// Somebody else created the specific funding -> proceed without modifying it.
$this->logError(
'Skipping campaign funding create as constraint failed with campaign ' .
($campaign->getId() ?? '[unknown]') . ', fund ' . ($fund->getId() ?? -1)
);
}
}
}
/**
* @param array{currencyCode: ?string, name: ?string, slug: ?string, type: string, id:string, ...} $fundData
*/
private function setAnyFundData(Fund $fund, array $fundData): Fund
{
$currencyCode = $fundData['currencyCode'] ?? 'GBP';
if ($fund->hasBeenPersisted() && $fund->getCurrencyCode() !== $currencyCode) {
$this->logWarning(sprintf(
'Refusing to update fund currency to %s for SF ID %s',
$currencyCode,
$fundData['id'],
));
throw new DomainCurrencyMustNotChangeException();
}
// For now, we let some charities collect non-Topup pledges with the Salesforce form, only
// after champion funds are used. So the mid-campaign matching behaviour is "usually" unaffected
// by the Topup status (with possible exceptions if e.g. there are refunds that release champion funds).
// At the end of a campaign however we want to treat them as Topups during redistribution. So until we have
// a form that gets it right from the start, we allow a type change from Pledge to TopupPledge based on
// Salesforce Pledge__c record edits.
$type = $fundData['type'];
try {
$fund->changeTypeIfNecessary(FundType::from($type));
} catch (DisallowedFundTypeChange $exception) {
$this->logError(sprintf(
'Refusing to update fund type to %s for SF ID %s',
$type,
$fundData['id'],
));
}
$fund->setCurrencyCode($currencyCode);
$fund->setName($fundData['name'] ?? '');
$fund->setSlug($fundData['slug']);
$fund->setSalesforceLastPull(new DateTime('now'));
return $fund;
}
/**
* @param fundArray $fundData
*/
protected function getNewFund(array $fundData): Fund
{
$currencyCode = $fundData['currencyCode'] ?? 'GBP';
$name = $fundData['name'] ?? '';
$slug = $fundData['slug'];
$type = $fundData['type'];
$id = $fundData['id'];
$fund = new Fund(
currencyCode: $currencyCode,
name: $name,
slug: $slug,
salesforceId: Salesforce18Id::ofFund($id),
fundType: FundType::from($type),
);
return $fund;
}
/**
* @param DateTime $closedBeforeDate Typically now
* @param DateTime $closedSinceDate Typically 1 hour ago as determined at the point retro match script started
* @return Fund[]
*/
public function findForCampaignsClosedSince(DateTime $closedBeforeDate, DateTime $closedSinceDate): array
{
$query = <<<EOT
SELECT fund FROM MatchBot\Domain\Fund fund
JOIN fund.campaignFundings campaignFunding
JOIN campaignFunding.campaigns campaign
WHERE
campaign.endDate < :closedBeforeDate AND
campaign.endDate > :closedSinceDate
GROUP BY fund
EOT;
/** @var Fund[] $result */
$result = $this->getEntityManager()->createQuery($query)
->setParameter('closedBeforeDate', $closedBeforeDate)
->setParameter('closedSinceDate', $closedSinceDate)
->disableResultCache()
->getResult();
return $result;
}
/**
* @param DateTimeImmutable $openAtDate Typically now
* @return Fund[]
*/
public function findForCampaignsOpenAt(DateTimeImmutable $openAtDate): array
{
$query = <<<EOT
SELECT fund FROM MatchBot\Domain\Fund fund
JOIN fund.campaignFundings campaignFunding
JOIN campaignFunding.campaigns campaign
WHERE
campaign.startDate < :openAtDate AND
campaign.endDate > :openAtDate
GROUP BY fund
EOT;
/** @var Fund[] $result */
$result = $this->getEntityManager()->createQuery($query)
->setParameter('openAtDate', $openAtDate)
->disableResultCache()
->getResult();
return $result;
}
private function getCampaignFundingRepository(): CampaignFundingRepository
{
return $this->campaignFundingRepository ?? throw new \Exception('CampaignFundingRepository not set');
}
/**
* @return Fund[]
*/
public function findOldTestPledges(DateTimeImmutable $olderThan): array
{
$query = <<<DQL
SELECT fund FROM MatchBot\Domain\Fund fund
WHERE
fund.createdAt < :olderThan AND
fund.fundType IN (:fundTypes)
DQL;
/** @var Fund[] $result */
$result = $this->getEntityManager()->createQuery($query)
->setParameter('olderThan', $olderThan)
->setParameter('fundTypes', [FundType::Pledge, FundType::TopupPledge])
->getResult();
return $result;
}
/**
* Check for balance increase and apply any in a high-volume-safe way.
* Note that a balance DECREASE after campaign open time is unsupported and would be error
* logged below, as this risks invalidating in-progress donation matches.
*
* @param numeric-string $amountForCampaign
*/
private function applyFundBalanceChange(string $amountForCampaign, CampaignFunding $campaignFunding, Campaign $campaign, DateTimeImmutable $at, string $fundId, string $campaignSFId): void
{
$increaseInAmount = bcsub($amountForCampaign, $campaignFunding->getAmount(), 2);
if ($this->matchingAdapter === null) {
throw new \Exception("Matching Adapter not set");
}
// No change
if (bccomp($increaseInAmount, '0.00', 2) === 0) {
$this->getLogger()->info(
"Campaign Funding ID {$campaignFunding->getId()} balance was already £{$amountForCampaign} " .
"for campaign {$campaignSFId} and fund SF ID {$fundId}"
);
return;
}
// Increase
if (bccomp($increaseInAmount, '0.00', 2) === 1) {
// This sets CampaignFunding::$amountAvailable as a side effect.
$newTotal = $this->matchingAdapter->addAmount(
funding: $campaignFunding,
amount: $increaseInAmount,
donationId: null,
extraComment: 'applyFundBalanceChange'
);
$this->getLogger()->info(
"Campaign Funding ID {$campaignFunding->getId()} balance increased " .
"£{$increaseInAmount} to £{$newTotal}"
);
$campaignFunding->setAmount($amountForCampaign);
return;
}
// By process of elimination, we're considering a decrease, so
\assert(bccomp($increaseInAmount, '0.00', 2) === -1); // @phpstan-ignore function.alreadyNarrowedType, identical.alwaysTrue
$decreaseInAmount = bcmul($increaseInAmount, '-1', 2);
if (!self::reductionsAreAllowed($campaign, $campaignFunding, $at)) {
$this->getLogger()->error(
"Campaign Funding ID {$campaignFunding->getId()} balance could not be decreased by " .
"£{$decreaseInAmount}. Salesforce Fund ID {$fundId} as campaign {$campaignSFId} opened in past"
);
return;
}
// This sets CampaignFunding::$amountAvailable as a side effect.
$newTotal = $this->matchingAdapter->subtractAmount(
funding: $campaignFunding,
amount: $decreaseInAmount,
donationId: null,
extraComment: 'applyFundBalanceChange',
);
$this->getLogger()->info(
"Campaign Funding ID {$campaignFunding->getId()} balance decreased " .
"£" . bcmul($decreaseInAmount, '-1', 2) . " to £{$newTotal}"
);
$campaignFunding->setAmount($amountForCampaign);
}
/**
* To avoid tricky scaling & race condition problems in big campaigns, the default position during
* a campaign is to allow top-ups but not reductions.
*
* Reductions are *only* allowed in 3 scenarios:
* 1. Campaign has yet to start
* 2. Campaign is a never-proceeding (Rejected) app campaign *and* all funds are unused
* 3. Campaign is closed *and* all funds are unused. This is likely to be useful only if a database
* migration was run *before* a Salesforce data change. Follow the process at {@link https://youneedawiki.com/app/page/1aktyZ90uoX-nuAYW7mxPhlDLWb902FLV?p=1AR_eRWSQCFYUnvPWxf_uBW6Yz0yePCo0}
* to avoid creating data inconsistencies.
*/
public static function reductionsAreAllowed(Campaign $campaign, CampaignFunding $campaignFunding, DateTimeImmutable $at): bool
{
if ($campaign->getStartDate() > $at) {
return true;
}
$fundsUnused = $campaignFunding->getAmountAvailable() === $campaignFunding->getAmount();
if (!$fundsUnused) {
// Never-proceeding app and closed campaign post-DB-migration cases both
// require all funds unused.
return false;
}
if ($campaign->isNeverProceedingAppCampaign()) {
// Campaign abandoned & no funds used; OK to zero even mid/post campaign.
return true;
}
if ($campaign->getEndDate() < $at) {
// Campaign closed & no funds used, maybe due to a DB migration moving allocations.
return true;
}
return false;
}
}