def save_paid_ticket(self, order): item_details = db.session.query(OrderDetails).filter_by(order_id=order['id']).all() for item in item_details: data = item.as_dict() for i in range(0, item.count): user_ticket = UserTicket() user_ticket.user_id = order['user_id'] user_ticket.ticket_id = data['ticket_id'] db.session.add(user_ticket) db.session.commit()
def update(self, code, user): response = ResponseBuilder() _result = {} _result['user'] = user token = db.session.query(AccessToken).filter_by( user_id=user['id']).first().as_dict() raw_user = db.session.query(User).filter_by(id=user['id']) raw_redeem_code = db.session.query(RedeemCode).filter_by(code=code) if raw_redeem_code.first() is None: return response.set_data(None).set_error(True).set_message( 'code not found').build() redeem_code = raw_redeem_code.first().as_dict() if raw_user.first() is None: return response.set_data(None).set_error(True).set_message( 'user not found').build() if redeem_code['used'] == 1: return response.set_data(None).set_error(True).set_message( 'code already used').build() try: if redeem_code['codeable_type'] in ['partner', 'user']: # become attendee userticket = UserTicket() userticket.user_id = user['id'] userticket.ticket_id = 1 db.session.add(userticket) db.session.commit() elif redeem_code['codeable_type'] == 'booth': # get booth of the code booth = db.session.query(Booth).filter_by( id=redeem_code['codeable_id']).first().as_dict() # become member of booth user_booth = UserBooth() user_booth.user_id = user['id'] user_booth.booth_id = booth['id'] db.session.add(user_booth) user['role_id'] = 3 db.session.commit() _result['user']['booth'] = booth raw_redeem_code.update({'used': True}) raw_user.update({'role_id': user['role_id']}) db.session.commit() token_payload = { 'access_token': token['access_token'], 'refresh_token': token['refresh_token'] } return response.set_data(token_payload).set_included( _result['user']).set_message('Redeem code updated successfully' ).set_error(False).build() except SQLAlchemyError as e: return response.set_data( e.orig.args).set_message('SQL error').set_error(True).build()
def create(payload): response = ResponseBuilder() new_user_ticket = UserTicket() new_user_ticket.user_id = payload['user_id'] new_user_ticket.ticket_id = payload['ticket_id'] db.session.add(new_user_ticket) try: db.session.commit() # return create success return response.set_data({ 'created': True }).set_error(False).set_message( 'User ticket created successfully').build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).set_message( 'SQL error').build()