Skip to main content
Version: 1.0.0

checkSkuHttp

Function Name: checkSKUHttp

Author: Domenico Cerone Creation Date: 25/09/2025
Last Reviewer: Domenico Cerone

Trigger: HTTPS (onRequest)

Purpose: Verifies the availability of a list of codes (SKU, EAN or UPC) through the Firestore database and Safilo API, returning the status of each product.

Detailed Functionality​

This Firebase Function performs the following operations in sequence:

1. Input Validation​

  • Accepts arrays of objects containing skuCode, eanCode and/or upcCode
  • Also supports the { skuList: [...] } format
  • Validates that at least one non-empty array is provided

2. Firestore Database Check​

  • For each element, searches in the Variants collection using:
    • skuModel for comparison with skuCode
    • eanCode for comparison with eanCode
    • upcCode for comparison with upcCode
  • Retrieves product status if found

3. Safilo API Check with Fallback Logic​

  • First call: Uses all available parameters together
  • Sequential fallback: If the first fails, tries:
    1. Only skuCode (if available)
    2. Only eanCode (if available)
    3. Only upcCode (if available)
  • Stops at the first positive result (status 200 with data)
  • Properly handles 404 errors (product not found) vs 500+ errors (API error)

4. Extended Search with API Data​

  • If the API returns data but Firestore finds no results
  • Performs a second search in Firestore using codes from the API
  • Searches with sku, eanCode and upc returned by the API

5. Status Determination (result)​

  • "Available": Product exists in Firestore with "Pubblicato" status
  • "In progress": Product exists in Firestore but not published
  • "Orderable": Product not in Firestore but available via Safilo API
  • "Error": Product not found in either Firestore or via API

6. API Status Determination​

  • "success": API returned status 200 with data
  • "Failed - Not Found": API returned 404 (product not found)
  • "Failed - API": Technical API error (status 500+, timeout, network errors)

7. Batch Management and Performance​

  • Batch size: 75 elements per batch
  • Processing: Sequential batches, parallel elements within batch
  • Delay: 100ms between API calls to avoid overload
  • Timeout: 30 seconds per single API call, 540 seconds (9 minutes) total
  • Memory: 1GiB allocated to handle large volumes

8. Progress Streaming​

  • Sends real-time progress updates
  • Includes progress (percentage), totalItems, processedItems
  • Uses HTTP chunked streaming for incremental updates
  • Frontend can display progress bars based on this data

9. Technical Configuration​

  • API Server: commportal-api.safilo.com/damAPI (updated API)
  • Region: europe-central2
  • CORS: Enabled for frontend calls
  • Axios: Configured with 30s timeout and keep-alive
  • Error Handling: Specific handling for ECONNRESET and network errors

10. Usage Examples​

  • Single verification: [{"skuCode": "123456"}]
  • Multiple verification: Array with mix of SKU, EAN, UPC
  • Large batches: Up to 250+ elements handled automatically
  • Progress monitoring: Streaming response for reactive UI

11. Response Logic​

Each processed element returns:

  • Original + enriched codes: Input data + any data from API
  • result: Final product status
  • isSelected: true only if result is "Available"
  • status: API call status

The function optimises performance for large data volumes whilst maintaining responsiveness through streaming and batch processing, ensuring robustness through multiple fallbacks and comprehensive error handling.

Input (Payload):​

For the HTTP function:

[
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046"
},
{
"skuCode": "1093728RU5219"
},
{
"eanCode": "0716736419046"
},
{
"upcCode": "xxxxx"
}
]

Or in the alternative format:

{
"skuList": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046"
}
]
}

Output (Success):​

{
"progress": 100,
"totalItems": 4,
"processedItems": 4,
"results": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046",
"result": "Available",
"isSelected": true,
"status": "success"
},
{
"skuCode": "1093728RU5219",
"eanCode": "",
"upcCode": "",
"result": "In progress",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "0716736419046",
"upcCode": "",
"result": "Orderable",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "",
"upcCode": "xxxxx",
"result": "Error",
"isSelected": false,
"status": "Failed - Not Found"
}
]
}

This output is returned in the HTTP response (Postman, browser, curl)

Testing​

URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp

Test with Emulator:

  1. Start the Firebase emulator: firebase emulators:start --only functions
  2. Ensure you're using Node.js version 20: nvm use 20
  3. Test with curl:
curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp" \
-H "Content-Type: application/json" \
-d '[{"skuCode": "3102687C55110", "eanCode": "0716736419046"}]'

Postman Testing:

Deploy Command:​

firebase deploy --only functions:checkSKUHttp

Production URL:​

Live Function: https://europe-central2-arshades-7e18a.cloudfunctions.net/checkSKUHttp