"""Schema for payment_group serialization.""" from abacus_common_logic.marshalling.custom_fields import ma from marshmallow import validate, validates_schema, ValidationError from payment.constants.constants import PAYMENT_SCHEDULE from payment.constants.error import ( ERROR_INVALID_GROUP_CRITERIA_ACCOUNT, ERROR_INVALID_GROUP_CRITERIA_DUPLICATE_ACCOUNT_ID, ) from payment.schemas.payment_group_payment import PaymentGroupPaymentDetailSchema class PaymentGroupCriteriaSchema(ma.Schema): """Schema for payment group criteria.""" account_id = ma.NonNegativeInteger() # TO BE DEPRECATED, use account_ids instead account_ids = ma.List( ma.NonNegativeInteger(), validate=validate.Length(min=1, max=100) ) currency_codes = ma.List(ma.String()) reference_payment_entities = ma.List(ma.NonNegativeInteger()) payment_schedules = ma.List(ma.Enum(options=PAYMENT_SCHEDULE)) # Special readonly field to support managed payment groups, e.g. for Check Payments reference_payment_type_id = ma.NonNegativeInteger() reference_agreement_types = ma.List(ma.NonNegativeInteger()) @validates_schema def validate_account_fields(self, data, **kwargs): """Validate mutual exclusivity and duplicates for account fields.""" account_id = data.get('account_id') account_ids = data.get('account_ids') if account_id and account_ids: raise ValidationError(ERROR_INVALID_GROUP_CRITERIA_ACCOUNT) if account_ids and len(set(account_ids)) != len(account_ids): raise ValidationError(ERROR_INVALID_GROUP_CRITERIA_DUPLICATE_ACCOUNT_ID) class PaymentGroupDetailSchema(ma.Schema): """Payment Group detail schema.""" group_criteria = ma.Nested(PaymentGroupCriteriaSchema) group_name = ma.NonemptyString(required=True) is_reusable = ma.Boolean() payment_group_id = ma.IntegerId(required=True) class PaymentGroupListSchema(ma.Schema): """Payment Group list schema.""" items = ma.Nested(PaymentGroupDetailSchema, many=True) total_count = ma.NonNegativeInteger() class PaymentGroupPostSchema(PaymentGroupDetailSchema): """Post Payment Group schema.""" group_criteria = ma.Nested( PaymentGroupCriteriaSchema(exclude=['reference_payment_type_id']) ) payment_name = ma.NonemptyString() class PaymentGroupPostResponseSchema( PaymentGroupDetailSchema, PaymentGroupPaymentDetailSchema ): """Post Payment Group response schema.""" class PaymentGroupPutSchema(ma.Schema): """Payment Group PUT schema.""" group_name = ma.NonemptyString() # Only False is allowed for now to archive the payment group is_reusable = ma.Boolean(validate=validate.Equal(False))