def save_new_customer_store(data): if data['customer_id'] != data['customer_store_id']: customer = Customer.query.filter_by( CustomerId=data['customer_id']).first() customer_store = Customer.query.filter_by( CustomerId=data['customer_store_id']).first() customer_store_current = CustomerStore.query.filter_by( CustomerId=data['customer_id'], CustomerStoreId=data['customer_store_id']).first() if (customer and customer_store): if not customer_store_current: try: new_customer_store = CustomerStore( CustomerId=data['customer_id'], CustomerStoreId=data['customer_store_id'], NameStore=data['name_store']) save_changes(new_customer_store) # response_object = { # 'status' : 'success', # 'message': 'Success create customer' # } return ResponseService().response('success', 200, data), 201 except: db.session.rollback() finally: db.session.close() else: customer_store_current.NameStore = data['name_store'] try: db.session.commit() response_object = { 'status': 'success', 'message': 'Success update customer store' } return ResponseService().response('success', 200, data), 201 except: db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Customer not exists. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Fail, please try again' } return response_object, 409
def update_transaction_remind(data): transaction_remind = TransactionRemind.query.filter_by( TransactionRemindId=data['transaction_remind_id']).first() if transaction_remind: try: transaction_remind.Status = TransactionRemind.STATUS_DELETED transaction_remind.Content = "content" in data and data[ 'content'] or transaction_remind.Content db.session.commit() response_object = { 'status': 'success', 'message': 'Success update customer' } return ResponseService().response('success', 200, data), 201 except Exception as e: #raise db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Transaction remind not exist. Please try again' } return response_object, 409
def get_profile_customer(customer_name): customer = Customer.query.filter( UserAccount.UserName == customer_name).join( Customer.user_account).options(subqueryload( Customer.user_account)).first() if customer: data = { 'name': customer.CustomerName, 'number_payment': customer.payment_account.NumberPaymentAccount, 'customer_id': customer.CustomerId, # 'amount': customer.payment_account.Amount, 'nick_name': customer.Nickname, 'email': customer.Email, 'phone': customer.Phone, 'username': customer.user_account.UserName, 'role': customer.Role is not None and json.JSONDecoder().decode(customer.Role) or '' } return ResponseService().response('success', 200, data), 200 else: response_object = { 'status': 'fail', 'message': 'Customer does not exist. Please try again' } return response_object, 409
def get_customer_by_number_payment(number_payment): payment_account = PaymentAccount.query.filter_by( NumberPaymentAccount=number_payment).first() if payment_account: customer = Customer.query.filter_by( PaymentAccount=payment_account.PaymentAccountId).options( joinedload('payment_account')).options( joinedload('user_account')).first() data = { 'name': customer.CustomerName, 'number_payment': customer.payment_account.NumberPaymentAccount, 'customer_id': customer.CustomerId, 'amount': customer.payment_account.Amount, 'nick_name': customer.Nickname, 'email': customer.Email, 'phone': customer.Phone, 'username': customer.user_account.UserName } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Customer not exists. Please try again' } return response_object, 409
def save_new_customer(data): user_account = UserAccount.query.filter_by( UserName=data['username']).first() if not user_account: try: password = '******' user_account_id = UserAccountService.save_user_account( UserAccount(UserName=data['username'], password=password)) payment_account_id = PaymentAccountService.save_payment_account( PaymentAccount( Amount=0, # init amount = 0, NumberPaymentAccount=randint(1000000000, 9999999999))) if user_account_id and payment_account_id: role = { "customer": 'customer' in data['role'] and data['role']['customer'] or False, 'employee': 'employee' in data['role'] and data['role']['employee'] or False, 'admin': 'admin' in data['role'] and data['role']['admin'] or False, 'any': True } new_customer = Customer(CustomerName=data['customername'], UserAccountId=user_account_id, PaymentAccount=payment_account_id, Nickname=data['nickname'], Phone=data['phone'], Email=data['email'], Address=data['address'], Gender=data["gender"], Role=json.JSONEncoder().encode(role)) save_changes(new_customer) # response_object = { # 'status' : 'success', # 'message': 'Success create customer' # } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Create payment_account and user_account fail. Please try again' } return response_object, 409 except: db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Customer already exists. Please login' } return response_object, 409
def create_transaction(data): try: read_private_key = open(ROOT_PATH + "/key/private_key.pem", "rb") private_key = RSA.importKey(read_private_key.read()) data_b64decode = base64.b64decode(bytes(data['uuid'], 'utf-8')) uuid = private_key.decrypt(data_b64decode) bank = Bank.query.filter_by(BankId=uuid).first() if bank: customer = Customer.query.join('payment_account').filter( PaymentAccount.NumberPaymentAccount == data['number_account']).first() if customer: customer.payment_account.Amount = data['amount'] db.session.commit() # add payment transaction tmp = PaymentTransactionService.save_payment_transaction( PaymentTransaction( PaymentAccountId=customer.CustomerId, PaymentAccountReceiveId=customer.CustomerId, Amount=data['amount'], Content='nap tien', OtpCode=None, SendOtpTime=None, Status=PaymentTransaction.STATUS_ACTIVE)) # add log history payment add_payment_history(type=PaymentHistory.SEND_AMOUNT, customer_id=customer.CustomerId) response_object = { 'status': 'success', 'message': 'Success confirm transaction' } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Customer does not exist. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Bank does not exist. Please try again' } return response_object, 409 except Exception as e: raise db.session.rollback() finally: db.session.close()
def confirm_transaction(data): customer = Customer.query.filter_by( CustomerId=data['customer_id']).options( joinedload('payment_account')).first() otp_code = data['otp_code'] if customer and otp_code: payment_transaction = PaymentTransaction.query.\ filter_by(PaymentAccountId=data['customer_id'], OtpCode=otp_code).\ first() current_time = datetime.utcnow().timestamp() if payment_transaction and payment_transaction.SendOtpTime <= current_time + PaymentTransaction.TIME_EXPIRE_OTP: customer_receive = Customer.query.filter_by( CustomerId=payment_transaction.PaymentAccountReceiveId ).options(joinedload('payment_account')).first() customer_amount = customer.payment_account.Amount # set amount of customer send customer_receive_amount = customer_receive.payment_account.Amount # set amount of customer will receive customer.payment_account.Amount = customer_amount - payment_transaction.Amount customer_receive.payment_account.Amount = customer_receive_amount + payment_transaction.Amount payment_transaction.Status = PaymentTransaction.STATUS_ACTIVE # set active try: db.session.commit() except: db.session.rollback() finally: db.session.close() # add log history payment add_payment_history(type=PaymentHistory.SEND_AMOUNT, customer_id=data['customer_id']) response_object = { 'status': 'success', 'message': 'Success confirm transaction' } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Confirm transaction fail or otp expire. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Confirm transaction fail. Please try again' } return response_object, 409
def create_transaction(data): customer = Customer.query.filter_by( CustomerId=data['customer_id']).options( joinedload('payment_account')).first() customer_receive = Customer.query.filter_by( CustomerId=data['customer_receive_id']).options( joinedload('payment_account')).first() if customer and customer_receive: if data['amount'] < customer.payment_account.Amount and data[ 'amount'] > 0: # create payment transaction here otpCode = PaymentTransactionService.generate_otp_code() tmp = PaymentTransactionService.save_payment_transaction( PaymentTransaction( PaymentAccountId=data['customer_id'], PaymentAccountReceiveId=data['customer_receive_id'], Amount=data['amount'], Content=data['content'], OtpCode=otpCode, SendOtpTime=datetime.utcnow().timestamp(), Status=PaymentTransaction.STATUS_INACTIVE)) data_sendmail = { 'customer_email': customer.Email, 'otp_code': otpCode } send_mail(data_sendmail) try: db.session.commit() except: db.session.rollback() finally: db.session.close() response_object = { 'status': 'success', 'message': 'Success create transaction' } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Create transaction fail. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Create transaction fail. Please try again' } return response_object, 409
def get_all_customer(): try: list_customer = db.session.query( Customer, PaymentAccount, Customer.CustomerName, PaymentAccount.NumberPaymentAccount).select_from(Customer).join( PaymentAccount).all() data = [] for i in list_customer: tmp = {'customer': i[2], 'number_payment': i[3]} data.append(tmp) return ResponseService().response('success', 200, data), 201 except: return jsonify(data=[])
def get_customer(id): customer = Customer.query.filter_by(CustomerId=id).options( joinedload('user_account')).first() data = { 'name': customer.CustomerName, 'phone': customer.Phone, 'email': customer.Email, 'id': customer.CustomerId, 'address': customer.Address, 'gender': customer.Gender, 'nickname': customer.Nickname, 'username': customer.user_account.UserName, 'amount': customer.payment_account.Amount, 'number_payment': customer.payment_account.NumberPaymentAccount, } return ResponseService().response('success', 200, data), 201
def update_customer(data): customer = Customer.query.filter_by(CustomerId=data['id']).first() if customer: try: role = { "customer": 'customer' in data['role'] and data['role']['customer'] or False, 'employee': 'employee' in data['role'] and data['role']['employee'] or False, 'admin': 'admin' in data['role'] and data['role']['admin'] or False, 'any': True } customer.CustomerName = "customername" in data and data[ "customername"] or customer.CustomerName customer.Nickname = "nickname" in data and data[ "nickname"] or customer.Nickname customer.Phone = "phone" in data and data['phone'] or customer.Phone customer.Address = "address" in data and data[ 'address'] or customer.Address customer.Email = "email" in data and data['email'] or customer.Email customer.Gender = "gender" in data and data[ 'gender'] or customer.Gender customer.Role = json.JSONEncoder().encode(role) db.session.commit() response_object = { 'status': 'success', 'message': 'Success update customer' } return ResponseService().response('success', 200, data), 201 # return response_object, 201 except Exception as e: #raise db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Customer not exists. Please try again' } return response_object, 409
def create_transaction_remind(data): customer = Customer.query.join("payment_account").filter( PaymentAccount.NumberPaymentAccount == data['number_payment']).first() customer_receive = Customer.query.join("payment_account").filter( PaymentAccount.NumberPaymentAccount == data['number_payment_remind']).first() if customer and customer_receive: # if data['amount'] < customer.payment_account.Amount and data['amount'] > 0: # print(customer.PaymentAccount) # print(customer_receive.PaymentAccount) # create payment transaction here # otpCode = PaymentTransactionService.generate_otp_code() try: tmp = TransactionRemind( PaymentAccountId=data['number_payment'], PaymentAccountRemindId=data['number_payment_remind'], Amount=data['amount'], Content=data['content'], Status=TransactionRemind.STATUS_CREATED) db.session.add(tmp) send_mail(customer_receive.Email, data['content'], data['amount']) db.session.commit() except: db.session.rollback() finally: db.session.close() response_object = { 'status': 'success', 'message': 'Success create transaction' } return ResponseService().response('success', 200, data), 201 else: response_object = { 'status': 'fail', 'message': 'Number payment not exist. Please try again' } return response_object, 409
def change_password(data): customer = Customer.query.filter_by(CustomerId=data['customer_id']).first() if 'customer_id' not in data or 'password' not in data or 'new_password' not in data: response_object = { 'status': 'fail', 'message': 'Customer not exists. Please try again' } return response_object, 409 if customer: if customer.user_account.check_password(data['password']): try: customer.user_account.password = data['new_password'] db.session.commit() response_object = { 'status': 'success', 'message': 'Success update customer' } del data['password'] del data['new_password'] return ResponseService().response('success', 200, data), 201 # return response_object, 201 except Exception as e: raise db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Old password does not match. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Customer not exists. Please try again' } return response_object, 409
def convert_uuid(data): try: # read public key read_public_key = open(ROOT_PATH + "/key/public_key.pem", "rb") # import public key public_key = RSA.importKey(read_public_key.read()) # encrypt uuid_encrypt = public_key.encrypt(bytes(data['uuid'], 'utf-8'), 32) # base64 encode base64_uuid = base64.b64encode(uuid_encrypt[0]) response_object = { 'status': 'success', 'message': 'Success convert uuid base64', 'base64_uuid': base64_uuid.decode('utf-8') } return ResponseService().response('success', 200, response_object), 201 except Exception as e: raise
def login(data): # customer = Customer.query.options(joinedload('user_account'))\ # .filter(or_(Customer.Email == data['email_or_username'], Customer.user_account==data['email_or_username'])).first() customer = Customer.query.join('user_account')\ .filter(or_(Customer.Email == data['email_or_username'], UserAccount.UserName==data['email_or_username'])).first() # auth_token = encode_auth_token(customer.CustomerId) access_token = create_access_token(identity=data['email_or_username']) refresh_token = create_refresh_token(identity=data['email_or_username']) # current_user = get_jwt_identity() # print(current_user) if customer: if customer.user_account.check_password(data['password']): response_object = { 'status': 'success', 'message': 'Success login customer', 'email_or_username': data['email_or_username'], 'access_token': access_token, 'refresh_token': refresh_token } return ResponseService().response('success', 200, response_object), 201 else: response_object = { 'status': 'fail', 'message': 'Password does not match. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Customer does not exist. Please try again' } return response_object, 409
def get_payment_history_customer(data, request): date_from = 'from' in request and request['from'] or date.today( ) - timedelta(30) date_to = 'to' in request and request['to'] or date.today() customer_1 = aliased(Customer, name='c1') customer_2 = aliased(Customer, name='c2') payment_account = aliased(PaymentAccount, name='pa') subquery = db.session.query( customer_2.CustomerName.label("received"), customer_1.CustomerName.label("send"), customer_1.CustomerId, PaymentTransaction.Amount, payment_account.NumberPaymentAccount ).filter(PaymentTransaction.Status == 1)\ .join(customer_1, PaymentTransaction.PaymentAccountId == customer_1.CustomerId)\ .join(payment_account, customer_1.PaymentAccount == payment_account.PaymentAccountId)\ .join(customer_2, PaymentTransaction.PaymentAccountReceiveId == customer_2.CustomerId)\ .subquery() result = db.session.query( case( [ ( PaymentHistory.Type == 1, literal_column("'nap tien'") ), ( PaymentHistory.Type == 2, literal_column("'chuyen tien'") ), ] ).label("content"), PaymentHistory.Type, subquery.c.NumberPaymentAccount, func.IF(PaymentHistory.Type == 2, subquery.c.send, None).label("sender"), func.IF(PaymentHistory.Type == 2, subquery.c.received, None).label("received"), func.IF(PaymentHistory.Type == 2, subquery.c.Amount, None).label("amount"), PaymentHistory.CreatedDate.label('created_date'), )\ .join(Customer)\ .join(subquery, subquery.c.CustomerId == PaymentHistory.CustomerId)\ .filter(PaymentHistory.CustomerId==data)\ .filter(and_(PaymentHistory.CreatedDate >= date_from, PaymentHistory.CreatedDate <= date_to))\ .all() list_key = [ "message", "type", "number_account", "sender", "received", "amount", "created_date" ] dict_result = [] for item in result: tmp_dict = {} for index, child_item in enumerate(item): tmp_dict[list_key[index]] = str(child_item) dict_result.append(tmp_dict) # return jsonify(data=[i.serialize for i in result]) return ResponseService().response('success', 200, dict_result), 201
def add_payment(data): #PaymentAccount.update().values(Amount=5).where(PaymentAccount.PaymentAccountId == Customer.PaymentAccount) # number_payment = data['number_payment_or_user_name'] # username = data['number_payment_or_user_name'] payment_account = PaymentAccount.query.filter_by( NumberPaymentAccount=data['number_payment_or_user_name']).first() user_account = UserAccount.query.filter_by( UserName=data['number_payment_or_user_name']).first() if payment_account: try: payment_account.Amount = payment_account.Amount + data['amount'] db.session.commit() response_object = { 'status': 'success', 'message': 'Success update amount' } # add log payment add_payment_history( type=PaymentHistory.ADD_AMOUNT, customer_id=payment_account.customer[0].CustomerId) # add payment transaction tmp = PaymentTransactionService.save_payment_transaction( PaymentTransaction( PaymentAccountId=payment_account.customer[0].CustomerId, PaymentAccountReceiveId=payment_account.customer[0]. CustomerId, Amount=data['amount'], Content='nap tien', OtpCode=None, SendOtpTime=datetime.utcnow().timestamp(), Status=PaymentTransaction.STATUS_ACTIVE)) return ResponseService().response('success', 200, data), 201 except: raise db.session.rollback() finally: db.session.close() elif user_account: customer = Customer.query.filter_by( UserAccountId=user_account.AccountId).first() if customer: payment_account = PaymentAccount.query.filter_by( PaymentAccountId=customer.PaymentAccount).first() if payment_account: try: payment_account.Amount = payment_account.Amount + data[ 'amount'] db.session.commit() response_object = { 'status': 'success', 'message': 'Success update amount' } # add log payment add_payment_history(type=PaymentHistory.ADD_AMOUNT, customer_id=customer.CustomerId) return ResponseService().response('success', 200, data), 201 except: db.session.rollback() finally: db.session.close() else: response_object = { 'status': 'fail', 'message': 'Update amount fail. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Update amount fail. Please try again' } return response_object, 409 else: response_object = { 'status': 'fail', 'message': 'Update amount fail. Please try again' } return response_object, 409