SushanthUV commited on
Commit
f0c7697
·
1 Parent(s): 4cf8989

FIxed get owner function to return correct owner email id

Browse files
backend/api/routes.py CHANGED
@@ -45,7 +45,10 @@ def analyze(request: AnalyzeRequest) -> dict:
45
 
46
  @router.post("/custom-task")
47
  def custom_task(request: CustomTaskRequest) -> dict:
48
- owner = db.get_owner(request.owner_id)
 
 
 
49
  try:
50
  return analyze_custom_task(
51
  business_description=owner.get("business_description", ""),
@@ -59,7 +62,10 @@ def custom_task(request: CustomTaskRequest) -> dict:
59
 
60
  @router.post("/suggest-workflows")
61
  def suggest_workflows(request: WorkflowSuggestionRequest) -> dict:
62
- owner = db.get_owner(request.owner_id)
 
 
 
63
  try:
64
  return suggest_workflow_options(
65
  task_name=request.task_name,
@@ -75,7 +81,10 @@ def suggest_workflows(request: WorkflowSuggestionRequest) -> dict:
75
 
76
  @router.post("/build-workflow", response_model=BuildWorkflowResponse)
77
  def build_workflow(request: BuildWorkflowRequest) -> BuildWorkflowResponse:
78
- owner = db.get_owner(request.owner_id)
 
 
 
79
  try:
80
  workflow_json = build_workflow_definition(request=request, owner=owner)
81
  compiled = compile_workflow(workflow_json)
@@ -87,9 +96,10 @@ def build_workflow(request: BuildWorkflowRequest) -> BuildWorkflowResponse:
87
 
88
  @router.post("/deploy", response_model=DeploymentResponse)
89
  def deploy_workflow(request: DeployRequest) -> DeploymentResponse:
90
- owner = db.get_owner(request.owner_id)
91
- if not owner:
92
- raise HTTPException(status_code=404, detail="owner not found")
 
93
  deployments = []
94
  for workflow in request.workflows:
95
  compiled = compile_workflow(workflow.model_dump())
@@ -119,7 +129,10 @@ def reset_data() -> dict[str, str]:
119
 
120
  @router.get("/status", response_model=OwnerStatusResponse)
121
  def status(owner_id: str) -> OwnerStatusResponse:
122
- owner = db.get_owner(owner_id)
 
 
 
123
  return OwnerStatusResponse(
124
  owner=owner,
125
  workflows=db.list_workflows(owner_id),
 
45
 
46
  @router.post("/custom-task")
47
  def custom_task(request: CustomTaskRequest) -> dict:
48
+ try:
49
+ owner = db.get_owner(request.owner_id)
50
+ except KeyError as exc:
51
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
52
  try:
53
  return analyze_custom_task(
54
  business_description=owner.get("business_description", ""),
 
62
 
63
  @router.post("/suggest-workflows")
64
  def suggest_workflows(request: WorkflowSuggestionRequest) -> dict:
65
+ try:
66
+ owner = db.get_owner(request.owner_id)
67
+ except KeyError as exc:
68
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
69
  try:
70
  return suggest_workflow_options(
71
  task_name=request.task_name,
 
81
 
82
  @router.post("/build-workflow", response_model=BuildWorkflowResponse)
83
  def build_workflow(request: BuildWorkflowRequest) -> BuildWorkflowResponse:
84
+ try:
85
+ owner = db.get_owner(request.owner_id)
86
+ except KeyError as exc:
87
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
88
  try:
89
  workflow_json = build_workflow_definition(request=request, owner=owner)
90
  compiled = compile_workflow(workflow_json)
 
96
 
97
  @router.post("/deploy", response_model=DeploymentResponse)
98
  def deploy_workflow(request: DeployRequest) -> DeploymentResponse:
99
+ try:
100
+ owner = db.get_owner(request.owner_id)
101
+ except KeyError as exc:
102
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
103
  deployments = []
104
  for workflow in request.workflows:
105
  compiled = compile_workflow(workflow.model_dump())
 
129
 
130
  @router.get("/status", response_model=OwnerStatusResponse)
131
  def status(owner_id: str) -> OwnerStatusResponse:
132
+ try:
133
+ owner = db.get_owner(owner_id)
134
+ except KeyError as exc:
135
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
136
  return OwnerStatusResponse(
137
  owner=owner,
138
  workflows=db.list_workflows(owner_id),
backend/storage/database.py CHANGED
@@ -21,6 +21,7 @@ class InMemoryDatabase:
21
  def ensure_owner(self, owner_id: str, email: str) -> dict:
22
  owner = self.owners.get(owner_id)
23
  if owner:
 
24
  return owner
25
  owner = {
26
  "id": owner_id,
@@ -49,7 +50,7 @@ class InMemoryDatabase:
49
 
50
  def get_owner(self, owner_id: str) -> dict:
51
  if owner_id not in self.owners:
52
- self.ensure_owner(owner_id, f"{owner_id}@example.com")
53
  return deepcopy(self.owners[owner_id])
54
 
55
  def save_workflow(self, owner_id: str, workflow: dict) -> dict:
 
21
  def ensure_owner(self, owner_id: str, email: str) -> dict:
22
  owner = self.owners.get(owner_id)
23
  if owner:
24
+ owner["email"] = email
25
  return owner
26
  owner = {
27
  "id": owner_id,
 
50
 
51
  def get_owner(self, owner_id: str) -> dict:
52
  if owner_id not in self.owners:
53
+ raise KeyError(f"owner {owner_id} not found")
54
  return deepcopy(self.owners[owner_id])
55
 
56
  def save_workflow(self, owner_id: str, workflow: dict) -> dict:
tests/test_flowpilot_api.py CHANGED
@@ -15,7 +15,34 @@ def test_in_memory_database_creates_timezone_aware_owner_timestamp():
15
  assert owner["created_at"].endswith("+00:00")
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def test_owner_flow_builds_and_deploys_workflows():
 
19
  analyze_response = client.post(
20
  "/api/analyze",
21
  json={
@@ -66,6 +93,7 @@ def test_owner_flow_builds_and_deploys_workflows():
66
 
67
 
68
  def test_upload_and_status_endpoints():
 
69
  client.post(
70
  "/api/analyze",
71
  json={"owner_id": "raj2", "owner_email": "raj2@orchard.com", "description": "Weekly pricing and orders."},
@@ -87,6 +115,7 @@ def test_upload_and_status_endpoints():
87
 
88
 
89
  def test_reset_endpoint_clears_all_in_memory_data():
 
90
  analyze_response = client.post(
91
  "/api/analyze",
92
  json={
 
15
  assert owner["created_at"].endswith("+00:00")
16
 
17
 
18
+ def test_status_returns_404_for_unknown_owner_without_creating_placeholder():
19
+ db.reset()
20
+
21
+ status_response = client.get("/api/status", params={"owner_id": "missing-owner"})
22
+
23
+ assert status_response.status_code == 404
24
+ assert "missing-owner" not in db.owners
25
+
26
+
27
+ def test_analyze_updates_existing_owner_email():
28
+ db.reset()
29
+ db.ensure_owner("sushanth", "sushanth@example.com")
30
+
31
+ analyze_response = client.post(
32
+ "/api/analyze",
33
+ json={
34
+ "owner_id": "sushanth",
35
+ "owner_email": "real@example.com",
36
+ "description": "Weekly pricing and orders.",
37
+ },
38
+ )
39
+
40
+ assert analyze_response.status_code == 200
41
+ assert db.owners["sushanth"]["email"] == "real@example.com"
42
+
43
+
44
  def test_owner_flow_builds_and_deploys_workflows():
45
+ db.reset()
46
  analyze_response = client.post(
47
  "/api/analyze",
48
  json={
 
93
 
94
 
95
  def test_upload_and_status_endpoints():
96
+ db.reset()
97
  client.post(
98
  "/api/analyze",
99
  json={"owner_id": "raj2", "owner_email": "raj2@orchard.com", "description": "Weekly pricing and orders."},
 
115
 
116
 
117
  def test_reset_endpoint_clears_all_in_memory_data():
118
+ db.reset()
119
  analyze_response = client.post(
120
  "/api/analyze",
121
  json={