Skip to content

Commit

Permalink
Added function delete all meters for customer
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua committed Dec 10, 2023
1 parent ce3b250 commit 403a2c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
8 changes: 8 additions & 0 deletions admin_cli/app/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def list_smart_meters_of_customer(self, customer_UID):
for meter in response.get('meters', []):
click.echo(f"Smartmeter: {meter['meter_UID']}")

def delete_smart_meters_of_customer(self, customer_UID):
data = {"api_key": self.api_key, "username": self.username, "customer_UID": customer_UID}
response = self.api_request("meter-delete", data, method='GET')
if "erfolgreich" in response:
click.echo(f"Die Smart Meter wurden erfolgreich gelöscht.")
else:
click.echo(f"Ein Fehler ist aufgetreten.")

def delete_customer_portal(self, customer_UID):
data = {"api_key": self.api_key, "username": self.username, "customer_UID": customer_UID}
response = self.api_request("customer-delete", data, method='DELETE')
Expand Down
4 changes: 3 additions & 1 deletion provider_portal/app/api/admin_api/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Response:
"error_delete_customer": ("Customer Portal konnte nicht gelöscht werden.", 404),
"success_list_customer_portals": ("Customer Portals wurden erfolgreich aufgelistet.", 200),
"success_list_smart_meters": ("Smart Meters wurden erfolgreich aufgelistet.", 200),
"success_delete_smart_meters": ("Smart Meters wurden erfolgreich geloescht.", 200),
"error_list_smart_meters": ("Smart Meters konnten nicht aufgelistet werden.", 500),
"error_list_customer_portals": ("Customer Portals konnten nicht aufgelistet werden.", 500)
"error_list_customer_portals": ("Customer Portals konnten nicht aufgelistet werden.", 500),
"error_delete_smart_meters": ("Smart Meter konnten nicht gelöscht werden.", 500)
}

def __init__(self, dict):
Expand Down
47 changes: 46 additions & 1 deletion provider_portal/app/api/admin_api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .response import Response
from . import admin_api_blueprint as bp
from app.utils.validation.string_validation import input_validation

from app.api.customer_api.customer_api import CustomerAPI

logger = logging.getLogger("main")

Expand Down Expand Up @@ -139,6 +139,51 @@ def list_customer_portals():
return Response(dict=res).create_response()


@bp.route('meter-delete', methods=['DELETE'])
def delete_smart_meters_for_customer():
"""
Route to list all smart meters of customer.
This route is used by administrators to get a list of all smart meters of customer.
Returns:
JSON response containing the list of smart meters.
"""
try:
data = json.loads(request.data)
api_key = data["api_key"]
username = data["username"]
customer_UID = data["customer_UID"]
if input_validation([api_key, username]):
# Initialize AdminAPI class
api = AdminAPI(api_key=api_key, username=username)
auth_status = api.authenticate_admin_user()
else:
raise ValueError("error_decoding")
except Exception as err:
logger.error(f"An error has occurred when deleting smart meters: {err}")
res = {"message": "error_authentication"}
return Response(dict=res).create_response()

if auth_status:
try:
meters = api.list_smart_meters_for_customer(customer_UID)
for meter in meters:
meter_uid = meter["meter_UID"]
api = CustomerAPI(customer_UID, None)
api.delete_meter(meter_uid)
res = {"message": "success_delete_smart_meters"}
return Response(dict=res).create_response()

except Exception as err:
logger.error(f"An error has occurred when listing smart meters: {err}")
res = {"message": "error_delete_smart_meters"}
return Response(dict=res).create_response()

res = {"message": "error_authentication"}
return Response(dict=res).create_response()


@bp.route('meter-list', methods=['GET'])
def list_smart_meters_for_customer():
"""
Expand Down

0 comments on commit 403a2c8

Please sign in to comment.