bulltrackers-module 1.0.604 → 1.0.605

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1835,13 +1835,81 @@ const getSyncStatus = async (db, targetId) => {
1835
1835
 
1836
1836
  if (snap.empty) return { status: 'never_requested' };
1837
1837
 
1838
- const data = snap.docs[0].data();
1839
- return {
1840
- requestId: data.requestId,
1841
- status: data.status, // queued, dispatched, processing, completed, failed
1842
- updatedAt: data.updatedAt,
1838
+ const doc = snap.docs[0];
1839
+ const data = doc.data();
1840
+ const requestId = data.requestId;
1841
+ const currentStatus = data.status;
1842
+
1843
+ // Server-side timeout: Check if request is stale (older than 2 minutes and still processing)
1844
+ const STALE_THRESHOLD_MS = 2 * 60 * 1000; // 2 minutes
1845
+ const processingStates = ['queued', 'dispatched', 'processing', 'indexing', 'computing'];
1846
+ const isProcessingState = processingStates.includes(currentStatus);
1847
+
1848
+ if (isProcessingState) {
1849
+ // Check age of request
1850
+ const createdAt = data.createdAt;
1851
+ const updatedAt = data.updatedAt;
1852
+
1853
+ // Use updatedAt if available, otherwise createdAt
1854
+ const checkTime = updatedAt || createdAt;
1855
+ if (checkTime) {
1856
+ const checkTimestamp = checkTime.toMillis ? checkTime.toMillis() : (checkTime.getTime ? checkTime.getTime() : new Date(checkTime).getTime());
1857
+ const age = Date.now() - checkTimestamp;
1858
+
1859
+ if (age > STALE_THRESHOLD_MS) {
1860
+ // Mark as failed due to timeout
1861
+ const errorMessage = `Sync request timed out after ${Math.round(age / 1000 / 60)} minutes. The task may have failed.`;
1862
+ await doc.ref.update({
1863
+ status: 'failed',
1864
+ error: errorMessage,
1865
+ failedAt: new Date(),
1866
+ updatedAt: new Date()
1867
+ });
1868
+
1869
+ return {
1870
+ requestId: requestId,
1871
+ status: 'failed',
1872
+ error: errorMessage,
1873
+ updatedAt: new Date(),
1874
+ type: data.userType,
1875
+ createdAt: createdAt,
1876
+ failedAt: new Date()
1877
+ };
1878
+ }
1879
+ }
1880
+ }
1881
+
1882
+ // Return current status with timestamps
1883
+ const result = {
1884
+ requestId: requestId,
1885
+ status: currentStatus,
1843
1886
  type: data.userType
1844
1887
  };
1888
+
1889
+ // Add timestamps if available
1890
+ if (data.createdAt) {
1891
+ result.createdAt = data.createdAt.toDate ? data.createdAt.toDate().toISOString() : (data.createdAt.toISOString ? data.createdAt.toISOString() : data.createdAt);
1892
+ }
1893
+ if (data.updatedAt) {
1894
+ result.updatedAt = data.updatedAt.toDate ? data.updatedAt.toDate().toISOString() : (data.updatedAt.toISOString ? data.updatedAt.toISOString() : data.updatedAt);
1895
+ }
1896
+ if (data.dispatchedAt) {
1897
+ result.dispatchedAt = data.dispatchedAt.toDate ? data.dispatchedAt.toDate().toISOString() : (data.dispatchedAt.toISOString ? data.dispatchedAt.toISOString() : data.dispatchedAt);
1898
+ }
1899
+ if (data.completedAt) {
1900
+ result.completedAt = data.completedAt.toDate ? data.completedAt.toDate().toISOString() : (data.completedAt.toISOString ? data.completedAt.toISOString() : data.completedAt);
1901
+ }
1902
+ if (data.failedAt) {
1903
+ result.failedAt = data.failedAt.toDate ? data.failedAt.toDate().toISOString() : (data.failedAt.toISOString ? data.failedAt.toISOString() : data.failedAt);
1904
+ }
1905
+ if (data.error) {
1906
+ result.error = data.error;
1907
+ }
1908
+
1909
+ // Add canRequest flag (true if not in processing state or failed)
1910
+ result.canRequest = !isProcessingState && currentStatus !== 'failed';
1911
+
1912
+ return result;
1845
1913
  };
1846
1914
 
1847
1915
  // ==========================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.604",
3
+ "version": "1.0.605",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [