def find_by_cpf(self, cpf: str): try: only_cpf = StringUtil.get_cpf(cpf) data = self.repository.find_by_cpf(cpf=only_cpf) return self.view_schema.dump(data) except NotFoundException: raise NotFoundException(message='Not found') except Exception as err: raise NotMappedException(message=err)
def login(self, cpf: str, password: str): try: only_cpf = StringUtil.get_cpf(cpf) data = self.repository.login(cpf=only_cpf, password=password) return self.create_schema.dump(data) except NotFoundException: raise NotFoundException('Not found') except Exception as err: raise NotMappedException(err)
def delete(self, id_reseller): try: logging.info('Start delete reseller') return self.repository.delete(id_reseller) except NotFoundException: raise NotFoundException('Not found') except InvalidDataException as err: raise InvalidDataException(err) except Exception as err: raise NotMappedException(err)
def find_by_cpf(self, cpf): try: logging.info('Start find purchase') only_cpf = StringUtil.get_cpf(cpf) return ViewPurchaseSchema(many=True).dump( self.repository.find_by_cpf(only_cpf)) except NotFoundException: raise NotFoundException('Not found') except Exception as err: raise NotMappedException(err)
def add(self, reseller): try: logging.info('Start create reseller') self.create_schema.load(reseller) data = ResellerToObject.get_reseller(reseller) return self.create_schema.dump(self.repository.add(data)) except ValidationError as valid_error: raise InvalidDataException(valid_error) except DuplicateDataException as duplicate_error: raise DuplicateDataException(duplicate_error) except Exception as err: raise NotMappedException(err)
def cash_back(self, cpf): try: only_cpf = StringUtil.get_cpf(cpf) if only_cpf == '': raise ConsumeApiException('Problem with cashback api') url = URL_CASH_BACK.format(only_cpf) header = {'token': TOKEN_CASH_BACK} response = requests.get(url, headers=header) return response.json()["body"]["credit"] except ConsumeApiException as err: raise ConsumeApiException(err) except Exception as err: raise NotMappedException(err)
def add(self, purchase): try: logging.info('Start create purchase') self.create_schema.load(purchase) purchase["status"] = _get_status(purchase["cpf"]) logging.info('Find a reseller by cpf purchase') reseller = self.reseller_service.find_by_cpf(purchase["cpf"]) purchase["reseller_id"] = reseller["id"] data = PurchaseToObject.get_purchase(purchase) data.value_cb = _get_cash_back(data.value) return self.create_schema.dump(self.repository.add(data)) except ValidationError as valid_error: raise InvalidDataException(valid_error) except Exception as err: raise NotMappedException(err)
def update(self, purchase): try: if "cash_back" in purchase: del purchase["cash_back"] logging.info('Start update purchase') self.update_schema.load(purchase) old_purchase = self.repository.find_by_id(purchase["id"]) if old_purchase.status == "Aprovado": raise BusinessException("Situation does not allow change") purchase["status"] = _get_status(purchase["cpf"]) logging.info('Find a reseller by cpf purchase') reseller = self.reseller_service.find_by_cpf(purchase["cpf"]) purchase["reseller_id"] = reseller["id"] data = PurchaseToObject.get_purchase(purchase) data.value_cb = _get_cash_back(data.value) return self.update_schema.dump(self.repository.update(data)) except ValidationError as valid_error: raise InvalidDataException(valid_error) except NotFoundException: raise NotFoundException('Not found') except BusinessException as business_error: raise BusinessException(business_error) except Exception as err: raise NotMappedException(err)