async def _get_user(id: str): _id = validate_object_id(id) user = await db.users.find_one({"_id": _id}) if user: return fix_id(user) else: raise HTTPException(status_code=404, detail="User not found")
async def _get_client(id: str): _id = validate_object_id(id) client = await db.clients.find_one({"_id": _id}) if client: return fix_id(client) else: raise HTTPException(status_code=404, detail="Client not found")
async def _get_history_loan_row_with_loans_id(row_id: str): _id = validate_object_id(row_id) row = await db.history_loans.find_one({"_id": _id}) if row: return fix_id(row) else: raise HTTPException(status_code=404, detail="row not found")
async def _get_loan(id: str): _id = validate_object_id(id) loan = await db.loans.find_one({"_id": _id}) if loan: return fix_id(loan) else: raise HTTPException(status_code=404, detail="Loan not found")
async def update_loan_by_id( loan_id: str, loan: LoansChange, current_user: UserResponse = Depends(get_current_active_user)): # validation loan.rate = float(loan.rate) loan.increased_rate = float(loan.increased_rate) try: loan.status = LoanStatus(loan.status) except: raise HTTPException(status_code=400, detail="Invalid status name " + loan.status + ". Valid status: ACTIVE, OVERDUE, ARCHIVED") if not isinstance(loan.expiration_at, datetime) \ or not isinstance(loan.created_at, datetime) \ or not isinstance(loan.issued_at, datetime): raise HTTPException(status_code=400, detail="Invalid type datetime") if loan.amount < 0 \ or loan.increased_rate < 0 \ or loan.rate < 0: raise HTTPException( status_code=400, detail="amount or rate or increased_rate must be non-negative ") if loan.expiration_at <= loan.issued_at: raise HTTPException( status_code=400, detail="Date issued must be early then date expiration") ############# result = await db.loans.update_one( { "$and": [{ "_id": ObjectId(loan_id) }, { "users_id": ObjectId(current_user.id) }] }, { "$set": { "amount": loan.amount, "rate": loan.rate, "increased_rate": loan.increased_rate, "goal": loan.goal, "created_at": loan.created_at, "issued_at": loan.issued_at, ## дата выдачи займа "expiration_at": loan.expiration_at, ## срок выплаты по договору "status": loan.status } }) if result: loan = await db.loans.find_one({"_id": ObjectId(loan_id)}) return fix_id(loan) else: raise HTTPException(status_code=500, detail="Loan hasn't been changed")
async def update_user_by_id(user: UserChange, current_user: UserResponse = Depends(get_current_active_user)): pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") result = await db.users.update_one({"_id": ObjectId(current_user.id)}, {"$set": { "name": user.name, "password": pwd_context.hash(user.password) }}) if result: user = await db.users.find_one({"_id": ObjectId(current_user.id)}) return fix_id(user) else: raise HTTPException(status_code=500, detail="User hasn't been changed")
async def update_client_by_id( history_loan_row_id: str, history_loan_row: HistoryLoansChange, current_user: UserResponse = Depends(get_current_active_user)): # # valid """ Valid status: DEPT, PROCENT, RETURN_DEPT, RETURN_PROCENT """ history_loan_row.amount = float(history_loan_row.amount) if history_loan_row.amount < 0: raise HTTPException(status_code=400, detail="amount must be non-negative ") try: tmp = await _get_history_loan_row_with_loans_id(history_loan_row_id) history_loan_row.type = HistoryLoanType(history_loan_row.type) except: raise HTTPException(status_code=400, detail="Invalid type name " + history_loan_row.type + ". Valid status: DEPT, PROCENT") if not isinstance(history_loan_row.date, datetime): raise HTTPException(status_code=400, detail="Invalid type datetime") history_loan_roww = await _get_history_loan_row_id(history_loan_row_id, tmp['loans_id']) loans_id = history_loan_roww["loans_id"] loan = await _get_loan(loans_id) loan_date = loan['issued_at'] if history_loan_row.date.isocalendar() < loan_date.isocalendar(): raise HTTPException(status_code=400, detail="Date pay must be older then date issued") ############################# if history_loan_row.type == HistoryLoanType.RETURN_DEPT or history_loan_row.type == HistoryLoanType.RETURN_PROCENT: history_loan_row.amount = -history_loan_row.amount result = await db.history_loans.update_one( {"_id": ObjectId(history_loan_row_id)}, { "$set": { "amount": history_loan_row.amount, "date": history_loan_row.date, "type": history_loan_row.type } }) if result: row = await db.history_loans.find_one( {"_id": ObjectId(history_loan_row_id)}) return fix_id(row) else: raise HTTPException(status_code=500, detail="Payment hasn't been changed")
async def create_history_loan_row( history_loan_row: HistoryLoansCreate, current_user: UserResponse = Depends(get_current_active_user)): """ Valid status: DEPT, PROCENT, RETURN_DEPT, RETURN_PROCENT """ # valid history_loan_row.amount = float(history_loan_row.amount) if history_loan_row.amount < 0: raise HTTPException(status_code=400, detail="amount must be non-negative ") try: history_loan_row.type = HistoryLoanType(history_loan_row.type) except: raise HTTPException( status_code=400, detail="Invalid type name " + history_loan_row.type + ". Valid status: DEPT, PROCENT, RETURN_DEPT, RETURN_PROCENT") if not isinstance(history_loan_row.date, datetime): raise HTTPException(status_code=400, detail="Invalid type datetime") loan = await _get_loan(history_loan_row.loans_id) loan_date = loan['issued_at'] if history_loan_row.date.isocalendar() < loan_date.isocalendar(): raise HTTPException(status_code=400, detail="Date pay must be older then date issued") ###################### if history_loan_row.type == HistoryLoanType.RETURN_DEPT or history_loan_row.type == HistoryLoanType.RETURN_PROCENT: history_loan_row.amount = -history_loan_row.amount result = await db.history_loans.insert_one({ "amount": history_loan_row.amount, "date": history_loan_row.date, "type": history_loan_row.type, "loans_id": ObjectId(history_loan_row.loans_id) }) if result: row = await db.history_loans.find_one({"_id": result.inserted_id}) return fix_id(row) else: raise HTTPException(status_code=500, detail="Payment hasn't been created")
async def create_loan( loan: LoansCreate, current_user: UserResponse = Depends(get_current_active_user)): loan.rate = float(loan.rate) loan.increased_rate = float(loan.increased_rate) try: loan.status = LoanStatus(loan.status) except: raise HTTPException(status_code=400, detail="Invalid status name " + loan.status + ". Valid status: ACTIVE, OVERDUE, ARCHIVED") if not isinstance(loan.expiration_at, datetime) \ or not isinstance(loan.created_at, datetime) \ or not isinstance(loan.issued_at, datetime): raise HTTPException(status_code=400, detail="Invalid type datetime") if loan.amount < 0 \ or loan.increased_rate < 0 \ or loan.rate < 0: raise HTTPException( status_code=400, detail="amount or rate or increased_rate must be non-negative ") if loan.expiration_at <= loan.issued_at: raise HTTPException( status_code=400, detail="Date issued must be early then date expiration") ############# result = await db.loans.insert_one({ "amount": loan.amount, "rate": loan.rate, "increased_rate": loan.increased_rate, "goal": loan.goal, "clients_id": ObjectId(loan.clients_id), "users_id": ObjectId(current_user.id), "created_at": loan.created_at, "issued_at": loan.issued_at, "expiration_at": loan.expiration_at, "status": loan.status }) if result: loan = await db.loans.find_one({"_id": result.inserted_id}) return fix_id(loan) else: raise HTTPException(status_code=500, detail="Loan hasn't been created")
async def update_client_by_id( client_id: str, client: ClientChange, current_user: UserResponse = Depends(get_current_active_user)): result = await db.clients.update_one( { "$and": [{ "_id": ObjectId(client_id) }, { "user_id": ObjectId(current_user.id) }] }, {"$set": { "name": client.name, "phone": client.phone, }}) if result: client = await db.clients.find_one({"_id": ObjectId(client_id)}) return fix_id(client) else: raise HTTPException(status_code=500, detail="Сlient hasn't been changed")
async def create_user( name: str, phone: str, current_user: UserResponse = Depends(get_current_active_user)): # unic client? phone_count = await db.clients.count_documents({"phone": phone}) if phone_count > 0: raise HTTPException( status_code=409, detail="This phone already exist. Try to use another one") # insert result = await db.clients.insert_one({ "name": name, "phone": phone, "user_id": ObjectId(current_user.id) }) if result: client = await db.clients.find_one({"_id": result.inserted_id}) return fix_id(client) else: raise HTTPException(status_code=500, detail="Сlient hasn't been created")
async def get_user(username): user = await db.users.find_one({"username": username}) if user: user = fix_id(user) return UserDB(**user)