@adobe/spacecat-shared-data-access 1.61.18 → 2.0.0

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.
Files changed (151) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/package.json +1 -2
  3. package/src/index.d.ts +4 -934
  4. package/src/index.js +1 -91
  5. package/src/{v2/models → models}/base/base.model.js +16 -3
  6. package/src/{v2 → models}/index.d.ts +14 -3
  7. package/src/{v2/models → models}/organization/organization.schema.js +1 -1
  8. package/src/{v2/models → models}/site/site.schema.js +1 -1
  9. package/src/service/index.d.ts +20 -0
  10. package/src/service/index.js +6 -46
  11. package/src/dto/api-key.js +0 -58
  12. package/src/dto/audit.js +0 -80
  13. package/src/dto/configuration.js +0 -49
  14. package/src/dto/dto-utils.js +0 -30
  15. package/src/dto/experiment.js +0 -66
  16. package/src/dto/import-job.js +0 -76
  17. package/src/dto/import-url.js +0 -55
  18. package/src/dto/key-event.js +0 -52
  19. package/src/dto/organization.js +0 -54
  20. package/src/dto/site-candidate.js +0 -54
  21. package/src/dto/site-top-page.js +0 -52
  22. package/src/dto/site.js +0 -62
  23. package/src/models/api-key/api-key.js +0 -182
  24. package/src/models/audit.js +0 -146
  25. package/src/models/base.js +0 -42
  26. package/src/models/configuration.js +0 -194
  27. package/src/models/experiment.js +0 -67
  28. package/src/models/importer/import-constants.js +0 -40
  29. package/src/models/importer/import-job.js +0 -252
  30. package/src/models/importer/import-url.js +0 -115
  31. package/src/models/key-event.js +0 -81
  32. package/src/models/organization.js +0 -120
  33. package/src/models/site-candidate.js +0 -111
  34. package/src/models/site-top-page.js +0 -62
  35. package/src/models/site.js +0 -218
  36. package/src/service/api-key/accessPatterns.js +0 -110
  37. package/src/service/api-key/index.js +0 -37
  38. package/src/service/audits/accessPatterns.js +0 -284
  39. package/src/service/audits/index.js +0 -72
  40. package/src/service/configurations/accessPatterns.js +0 -123
  41. package/src/service/configurations/index.js +0 -38
  42. package/src/service/experiments/accessPatterns.js +0 -98
  43. package/src/service/experiments/index.js +0 -40
  44. package/src/service/import-job/accessPatterns.js +0 -134
  45. package/src/service/import-job/index.js +0 -60
  46. package/src/service/import-url/accessPatterns.js +0 -153
  47. package/src/service/import-url/index.js +0 -53
  48. package/src/service/key-events/accessPatterns.js +0 -92
  49. package/src/service/key-events/index.js +0 -38
  50. package/src/service/organizations/accessPatterns.js +0 -160
  51. package/src/service/organizations/index.js +0 -51
  52. package/src/service/site-candidates/accessPatterns.js +0 -112
  53. package/src/service/site-candidates/index.js +0 -48
  54. package/src/service/site-top-pages/accessPatterns.js +0 -83
  55. package/src/service/site-top-pages/index.js +0 -42
  56. package/src/service/sites/accessPatterns.js +0 -467
  57. package/src/service/sites/index.js +0 -116
  58. package/src/v2/index.js +0 -15
  59. package/src/v2/models/index.d.ts +0 -26
  60. /package/src/{v2/errors → errors}/data-access.error.js +0 -0
  61. /package/src/{v2/errors → errors}/index.d.ts +0 -0
  62. /package/src/{v2/errors → errors}/index.js +0 -0
  63. /package/src/{v2/errors → errors}/reference.error.js +0 -0
  64. /package/src/{v2/errors → errors}/schema-validation.error.js +0 -0
  65. /package/src/{v2/errors → errors}/schema.builder.error.js +0 -0
  66. /package/src/{v2/errors → errors}/schema.error.js +0 -0
  67. /package/src/{v2/errors → errors}/validation.error.js +0 -0
  68. /package/src/{v2/models → models}/api-key/api-key.collection.js +0 -0
  69. /package/src/{v2/models → models}/api-key/api-key.model.js +0 -0
  70. /package/src/{v2/models → models}/api-key/api-key.schema.js +0 -0
  71. /package/src/{v2/models → models}/api-key/index.d.ts +0 -0
  72. /package/src/{v2/models → models}/api-key/index.js +0 -0
  73. /package/src/{v2/models → models}/audit/audit.collection.js +0 -0
  74. /package/src/{v2/models → models}/audit/audit.model.js +0 -0
  75. /package/src/{v2/models → models}/audit/audit.schema.js +0 -0
  76. /package/src/{v2/models → models}/audit/index.d.ts +0 -0
  77. /package/src/{v2/models → models}/audit/index.js +0 -0
  78. /package/src/{v2/models → models}/base/base.collection.js +0 -0
  79. /package/src/{v2/models → models}/base/entity.registry.js +0 -0
  80. /package/src/{v2/models → models}/base/index.d.ts +0 -0
  81. /package/src/{v2/models → models}/base/index.js +0 -0
  82. /package/src/{v2/models → models}/base/reference.js +0 -0
  83. /package/src/{v2/models → models}/base/schema.builder.js +0 -0
  84. /package/src/{v2/models → models}/base/schema.js +0 -0
  85. /package/src/{v2/models → models}/configuration/configuration.collection.js +0 -0
  86. /package/src/{v2/models → models}/configuration/configuration.model.js +0 -0
  87. /package/src/{v2/models → models}/configuration/configuration.schema.js +0 -0
  88. /package/src/{v2/models → models}/configuration/index.d.ts +0 -0
  89. /package/src/{v2/models → models}/configuration/index.js +0 -0
  90. /package/src/{v2/models → models}/experiment/experiment.collection.js +0 -0
  91. /package/src/{v2/models → models}/experiment/experiment.model.js +0 -0
  92. /package/src/{v2/models → models}/experiment/experiment.schema.js +0 -0
  93. /package/src/{v2/models → models}/experiment/index.d.ts +0 -0
  94. /package/src/{v2/models → models}/experiment/index.js +0 -0
  95. /package/src/{v2/models → models}/import-job/import-job.collection.js +0 -0
  96. /package/src/{v2/models → models}/import-job/import-job.model.js +0 -0
  97. /package/src/{v2/models → models}/import-job/import-job.schema.js +0 -0
  98. /package/src/{v2/models → models}/import-job/index.d.ts +0 -0
  99. /package/src/{v2/models → models}/import-job/index.js +0 -0
  100. /package/src/{v2/models → models}/import-url/import-url.collection.js +0 -0
  101. /package/src/{v2/models → models}/import-url/import-url.model.js +0 -0
  102. /package/src/{v2/models → models}/import-url/import-url.schema.js +0 -0
  103. /package/src/{v2/models → models}/import-url/index.d.ts +0 -0
  104. /package/src/{v2/models → models}/import-url/index.js +0 -0
  105. /package/src/{v2/models → models}/index.js +0 -0
  106. /package/src/{v2/models → models}/key-event/index.d.ts +0 -0
  107. /package/src/{v2/models → models}/key-event/index.js +0 -0
  108. /package/src/{v2/models → models}/key-event/key-event.collection.js +0 -0
  109. /package/src/{v2/models → models}/key-event/key-event.model.js +0 -0
  110. /package/src/{v2/models → models}/key-event/key-event.schema.js +0 -0
  111. /package/src/{v2/models → models}/latest-audit/index.d.ts +0 -0
  112. /package/src/{v2/models → models}/latest-audit/index.js +0 -0
  113. /package/src/{v2/models → models}/latest-audit/latest-audit.collection.js +0 -0
  114. /package/src/{v2/models → models}/latest-audit/latest-audit.model.js +0 -0
  115. /package/src/{v2/models → models}/latest-audit/latest-audit.schema.js +0 -0
  116. /package/src/{v2/models → models}/opportunity/index.d.ts +0 -0
  117. /package/src/{v2/models → models}/opportunity/index.js +0 -0
  118. /package/src/{v2/models → models}/opportunity/opportunity.collection.js +0 -0
  119. /package/src/{v2/models → models}/opportunity/opportunity.model.js +0 -0
  120. /package/src/{v2/models → models}/opportunity/opportunity.schema.js +0 -0
  121. /package/src/{v2/models → models}/organization/index.d.ts +0 -0
  122. /package/src/{v2/models → models}/organization/index.js +0 -0
  123. /package/src/{v2/models → models}/organization/organization.collection.js +0 -0
  124. /package/src/{v2/models → models}/organization/organization.model.js +0 -0
  125. /package/src/{v2/models → models}/site/index.d.ts +0 -0
  126. /package/src/{v2/models → models}/site/index.js +0 -0
  127. /package/src/{v2/models → models}/site/site.collection.js +0 -0
  128. /package/src/{v2/models → models}/site/site.model.js +0 -0
  129. /package/src/{v2/models → models}/site-candidate/index.d.ts +0 -0
  130. /package/src/{v2/models → models}/site-candidate/index.js +0 -0
  131. /package/src/{v2/models → models}/site-candidate/site-candidate.collection.js +0 -0
  132. /package/src/{v2/models → models}/site-candidate/site-candidate.model.js +0 -0
  133. /package/src/{v2/models → models}/site-candidate/site-candidate.schema.js +0 -0
  134. /package/src/{v2/models → models}/site-top-page/index.d.ts +0 -0
  135. /package/src/{v2/models → models}/site-top-page/index.js +0 -0
  136. /package/src/{v2/models → models}/site-top-page/site-top-page.collection.js +0 -0
  137. /package/src/{v2/models → models}/site-top-page/site-top-page.model.js +0 -0
  138. /package/src/{v2/models → models}/site-top-page/site-top-page.schema.js +0 -0
  139. /package/src/{v2/models → models}/suggestion/index.d.ts +0 -0
  140. /package/src/{v2/models → models}/suggestion/index.js +0 -0
  141. /package/src/{v2/models → models}/suggestion/suggestion.collection.js +0 -0
  142. /package/src/{v2/models → models}/suggestion/suggestion.model.js +0 -0
  143. /package/src/{v2/models → models}/suggestion/suggestion.schema.js +0 -0
  144. /package/src/{v2/readme.md → readme.md} +0 -0
  145. /package/src/{v2/util → util}/accessor.utils.js +0 -0
  146. /package/src/{v2/util → util}/guards.d.ts +0 -0
  147. /package/src/{v2/util → util}/guards.js +0 -0
  148. /package/src/{v2/util → util}/index.d.ts +0 -0
  149. /package/src/{v2/util → util}/index.js +0 -0
  150. /package/src/{v2/util → util}/patcher.js +0 -0
  151. /package/src/{v2/util → util}/util.js +0 -0
