def delete_provider(id): try: provider = ProviderModel.get_by_user_id(id) provider.delete() return {"success": True, "message": 'provider deleted'} except Exception as e: return {"success": False, "message": f"{e}"}
def post(self): data = request.get_json() email = data['email'].strip() password = encrypt(data['password']) user = UserModel.authenticate(email, password) if user: if user.type_user == 'insured': user_data = InsuredModel.get_by_user_id(user.id) user_data_json = { 'id': user_data.id, 'first_name':user_data.first_name, 'last_name': user_data.last_name, 'cpf':user_data.cpf, } elif user.type_user == 'provider': user_data = ProviderModel.get_by_user_id(user.id) user_data_json = { 'id': user_data.id, 'business_name':user_data.business_name, 'fantasy_name': user_data.fantasy_name, 'cnpj':user_data.cnpj, } else: user_data = CollaboratorModel.get_by_user_id(user.id) user_data_json ={ 'id': user_data.id, 'first_name':user_data.first_name, 'last_name': user_data.last_name, 'registration':user_data.registration, } access = create_jwt({ 'id_insured': user.id, 'email': user.email, 'status': user.status, 'type_user':user.type_user }) return { 'id_insured': user.id, 'email': user.email, 'status': user.status, 'type_user':user.type_user, 'created_date':user.created_date.strftime("%d/%m/%Y"), 'jwt': access, 'user_data':user_data_json }, 200 else: return {'message': 'Invalid credentials'}, 400
def select_provider_by_user_id(user_id): try: provider = ProviderModel.get_by_user_id(user_id) if provider is None: return {'success': False, 'message': 'Provider not found'} else: return { 'id': provider.id, 'business_name': provider.business_name, 'fantasy_name': provider.fantasy_name, 'cnpj': provider.cnpj, 'phone': select_phone_by_user_id(user_id), 'address': provider.address, 'type_provider': provider.type_provider, } return {"succes": True, "message": 'Provider found'} except Exception as e: return {"succes": False, "message": f'{e} invalid payload'}
def update_provider(item, user): try: if item: provider = ProviderModel.get_by_user_id(user.id) if 'business_name' in item: provider.business_name = item['business_name'] if 'fantasy_name' in item: provider.fantasy_name = item['fantasy_name'] if 'type_provider' in item: provider.type_provider = item['type_provider'] if 'cnpj' in item: provider.cnpj = item['cnpj'] if 'tel' in item: provider.tel = item['tel'] if 'cel' in item: provider.cel = item['cel'] if 'address' in item: provider.address = item['address'] if 'status' in item: provider.status = item['status'] if 'password' in item: provider.password = item['password'] provider.save() return {"success": True, "message": 'Provider edited'} else: return { "success": False, "message": 'Not edited provider, invalid payload' } except Exception as e: return { "success": False, "message": f'{e} invalid payload', 'type_error': 'provider/utils' }