File size: 8,650 Bytes
7781557
 
 
 
 
 
3bb94b1
7781557
 
 
 
 
 
3bb94b1
 
 
 
7781557
 
 
 
3bb94b1
7781557
3bb94b1
 
7781557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bb94b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7781557
3bb94b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7781557
 
 
 
 
 
 
 
 
 
3bb94b1
 
7781557
 
 
 
 
 
 
 
 
3bb94b1
 
 
 
 
 
 
 
 
 
 
 
 
7781557
 
 
 
 
 
 
3bb94b1
7781557
 
3bb94b1
7781557
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os
import io
import json
import datetime
import pandas as pd
from datetime import timedelta
from datetime import datetime as dt
from fastapi import FastAPI,WebSocket, Depends, HTTPException, status, UploadFile, File
from fastapi.responses import FileResponse, JSONResponse
from fastapi.requests import Request
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordRequestForm
from pydantic import ValidationError, BaseModel
from bson import ObjectId


from .auth import (
    get_current_user,
    create_access_token,
    verify_password,
    get_password_hash
)
from .db.models import User, Token, Opportunity
from .db.database import get_user_by_username, create_user, save_file, create_opportunity, get_opportunities, get_opportunity_count
from .websocket import handle_websocket
from .llm_models import invoke_general_model, invoke_customer_search

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Get the directory of the current file
current_dir = os.path.dirname(os.path.realpath(__file__))

# Construct the path to the frontend dist directory
frontend_dir = os.path.join(current_dir, "..", "..", "frontend", "dist")

@app.get("/")
async def serve_react_root() -> FileResponse:
    return FileResponse(os.path.join(frontend_dir, "index.html"))

@app.post("/register", response_model=Token)
async def register(user: User) -> Token:
    if await get_user_by_username(user.username):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Username already registered"
        )
    
    hashed_password = get_password_hash(user.password)
    user.password = hashed_password
    
    if not await create_user(user):
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Could not create user"
        )
    
    access_token = create_access_token(
        data={"sub": user.username},
        expires_delta=timedelta(minutes=30)
    )
    return Token(access_token=access_token, token_type="bearer")

@app.post("/token", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()) -> Token:
    user = await get_user_by_username(form_data.username)
    if not user or not verify_password(form_data.password, user.password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    access_token = create_access_token(
        data={"sub": user.username},
        expires_delta=timedelta(minutes=30)
    )
    return Token(access_token=access_token, token_type="bearer")

@app.post("/upload")
async def upload_file(
    file: UploadFile = File(...),
    current_user: User = Depends(get_current_user)
) -> dict:
    contents = await file.read()
       # Convert to pandas DataFrame
    df = pd.read_csv(io.StringIO(contents.decode('utf-8')))
        
    # Convert DataFrame to list of dictionaries
    records = json.loads(df.to_json(orient='records'))
        
    if not await save_file(current_user.username, records, file.filename):
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Could not save file"
        )
    
    return {"message": "File uploaded successfully"}

@app.post("/api/save_opportunity")
async def save_opportunity(opportunity_data: dict, current_user: User = Depends(get_current_user)) -> dict:
    try:
        opportunity_data= {
            **opportunity_data,
            "username":current_user.username,
            "created_at":datetime.datetime.now(datetime.UTC),
            "updated_at":datetime.datetime.now(datetime.UTC)
        }
        print("data********", opportunity_data)
        opportunity = Opportunity(**opportunity_data)
        if not await create_opportunity(opportunity):
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail="Could not save opportunity"
            )
        return {"message": "Opportunity saved successfully"}
    except ValidationError as e:
        print(f"Validation error: {e}")
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Invalid opportunity data"
        )

@app.get("/api/opportunities")
async def retrieve_opportunities(
    request: Request,
    page: int = 1,
    limit: int = 100,
    current_user: User = Depends(get_current_user)
) -> JSONResponse:
    """
    Retrieve paginated opportunities for the current user
    """
    class JSONEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, dt):
                return obj.isoformat()
            if isinstance(obj, ObjectId):
                return str(obj)
            return super().default(obj)
        
    try:
        skip = (page - 1) * limit
        
        # Get paginated records
        records = await get_opportunities(
            username=current_user.username,
            skip=skip,
            limit=limit
        )
        
        # Process records with proper serialization
        all_records = []
        for record in records:
            # Convert MongoDB document to dict and handle ObjectId
            record_dict = record.dict(by_alias=True)
            if "_id" in record_dict:
                record_dict["_id"] = str(record_dict["_id"])
            
            # Process content if it exists
            if hasattr(record, 'content') and isinstance(record.content, (list, tuple)):
                all_records.extend(record.content)
            else:
                all_records.append(record_dict)
        
        # Count total records
        total_count = await get_opportunity_count(current_user.username)
        
        # Create response using Pydantic model
        response_data = PaginatedResponse(
            page=page,
            limit=limit,
            total_records=total_count,
            total_pages=-(-total_count // limit),
            has_more=(skip + limit) < total_count,
            records=all_records
        )
        
        # Convert to JSON with custom encoder
        return JSONResponse(
            content=json.loads(
                json.dumps(
                    {
                        "success": True,
                        "data": response_data.model_dump()
                    },
                    cls=JSONEncoder
                )
            ),
            status_code=200
        )
            
    except Exception as e:
        print(f"Error retrieving opportunities: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"An error occurred while retrieving opportunities: {str(e)}"
        )


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> None:
    await handle_websocket(websocket)
    
@app.post("/api/message")
async def message(obj: dict, current_user: User = Depends(get_current_user)) -> JSONResponse:
    """Endpoint to handle general incoming messages from the frontend."""
    answer = invoke_general_model(obj["message"])
    print("answer**********", answer)
    return JSONResponse(content={"message": json.loads(answer.model_dump_json() )})

@app.post("/api/customer_insights")
async def customer_insights(obj: dict) -> JSONResponse:
    """Endpoint to launch a customer insight search."""
    answer = invoke_customer_search(obj["message"])
    return JSONResponse(content={"AIMessage": answer.model_dump_json()})
    
app.mount("/assets", StaticFiles(directory=os.path.join(frontend_dir, "assets")), name="static")

class PaginatedResponse(BaseModel):
    page: int
    limit: int
    total_records: int
    total_pages: int
    has_more: bool
    records: list 

    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat(),
            ObjectId: lambda v: str(v)
        }
if __name__ == "__main__":
    from fastapi.testclient import TestClient

    client = TestClient(app)

    def test_message_endpoint():
        # Test that the message endpoint returns answers to questions.
        # Update test as this api requires a token for authorization to use
        response = client.post("/api/message", json={"message": "What is MEDDPICC?"})
        print(response.json())
        assert response["status_code"] == 200
        assert "AIMessage" in response.json()

    test_message_endpoint()