Skip to content

Commit

Permalink
DON-596 - fix logic when a tip is pending
Browse files Browse the repository at this point in the history
* set tip numbers to 0 explicitly as an existing tip's detected
* add an extra check to prevent creating another tip if form data
somehow gets out of sync
  • Loading branch information
NoelLH committed Oct 30, 2023
1 parent 5c608d1 commit af6b4b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/app/transfer-funds/transfer-funds.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ <h3 class="b-rh-1 b-bold">Transfer Donation Funds</h3>
Must be between {{ minimumCreditAmount | exactCurrency:currency }} and {{ maximumCreditAmount | exactCurrency:currency }} inclusive.
</p>

<div *ngIf="(donor.pending_tip_balance?.gbp || 0) > 0">
<p>Thanks for tipping Big Give {{ ((donor.pending_tip_balance?.gbp || 0) / 100) | exactCurrency:"GBP" }} </p>
<div *ngIf="pendingTipBalance > 0">
<p>Thanks for tipping Big Give {{ (pendingTipBalance / 100) | exactCurrency:"GBP" }} </p>
</div>

<!-- All tip bits are shown iff the pending bank-funded tip balance in GBP is £0 or undefined -->
Expand Down
36 changes: 24 additions & 12 deletions src/app/transfer-funds/transfer-funds.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import {RegisterModalComponent} from '../register-modal/register-modal.component
import {getCurrencyMinValidator} from '../validators/currency-min';
import {getCurrencyMaxValidator} from '../validators/currency-max';

/**
* Support for topping up Stripe customer_balance via bank transfer. Only
* available for GBP campaigns and UK donors for now.
*/
@Component({
selector: 'app-transfer-funds',
templateUrl: './transfer-funds.component.html',
Expand Down Expand Up @@ -265,10 +269,7 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {
onTipSelectorChanged(e: MatSelectChange) {
if (e.value === 'Other') {
this.creditForm.get('amounts')?.get('customTipAmount')?.addValidators(Validators.required);

}

else {
} else {
this.creditForm.get('amounts')?.get('customTipAmount')?.removeValidators(Validators.required);
}

Expand Down Expand Up @@ -340,6 +341,14 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {
this.identityService.clearJWT();
}

/**
* Amount in existing committed tips to be fulfilled, in minor units (i.e. pence),
* currently just for GBP / UK bank transfers. 0 if donor's not yet loaded.
*/
get pendingTipBalance() {
return this.donor?.pending_tip_balance?.gbp || 0;
}

private loadAuthedPersonInfo(id: string, jwt: string) {
this.isLoading = true;
this.identityService.get(id, jwt, {withTipBalances: true}).subscribe((person: Person) => {
Expand All @@ -348,7 +357,11 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {

this.setConditionalValidators();

if ((this.donor?.pending_tip_balance?.gbp || 0) > 0) {
if (this.pendingTipBalance > 0) {
this.amountsGroup.patchValue({
customTipAmount: 0,
tipPercentage: 0,
});
// Ensure form field for Gift Aid is off as it's N/A; this also turns off its validation and `isOptedIntoGiftAid`
// as side effects.
this.giftAidGroup.patchValue({
Expand All @@ -366,10 +379,10 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {
}

private setConditionalValidators() {
const tipMayBeSet = !((this.donor?.pending_tip_balance?.gbp || 0) > 0);
const validatorsForFieldsRequiredIfTipMayBeSet = tipMayBeSet ? [Validators.required] : [];
const tipFieldsAvailable = this.pendingTipBalance === 0;
const validatorsForFieldsRequiredIfTipFieldsAvailable = tipFieldsAvailable ? [Validators.required] : [];

this.marketingGroup.controls.optInTbgEmail!.setValidators(validatorsForFieldsRequiredIfTipMayBeSet);
this.marketingGroup.controls.optInTbgEmail!.setValidators(validatorsForFieldsRequiredIfTipFieldsAvailable);
}

private getHomePostcodeValidatorsWhenClaimingGiftAid(homeOutsideUK: boolean) {
Expand All @@ -391,10 +404,9 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {
// The donation amount to Big Give is whatever the user is 'tipping' in the Buy Credits form.
const donationAmount = this.calculatedTipAmount();

// If user fills Buy Credits form with a £0 tip to Big Give, then do NOT attempt to create
// the donation, because MatchBot will respond with an error saying donationAmount is a
// required field. DON-689.
if (donationAmount <= 0) {
// If user didn't tip, OR if an existing tip's detected but we somehow have tip numbers
// set, do not create a new tip.
if (donationAmount <= 0 || this.pendingTipBalance > 0) {
return;
}

Expand Down

0 comments on commit af6b4b3

Please sign in to comment.