@patch-adams/core 1.4.0 → 1.4.2

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.
package/dist/index.cjs CHANGED
@@ -1573,24 +1573,12 @@ function generateLrsBridgeCode(options) {
1573
1573
  // Store for document API data
1574
1574
  var documentApiData = null;
1575
1575
  var documentApiFetched = false;
1576
+ var sharedLinkApiData = null;
1576
1577
 
1577
1578
  /**
1578
- * Fetch document metadata from Bravais API to get GUIDs
1579
- * Endpoint: /api/v3/documents/{documentId}
1579
+ * Detect Bravais core URL from current page context
1580
1580
  */
1581
- function fetchDocumentMetadata(documentId, callback) {
1582
- if (!documentId) {
1583
- callback(null);
1584
- return;
1585
- }
1586
-
1587
- if (documentApiFetched) {
1588
- callback(documentApiData);
1589
- return;
1590
- }
1591
-
1592
- // Detect Bravais core URL
1593
- var coreUrl = null;
1581
+ function detectCoreUrl() {
1594
1582
  var urlsToCheck = [window.location.href, document.referrer];
1595
1583
 
1596
1584
  try {
@@ -1606,13 +1594,91 @@ function generateLrsBridgeCode(options) {
1606
1594
  } catch (e) {}
1607
1595
 
1608
1596
  for (var j = 0; j < urlsToCheck.length; j++) {
1609
- var match = urlsToCheck[j].match(/(https?:\\/\\/core-[a-zA-Z0-9_-]+\\.bravais\\.com)/);
1597
+ var match = urlsToCheck[j].match(/(https?://core-[a-zA-Z0-9_-]+.bravais.com)/);
1610
1598
  if (match) {
1611
- coreUrl = match[1];
1612
- break;
1599
+ return match[1];
1613
1600
  }
1614
1601
  }
1602
+ return null;
1603
+ }
1604
+
1605
+ /**
1606
+ * Fetch shared link data from Bravais API to get the real document ID
1607
+ * Endpoint: /api/v3/sharedLinks/token/{token}
1608
+ * Returns: { documentId, documentName, format, ... }
1609
+ */
1610
+ function fetchSharedLinkData(token, callback) {
1611
+ if (!token) {
1612
+ callback(null);
1613
+ return;
1614
+ }
1615
1615
 
1616
+ var coreUrl = detectCoreUrl();
1617
+ if (!coreUrl) {
1618
+ log('No Bravais core URL detected for shared link fetch');
1619
+ callback(null);
1620
+ return;
1621
+ }
1622
+
1623
+ var apiUrl = coreUrl + '/api/v3/sharedLinks/token/' + token;
1624
+ log('Fetching shared link data from:', apiUrl);
1625
+
1626
+ var xhr = new XMLHttpRequest();
1627
+ xhr.open('GET', apiUrl, true);
1628
+ xhr.withCredentials = true;
1629
+ xhr.setRequestHeader('Accept', 'application/json');
1630
+
1631
+ xhr.onreadystatechange = function() {
1632
+ if (xhr.readyState === 4) {
1633
+ log('Shared link API response status:', xhr.status);
1634
+ if (xhr.status >= 200 && xhr.status < 300) {
1635
+ try {
1636
+ sharedLinkApiData = JSON.parse(xhr.responseText);
1637
+ log('Shared link data received:', sharedLinkApiData);
1638
+ if (sharedLinkApiData.documentId) {
1639
+ log('Real document ID from shared link:', sharedLinkApiData.documentId);
1640
+ }
1641
+ callback(sharedLinkApiData);
1642
+ } catch (e) {
1643
+ warn('Error parsing shared link data:', e);
1644
+ callback(null);
1645
+ }
1646
+ } else {
1647
+ warn('Shared link fetch failed. Status:', xhr.status);
1648
+ callback(null);
1649
+ }
1650
+ }
1651
+ };
1652
+
1653
+ xhr.onerror = function() {
1654
+ log('Network error fetching shared link data');
1655
+ callback(null);
1656
+ };
1657
+
1658
+ try {
1659
+ xhr.send();
1660
+ } catch (e) {
1661
+ warn('Error sending shared link request:', e);
1662
+ callback(null);
1663
+ }
1664
+ }
1665
+
1666
+ /**
1667
+ * Fetch document metadata from Bravais API to get GUIDs
1668
+ * Endpoint: /api/v3/documents/{documentId}
1669
+ */
1670
+ function fetchDocumentMetadata(documentId, callback) {
1671
+ if (!documentId) {
1672
+ callback(null);
1673
+ return;
1674
+ }
1675
+
1676
+ if (documentApiFetched) {
1677
+ callback(documentApiData);
1678
+ return;
1679
+ }
1680
+
1681
+ var coreUrl = detectCoreUrl();
1616
1682
  if (!coreUrl) {
1617
1683
  log('No Bravais core URL detected for document metadata fetch');
1618
1684
  documentApiFetched = true;
@@ -1636,13 +1702,24 @@ function generateLrsBridgeCode(options) {
1636
1702
  try {
1637
1703
  documentApiData = JSON.parse(xhr.responseText);
1638
1704
  log('Document metadata received:', documentApiData);
1705
+ // Validate that we got the required fields
1706
+ if (documentApiData.guid) {
1707
+ log('Document GUID found:', documentApiData.guid);
1708
+ } else {
1709
+ warn('Document API response missing guid field!', documentApiData);
1710
+ }
1711
+ if (documentApiData.latestVersion && documentApiData.latestVersion.guid) {
1712
+ log('Version GUID found:', documentApiData.latestVersion.guid);
1713
+ } else {
1714
+ warn('Document API response missing latestVersion.guid field!', documentApiData);
1715
+ }
1639
1716
  callback(documentApiData);
1640
1717
  } catch (e) {
1641
1718
  warn('Error parsing document metadata:', e);
1642
1719
  callback(null);
1643
1720
  }
1644
1721
  } else {
1645
- log('Document metadata fetch failed. Status:', xhr.status);
1722
+ warn('Document metadata fetch failed. Status:', xhr.status, 'Response:', xhr.responseText);
1646
1723
  callback(null);
1647
1724
  }
1648
1725
  }
@@ -2026,6 +2103,14 @@ function generateLrsBridgeCode(options) {
2026
2103
  function buildCourseActivityObject() {
2027
2104
  if (!LRS.courseInfo) return null;
2028
2105
 
2106
+ // Warn if we don't have the required GUIDs for Bravais aggregation
2107
+ if (!LRS.courseInfo.guid) {
2108
+ warn('Missing document GUID - statement may fail Bravais aggregation. Using numeric ID:', LRS.courseInfo.documentId);
2109
+ }
2110
+ if (!LRS.courseInfo.versionGuid) {
2111
+ warn('Missing version GUID - statement may fail Bravais aggregation (document_version_id will be null)');
2112
+ }
2113
+
2029
2114
  var obj = {
2030
2115
  objectType: 'Activity',
2031
2116
  id: LRS.courseInfo.id,
@@ -2038,10 +2123,13 @@ function generateLrsBridgeCode(options) {
2038
2123
  // Add Xyleme-format extensions
2039
2124
  obj.definition.extensions = {};
2040
2125
 
2041
- // Version ID in Xyleme IRI format
2126
+ // Version ID in Xyleme IRI format - REQUIRED for Bravais aggregation
2042
2127
  if (LRS.courseInfo.versionGuid) {
2043
2128
  obj.definition.extensions['versionId'] =
2044
2129
  'http://xyleme.com/bravais/document.version/' + LRS.courseInfo.versionGuid;
2130
+ } else {
2131
+ // Log error - this will cause aggregation failure
2132
+ warn('versionId extension not set - this statement will fail Bravais aggregation!');
2045
2133
  }
2046
2134
 
2047
2135
  // Format (e.g., "SCORM by Rise")
@@ -2966,9 +3054,18 @@ function generateLrsBridgeCode(options) {
2966
3054
 
2967
3055
  // Fetch document metadata from API to get GUIDs (async)
2968
3056
  // Then send course launched event
3057
+ var sharedLinkToken = LRS.courseInfo ? LRS.courseInfo.sharedLinkToken : null;
2969
3058
  var documentId = LRS.courseInfo ? LRS.courseInfo.documentId : null;
2970
3059
 
2971
3060
  function sendLaunchEvents() {
3061
+ // Log the course info state before sending statements
3062
+ log('Sending launch events with course info:', {
3063
+ id: LRS.courseInfo ? LRS.courseInfo.id : null,
3064
+ guid: LRS.courseInfo ? LRS.courseInfo.guid : null,
3065
+ versionGuid: LRS.courseInfo ? LRS.courseInfo.versionGuid : null,
3066
+ documentId: LRS.courseInfo ? LRS.courseInfo.documentId : null
3067
+ });
3068
+
2972
3069
  if (TRACK_NAVIGATION) {
2973
3070
  LRS.courseLaunched();
2974
3071
  LRS.contentOpened({
@@ -2979,17 +3076,52 @@ function generateLrsBridgeCode(options) {
2979
3076
  }
2980
3077
  }
2981
3078
 
2982
- if (documentId && !LRS.courseInfo.guid) {
2983
- // Try to fetch GUIDs from API before sending statements
2984
- fetchDocumentMetadata(documentId, function(docData) {
3079
+ function fetchDocDataAndSend(docId) {
3080
+ fetchDocumentMetadata(docId, function(docData) {
2985
3081
  if (docData) {
2986
3082
  updateCourseInfoFromApi(docData);
2987
3083
  }
2988
3084
  // Send launch events after API fetch (success or failure)
2989
3085
  setTimeout(sendLaunchEvents, 100);
2990
3086
  });
3087
+ }
3088
+
3089
+ if (!LRS.courseInfo.guid) {
3090
+ // First, try to get real document ID from shared link token
3091
+ // The URL path contains a thin pack file ID, not the real document ID
3092
+ if (sharedLinkToken) {
3093
+ log('Fetching shared link data to get real document ID...');
3094
+ fetchSharedLinkData(sharedLinkToken, function(linkData) {
3095
+ if (linkData && linkData.documentId) {
3096
+ // Got the real document ID from shared link
3097
+ log('Got real document ID from shared link:', linkData.documentId);
3098
+ LRS.courseInfo.documentId = linkData.documentId;
3099
+ // Update shared link name if available
3100
+ if (linkData.documentName) {
3101
+ LRS.courseInfo.sharedLinkName = linkData.documentName;
3102
+ }
3103
+ fetchDocDataAndSend(linkData.documentId);
3104
+ } else if (documentId) {
3105
+ // Fallback to extracted document ID (may be thin pack ID)
3106
+ warn('Could not get document ID from shared link, using extracted ID:', documentId);
3107
+ fetchDocDataAndSend(documentId);
3108
+ } else {
3109
+ // No document ID available
3110
+ warn('No document ID available - statements may fail aggregation');
3111
+ setTimeout(sendLaunchEvents, 100);
3112
+ }
3113
+ });
3114
+ } else if (documentId) {
3115
+ // No shared link token, try with extracted document ID
3116
+ log('No shared link token, fetching document data with ID:', documentId);
3117
+ fetchDocDataAndSend(documentId);
3118
+ } else {
3119
+ // No identifiers available
3120
+ warn('No shared link token or document ID - statements may fail aggregation');
3121
+ setTimeout(sendLaunchEvents, 500);
3122
+ }
2991
3123
  } else {
2992
- // Already have GUID or no document ID - send after short delay
3124
+ // Already have GUID - send after short delay
2993
3125
  setTimeout(sendLaunchEvents, 500);
2994
3126
  }
2995
3127