"""Logic for Ledger Adjustment.""" from typing import Type from owsresponse import response from ledger.constants.constants import DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET from ledger.models.ledger_adjustment import LedgerAdjustment from ledger.schemas.ledger_adjustment import ( LedgerAdjustmentExtendedSchema, LedgerAdjustmentFilterSchema, LedgerAdjustmentListSchema, ) from ledger.utils.format_error import validation_error from ledger.utils.request import validate_pagination_params def get_pending_ledger_adjustments_by_statement_period_id( statement_period_id: int, request_params: dict ) -> Type[response.Response]: """Get a list of ledger_adjustment by statement_period_id. Args: statement_period_id (int): id of an statement period request_params (dict)(Optional): dict of query string passed to the url - limit(int): the size of page - offset(int): the page number """ try: limit = request_params.get('limit', DEFAULT_PAGE_LIMIT) offset = request_params.get('offset', DEFAULT_PAGE_OFFSET) pagination_params = validate_pagination_params(limit, offset) items, total_count = LedgerAdjustment.get_pending_by_statement_period_id( statement_period_id, **pagination_params ) message = { 'items': LedgerAdjustmentExtendedSchema(many=True).dump(items), 'total_count': total_count, } except Exception as e: return validation_error(str(e)) return response.Response(message=message, status=200) def get_ledger_adjustments(request_params: dict) -> Type[response.Response]: """Get a list of ledger adjustments. Args: request_params (dict)(Optional): dict of query string passed to the url - limit(int): the size of page - offset(int): the page number - account_id(int): id of an account id - start_apply_to_statement_period_id(int): id of apply_to_statement period_id. filtering ledger_adjustments list from start_apply_to_statement_period_id - end_apply_to_statement_period_id(int): id of apply_to_statement period_id. filters ledger_adjustments list up to end_apply_to_statement_period_id. - show_only_applied_adjustments(bool): Either 1 or 0 Returns: A list of ledger adjustments """ try: params = LedgerAdjustmentFilterSchema().load(request_params) items, total_count = LedgerAdjustment.get_ledger_adjustments(**params) message = { 'items': LedgerAdjustmentListSchema(many=True).dump(items), 'total_count': total_count, } except Exception as e: return validation_error(str(e)) return response.Response(message=message, status=200)