@@ -1,110 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import { ApiKeyDto } from '../../dto/api-key.js';
14
- import { createApiKey } from '../../models/api-key/api-key.js';
15
-
16
- /**
17
- * Get ApiKey by Hashed Key
18
- * @param {string} hashedApiKey
19
- * @param {DynamoClient} dynamoClient
20
- * @param {Object} config
21
- * @param {Logger} log
22
- * @returns {Promise<ApiKey>}
23
- */
24
- export const getApiKeyByHashedApiKey = async (hashedApiKey, dynamoClient, log, config) => {
25
- const items = await dynamoClient.query({
26
- TableName: config.tableNameApiKeys,
27
- IndexName: config.indexNameApiKeyByHashedApiKey,
28
- KeyConditionExpression: '#hashedApiKey = :hashedApiKey',
29
- ExpressionAttributeNames: {
30
- '#hashedApiKey': 'hashedApiKey',
31
- },
32
- ExpressionAttributeValues: {
33
- ':hashedApiKey': hashedApiKey,
34
- },
35
- Limit: 1,
36
- });
37
- return items.length > 0 ? ApiKeyDto.fromDynamoItem(items[0]) : null;
38
- };
39
-
40
- /**
41
- * Create new ApiKey
42
- * @param {ApiKey} apiKey
43
- * @param {DynamoClient} dynamoClient
44
- * @param {Object} config
45
- * @param {Logger} log
46
- * @returns {Promise<ApiKey>}
47
- */
48
- export const createNewApiKey = async (apiKey, dynamoClient, config, log) => {
49
- const item = createApiKey(apiKey);
50
- await dynamoClient.putItem(config.tableNameApiKeys, ApiKeyDto.toDynamoItem(item), log);
51
- return item;
52
- };
53
-
54
- /**
55
- * Get ApiKeys by IMS User ID and IMS Org ID
56
- * @param imsUserId
57
- * @param imsOrgId
58
- * @param dynamoClient
59
- * @param config
60
- * @returns {Promise<ApiKey[]>}
61
- */
62
- export const getApiKeysByImsUserIdAndImsOrgId = async (
63
- imsUserId,
64
- imsOrgId,
65
- dynamoClient,
66
- config,
67
- ) => {
68
- const items = await dynamoClient.query({
69
- TableName: config.tableNameApiKeys,
70
- IndexName: config.indexNameApiKeyByImsUserIdAndImsOrgId,
71
- KeyConditionExpression: '#imsUserId = :imsUserId AND #imsOrgId = :imsOrgId',
72
- ExpressionAttributeNames: {
73
- '#imsUserId': 'imsUserId',
74
- '#imsOrgId': 'imsOrgId',
75
- },
76
- ExpressionAttributeValues: {
77
- ':imsUserId': imsUserId,
78
- ':imsOrgId': imsOrgId,
79
- },
80
- });
81
- return items.map((item) => ApiKeyDto.fromDynamoItem(item));
82
- };
83
-
84
- /**
85
- * Get ApiKey by ID
86
- * @param id
87
- * @param dynamoClient
88
- * @param config
89
- * @returns {Promise<ApiKey|null>}
90
- */
91
- export const getApiKeyById = async (id, dynamoClient, config) => {
92
- const item = await dynamoClient.getItem(config.tableNameApiKeys, { id });
93
- return item ? ApiKeyDto.fromDynamoItem(item) : null;
94
- };
95
-
96
- /**
97
- * Update ApiKey
98
- * @param apiKey
99
- * @param dynamoClient
100
- * @param config
101
- * @returns {Promise<ApiKey>}
102
- */
103
- export const updateApiKey = async (apiKey, dynamoClient, config) => {
104
- const existingApiKey = await getApiKeyById(apiKey.getId(), dynamoClient, config);
105
- if (!existingApiKey) {
106
- throw new Error(`API Key with id ${apiKey.getId()} not found`);
107
- }
108
- await dynamoClient.putItem(config.tableNameApiKeys, ApiKeyDto.toDynamoItem(apiKey));
109
- return apiKey;
110
- };
@@ -1,37 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {
14
- createNewApiKey,
15
- getApiKeyByHashedApiKey,
16
- getApiKeyById,
17
- getApiKeysByImsUserIdAndImsOrgId,
18
- updateApiKey,
19
- } from './accessPatterns.js';
20
-
21
- export const apiKeyFunctions = (dynamoClient, config, log) => ({
22
- getApiKeyByHashedApiKey: (hashedApiKey) => getApiKeyByHashedApiKey(
23
- hashedApiKey,
24
- dynamoClient,
25
- log,
26
- config,
27
- ),
28
- createNewApiKey: (apiKey) => createNewApiKey(apiKey, dynamoClient, config, log),
29
- getApiKeysByImsUserIdAndImsOrgId: (imsUserId, imsOrgId) => getApiKeysByImsUserIdAndImsOrgId(
30
- imsUserId,
31
- imsOrgId,
32
- dynamoClient,
33
- config,
34
- ),
35
- getApiKeyById: (id) => getApiKeyById(id, dynamoClient, config),
36
- updateApiKey: (apiKey) => updateApiKey(apiKey, dynamoClient, config),
37
- });
@@ -1,284 +0,0 @@
1
- /*
2
- * Copyright 2023 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import { hasText, isObject } from '@adobe/spacecat-shared-utils';
14
-
15
- import { AuditDto } from '../../dto/audit.js';
16
- import { createAudit } from '../../models/audit.js';
17
-
18
- /**
19
- * Retrieves audits for a specified site. If an audit type is provided,
20
- * it returns only audits of that type.
21
- *
22
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
23
- * @param {DataAccessConfig} config - The data access config.
24
- * @param {Logger} log - The log.
25
- * @param {string} siteId - The ID of the site for which audits are being retrieved.
26
- * @param {string} [auditType] - Optional. The type of audits to retrieve.
27
- * @param {boolean} [ascending] - Optional. Determines if the audits should be sorted
28
- * ascending. Default is true.
29
- * @returns {Promise<Readonly<Audit>[]>} A promise that resolves to an array of audits
30
- * for the specified site.
31
- */
32
- export const getAuditsForSite = async (
33
- dynamoClient,
34
- config,
35
- log,
36
- siteId,
37
- auditType,
38
- ascending = true,
39
- ) => {
40
- const queryParams = {
41
- TableName: config.tableNameAudits,
42
- KeyConditionExpression: 'siteId = :siteId',
43
- ExpressionAttributeValues: {
44
- ':siteId': siteId,
45
- },
46
- ScanIndexForward: ascending, // Sorts ascending if true, descending if false
47
- };
48
-
49
- if (hasText(auditType)) {
50
- queryParams.KeyConditionExpression += ' AND begins_with(SK, :auditType)';
51
- queryParams.ExpressionAttributeValues[':auditType'] = `${auditType}#`;
52
- }
53
-
54
- const dynamoItems = await dynamoClient.query(queryParams);
55
-
56
- return dynamoItems.map((item) => AuditDto.fromDynamoItem(item));
57
- };
58
-
59
- /**
60
- * Retrieves a specific audit for a specified site.
61
- *
62
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
63
- * @param {DataAccessConfig} config - The data access config.
64
- * @param {Logger} log - The log.
65
- * @param {string} siteId - The ID of the site for which to retrieve the audit.
66
- * @param {string} auditType - The type of audit to retrieve.
67
- * @param auditedAt - The ISO 8601 timestamp of the audit.
68
- * @returns {Promise<Readonly<Audit>|null>}
69
- */
70
- export const getAuditForSite = async (
71
- dynamoClient,
72
- config,
73
- log,
74
- siteId,
75
- auditType,
76
- auditedAt,
77
- ) => {
78
- const audit = await dynamoClient.getItem(config.tableNameAudits, {
79
- siteId,
80
- SK: `${auditType}#${auditedAt}`,
81
- });
82
-
83
- return audit ? AuditDto.fromDynamoItem(audit) : null;
84
- };
85
-
86
- /**
87
- * Retrieves the latest audits of a specific type across all sites.
88
- *
89
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
90
- * @param {DataAccessConfig} config - The data access config.
91
- * @param {Logger} log - The log.
92
- * @param {string} auditType - The type of audits to retrieve.
93
- * @param {boolean} ascending - Determines if the audits should be sorted ascending
94
- * or descending by scores.
95
- * @returns {Promise<Readonly<Audit>[]>} A promise that resolves to an array of the latest
96
- * audits of the specified type.
97
- */
98
- export const getLatestAudits = async (
99
- dynamoClient,
100
- config,
101
- log,
102
- auditType,
103
- ascending = true,
104
- ) => {
105
- const dynamoItems = await dynamoClient.query({
106
- TableName: config.tableNameLatestAudits,
107
- IndexName: config.indexNameAllLatestAuditScores,
108
- KeyConditionExpression: 'GSI1PK = :gsi1pk AND begins_with(GSI1SK, :auditType)',
109
- ExpressionAttributeValues: {
110
- ':gsi1pk': config.pkAllLatestAudits,
111
- ':auditType': `${auditType}#`,
112
- },
113
- ScanIndexForward: ascending, // Sorts ascending if true, descending if false
114
- });
115
-
116
- return dynamoItems.map((item) => AuditDto.fromDynamoItem(item));
117
- };
118
-
119
- /**
120
- * Retrieves latest audits for a specified site.
121
- *
122
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
123
- * @param {DataAccessConfig} config - The data access config.
124
- * @param {Logger} log - The log.
125
- * @param {string} siteId - The ID of the site for which audits are being retrieved.
126
- * @returns {Promise<Readonly<Audit>[]>} A promise that resolves to an array of latest audits
127
- * for the specified site.
128
- */
129
- export const getLatestAuditsForSite = async (
130
- dynamoClient,
131
- config,
132
- log,
133
- siteId,
134
- ) => {
135
- const queryParams = {
136
- TableName: config.tableNameLatestAudits,
137
- KeyConditionExpression: 'siteId = :siteId',
138
- ExpressionAttributeValues: { ':siteId': siteId },
139
- };
140
-
141
- const dynamoItems = await dynamoClient.query(queryParams);
142
-
143
- return dynamoItems.map((item) => AuditDto.fromDynamoItem(item));
144
- };
145
-
146
- /**
147
- * Retrieves the latest audit for a specified site and audit type.
148
- *
149
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
150
- * @param {DataAccessConfig} config - The data access config.
151
- * @param {Logger} log - The log.
152
- * @param {string} siteId - The ID of the site for which the latest audit is being retrieved.
153
- * @param {string} auditType - The type of audit to retrieve the latest instance of.
154
- * @returns {Promise<Audit|null>} A promise that resolves to the latest audit of the
155
- * specified type for the site, or null if none is found.
156
- */
157
- export const getLatestAuditForSite = async (
158
- dynamoClient,
159
- config,
160
- log,
161
- siteId,
162
- auditType,
163
- ) => {
164
- const latestAudit = await dynamoClient.getItem(config.tableNameLatestAudits, {
165
- siteId,
166
- auditType,
167
- });
168
-
169
- return latestAudit ? AuditDto.fromDynamoItem(latestAudit) : null;
170
- };
171
-
172
- /**
173
- * Adds an audit.
174
- *
175
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
176
- * @param {DataAccessConfig} config - The data access config.
177
- * @param {Logger} log - The log.
178
- * @param {object} auditData - The audit data.
179
- * @returns {Promise<Readonly<Audit>>}
180
- */
181
- export const addAudit = async (
182
- dynamoClient,
183
- config,
184
- log,
185
- auditData,
186
- ) => {
187
- const newAudit = createAudit(auditData);
188
- const existingAudit = await getAuditForSite(
189
- dynamoClient,
190
- config,
191
- log,
192
- newAudit.getSiteId(),
193
- newAudit.getAuditType(),
194
- newAudit.getAuditedAt(),
195
- );
196
-
197
- if (isObject(existingAudit)) {
198
- throw new Error('Audit already exists');
199
- }
200
-
201
- const latestAudit = await getLatestAuditForSite(
202
- dynamoClient,
203
- config,
204
- log,
205
- newAudit.getSiteId(),
206
- newAudit.getAuditType(),
207
- );
208
-
209
- if (isObject(latestAudit)) {
210
- newAudit.setPreviousAuditResult({
211
- ...latestAudit.getAuditResult(),
212
- auditedAt: latestAudit.getAuditedAt(),
213
- fullAuditRef: latestAudit.getFullAuditRef(),
214
- });
215
- }
216
-
217
- // TODO: Add transaction support
218
- await dynamoClient.putItem(config.tableNameAudits, AuditDto.toDynamoItem(newAudit));
219
- await dynamoClient.putItem(
220
- config.tableNameLatestAudits,
221
- AuditDto.toDynamoItem(newAudit, true),
222
- );
223
-
224
- return newAudit;
225
- };
226
-
227
- /**
228
- * Removes audits from the database.
229
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
230
- * @param {DataAccessConfig} config - The data access config.
231
- * @param audits
232
- * @param latest
233
- * @returns {Promise<void>}
234
- */
235
- async function removeAudits(
236
- dynamoClient,
237
- config,
238
- audits,
239
- latest = false,
240
- ) {
241
- const tableName = latest ? config.tableNameLatestAudits : config.tableNameAudits;
242
- // TODO: use batch-remove (needs dynamo client update)
243
- const removeAuditPromises = audits.map((audit) => {
244
- const sortKey = latest
245
- ? { auditType: `${audit.getAuditType()}` }
246
- : { SK: `${audit.getAuditType()}#${audit.getAuditedAt()}` };
247
- return dynamoClient.removeItem(
248
- tableName,
249
- {
250
- siteId: audit.getSiteId(),
251
- ...sortKey,
252
- },
253
- );
254
- });
255
-
256
- await Promise.all(removeAuditPromises);
257
- }
258
-
259
- /**
260
- * Removes all audits for a specified site and the latest audit entry.
261
- *
262
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
263
- * @param {DataAccessConfig} config - The data access config.
264
- * @param {Logger} log - The log.
265
- * @param {string} siteId - The ID of the site for which audits are being removed.
266
- * @returns {Promise<void>}
267
- */
268
- export const removeAuditsForSite = async (
269
- dynamoClient,
270
- config,
271
- log,
272
- siteId,
273
- ) => {
274
- try {
275
- const audits = await getAuditsForSite(dynamoClient, config, log, siteId);
276
- const latestAudits = await getLatestAuditsForSite(dynamoClient, config, log, siteId);
277
-
278
- await removeAudits(dynamoClient, config, audits);
279
- await removeAudits(dynamoClient, config, latestAudits, true);
280
- } catch (error) {
281
- log.error(`Error removing audits for site ${siteId}: ${error.message}`);
282
- throw error;
283
- }
284
- };
@@ -1,72 +0,0 @@
1
- /*
2
- * Copyright 2023 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {
14
- addAudit,
15
- getAuditForSite,
16
- getAuditsForSite,
17
- getLatestAuditForSite,
18
- getLatestAudits,
19
- getLatestAuditsForSite,
20
- removeAuditsForSite,
21
- } from './accessPatterns.js';
22
-
23
- export const auditFunctions = (dynamoClient, config, log) => ({
24
- getAuditForSite: (siteId, auditType, auditedAt) => getAuditForSite(
25
- dynamoClient,
26
- config,
27
- log,
28
- siteId,
29
- auditType,
30
- auditedAt,
31
- ),
32
- getAuditsForSite: (siteId, auditType, ascending) => getAuditsForSite(
33
- dynamoClient,
34
- config,
35
- log,
36
- siteId,
37
- auditType,
38
- ascending,
39
- ),
40
- getLatestAudits: (auditType, ascending) => getLatestAudits(
41
- dynamoClient,
42
- config,
43
- log,
44
- auditType,
45
- ascending,
46
- ),
47
- getLatestAuditForSite: (siteId, auditType) => getLatestAuditForSite(
48
- dynamoClient,
49
- config,
50
- log,
51
- siteId,
52
- auditType,
53
- ),
54
- getLatestAuditsForSite: (siteId) => getLatestAuditsForSite(
55
- dynamoClient,
56
- config,
57
- log,
58
- siteId,
59
- ),
60
- addAudit: (auditData) => addAudit(
61
- dynamoClient,
62
- config,
63
- log,
64
- auditData,
65
- ),
66
- removeAuditsForSite: (siteId) => removeAuditsForSite(
67
- dynamoClient,
68
- config,
69
- log,
70
- siteId,
71
- ),
72
- });
@@ -1,123 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {
14
- isInteger,
15
- isObject,
16
- } from '@adobe/spacecat-shared-utils';
17
-
18
- import { ConfigurationDto } from '../../dto/configuration.js';
19
- import { createConfiguration } from '../../models/configuration.js';
20
-
21
- /**
22
- * Retrieves configuration with latest version.
23
- *
24
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
25
- * @param {DataAccessConfig} config - The data access config.
26
- * @returns {Promise<Readonly<Configuration>>} A promise that resolves to the configuration
27
- * object if found, otherwise null.
28
- */
29
- export const getConfiguration = async (
30
- dynamoClient,
31
- config,
32
- ) => {
33
- const dynamoItems = await dynamoClient.query({
34
- TableName: config.tableNameConfigurations,
35
- KeyConditionExpression: 'PK = :pk',
36
- ExpressionAttributeValues: {
37
- ':pk': config.pkAllConfigurations,
38
- },
39
- Limit: 1,
40
- ScanIndexForward: false, // Sorts ascending if true, descending if false
41
- });
42
-
43
- if (dynamoItems.length === 0) return null;
44
- return ConfigurationDto.fromDynamoItem(dynamoItems[0]);
45
- };
46
-
47
- /**
48
- * Retrieves all configurations.
49
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
50
- * @param {DataAccessConfig} config - The data access config.
51
- * @return {Promise<Readonly<Configuration>[]>} A promise that resolves to the configurations.
52
- */
53
- export const getConfigurations = async (
54
- dynamoClient,
55
- config,
56
- ) => {
57
- const dynamoItems = await dynamoClient.query({
58
- TableName: config.tableNameConfigurations,
59
- KeyConditionExpression: 'PK = :pk',
60
- ExpressionAttributeValues: {
61
- ':pk': config.pkAllConfigurations,
62
- },
63
- });
64
-
65
- return dynamoItems.map(ConfigurationDto.fromDynamoItem);
66
- };
67
-
68
- /**
69
- * Retrieves a site by its version.
70
- *
71
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
72
- * @param {DataAccessConfig} config - The data access config.
73
- * @param {string} version - The version of the configuration to retrieve.
74
- * @returns {Promise<Readonly<Configuration>|null>} A promise that resolves to the configuration
75
- * object if found, otherwise null.
76
- */
77
- export const getConfigurationByVersion = async (
78
- dynamoClient,
79
- config,
80
- version,
81
- ) => {
82
- const dynamoItem = await dynamoClient.getItem(config.tableNameConfigurations, {
83
- PK: config.pkAllConfigurations,
84
- version,
85
- });
86
-
87
- return isObject(dynamoItem) ? ConfigurationDto.fromDynamoItem(dynamoItem) : null;
88
- };
89
-
90
- function incrementVersion(version) {
91
- if (!isInteger(version)) return 1;
92
-
93
- const versionNumber = parseInt(version, 10);
94
- return versionNumber + 1;
95
- }
96
-
97
- /**
98
- * Updates the configuration. Updating the configuration will create a new version of the
99
- * configuration. The version is a string of the format "v<version-number>", for example "v1".
100
- * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
101
- * @param {DataAccessConfig} config - The data access config.
102
- * @param {Configuration} configurationData - The configuration data.
103
- * @return {Promise<void>} A promise that resolves when the configuration is updated.
104
- */
105
- export const updateConfiguration = async (
106
- dynamoClient,
107
- config,
108
- configurationData,
109
- ) => {
110
- const newConfigurationData = { ...configurationData };
111
- const latestConfiguration = await getConfiguration(dynamoClient, config);
112
-
113
- newConfigurationData.version = incrementVersion(latestConfiguration?.getVersion());
114
-
115
- const newConfiguration = createConfiguration(newConfigurationData);
116
-
117
- await dynamoClient.putItem(
118
- config.tableNameConfigurations,
119
- ConfigurationDto.toDynamoItem(newConfiguration),
120
- );
121
-
122
- return newConfiguration;
123
- };
@@ -1,38 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
- import {
13
- getConfiguration,
14
- getConfigurationByVersion,
15
- getConfigurations,
16
- updateConfiguration,
17
- } from './accessPatterns.js';
18
-
19
- export const configurationFunctions = (dynamoClient, config) => ({
20
- getConfiguration: () => getConfiguration(
21
- dynamoClient,
22
- config,
23
- ),
24
- getConfigurations: () => getConfigurations(
25
- dynamoClient,
26
- config,
27
- ),
28
- getConfigurationByVersion: (version) => getConfigurationByVersion(
29
- dynamoClient,
30
- config,
31
- version,
32
- ),
33
- updateConfiguration: (configurationData) => updateConfiguration(
34
- dynamoClient,
35
- config,
36
- configurationData,
37
- ),
38
- });