@adobe/spacecat-shared-data-access 1.10.1 → 1.10.3

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,17 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.10.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.10.2...@adobe/spacecat-shared-data-access-v1.10.3) (2024-01-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * organizations query ([37b4b14](https://github.com/adobe/spacecat-shared/commit/37b4b1439e6f2b6ab7c87b1abcfcefb9b2c8a394))
7
+
8
+ # [@adobe/spacecat-shared-data-access-v1.10.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.10.1...@adobe/spacecat-shared-data-access-v1.10.2) (2024-01-24)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * add missing org data layer methods ([#107](https://github.com/adobe/spacecat-shared/issues/107)) ([0504d8e](https://github.com/adobe/spacecat-shared/commit/0504d8e8f0486d1f1a00f44a18c42948ddfd93c7))
14
+
1
15
  # [@adobe/spacecat-shared-data-access-v1.10.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.10.0...@adobe/spacecat-shared-data-access-v1.10.1) (2024-01-23)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.10.1",
3
+ "version": "1.10.3",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.d.ts CHANGED
@@ -336,6 +336,9 @@ export interface DataAccess {
336
336
  removeSite: (
337
337
  siteId: string,
338
338
  ) => Promise<void>;
339
+ removeSitesForOrganization: (
340
+ organizationId: string,
341
+ ) => Promise<void>;
339
342
  getOrganizations:
340
343
  () => Promise<Organization[]>;
341
344
  getOrganizationByID: (
@@ -347,7 +350,7 @@ export interface DataAccess {
347
350
  updateOrganization: (
348
351
  organization: Organization,
349
352
  ) => Promise<Organization>;
350
- removeRemoveOrganization: (
353
+ removeOrganization: (
351
354
  organizationId: string,
352
355
  ) => Promise<void>;
353
356
  }
package/src/index.js CHANGED
@@ -50,7 +50,7 @@ export default function dataAccessWrapper(fn) {
50
50
  tableNameOrganizations: DYNAMO_TABLE_NAME_ORGANIZATIONS,
51
51
  tableNameSites: DYNAMO_TABLE_NAME_SITES,
52
52
  indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
53
- indexNameOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
53
+ indexNameAllOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
54
54
  indexNameAllSitesByDeliveryType: DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
55
55
  indexNameAllLatestAuditScores: DYNAMO_INDEX_NAME_ALL_LATEST_AUDIT_SCORES,
56
56
  indexNameAllSitesOrganizations: DYNAMO_INDEX_NAME_ALL_SITES_ORGANIZATIONS,
@@ -14,6 +14,7 @@ import { isObject } from '@adobe/spacecat-shared-utils';
14
14
 
15
15
  import { createOrganization } from '../../models/organization.js';
16
16
  import { OrganizationDto } from '../../dto/organization.js';
17
+ import { removeSitesForOrganization } from '../sites/accessPatterns.js';
17
18
 
18
19
  /**
19
20
  * Retrieves all organizations.
@@ -123,6 +124,8 @@ export const removeOrganization = async (
123
124
  organizationId,
124
125
  ) => {
125
126
  try {
127
+ await removeSitesForOrganization(dynamoClient, config, log, organizationId);
128
+
126
129
  await dynamoClient.removeItem(config.tableNameOrganizations, { id: organizationId });
127
130
  } catch (error) {
128
131
  log.error(`Error removing organization: ${error.message}`);
@@ -15,7 +15,8 @@ import { isObject } from '@adobe/spacecat-shared-utils';
15
15
  import {
16
16
  getAuditsForSite,
17
17
  getLatestAuditForSite,
18
- getLatestAudits, removeAuditsForSite,
18
+ getLatestAudits,
19
+ removeAuditsForSite,
19
20
  } from '../audits/accessPatterns.js';
20
21
 
21
22
  import { createSite } from '../../models/site.js';
@@ -369,3 +370,49 @@ export const removeSite = async (
369
370
  throw error;
370
371
  }
371
372
  };
373
+
374
+ /**
375
+ * Removes all given sites.
376
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
377
+ * @param {DataAccessConfig} config - The data access config.
378
+ * @param {Array<Site>} sites - The sites to remove.
379
+ * @return {Promise<void>} A promise that resolves when all sites have been removed.
380
+ */
381
+ async function removeSites(
382
+ dynamoClient,
383
+ config,
384
+ sites,
385
+ ) {
386
+ const tableName = config.tableNameSites;
387
+ // TODO: use batch-remove (needs dynamo client update)
388
+ const removeSitePromises = sites.map((site) => dynamoClient.removeItem(
389
+ tableName,
390
+ { id: site.getId() },
391
+ ));
392
+
393
+ await Promise.all(removeSitePromises);
394
+ }
395
+
396
+ /**
397
+ * Removes all sites for an organization.
398
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
399
+ * @param {DataAccessConfig} config - The data access config.
400
+ * @param {Logger} log - The logger.
401
+ * @param {string} organizationId - The ID of the organization to remove the sites for.
402
+ * @return {Promise<void>} A promise that resolves when all sites for the organization have been
403
+ * removed.
404
+ */
405
+ export const removeSitesForOrganization = async (
406
+ dynamoClient,
407
+ config,
408
+ log,
409
+ organizationId,
410
+ ) => {
411
+ try {
412
+ const sites = await getSitesByOrganizationID(dynamoClient, config, organizationId);
413
+ await removeSites(dynamoClient, config, sites);
414
+ } catch (error) {
415
+ log.error(`Error removing sites for organization ${organizationId}: ${error.message}`);
416
+ throw error;
417
+ }
418
+ };
@@ -17,9 +17,13 @@ import {
17
17
  getSiteByBaseURLWithAudits,
18
18
  getSiteByBaseURLWithLatestAudit,
19
19
  getSiteByID,
20
- getSites, getSitesByDeliveryType, getSitesByOrganizationID,
20
+ getSites,
21
+ getSitesByDeliveryType,
22
+ getSitesByOrganizationID,
21
23
  getSitesToAudit,
22
- getSitesWithLatestAudit, removeSite,
24
+ getSitesWithLatestAudit,
25
+ removeSite,
26
+ removeSitesForOrganization,
23
27
  updateSite,
24
28
  } from './accessPatterns.js';
25
29
 
@@ -91,4 +95,10 @@ export const siteFunctions = (dynamoClient, config, log) => ({
91
95
  addSite: (siteData) => addSite(dynamoClient, config, log, siteData),
92
96
  updateSite: (site) => updateSite(dynamoClient, config, log, site),
93
97
  removeSite: (siteId) => removeSite(dynamoClient, config, log, siteId),
98
+ removeSitesForOrganization: (organizationId) => removeSitesForOrganization(
99
+ dynamoClient,
100
+ config,
101
+ log,
102
+ organizationId,
103
+ ),
94
104
  });