-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Delivery alerts backend Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * api-client updates Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Adding message field to model Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Frontend without action calls yet Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Working, except refreshing removes the alerts Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Refresh retrieves delivery alerts too, however waiting errors are always shown Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Filters through all delivery alerts for unresolved and up-to-date ones to display Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Lint Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Refactored if statement, remove unnecessary null check Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Using comparison between alert ids to filter more efficiently Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Allow cancellation if task_id is present, only allow override for missing carts Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Update cancel button and delivery alert action only after task has been properly cancelled Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Only update action based on task state update when the cancel button was clicked Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Remove stale console log Signed-off-by: Aaron Chong <aaronchongth@gmail.com> --------- Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
- Loading branch information
1 parent
828171e
commit 8add1a6
Showing
14 changed files
with
1,456 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/api-server/api_server/models/tortoise_models/delivery_alerts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from enum import Enum | ||
|
||
from tortoise.contrib.pydantic.creator import pydantic_model_creator | ||
from tortoise.fields import CharEnumField, CharField | ||
from tortoise.models import Model | ||
|
||
|
||
class DeliveryAlert(Model): | ||
""" | ||
Custom alerts for custom delivery tasks | ||
""" | ||
|
||
class Category(str, Enum): | ||
Missing = "missing" | ||
Wrong = "wrong" | ||
|
||
class Tier(str, Enum): | ||
Warning = "warning" | ||
Error = "error" | ||
|
||
class Action(str, Enum): | ||
Waiting = "waiting" | ||
Cancel = "cancelled" | ||
Override = "override" | ||
Resume = "resume" | ||
|
||
id = CharField(255, pk=True) | ||
category = CharEnumField(Category, index=True) | ||
tier = CharEnumField(Tier, index=True) | ||
task_id = CharField(255, index=True, null=True) | ||
action = CharEnumField(Action, index=True) | ||
message = CharField(255, null=True) | ||
|
||
|
||
DeliveryAlertPydantic = pydantic_model_creator(DeliveryAlert) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from fastapi import HTTPException | ||
from rx import operators as rxops | ||
|
||
from api_server.fast_io import FastIORouter, SubscriptionRequest | ||
from api_server.models import tortoise_models as ttm | ||
from api_server.rmf_io import delivery_alert_events | ||
|
||
router = FastIORouter(tags=["DeliveryAlerts"]) | ||
|
||
|
||
@router.sub("", response_model=ttm.DeliveryAlertPydantic) | ||
async def sub_delivery_alerts(_req: SubscriptionRequest): | ||
return delivery_alert_events.delivery_alerts.pipe( | ||
rxops.filter(lambda x: x is not None) | ||
) | ||
|
||
|
||
@router.get("", response_model=List[ttm.DeliveryAlertPydantic]) | ||
async def get_delivery_alerts(): | ||
delivery_alerts = await ttm.DeliveryAlert.all() | ||
return [ | ||
await ttm.DeliveryAlertPydantic.from_tortoise_orm(a) for a in delivery_alerts | ||
] | ||
|
||
|
||
@router.get("/{delivery_alert_id}", response_model=ttm.DeliveryAlertPydantic) | ||
async def get_delivery_alert(delivery_alert_id: str): | ||
delivery_alert = await ttm.DeliveryAlert.get_or_none(id=delivery_alert_id) | ||
if delivery_alert is None: | ||
raise HTTPException( | ||
404, f"Delivery alert with ID {delivery_alert_id} not found" | ||
) | ||
delivery_alert_pydantic = await ttm.DeliveryAlertPydantic.from_tortoise_orm( | ||
delivery_alert | ||
) | ||
return delivery_alert_pydantic | ||
|
||
|
||
@router.post("", status_code=201, response_model=ttm.DeliveryAlertPydantic) | ||
async def create_delivery_alert(category: str, tier: str, task_id: str, message: str): | ||
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | ||
delivery_alert_id = f"delivery-alert-{timestamp}" | ||
try: | ||
delivery_alert = await ttm.DeliveryAlert.create( | ||
id=delivery_alert_id, | ||
category=category, | ||
tier=tier, | ||
task_id=task_id, | ||
action="waiting", | ||
message=message, | ||
) | ||
except Exception as e: | ||
raise HTTPException(400, f"Could not create delivery alert: {e}") from e | ||
|
||
delivery_alert_pydantic = await ttm.DeliveryAlertPydantic.from_tortoise_orm( | ||
delivery_alert | ||
) | ||
delivery_alert_events.delivery_alerts.on_next(delivery_alert_pydantic) | ||
return delivery_alert_pydantic | ||
|
||
|
||
@router.post("/{delivery_alert_id}/action", response_model=ttm.DeliveryAlertPydantic) | ||
async def update_delivery_alert_action(delivery_alert_id: str, action: str): | ||
delivery_alert = await ttm.DeliveryAlert.get_or_none(id=delivery_alert_id) | ||
if delivery_alert is None: | ||
raise HTTPException( | ||
404, f"Delivery alert with ID {delivery_alert_id} not found" | ||
) | ||
|
||
try: | ||
delivery_alert.update_from_dict({"action": action}) | ||
except Exception as e: | ||
raise HTTPException( | ||
404, | ||
f"Failed to update delivery alert {delivery_alert_id} with action {action}: {e}", | ||
) from e | ||
await delivery_alert.save() | ||
|
||
delivery_alert_pydantic = await ttm.DeliveryAlertPydantic.from_tortoise_orm( | ||
delivery_alert | ||
) | ||
return delivery_alert_pydantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.