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

DON-596 - fix logic when a tip is pending #1367

Merged
merged 3 commits into from
Oct 30, 2023
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
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="donorHasPendingTipBalance">
<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
40 changes: 28 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,18 @@ 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(): number {
return this.donor?.pending_tip_balance?.gbp || 0;
}

get donorHasPendingTipBalance(): boolean {
return this.pendingTipBalance > 0;
}

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

this.setConditionalValidators();

if ((this.donor?.pending_tip_balance?.gbp || 0) > 0) {
if (this.donorHasPendingTipBalance) {
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 +383,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 +408,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.donorHasPendingTipBalance) {
return;
}

Expand Down