Spaces:
Running
Running
Anousone Worlakoumman commited on
Commit ·
f398a45
1
Parent(s): c854a4a
query order status
Browse files- cmd/exec.go +1 -0
- internal/chatbot/chatbot.go +74 -17
cmd/exec.go
CHANGED
|
@@ -157,6 +157,7 @@ func Execute(app *pocketbase.PocketBase) (*pocketbase.PocketBase, error) {
|
|
| 157 |
if redisAddr == "" {
|
| 158 |
redisAddr = "localhost:6379"
|
| 159 |
}
|
|
|
|
| 160 |
redisPassword := os.Getenv("REDIS_PASSWORD")
|
| 161 |
redisClient := redis.NewClient(&redis.Options{
|
| 162 |
Addr: redisAddr,
|
|
|
|
| 157 |
if redisAddr == "" {
|
| 158 |
redisAddr = "localhost:6379"
|
| 159 |
}
|
| 160 |
+
|
| 161 |
redisPassword := os.Getenv("REDIS_PASSWORD")
|
| 162 |
redisClient := redis.NewClient(&redis.Options{
|
| 163 |
Addr: redisAddr,
|
internal/chatbot/chatbot.go
CHANGED
|
@@ -123,8 +123,10 @@ func (s *ChatbotService) detectIntent(message string) (string, map[string]string
|
|
| 123 |
params := make(map[string]string)
|
| 124 |
|
| 125 |
// Order status patterns
|
| 126 |
-
if strings.Contains(message, "order") &&
|
| 127 |
-
(strings.Contains(message, "status") || strings.Contains(message, "where") || strings.Contains(message, "track"))
|
|
|
|
|
|
|
| 128 |
// Extract order ID if mentioned
|
| 129 |
if orderID := extractOrderID(message); orderID != "" {
|
| 130 |
params["orderID"] = orderID
|
|
@@ -157,38 +159,93 @@ func (s *ChatbotService) detectIntent(message string) (string, map[string]string
|
|
| 157 |
|
| 158 |
// Handler: Order Status
|
| 159 |
func (s *ChatbotService) handleOrderStatus(ctx context.Context, userID string, params map[string]string) (string, error) {
|
| 160 |
-
// Build query with
|
| 161 |
-
query := s.app.
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
// If specific order ID provided
|
| 165 |
if orderID, ok := params["orderID"]; ok {
|
| 166 |
-
query = query.AndWhere(dbx.
|
| 167 |
} else {
|
| 168 |
// Get recent orders
|
| 169 |
-
query = query.OrderBy("created DESC").Limit(5)
|
| 170 |
}
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
}
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
return "I couldn't find any orders for your account. Please check your order ID or contact support.", nil
|
| 179 |
}
|
| 180 |
|
| 181 |
// Format response
|
| 182 |
var response strings.Builder
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
for _, order := range records {
|
| 186 |
response.WriteString(fmt.Sprintf(
|
| 187 |
-
"Order #%s:\n- Status: %s\n-
|
| 188 |
order.Id,
|
| 189 |
-
order.
|
| 190 |
-
order.
|
| 191 |
-
order.
|
|
|
|
|
|
|
|
|
|
| 192 |
))
|
| 193 |
}
|
| 194 |
|
|
|
|
| 123 |
params := make(map[string]string)
|
| 124 |
|
| 125 |
// Order status patterns
|
| 126 |
+
if (strings.Contains(message, "order") &&
|
| 127 |
+
(strings.Contains(message, "status") || strings.Contains(message, "where") || strings.Contains(message, "track"))) ||
|
| 128 |
+
(strings.Contains(message, "current") && strings.Contains(message, "order")) ||
|
| 129 |
+
strings.Contains(message, "my order") {
|
| 130 |
// Extract order ID if mentioned
|
| 131 |
if orderID := extractOrderID(message); orderID != "" {
|
| 132 |
params["orderID"] = orderID
|
|
|
|
| 159 |
|
| 160 |
// Handler: Order Status
|
| 161 |
func (s *ChatbotService) handleOrderStatus(ctx context.Context, userID string, params map[string]string) (string, error) {
|
| 162 |
+
// Build query with customer_id scope using direct SQL
|
| 163 |
+
query := s.app.DB().
|
| 164 |
+
Select(
|
| 165 |
+
"o.id AS id",
|
| 166 |
+
"o.reference_id",
|
| 167 |
+
"o.customer_id",
|
| 168 |
+
"o.customer_name",
|
| 169 |
+
"o.phone_number",
|
| 170 |
+
"o.address",
|
| 171 |
+
"o.status",
|
| 172 |
+
"o.created",
|
| 173 |
+
"o.updated",
|
| 174 |
+
"COALESCE(SUM(item.quantity), 0) AS quantity",
|
| 175 |
+
"COALESCE(SUM(item.quantity * item.price_lak), 0) AS price_lak",
|
| 176 |
+
"COALESCE(SUM(item.quantity * item.price_thb), 0) AS price_thb",
|
| 177 |
+
"COALESCE(SUM(item.quantity * item.price_usd), 0) AS price_usd",
|
| 178 |
+
).
|
| 179 |
+
From("orders o").
|
| 180 |
+
LeftJoin("order_items item", dbx.NewExp("item.order_id = o.id")).
|
| 181 |
+
Where(dbx.NewExp("o.customer_id = {:custId}", dbx.Params{"custId": userID})).
|
| 182 |
+
GroupBy("o.id", "o.reference_id", "o.customer_id", "o.customer_name", "o.phone_number", "o.address", "o.status", "o.created", "o.updated")
|
| 183 |
|
| 184 |
// If specific order ID provided
|
| 185 |
if orderID, ok := params["orderID"]; ok {
|
| 186 |
+
query = query.AndWhere(dbx.NewExp("o.id = {:orderID}", dbx.Params{"orderID": orderID}))
|
| 187 |
} else {
|
| 188 |
// Get recent orders
|
| 189 |
+
query = query.OrderBy("o.created DESC").Limit(5)
|
| 190 |
}
|
| 191 |
|
| 192 |
+
type OrderResult struct {
|
| 193 |
+
Id string `db:"id"`
|
| 194 |
+
ReferenceID string `db:"reference_id"`
|
| 195 |
+
CustomerID string `db:"customer_id"`
|
| 196 |
+
CustomerName string `db:"customer_name"`
|
| 197 |
+
PhoneNumber string `db:"phone_number"`
|
| 198 |
+
Address string `db:"address"`
|
| 199 |
+
Status string `db:"status"`
|
| 200 |
+
Created string `db:"created"`
|
| 201 |
+
Updated string `db:"updated"`
|
| 202 |
+
Quantity int `db:"quantity"`
|
| 203 |
+
PriceLAK float64 `db:"price_lak"`
|
| 204 |
+
PriceTHB float64 `db:"price_thb"`
|
| 205 |
+
PriceUSD float64 `db:"price_usd"`
|
| 206 |
}
|
| 207 |
|
| 208 |
+
var orders []OrderResult
|
| 209 |
+
if err := query.All(&orders); err != nil {
|
| 210 |
+
return "", fmt.Errorf("failed to query orders: %w", err)
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
if len(orders) == 0 {
|
| 214 |
return "I couldn't find any orders for your account. Please check your order ID or contact support.", nil
|
| 215 |
}
|
| 216 |
|
| 217 |
// Format response
|
| 218 |
var response strings.Builder
|
| 219 |
+
if len(orders) == 1 && params["orderID"] != "" {
|
| 220 |
+
response.WriteString("Here's your order information:\n\n")
|
| 221 |
+
} else {
|
| 222 |
+
response.WriteString("Here are your recent orders:\n\n")
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
for _, order := range orders {
|
| 226 |
+
// Determine the primary currency and amount to display
|
| 227 |
+
var amount float64
|
| 228 |
+
var currency string
|
| 229 |
+
if order.PriceUSD > 0 {
|
| 230 |
+
amount = order.PriceUSD
|
| 231 |
+
currency = "USD"
|
| 232 |
+
} else if order.PriceTHB > 0 {
|
| 233 |
+
amount = order.PriceTHB
|
| 234 |
+
currency = "THB"
|
| 235 |
+
} else {
|
| 236 |
+
amount = order.PriceLAK
|
| 237 |
+
currency = "LAK"
|
| 238 |
+
}
|
| 239 |
|
|
|
|
| 240 |
response.WriteString(fmt.Sprintf(
|
| 241 |
+
"Order #%s (Ref: %s):\n- Status: %s\n- Items: %d\n- Total: %.2f %s\n- Date: %s\n\n",
|
| 242 |
order.Id,
|
| 243 |
+
order.ReferenceID,
|
| 244 |
+
order.Status,
|
| 245 |
+
order.Quantity,
|
| 246 |
+
amount,
|
| 247 |
+
currency,
|
| 248 |
+
order.Created,
|
| 249 |
))
|
| 250 |
}
|
| 251 |
|