|
@@ -40,6 +40,7 @@ import {
|
|
|
lookupOrder,
|
|
lookupOrder,
|
|
|
lookupOrdersByCustomer,
|
|
lookupOrdersByCustomer,
|
|
|
lookupOrdersByPO,
|
|
lookupOrdersByPO,
|
|
|
|
|
+ lookupTrackingByCustomer,
|
|
|
lookupCatalog,
|
|
lookupCatalog,
|
|
|
lookupStock,
|
|
lookupStock,
|
|
|
lookupContact,
|
|
lookupContact,
|
|
@@ -371,7 +372,16 @@ export const appRouter = router({
|
|
|
erpContext = "NOTE: This user has not been linked to an ERP dealer account. Their erpContactCid is not set. Let them know they should contact support to link their dealer account before order history can be retrieved.";
|
|
erpContext = "NOTE: This user has not been linked to an ERP dealer account. Their erpContactCid is not set. Let them know they should contact support to link their dealer account before order history can be retrieved.";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 3. PO number lookup — "PO-12345", "purchase order 5678"
|
|
|
|
|
|
|
+ // 3. Shipment tracking — "track my shipment", "tracking number", "where is my package"
|
|
|
|
|
+ } else if (/\b(track|tracking|shipment|shipped|where.*order|delivery status|carrier|ship.*status)\b/.test(msgLower)) {
|
|
|
|
|
+ const cid = ctx.user?.erpContactCid ?? (conversation as any).customerId as string | undefined;
|
|
|
|
|
+ if (cid) {
|
|
|
|
|
+ erpContext = await lookupTrackingByCustomer(cid, 5, userCtx);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ erpContext = "NOTE: This user has not been linked to an ERP dealer account. Their erpContactCid is not set. Ask for their order number to look up tracking.";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. PO number lookup — "PO-12345", "purchase order 5678"
|
|
|
} else {
|
|
} else {
|
|
|
const poMatch = msg.match(/\bPO[-\s]?\d{3,}\b/i);
|
|
const poMatch = msg.match(/\bPO[-\s]?\d{3,}\b/i);
|
|
|
if (poMatch) {
|
|
if (poMatch) {
|
|
@@ -379,7 +389,7 @@ export const appRouter = router({
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 4. Stock / inventory
|
|
|
|
|
|
|
+ // 5. Stock / inventory
|
|
|
if (!erpContext && /\b(in stock|available|inventory|stock|availability)\b/.test(msgLower)) {
|
|
if (!erpContext && /\b(in stock|available|inventory|stock|availability)\b/.test(msgLower)) {
|
|
|
// Try to extract a model number: capital letters + digits, e.g. "B1234-1"
|
|
// Try to extract a model number: capital letters + digits, e.g. "B1234-1"
|
|
|
const modelMatch = msg.match(/\b([A-Z]{1,4}[-\s]?\d{3,}[-\w]*)\b/);
|
|
const modelMatch = msg.match(/\b([A-Z]{1,4}[-\s]?\d{3,}[-\w]*)\b/);
|
|
@@ -388,7 +398,7 @@ export const appRouter = router({
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 5. Product / catalog search
|
|
|
|
|
|
|
+ // 6. Product / catalog search
|
|
|
if (!erpContext && /\b(product|catalog|collection|furniture|model|item|sofa|bed|table|chair|dresser|cabinet)\b/.test(msgLower)) {
|
|
if (!erpContext && /\b(product|catalog|collection|furniture|model|item|sofa|bed|table|chair|dresser|cabinet)\b/.test(msgLower)) {
|
|
|
// Pull keywords: skip common stop words
|
|
// Pull keywords: skip common stop words
|
|
|
const stopWords = new Set(["the","a","an","is","are","do","you","have","i","can","tell","me","about","show","what","which"]);
|
|
const stopWords = new Set(["the","a","an","is","are","do","you","have","i","can","tell","me","about","show","what","which"]);
|
|
@@ -403,7 +413,7 @@ export const appRouter = router({
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 6. Customer / dealer lookup (admin/agent only — dealers see their own record via CID)
|
|
|
|
|
|
|
+ // 7. Customer / dealer lookup (admin/agent only — dealers see their own record via CID)
|
|
|
if (!erpContext && ctx.user?.role !== "user" && /\b(customer|dealer|account|contact|company)\b/.test(msgLower)) {
|
|
if (!erpContext && ctx.user?.role !== "user" && /\b(customer|dealer|account|contact|company)\b/.test(msgLower)) {
|
|
|
const nameMatch = msg.match(/(?:customer|dealer|account|contact|company)[:\s]+([A-Za-z &'.-]{3,40})/i);
|
|
const nameMatch = msg.match(/(?:customer|dealer|account|contact|company)[:\s]+([A-Za-z &'.-]{3,40})/i);
|
|
|
if (nameMatch) {
|
|
if (nameMatch) {
|