@adobe/spacecat-shared-data-access 1.2.6 → 1.2.7

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.2.7](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.2.6...@adobe/spacecat-shared-data-access-v1.2.7) (2023-12-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * sort order sites with latest audits ([#46](https://github.com/adobe-rnd/spacecat-shared/issues/46)) ([8da2bf1](https://github.com/adobe-rnd/spacecat-shared/commit/8da2bf16b904ced6787d8c56ba2b4b72678687a0))
7
+
1
8
  # [@adobe/spacecat-shared-data-access-v1.2.6](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.2.5...@adobe/spacecat-shared-data-access-v1.2.6) (2023-12-11)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -55,19 +55,18 @@ export const getSitesToAudit = async (dynamoClient, config) => {
55
55
  };
56
56
 
57
57
  /**
58
- * Retrieves sites with their latest audit of a specified type. If there is
59
- * no audit of the specified type for a site, the site will be returned with
60
- * an empty audits array.
61
- * The audits are sorted ascending or descending by scores.
62
- *
58
+ * Retrieves all sites with their latest audit. Sites without a latest audit will be included
59
+ * in the result, but will have an empty audits array. The sites are sorted by their latest
60
+ * audit scores in ascending order by default. The sortAuditsAscending parameter can be used
61
+ * to change the sort order. If a site has no latest audit, it will be sorted at the end of
62
+ * the list.
63
63
  * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
64
64
  * @param {DataAccessConfig} config - The data access config.
65
65
  * @param {Logger} log - The logger.
66
- * @param {string} auditType - The type of the latest audits to retrieve for each site.
67
- * @param {boolean} [sortAuditsAscending] - Optional. Determines if the audits
68
- * should be sorted ascending or descending by scores.
69
- * @returns {Promise<Readonly<Site>[]>} A promise that resolves to an array of site objects,
70
- * each with its latest audit of the specified type.
66
+ * @param {string} auditType - The type of audits to retrieve for the sites.
67
+ * @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in
68
+ * @return {Promise<Readonly<Site>[]>} A promise that resolves to an array of sites with their
69
+ * latest audit.
71
70
  */
72
71
  export const getSitesWithLatestAudit = async (
73
72
  dynamoClient,
@@ -81,17 +80,26 @@ export const getSitesWithLatestAudit = async (
81
80
  getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending),
82
81
  ]);
83
82
 
84
- const auditsMap = new Map(latestAudits.map((audit) => [audit.getSiteId(), audit]));
83
+ const sitesMap = new Map(sites.map((site) => [site.getId(), site]));
84
+ const orderedSites = [];
85
85
 
86
- return sites.map((site) => {
87
- const audit = auditsMap.get(site.getId());
88
- if (audit) {
86
+ // First, append sites with a latest audit in the sorted order
87
+ latestAudits.forEach((audit) => {
88
+ const site = sitesMap.get(audit.getSiteId());
89
+ if (site) {
89
90
  site.setAudits([audit]);
90
- } else {
91
- site.setAudits([]);
91
+ orderedSites.push(site);
92
+ sitesMap.delete(site.getId()); // Remove the site from the map to avoid adding it again
92
93
  }
93
- return site;
94
94
  });
95
+
96
+ // Then, append the remaining sites (without a latest audit)
97
+ sitesMap.forEach((site) => {
98
+ site.setAudits([]);
99
+ orderedSites.push(site);
100
+ });
101
+
102
+ return orderedSites;
95
103
  };
96
104
 
97
105
  /**