feat: balance and reward
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from routes.auth import auth_blueprint
|
||||
from routes.orders import orders_blueprint
|
||||
from routes.barcode import barcode_blueprint
|
||||
from routes.printing import printing_blueprint
|
||||
from routes.assembly import assembly_blueprint
|
||||
from routes.general import general_blueprint
|
||||
from routes.application import application_blueprint
|
||||
from routes.sipro import sipro_blueprint
|
||||
from routes.admin import admin_blueprint
|
||||
from .auth import auth_blueprint
|
||||
from .orders import orders_blueprint
|
||||
from .barcode import barcode_blueprint
|
||||
from .printing import printing_blueprint
|
||||
from .assembly import assembly_blueprint
|
||||
from .general import general_blueprint
|
||||
from .application import application_blueprint
|
||||
from .sipro import sipro_blueprint
|
||||
from .admin import admin_blueprint
|
||||
from .balance import balance_blueprint
|
||||
|
||||
@@ -8,6 +8,7 @@ import database
|
||||
from database.enums import AssemblyState
|
||||
from routes.utils import jwt_protect_blueprint
|
||||
import sipro.api.orders
|
||||
import utils.balance
|
||||
|
||||
assembly_blueprint = jwt_protect_blueprint(Blueprint('assembly', __name__))
|
||||
|
||||
@@ -86,7 +87,17 @@ def close_assembly():
|
||||
assembly.ended_at = datetime.datetime.now()
|
||||
database.db.session.commit()
|
||||
order_id = assembly.order_id
|
||||
return sipro.api.orders.close_order(order_id)
|
||||
sipro_response = sipro.api.orders.close_order(order_id)
|
||||
reward = sipro_response.get('reward')
|
||||
ok = sipro_response.get('ok')
|
||||
if ok:
|
||||
utils.balance.add_top_up(user_id=assembly.user_id,
|
||||
amount=reward,
|
||||
description=f'Начисление за сборку заказа {order_id}',
|
||||
json_data={'order_id': order_id},
|
||||
commit=True)
|
||||
|
||||
return sipro_response
|
||||
|
||||
|
||||
@assembly_blueprint.post('/cancel')
|
||||
@@ -142,7 +153,6 @@ def cancel_assembly_by_id():
|
||||
return jsonify(response)
|
||||
|
||||
|
||||
|
||||
@assembly_blueprint.get('/hasActive')
|
||||
def user_has_active_assembly():
|
||||
user_id = get_jwt_identity()
|
||||
|
||||
23
routes/balance.py
Normal file
23
routes/balance.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from flask import Blueprint, request
|
||||
from flask_jwt_extended import get_jwt_identity
|
||||
from routes.utils import jwt_protect_blueprint
|
||||
|
||||
import queries.balance.api as api
|
||||
|
||||
balance_blueprint = jwt_protect_blueprint(Blueprint('balance', __name__))
|
||||
|
||||
|
||||
@balance_blueprint.get('/transactions')
|
||||
def get_transactions():
|
||||
data = dict(request.args)
|
||||
if 'user_id' not in data:
|
||||
data['user_id'] = get_jwt_identity()
|
||||
response = api.get_balance_transactions(dict(data))
|
||||
return response
|
||||
|
||||
|
||||
@balance_blueprint.get('/info')
|
||||
def get_balance_info():
|
||||
data = {'user_id': get_jwt_identity()}
|
||||
response = api.get_balance_info(data)
|
||||
return response
|
||||
@@ -1,4 +1,4 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint
|
||||
from routes.utils import jwt_protect_blueprint
|
||||
import sipro.api.general
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
from enum import StrEnum
|
||||
from flask import Blueprint, request, jsonify
|
||||
from sqlalchemy import func
|
||||
@@ -127,3 +128,22 @@ def get_users():
|
||||
.all())
|
||||
response = [{'id': user.id, 'login': user.login} for user in query]
|
||||
return jsonify(response)
|
||||
|
||||
|
||||
@sipro_blueprint.get('/statistics')
|
||||
def get_users_statistics():
|
||||
data: dict = request.args
|
||||
user_id = int(data['userId'])
|
||||
date_from = datetime.datetime.fromisoformat(data['dateFrom'])
|
||||
date_to = datetime.datetime.fromisoformat(data['dateTo']) + datetime.timedelta(hours=24, minutes=59, seconds=59)
|
||||
|
||||
query = (database.BalanceTransaction.query
|
||||
.filter(database.BalanceTransaction.user_id == user_id,
|
||||
database.BalanceTransaction.created_at.between(date_from, date_to))
|
||||
.order_by(func.date_trunc('day', database.BalanceTransaction.created_at))
|
||||
.group_by(func.date_trunc('day', database.BalanceTransaction.created_at))
|
||||
.with_entities(func.date_trunc('day', database.BalanceTransaction.created_at).label('date'),
|
||||
func.sum(database.BalanceTransaction.amount).label('value'))
|
||||
.all())
|
||||
result = [{'date': row.date.isoformat(), 'value': row.value} for row in query]
|
||||
return jsonify(result)
|
||||
|
||||
Reference in New Issue
Block a user