1+ interface ProductInfo {
2+ name : string
3+ sku : string
4+ availability : number
5+ price : number
6+ }
7+
8+ interface WarrantyInfo {
9+ status : 'active' | 'expired' | 'not_found'
10+ expiryDate ?: string
11+ purchaseDate ?: string
12+ productName ?: string
13+ serialNumber ?: string
14+ }
15+
16+ /**
17+ * Parses assistant response to extract product information
18+ */
19+ export const extractProductInfo = ( content : string ) : ProductInfo | null => {
20+ const productRegex = / (?: p r o d u c t | i t e m | s k u ) \s * [: ] \s * ( [ ^ \n ] + ) / i
21+ const availabilityRegex = / (?: a v a i l a b i l i t y | i n s t o c k | a v a i l a b l e ) \s * [: ] \s * ( \d + ) | ( \d + ) \s * (?: a v a i l a b l e | i n s t o c k ) / i
22+ const priceRegex = / (?: p r i c e | c o s t ) \s * [: ] \s * \$ ? ( \d + (?: \. \d { 2 } ) ? ) / i
23+ const skuRegex = / (?: s k u | p a r t n u m b e r ) \s * [: ] \s * ( [ A - Z 0 - 9 - ] + ) / i
24+
25+ const productMatch = content . match ( productRegex )
26+ const availabilityMatch = content . match ( availabilityRegex )
27+ const priceMatch = content . match ( priceRegex )
28+ const skuMatch = content . match ( skuRegex )
29+
30+ if ( ! productMatch && ! skuMatch ) return null
31+
32+ return {
33+ name : productMatch ?. [ 1 ] ?. trim ( ) || 'Unknown Product' ,
34+ sku : skuMatch ?. [ 1 ] ?. trim ( ) || 'N/A' ,
35+ availability : parseInt ( availabilityMatch ?. [ 1 ] || availabilityMatch ?. [ 2 ] || '0' ) ,
36+ price : parseFloat ( priceMatch ?. [ 1 ] || '0' )
37+ }
38+ }
39+
40+ /**
41+ * Parses assistant response to extract warranty information
42+ */
43+ export const extractWarrantyInfo = ( content : string ) : WarrantyInfo | null => {
44+ const warrantyRegex = / w a r r a n t y \s * (?: s t a t u s ) ? \s * [: ] \s * ( a c t i v e | e x p i r e d | v a l i d | i n v a l i d | n o t f o u n d ) / i
45+ const expiryRegex = / (?: e x p i r e s ? | e x p i r y ) \s * (?: d a t e ) ? \s * [: ] \s * ( [ ^ \n ] + ) / i
46+ const purchaseRegex = / (?: p u r c h a s e | b o u g h t ) \s * (?: d a t e ) ? \s * [: ] \s * ( [ ^ \n ] + ) / i
47+ const serialRegex = / (?: s e r i a l | s e r i a l n u m b e r ) \s * [: ] \s * ( [ A - Z 0 - 9 - ] + ) / i
48+ const productRegex = / (?: p r o d u c t | m o d e l ) \s * [: ] \s * ( [ ^ \n ] + ) / i
49+
50+ const warrantyMatch = content . match ( warrantyRegex )
51+ const expiryMatch = content . match ( expiryRegex )
52+ const purchaseMatch = content . match ( purchaseRegex )
53+ const serialMatch = content . match ( serialRegex )
54+ const productMatch = content . match ( productRegex )
55+
56+ if ( ! warrantyMatch ) return null
57+
58+ let status : 'active' | 'expired' | 'not_found' = 'not_found'
59+ if ( warrantyMatch [ 1 ] . toLowerCase ( ) . includes ( 'active' ) || warrantyMatch [ 1 ] . toLowerCase ( ) . includes ( 'valid' ) ) {
60+ status = 'active'
61+ } else if ( warrantyMatch [ 1 ] . toLowerCase ( ) . includes ( 'expired' ) || warrantyMatch [ 1 ] . toLowerCase ( ) . includes ( 'invalid' ) ) {
62+ status = 'expired'
63+ }
64+
65+ return {
66+ status,
67+ expiryDate : expiryMatch ?. [ 1 ] ?. trim ( ) ,
68+ purchaseDate : purchaseMatch ?. [ 1 ] ?. trim ( ) ,
69+ productName : productMatch ?. [ 1 ] ?. trim ( ) ,
70+ serialNumber : serialMatch ?. [ 1 ] ?. trim ( )
71+ }
72+ }
73+
74+ /**
75+ * Determines what type of flow should be shown based on the content
76+ */
77+ export const getFlowType = ( content : string ) : 'product' | 'warranty' | null => {
78+ const productKeywords = [ 'availability' , 'in stock' , 'backorder' , 'reserve' , 'inventory' ]
79+ const warrantyKeywords = [ 'warranty' , 'rma' , 'return' , 'defective' , 'repair' ]
80+
81+ const lowerContent = content . toLowerCase ( )
82+
83+ const hasProductKeywords = productKeywords . some ( keyword => lowerContent . includes ( keyword ) )
84+ const hasWarrantyKeywords = warrantyKeywords . some ( keyword => lowerContent . includes ( keyword ) )
85+
86+ if ( hasWarrantyKeywords ) return 'warranty'
87+ if ( hasProductKeywords ) return 'product'
88+
89+ return null
90+ }
91+
92+ /**
93+ * Mock function to simulate backorder creation
94+ */
95+ export const createBackorder = async ( data : any ) : Promise < void > => {
96+ // Simulate API call delay
97+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
98+
99+ // In a real implementation, this would call the backend API
100+ console . log ( 'Creating backorder:' , data )
101+ }
102+
103+ /**
104+ * Mock function to simulate product reservation
105+ */
106+ export const reserveProduct = async ( data : any ) : Promise < void > => {
107+ // Simulate API call delay
108+ await new Promise ( resolve => setTimeout ( resolve , 1500 ) )
109+
110+ // In a real implementation, this would call the backend API
111+ console . log ( 'Reserving product:' , data )
112+ }
113+
114+ /**
115+ * Mock function to simulate RMA creation
116+ */
117+ export const createRMA = async ( data : any ) : Promise < { rmaNumber : string } > => {
118+ // Simulate API call delay
119+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
120+
121+ // In a real implementation, this would call the backend API
122+ const rmaNumber = `RMA-${ Date . now ( ) } `
123+ console . log ( 'Creating RMA:' , data , 'RMA Number:' , rmaNumber )
124+
125+ return { rmaNumber }
126+ }
127+
128+ /**
129+ * Mock function to simulate return label generation
130+ */
131+ export const generateReturnLabel = async ( data : any ) : Promise < { trackingNumber : string } > => {
132+ // Simulate API call delay
133+ await new Promise ( resolve => setTimeout ( resolve , 1500 ) )
134+
135+ // In a real implementation, this would call the backend API
136+ const trackingNumber = `1Z${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 12 ) . toUpperCase ( ) } `
137+ console . log ( 'Generating return label:' , data , 'Tracking:' , trackingNumber )
138+
139+ return { trackingNumber }
140+ }
0 commit comments