add bitcoin shop config and payment method enablement
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { validateSync } from 'class-validator';
|
||||
import { IsEnabledPaymentMethods } from './isEnabledPaymentMethods';
|
||||
|
||||
class TestDto {
|
||||
@IsEnabledPaymentMethods()
|
||||
PAYMENT_METHODS_ENABLED: string;
|
||||
}
|
||||
|
||||
const validateMethods = (value: string) => {
|
||||
const dto = Object.assign(new TestDto(), { PAYMENT_METHODS_ENABLED: value });
|
||||
|
||||
return validateSync(dto);
|
||||
};
|
||||
|
||||
describe('IsEnabledPaymentMethods', () => {
|
||||
it('accepts xmr only', () => {
|
||||
expect(validateMethods('xmr')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('accepts xmr and btc', () => {
|
||||
expect(validateMethods('xmr,btc')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('rejects empty string', () => {
|
||||
expect(validateMethods('').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('rejects unsupported methods', () => {
|
||||
expect(validateMethods('eth').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('rejects duplicate methods', () => {
|
||||
expect(validateMethods('xmr,xmr').length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Validate, ValidatorConstraint, type ValidatorConstraintInterface } from 'class-validator';
|
||||
import { isPaymentMethodValue } from '../../utils/payment/isPaymentMethodValue';
|
||||
|
||||
@ValidatorConstraint({ name: 'isEnabledPaymentMethods' })
|
||||
class IsEnabledPaymentMethodsConstraint implements ValidatorConstraintInterface {
|
||||
validate(value: unknown): boolean {
|
||||
if (typeof value !== 'string' || !value.trim()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const tokens = value
|
||||
.split(',')
|
||||
.map(token => token.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
if (tokens.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (new Set(tokens).size !== tokens.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return tokens.every(isPaymentMethodValue);
|
||||
}
|
||||
|
||||
defaultMessage(): string {
|
||||
return '$property must be a comma-separated list of supported payment methods without duplicates';
|
||||
}
|
||||
}
|
||||
|
||||
export const IsEnabledPaymentMethods = () => Validate(IsEnabledPaymentMethodsConstraint);
|
||||
Reference in New Issue
Block a user