Ejemplo n.º 1
0
    def get(self, page=1, page_size=10, *args, **kwargs):
        try:
            resellers = self.resellerService.get(page, page_size)

            return ok(data=resellers)
        except Exception as e:
            return error()
Ejemplo n.º 2
0
 def post(self, *args, **kwargs):
     json = request.get_json() or None
     if json is None:
         return bad_request()
     try:
         data = LoginSchema().load(json)
         reseller = self.resellerService.get_by_cpf(data["cpf"])
         if reseller == None:
             return bad_request(
                 f'Reseller: {data["cpf"]} not found, please register before to tryng login'
             )
         if not reseller.active:
             return bad_request(f'Reseller: {data["cpf"]} are disabled')
         if not self.authService.login(reseller, data["password"]):
             return bad_request("Check username and password")
         jwt = {
             'token': create_access_token(identity=reseller.cpf),
             'refresh': create_refresh_token(identity=reseller.cpf)
         }
         return ok(data=jwt)
     except NotFoundError:
         return bad_request('Reseller not found')
     except SchemaValidationError as e:
         return bad_request(errors=e.messages)
     except Exception as e:
         return error(e)
Ejemplo n.º 3
0
 def get(self):
     try:
         cpf = get_jwt_identity()
         purchases = self.cashbackService.get_purchases(cpf)
     except Exception as e:
         return error(e)
     else:
         return ok(data=purchases)
Ejemplo n.º 4
0
 def get(self):
     try:
         cpf = get_jwt_identity()
         cashback = self.cashbackService.calcule_cashback(cpf)
     except Exception as e:
         return error(e)
     else:
         return ok(data=cashback)
Ejemplo n.º 5
0
    def post(self, *args, **kwargs):
        json = request.get_json() or None
        if json is None:
            return bad_request('Invalid data')

        try:
            data = self.resellerRegisterSchema.load(json)
            reseller = self.resellerService.create(data, register=True)
        except NotUniqueError as e:
            return bad_request("This email OR cpf already exist")
        except MongoValidationError as e:
            return bad_request('Error storage', e.message)
        except SchemaValidationError as e:
            return bad_request('Invalid request', e.messages)
        except Exception as e:
            return error(e)
        else:
            return ok(data=reseller)
Ejemplo n.º 6
0
    def post(self):
        json = request.get_json() or None
        if json is None:
            return bad_request()

        try:
            cpf = get_jwt_identity()
            data = self.purchaseSchema.load(json)
            self.cashbackService.register_purchase(cpf, data)
        except NotUniqueError:
            return bad_request("This sale code is already in use")
        except MongoValidationError as e:
            return bad_request('Error storage', e.message)
        except SchemaValidationError as e:
            return bad_request('Invalid request', e.messages)
        except Exception as e:
            return error(e)
        else:
            return ok(data=f'Sale {json["code"]} saved')
Ejemplo n.º 7
0
    def put(self, cpf: str, *args, **kwargs):
        json = request.get_json() or None
        if json is None:
            return bad_request()

        try:
            data = self.resellerUpdateSchema.load(json)
            data["updated_by"] = get_jwt_identity()

            reseller = self.resellerService.update(cpf, data)
        except NotFoundError:
            return bad_request("Reseller not found")
        except NotUniqueError:
            return bad_request("This email OR cpf already exist")
        except MongoValidationError as e:
            return bad_request('Error storage', e.message)
        except SchemaValidationError as e:
            return bad_request('Invalid request', e.messages)
        except Exception as e:
            return error(e)
        else:
            return ok(data=reseller)
Ejemplo n.º 8
0
    def post(self, *args, **kwargs):
        json = request.get_json() or None
        if json is None:
            return bad_request()

        try:
            data = self.resellerCreateSchema.load(json)
            data["created_by"] = get_jwt_identity()
            data["updated_by"] = get_jwt_identity()
            if "purchases" in data.keys():
                for purchase in data["purchases"]:
                    purchase["created_by"] = get_jwt_identity()

            reseller = self.resellerService.create(data)
        except NotUniqueError:
            return bad_request("This email OR cpf already exist")
        except MongoValidationError as e:
            return bad_request('Error storage', e.message)
        except SchemaValidationError as e:
            return bad_request('Invalid request', e.messages)
        except Exception as e:
            return error(e)
        else:
            return ok(data=reseller)