221 lines
8.0 KiB
Python
221 lines
8.0 KiB
Python
import datetime
|
|
#from datetime import datetime, time, timedelta
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.lib_general import log, logging
|
|
from app.config import settings
|
|
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, redis_lookup_id_random
|
|
|
|
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.journal_entry_methods import get_journal_entry_rec_list, load_journal_entry_obj
|
|
|
|
from app.models.journal_entry_models import Journal_Entry_Base
|
|
from app.models.response_models import *
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/journal/entry', response_model=Resp_Body_Base)
|
|
async def post_journal_entry_obj(
|
|
obj: Journal_Entry_Base,
|
|
x_account_id: str = Header(...),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'journal_entry'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
result = post_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.patch('/journal/entry/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_journal_entry_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
obj: Journal_Entry_Base = None,
|
|
x_account_id: Optional[str] = Header(..., ),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'journal_entry'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
|
|
obj_data_dict['id_random'] = obj_id
|
|
result = patch_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
obj_id=obj_id,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get('/journal/entry/list', response_model=Resp_Body_Base)
|
|
async def get_journal_entry_obj_li(
|
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
|
#prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'journal_entry'
|
|
base_name = Journal_Entry_Base
|
|
data = {}
|
|
|
|
if for_obj_type == 'account' and for_obj_id:
|
|
for_obj_id_random = for_obj_id
|
|
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id_random, table_name=for_obj_type)
|
|
|
|
data['for_obj_type'] = for_obj_type
|
|
data['for_obj_id'] = for_obj_id
|
|
data['for_obj_id_random'] = for_obj_id_random
|
|
|
|
sql_for_obj_type = f"""`journal_entry`.account_id = :for_obj_id"""
|
|
elif for_obj_type == 'user' and for_obj_id:
|
|
for_obj_id_random = for_obj_id
|
|
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id_random, table_name=for_obj_type)
|
|
|
|
data['user_id'] = for_obj_id
|
|
data['user_id_id_random'] = for_obj_id_random
|
|
|
|
sql_for_obj_type = f"""`journal_entry`.user_id = :user_id"""
|
|
else: sql_for_obj_type = ''
|
|
|
|
sql = f"""
|
|
SELECT *
|
|
FROM `v_journal_entry` AS journal_entry
|
|
WHERE {sql_for_obj_type}
|
|
"""
|
|
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(data)
|
|
log.debug(sql)
|
|
|
|
if sql_result := sql_select(data=data, sql=sql, as_list=True):
|
|
resp_data_li = []
|
|
for record in sql_result:
|
|
resp_data = base_name(**record).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
|
resp_data_li.append(resp_data)
|
|
|
|
return mk_resp(data=resp_data_li)
|
|
else:
|
|
log.debug(sql_result)
|
|
return mk_resp(data=False, status_code=404, response=response)
|
|
|
|
|
|
@router.get('/journal/entry/{obj_id}', response_model=Resp_Body_Base)
|
|
async def get_journal_entry_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'journal_entry'
|
|
result = get_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
# ### BEGIN ### API Post ### get_journal_obj_journal_entry_list() ###
|
|
# Updated 2021-07-22
|
|
@router.get('/journal/{journal_id}/entry/list', response_model=Resp_Body_Base)
|
|
async def get_journal_obj_journal_entry_list(
|
|
journal_id: str = Path(min_length=11, max_length=22),
|
|
limit: int = 500, # For now this covers any included objects or object lists
|
|
enabled: str = 'enabled', # For now this covers any included objects or object lists
|
|
inc_private: bool = False,
|
|
inc_public: bool = False,
|
|
inc_personal: bool = False,
|
|
inc_professional: bool = False,
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if journal_id := redis_lookup_id_random(record_id_random=journal_id, table_name='journal'): pass
|
|
else:
|
|
return mk_resp(data=None, status_code=404)
|
|
|
|
response_data = None
|
|
|
|
# Updated 2021-07-22
|
|
if journal_entry_rec_list_result := get_journal_entry_rec_list(
|
|
journal_id = journal_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
# archived = archived,
|
|
# archive_on = archive_on,
|
|
):
|
|
journal_entry_result_list = []
|
|
for journal_entry_rec in journal_entry_rec_list_result:
|
|
if load_journal_entry_result := load_journal_entry_obj(
|
|
journal_entry_id = journal_entry_rec.get('journal_entry_id', None),
|
|
limit = limit,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
# enabled = enabled,
|
|
):
|
|
journal_entry_result_list.append(load_journal_entry_result)
|
|
else:
|
|
journal_entry_result_list.append(None)
|
|
response_data = journal_entry_result_list
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
|
|
|
return mk_resp(data=response_data)
|
|
# ### END ### API Post ### get_account_obj_journal_entry_list() ###
|
|
|
|
|
|
@router.delete('/journal/entry/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_journal_entry_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'journal_entry'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result |