Skip to content

Commit

Permalink
add mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
agarrido19 committed Dec 28, 2021
1 parent d4719bd commit 5a2718e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
20 changes: 20 additions & 0 deletions agave/lib/core_mapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
__all__ = [
'core_mapper',
'mapper_ledger_accounts',
'mapper_service_providers',
'mapper_service_transactions',
]
from .ledger_accounts import mapper_ledger_accounts
from .service_transactions import (
mapper_service_providers,
mapper_service_transactions,
)

# All mapper functions has to return a dictionary with these key, value
# key -> oaxaca collection
# value -> oaxaca data
core_mapper = dict(
service_providers=mapper_service_providers,
service_transactions=mapper_service_transactions,
ledger_accounts=mapper_ledger_accounts,
)
4 changes: 4 additions & 0 deletions agave/lib/core_mapper/exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NotRequiredModelError(Exception):
"""
Objects not required in Oaxaca.
"""
31 changes: 31 additions & 0 deletions agave/lib/core_mapper/ledger_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from cuenca_validations.typing import DictStrAny

from .exc import NotRequiredModelError

STP_BANK_CODE = '90646'


def mapper_ledger_accounts(data: DictStrAny) -> DictStrAny:
account_type = data['account_type']
# Ignore hold an internal
if account_type not in ['saving', 'default']:
raise NotRequiredModelError
model = dict(
id=data['id'],
created_at=data['created_at'],
user_id=data['user_id'],
)

if clabe := data.get('_clabe'):
model = dict(
**model,
bank_code=STP_BANK_CODE,
account_number=clabe['clabe'],
)
if account_type == 'saving':
collection = 'savings'
model['balance'] = data['balance']
else:
collection = 'accounts'
model['name'] = data['user']['name']
return {collection: model}
39 changes: 39 additions & 0 deletions agave/lib/core_mapper/service_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from cuenca_validations.typing import DictStrAny

from .types import CoreTransactionStatus, transaction_status_mapper


def mapper_service_providers(data: DictStrAny) -> DictStrAny:
fields = data['fields']
provider_key = fields[0]['service_provider_key'].replace('_barcode', '')
categories = data.get('categories', [])
model = dict(
id=data['id'],
created_at=data['created_at'],
name=data['name'],
provider_key=provider_key,
categories=[category['name'] for category in categories],
)
return dict(service_providers=model)


def mapper_service_transactions(data: DictStrAny) -> DictStrAny:
core_status = CoreTransactionStatus[data['status']]
account = data.get('service_account')
ledger = data.get('ledger_account')
model = dict(
id=data['id'],
status=transaction_status_mapper[core_status],
created_at=data['created_at'],
amount=data['amount'],
)
if ledger:
model['user_id'] = data['ledger_account']['user_id']
if account:
model = dict(
**model,
descriptor=f'Pago de servicio: {account["name"]}',
account_number=account['account_number'],
provider=account['service_field']['service_provider_id'],
)
return dict(bill_payments=model)
30 changes: 30 additions & 0 deletions agave/lib/core_mapper/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from enum import Enum

from cuenca_validations.types import TransactionStatus


class CoreTransactionStatus(str, Enum):
created = 'created'
failed = 'failed'
submitted = 'submitted'
succeeded = 'succeeded'
transfer_to_default = 'transfer_to_default'
return_to_origin = 'return_to_origin'
suspect = 'suspect'
completed = 'completed'
duplicated = 'duplicated'
pld_review = 'pld_review'


transaction_status_mapper = {
CoreTransactionStatus.created: TransactionStatus.in_review,
CoreTransactionStatus.failed: TransactionStatus.failed,
CoreTransactionStatus.submitted: TransactionStatus.in_review,
CoreTransactionStatus.succeeded: TransactionStatus.succeeded,
CoreTransactionStatus.transfer_to_default: TransactionStatus.succeeded,
CoreTransactionStatus.return_to_origin: TransactionStatus.failed,
CoreTransactionStatus.suspect: TransactionStatus.in_review,
CoreTransactionStatus.completed: TransactionStatus.succeeded,
CoreTransactionStatus.duplicated: TransactionStatus.failed,
CoreTransactionStatus.pld_review: TransactionStatus.in_review,
}

0 comments on commit 5a2718e

Please sign in to comment.