From 495a6855a1ebf84e27de4199f9230c54b6756840 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 22 Aug 2022 22:37:49 +0000 Subject: [PATCH] Improve validation --- apiserver/apiserver/api/serializers.py | 22 ++++++++++++++++------ webclient/src/Transactions.js | 3 ++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 9f83091..77f6f38 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -109,12 +109,6 @@ class TransactionSerializer(serializers.ModelSerializer): if validated_data['amount'] == 0: raise ValidationError(dict(account_type='Can\'t have a $0.00 {} transaction. Do you want "Membership Adjustment"?'.format(validated_data['account_type']))) - if validated_data['protocoin'] < 0: - current_protocoin = member.user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] - new_protocoin = current_protocoin + validated_data['protocoin'] - if new_protocoin < 0: - raise ValidationError(dict(protocoin='Insufficient funds. Member only has {} protocoin.'.format(current_protocoin))) - if validated_data['account_type'] in ['Interac', 'Dream Pmt', 'Square Pmt', 'PayPal']: if not validated_data.get('reference_number', None): raise ValidationError(dict(reference_number='This field is required.')) @@ -124,6 +118,13 @@ class TransactionSerializer(serializers.ModelSerializer): def create(self, validated_data): validated_data = self.validate_transaction(validated_data) + if validated_data['protocoin'] < 0: + user = validated_data['user'] + current_protocoin = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] + new_protocoin = current_protocoin + validated_data['protocoin'] + if new_protocoin < 0: + raise ValidationError(dict(category='Insufficient funds. Member only has {} protocoin.'.format(current_protocoin))) + if validated_data['account_type'] == 'PayPal': msg = 'Manual PayPal transaction added:\n' + str(validated_data) utils.alert_tanner(msg) @@ -136,6 +137,15 @@ class TransactionSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): validated_data = self.validate_transaction(validated_data) + + if validated_data['protocoin'] < 0: + user = validated_data['user'] + # when updating, we need to subtract out the transaction being edited + current_protocoin = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] - instance.protocoin + new_protocoin = current_protocoin + validated_data['protocoin'] + if new_protocoin < 0: + raise ValidationError(dict(category='Insufficient funds. Member only had {} protocoin.'.format(current_protocoin))) + return super().update(instance, validated_data) def get_member_id(self, obj): diff --git a/webclient/src/Transactions.js b/webclient/src/Transactions.js index 289f374..5ba5c54 100644 --- a/webclient/src/Transactions.js +++ b/webclient/src/Transactions.js @@ -116,7 +116,8 @@ export function TransactionEditor(props) { {input?.account_type !== prevInput?.account_type && input?.account_type === 'Protocoin' && Are you sure? -

Protocoin spending transactions are automatic. Do you want "Purchase of Protocoin" category below?

+

Protocoin spending transactions are automatic. Do you want the "Purchase of Protocoin" category below?

+ {input.protocoin > 0 &&

Also, the value should be a negative number if they are spending Protocoin.

}
}