Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
285 lines (235 loc) · 8.38 KB
/
Copy pathserver.js
File metadata and controls
285 lines (235 loc) · 8.38 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import express from 'express'
import dotenv from 'dotenv'
// Load environment variables
dotenv.config()
const app = express()
const PORT = process.env.PORT || 3000
// Environment variables
const CRM_BASE_URL = process.env.CRM_BASE_URL
const CRM_USERNAME = process.env.CRM_USERNAME
const CRM_PASSWORD = process.env.CRM_PASSWORD
// Validate environment variables
if (!CRM_BASE_URL || !CRM_USERNAME || !CRM_PASSWORD) {
console.error('❌ ERROR: Missing required environment variables!')
console.error('Please ensure the following are set in your .env file:')
console.error(' - CRM_BASE_URL')
console.error(' - CRM_USERNAME')
console.error(' - CRM_PASSWORD')
process.exit(1)
}
// Create Basic Auth header
const authString = `${CRM_USERNAME}:${CRM_PASSWORD}`
const authHeader = 'Basic ' + Buffer.from(authString).toString('base64')
// Middleware
app.use(express.json())
// CORS middleware
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
// Handle preflight requests
if (req.method === 'OPTIONS') {
return res.status(200).end()
}
next()
})
// Startup logging
console.log('✅ CRM Webhook Service Configuration:')
console.log(`✅ CRM API configured: ${CRM_BASE_URL}`)
console.log(`✅ Username: ${CRM_USERNAME}`)
console.log('✅ Credentials loaded successfully')
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
/**
* Makes an authenticated API call to SAP CRM
*/
async function callCrmApi(endpoint, options = {}) {
const url = `${CRM_BASE_URL}${endpoint}`
const defaultOptions = {
headers: {
'Authorization': authHeader,
'Content-Type': 'application/json'
}
}
try {
const response = await fetch(url, {
...defaultOptions,
...options,
headers: {
...defaultOptions.headers,
...options.headers
}
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(`CRM API Error (${response.status}): ${errorText}`)
}
return response
} catch (error) {
console.error(`❌ CRM API call failed: ${error.message}`)
throw error
}
}
/**
* Validates webhook payload structure
*/
function validateWebhookPayload(body) {
if (!body) {
return { error: 'Request body is empty', field: 'body' }
}
// Support CloudEvents format (data wrapper) or direct format
const data = body.data || body
if (!data.currentImage) {
return { error: 'Missing currentImage object', field: 'currentImage' }
}
if (!data.currentImage.id) {
return { error: 'Missing account ID', field: 'currentImage.id' }
}
return null // Valid
}
/**
* Calculates CustomScore based on ABC classification
*/
function calculateScore(abcClassification) {
const classification = abcClassification?.toUpperCase()
switch (classification) {
case 'A':
return 90
case 'B':
return 70
case 'C':
return 50
default:
console.log(`⚠️ Unknown ABC classification: ${abcClassification}, defaulting to C (50)`)
return 50
}
}
// ============================================================================
// ENDPOINTS
// ============================================================================
/**
* Health check endpoint
*/
app.get('/health', (req, res) => {
res.json({
status: 'ok',
service: 'CRM Webhook Service',
timestamp: new Date().toISOString()
})
})
/**
* Synchronous webhook - returns calculated score immediately
*/
app.post('/webhooks/calculate-score-sync', (req, res) => {
try {
// Validate payload
const validationError = validateWebhookPayload(req.body)
if (validationError) {
console.error(`❌ Validation failed: ${validationError.error}`)
return res.status(400).json({ error: validationError.error })
}
// Support CloudEvents format (data wrapper) or direct format
const data = req.body.data || req.body
// Extract ABC classification
const abcClassification = data.currentImage.customerABCClassification
// Calculate score
const calculatedScore = calculateScore(abcClassification)
console.log(`✅ Calculated score: ${calculatedScore} (ABC: ${abcClassification})`)
console.log(`✅ Returning response with updated CustomScore...`)
// Create response data from currentImage and update only CustomScore
const responseData = {
...data.currentImage,
extensions: {
...data.currentImage.extensions,
CustomScore: calculatedScore
}
}
// Return response in CRM expected format
res.status(200).json({
data: responseData
})
console.log(`✅ Returned updated account data with CustomScore: ${calculatedScore}`)
} catch (error) {
console.error('❌ Sync webhook error:', error)
res.status(500).json({ error: 'Internal server error' })
}
})
/**
* Asynchronous webhook - accepts request and processes in background
*/
app.post('/webhooks/calculate-score-async', async (req, res) => {
try {
// Validate payload
const validationError = validateWebhookPayload(req.body)
if (validationError) {
console.error(`❌ Validation failed: ${validationError.error}`)
return res.status(400).json({ error: validationError.error })
}
// Immediately respond to acknowledge receipt
res.status(202).json({
accepted: true,
message: 'Processing in background'
})
// Support CloudEvents format (data wrapper) or direct format
const data = req.body.data || req.body
// Process asynchronously
const accountId = data.currentImage.id
const abcClassification = data.currentImage.customerABCClassification
console.log(`🔄 Starting async processing for account ${accountId}...`)
// Spawn background task
setImmediate(async () => {
try {
// Simulate processing delay
console.log(`⏳ Simulating 10-second processing delay...`)
await new Promise(resolve => setTimeout(resolve, 10000))
// Calculate score
const calculatedScore = calculateScore(abcClassification)
console.log(`✅ Async calculated score: ${calculatedScore} (ABC: ${abcClassification})`)
// Fetch current account to get fresh ETag
console.log(`📡 Fetching current account data for ${accountId}...`)
const getResponse = await callCrmApi(`/sap/c4c/api/v1/account-service/accounts/${accountId}`)
const etag = getResponse.headers.get('ETag')
if (!etag) {
throw new Error('No ETag received from CRM')
}
console.log(`✅ Received ETag: ${etag}`)
// Update account with calculated score
console.log(`📡 Updating account ${accountId} with score ${calculatedScore}...`)
await callCrmApi(
`/sap/c4c/api/v1/account-service/accounts/${accountId}`,
{
method: 'PATCH',
headers: {
'If-Match': etag,
'Content-Type': 'application/merge-patch+json'
},
body: JSON.stringify({
extensions: {
CustomScore: calculatedScore
}
})
}
)
console.log(`✅ Successfully updated account ${accountId} with CustomScore: ${calculatedScore}`)
} catch (error) {
console.error('❌ Async webhook processing failed:')
console.error(` Account ID: ${accountId}`)
console.error(` ABC Classification: ${abcClassification}`)
console.error(` Error: ${error.message}`)
}
})
} catch (error) {
console.error('❌ Async webhook error:', error)
// Note: Response already sent, so we can't respond with error
}
})
// ============================================================================
// START SERVER
// ============================================================================
app.listen(PORT, () => {
console.log(`\n🚀 CRM Webhook Service running on port ${PORT}`)
console.log(`📍 Health check: http://localhost:${PORT}/health`)
console.log(`📍 Sync webhook: http://localhost:${PORT}/webhooks/calculate-score-sync`)
console.log(`📍 Async webhook: http://localhost:${PORT}/webhooks/calculate-score-async\n`)
})