Skip to content

Commit d981804

Browse files
committed
Apply all constraints without early exits
We need generators to not early exit on constraints so they're all applied. This is nice so we don't need to couple our constraints to other constrains when adding/removing. The contract is that all the constraints will get equal chance of being applied.
1 parent de20547 commit d981804

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/plugins/ramps/rampConstraints.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,33 @@ export interface RampConstraintParams {
1313
paymentType: FiatPaymentType
1414
}
1515

16-
export const validateRampConstraintParams = (
16+
export function validateRampConstraintParams(
1717
params: RampConstraintParams
18-
): boolean => {
18+
): boolean {
19+
for (const constraint of constraintGenerator(params)) {
20+
if (!constraint) {
21+
return false
22+
}
23+
}
24+
return true
25+
}
26+
27+
function* constraintGenerator(
28+
params: RampConstraintParams
29+
): Generator<boolean, boolean> {
1930
// Restrict ACH to only US
2031
if (params.paymentType === 'ach') {
21-
return params.regionCode.countryCode === 'US'
32+
yield params.regionCode.countryCode === 'US'
2233
}
2334

2435
// Venmo payment method is only supported in the USA
2536
if (params.paymentType === 'venmo') {
26-
return params.regionCode.countryCode === 'US'
37+
yield params.regionCode.countryCode === 'US'
2738
}
2839

2940
// Filter out credit for sell in US for Paybis
3041
if (params.rampPluginId === 'paybis' && params.direction === 'sell') {
31-
return params.regionCode.countryCode === 'US'
42+
yield params.regionCode.countryCode === 'US'
3243
}
3344

3445
// Constrain Revolut to the supported regions
@@ -43,7 +54,7 @@ export const validateRampConstraintParams = (
4354
.replace(/\s/g, '')
4455
.split(',')
4556

46-
return forCountries.includes(params.regionCode.countryCode)
57+
yield forCountries.includes(params.regionCode.countryCode)
4758
}
4859

4960
return true

0 commit comments

Comments
 (0)