def attend_request(self, id_request, *args, **kwargs): db = Database() attended_request = { 'id': id_request, 'id_driver': self.id, 'status': Request.REQUEST_STATUS.get('ongoing') } request = Request(**attended_request) db.update(request) driver = Driver(**{'id': self.id, 'current_request': id_request}) db.update(driver) new_history = { 'id_request': request.id, 'old_status': Request.REQUEST_STATUS.get('new'), 'new_status': Request.REQUEST_STATUS.get('ongoing'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) return { 'request': object_to_dict(request), 'msg': 'Request attended successfully' }
def deliver_request(self, id_request, delivery_code): db = Database() code_search = db.query( f"SELECT * FROM delivery_codes WHERE code = '{delivery_code}'") id_collector = code_search[0][1] delivered_request = { 'id': id_request, 'id_collector': id_collector, 'status': Request.REQUEST_STATUS.get('evaluation') } request = Request(**delivered_request) db.update(request) driver = Driver(**{'id': self.id, 'current_request': None}) db.update(driver) dc = db.get(DeliveryCode, code_search[0][0]) db.delete(dc) new_history = { 'id_request': request.id, 'old_status': Request.REQUEST_STATUS.get('ongoing'), 'new_status': Request.REQUEST_STATUS.get('evaluation'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) return { 'request': object_to_dict(request), 'msg': 'Request delivered successfully' }
def handle(event, context): body = json.loads(event.get('body')) collector = Collector(**body) db = Database() db.add(collector) collector.__dict__.pop('_sa_instance_state') response = { 'statusCode': 200, 'headers': { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': True }, 'body': json.dumps({ 'collector': collector.__dict__, 'msg': f'Collector {collector.id} added successfully' }), } return response
def exchange_points(self, product_id, *args, **kwargs): db = Database() product = db.get(Product, product_id) if self.points < product.price: return {'msg': 'Insufficient points'} user = User(**{'id': self.id, 'points': self.points - product.price}) db.update(user) order = Order(id_user=self.id, id_product=product.id, timestamp=str(datetime.datetime.now().timestamp())) db.add(order) return {'msg': f'{product.name} bought successfully'}
def receive_request(self, *args, **kwargs): code = self.generate_delivery_code() db = Database() delivery_code = DeliveryCode(**{'collector_id': self.id, 'code': code}) db.add(delivery_code) return { 'delivery_code': object_to_dict(delivery_code), 'msg': 'Delivery code generated successfully' }
def approve_request(self, id_request, *args, **kwargs): db = Database() approved_request = { 'id': id_request, 'status': Request.REQUEST_STATUS.get('approved') } request = Request(**approved_request) db.update(request) new_history = { 'id_request': request.id, 'old_status': Request.REQUEST_STATUS.get('waiting_approval'), 'new_status': Request.REQUEST_STATUS.get('approved'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) updated_user = { 'id': self.id, 'points': self.points + db.get(Request, id_request).points } user = User(**updated_user) db.update(user) req = db.get(Request, id_request) id_driver = req.id_driver drv = db.get(Driver, id_driver) updated_driver = { 'id': id_driver, 'points': drv.points + (req.points / 2) } driver = Driver(**updated_driver) db.update(driver) request.status = Request.REQUEST_STATUS.get('concluded') db.update(request) return { 'request': object_to_dict(request), 'msg': 'Request approved successfully' }
def handle(event, context): body = json.loads(event.get('body')) driver = Driver(**body) db = Database() db.add(driver) driver.__dict__.pop('_sa_instance_state') response = { 'statusCode': 200, 'body': json.dumps({ 'driver': driver.__dict__, 'msg': f'Driver {driver.id} added successfully' }), } return response
def evaluate_request(self, id_request, weight, *args, **kwargs): db = Database() request_evaluated = { 'id': id_request, 'status': Request.REQUEST_STATUS.get('concluded'), 'weight': weight, 'points': weight / 5 } request = Request(**request_evaluated) db.update(request) new_history = { 'id_request': request.id, 'old_status': Request.REQUEST_STATUS.get('evaluation'), 'new_status': Request.REQUEST_STATUS.get('concluded'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) req = db.get(Request, id_request) us = db.get(User, req.id_user) updated_user = {'id': us.id, 'points': us.points + req.points} user = User(**updated_user) db.update(user) drv = db.get(Driver, req.id_driver) updated_driver = { 'id': req.id_driver, 'points': drv.points + (req.points / 2) } driver = Driver(**updated_driver) db.update(driver) return { 'request': object_to_dict(request), 'msg': 'Request evaluated successfully' }
def handle(event, context): body = json.loads(event.get('body')) product = Product(**body) db = Database() db.add(product) product.__dict__.pop('_sa_instance_state') response = { 'statusCode': 200, 'body': json.dumps({ 'product': product.__dict__, 'msg': f'Product {product.id} added successfully' }), } return response
def handle(event, context): body = json.loads(event.get('body')) adress = Adress(**body) db = Database() db.add(adress) adress.__dict__.pop('_sa_instance_state') response = { 'statusCode': 200, 'body': json.dumps({ 'adress': adress.__dict__, 'msg': f'Adress {adress.id} added successfully' }), } return response
def contest_request(self, id_request, *args, **kwargs): db = Database() contested_request = { 'id': id_request, 'status': Request.REQUEST_STATUS.get('contested') } request = Request(**contested_request) db.update(request) new_history = { 'id_request': request.id, 'old_status': Request.REQUEST_STATUS.get('waiting_approval'), 'new_status': Request.REQUEST_STATUS.get('contested'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) return { 'request': object_to_dict(request), 'msg': 'Request contested successfully' }
def start_request(self, address_id, *args, **kwargs): db = Database() new_request = { 'id_user': self.id, 'id_address': address_id, 'status': Request.REQUEST_STATUS.get('new') } request = Request(**new_request) db.add(request) new_history = { 'id_request': request.id, 'new_status': Request.REQUEST_STATUS.get('new'), 'timestamp': str(datetime.datetime.now().timestamp()) } history = History(**new_history) db.add(history) return { 'request': object_to_dict(request), 'msg': 'Request started successfully' }