Commit 1dcac753 authored by confusion's avatar confusion

修改净值接口查询 添加权限验证

parent c05520fe
import datetime
from typing import Optional
import re
from loguru import logger
import pytz
from fastapi import APIRouter, Depends, Query, Body
from fastapi.background import BackgroundTasks
from motor.core import AgnosticCollection
from dependencies import get_nav_collect, get_fund_collect
from dependencies import get_nav_collect, get_fund_collect, get_permission_user_collect, get_permission_role_collect, \
get_current_user
from model import SortParams, FilterTime, Page, PageResponse, Response
from model.fund import StakingFundNav
from service.nav import calculate_nav, build_fund_nav
from service.permission import check_permission
from tools.jwt_tools import User
router = APIRouter()
......@@ -17,11 +19,18 @@ router = APIRouter()
@router.get("/{fund_id}/", response_model=PageResponse[StakingFundNav], summary="查询净值记录")
async def get_nav(
fund_id: str,
user: User = Depends(get_current_user),
sort_by: SortParams = Depends(SortParams),
filter_time: FilterTime = Depends(FilterTime),
page: Page = Depends(Page),
nav_collect: AgnosticCollection = Depends(get_nav_collect)
nav_collect: AgnosticCollection = Depends(get_nav_collect),
permission_user_collect: AgnosticCollection = Depends(get_permission_user_collect),
permission_role_collect: AgnosticCollection = Depends(get_permission_role_collect)
):
await check_permission(['data_permission.nav.query'],
fund_id,
user.email, permission_user_collect,
permission_role_collect)
query = {"fund_id": fund_id}
if filter_time.start_time and filter_time.end_time:
query.update({'record_date': filter_time.to_mongodb_query()})
......@@ -34,16 +43,6 @@ async def get_nav(
return response
@router.post("/{fund_id}/", response_model=Response[StakingFundNav], summary="插入净值记录")
async def insert_nav(
fund_id: str,
calc_time: Optional[datetime.datetime] = Query(default=None, title="时间"),
):
fund_nav = await calculate_nav(fund_id=fund_id, calc_time=calc_time) if calc_time else await calculate_nav(fund_id)
response = Response[StakingFundNav](data=fund_nav)
return response
@router.post("/", response_model=Response[bool], summary="新增净值记录")
async def create_nav(
fund_id: str = Body(..., title="基金id"),
......@@ -52,9 +51,18 @@ async def create_nav(
fund_share: float = Body(..., title="份额"),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
nav_collect: AgnosticCollection = Depends(get_nav_collect),
user: User = Depends(get_current_user),
permission_user_collect: AgnosticCollection = Depends(get_permission_user_collect),
permission_role_collect: AgnosticCollection = Depends(get_permission_role_collect)
):
await check_permission(['data_permission.nav.add'],
fund_id,
user.email, permission_user_collect,
permission_role_collect)
fund_data = await fund_collect.find_one({"id": fund_id})
fund_nav = build_fund_nav(fund_data, record_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC), fund_share, nav)
fund_nav = build_fund_nav(fund_data,
record_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC),
fund_share, nav)
insert = await nav_collect.insert_one(fund_nav)
response = Response(data=insert.acknowledged)
return response
......@@ -67,12 +75,19 @@ async def update_nav(
record_date: datetime.datetime = Body(..., title="记录日期"),
nav: float = Body(..., title="净值"),
fund_share: float = Body(..., title="份额"),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
nav_collect: AgnosticCollection = Depends(get_nav_collect),
user: User = Depends(get_current_user),
permission_user_collect: AgnosticCollection = Depends(get_permission_user_collect),
permission_role_collect: AgnosticCollection = Depends(get_permission_role_collect)
):
await check_permission(['data_permission.nav.edit'],
fund_id,
user.email, permission_user_collect,
permission_role_collect)
update_result = await nav_collect.update_one(
{"id": nav_id, "fund_id": fund_id},
{"$set": {"record_date": record_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC), "nav": nav, "fund_share": fund_share}}
{"$set": {"record_date": record_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC),
"nav": nav, "fund_share": fund_share}}
)
response = Response(data=update_result.modified_count)
return response
......@@ -85,7 +100,14 @@ async def recalculate_nav(
start: datetime.datetime = Query(..., title="开始时间"),
end: datetime.datetime = Query(None, title="结束时间"),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
user: User = Depends(get_current_user),
permission_user_collect: AgnosticCollection = Depends(get_permission_user_collect),
permission_role_collect: AgnosticCollection = Depends(get_permission_role_collect)
):
await check_permission(['data_permission.nav.recalculate'],
fund_id,
user.email, permission_user_collect,
permission_role_collect)
fund_data = await fund_collect.find_one({"id": fund_id})
end = end or datetime.datetime.utcnow()
delta = end - start
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment