@adobe/spacecat-shared-data-access 1.60.2 → 1.61.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 (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +5 -5
  3. package/src/v2/errors/data-access.error.js +24 -0
  4. package/src/v2/errors/index.d.ts +5 -1
  5. package/src/v2/errors/index.js +11 -1
  6. package/src/v2/errors/reference.error.js +22 -0
  7. package/src/v2/{models/base/constants.js → errors/schema-validation.error.js} +4 -6
  8. package/src/v2/errors/schema.builder.error.js +27 -0
  9. package/src/v2/errors/schema.error.js +19 -0
  10. package/src/v2/errors/validation.error.js +3 -1
  11. package/src/v2/models/api-key/index.d.ts +15 -2
  12. package/src/v2/models/audit/audit.collection.js +25 -1
  13. package/src/v2/models/audit/audit.schema.js +3 -0
  14. package/src/v2/models/audit/index.d.ts +16 -11
  15. package/src/v2/models/base/base.collection.js +148 -85
  16. package/src/v2/models/base/base.model.js +73 -14
  17. package/src/v2/models/base/entity.registry.js +7 -2
  18. package/src/v2/models/base/index.d.ts +30 -11
  19. package/src/v2/models/base/reference.js +81 -28
  20. package/src/v2/models/base/schema.builder.js +96 -24
  21. package/src/v2/models/base/schema.js +78 -10
  22. package/src/v2/models/configuration/index.d.ts +24 -90
  23. package/src/v2/models/experiment/index.d.ts +11 -3
  24. package/src/v2/models/import-job/index.d.ts +10 -3
  25. package/src/v2/models/import-url/index.d.ts +6 -3
  26. package/src/v2/models/index.d.ts +3 -0
  27. package/src/v2/models/index.js +1 -0
  28. package/src/v2/models/key-event/index.d.ts +5 -1
  29. package/src/v2/models/latest-audit/index.d.ts +43 -0
  30. package/src/v2/models/latest-audit/index.js +19 -0
  31. package/src/v2/models/latest-audit/latest-audit.collection.js +32 -0
  32. package/src/v2/models/latest-audit/latest-audit.model.js +26 -0
  33. package/src/v2/models/latest-audit/latest-audit.schema.js +72 -0
  34. package/src/v2/models/opportunity/index.d.ts +17 -1
  35. package/src/v2/models/opportunity/opportunity.schema.js +1 -0
  36. package/src/v2/models/organization/index.d.ts +3 -1
  37. package/src/v2/models/site/index.d.ts +43 -6
  38. package/src/v2/models/site/site.model.js +0 -6
  39. package/src/v2/models/site/site.schema.js +2 -0
  40. package/src/v2/models/site-candidate/index.d.ts +5 -7
  41. package/src/v2/models/site-top-page/index.d.ts +16 -2
  42. package/src/v2/models/suggestion/index.d.ts +2 -0
  43. package/src/v2/util/patcher.js +11 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.61.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.60.3...@adobe/spacecat-shared-data-access-v1.61.0) (2024-12-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * latest audit entity ([#503](https://github.com/adobe/spacecat-shared/issues/503)) ([2d01b09](https://github.com/adobe/spacecat-shared/commit/2d01b0969c0c046cdbffa480f8e40991e5abf91e))
7
+
8
+ # [@adobe/spacecat-shared-data-access-v1.60.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.60.2...@adobe/spacecat-shared-data-access-v1.60.3) (2024-12-22)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update external fixes ([#502](https://github.com/adobe/spacecat-shared/issues/502)) ([6ea4ef2](https://github.com/adobe/spacecat-shared/commit/6ea4ef2889ac665160adc9ebdad38b41b67d0782))
14
+
1
15
  # [@adobe/spacecat-shared-data-access-v1.60.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.60.1...@adobe/spacecat-shared-data-access-v1.60.2) (2024-12-19)
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.60.2",
3
+ "version": "1.61.0",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "engines": {
@@ -34,10 +34,10 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@adobe/spacecat-shared-dynamo": "1.4.0",
38
- "@adobe/spacecat-shared-utils": "1.23.1",
39
- "@aws-sdk/client-dynamodb": "3.712.0",
40
- "@aws-sdk/lib-dynamodb": "3.712.0",
37
+ "@adobe/spacecat-shared-dynamo": "1.4.2",
38
+ "@adobe/spacecat-shared-utils": "1.24.1",
39
+ "@aws-sdk/client-dynamodb": "3.716.0",
40
+ "@aws-sdk/lib-dynamodb": "3.716.0",
41
41
  "@types/joi": "17.2.3",
42
42
  "aws-xray-sdk": "3.10.2",
43
43
  "electrodb": "3.0.1",
@@ -0,0 +1,24 @@
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
+ export default class DataAccessError extends Error {
14
+ constructor(message, details = {}, cause = null) {
15
+ super(message);
16
+ this.name = this.constructor.name;
17
+ this.details = details;
18
+ this.cause = cause;
19
+
20
+ if (cause?.stack) {
21
+ this.stack += `\nCaused by: ${cause.stack}`;
22
+ }
23
+ }
24
+ }
@@ -10,4 +10,8 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export type ValidationError = Error
13
+ export type DataAccessError = Error
14
+ export type SchemaBuilderError = DataAccessError
15
+ export type SchemaError = DataAccessError
16
+ export type SchemaValidationError = DataAccessError
17
+ export type ValidationError = DataAccessError
@@ -10,6 +10,16 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import DataAccessError from './data-access.error.js';
14
+ import SchemaBuilderError from './schema.builder.error.js';
15
+ import SchemaError from './schema.error.js';
16
+ import SchemaValidationError from './schema-validation.error.js';
13
17
  import ValidationError from './validation.error.js';
14
18
 
15
- export { ValidationError };
19
+ export {
20
+ DataAccessError,
21
+ SchemaBuilderError,
22
+ SchemaError,
23
+ SchemaValidationError,
24
+ ValidationError,
25
+ };
@@ -0,0 +1,22 @@
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 DataAccessError from './data-access.error.js';
14
+
15
+ export default class ReferenceError extends DataAccessError {
16
+ constructor(reference, message, cause) {
17
+ const { type, target } = reference;
18
+ const prefix = type && target ? `[${type} -> ${target}] ` : '';
19
+
20
+ super(`${prefix}${message}`, { reference }, cause);
21
+ }
22
+ }
@@ -9,9 +9,7 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- export const INDEX_TYPES = {
13
- PRIMARY: 'primary',
14
- ALL: 'all',
15
- BELONGS_TO: 'belongs_to',
16
- OTHER: 'other',
17
- };
12
+
13
+ import DataAccessError from './data-access.error.js';
14
+
15
+ export default class SchemaValidationError extends DataAccessError {}
@@ -0,0 +1,27 @@
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 { hasText } from '@adobe/spacecat-shared-utils';
14
+
15
+ import DataAccessError from './data-access.error.js';
16
+
17
+ export default class SchemaBuilderError extends DataAccessError {
18
+ constructor(builder, message, cause) {
19
+ const { serviceName, entityName } = builder;
20
+ const prefix = hasText(serviceName) && hasText(entityName)
21
+ ? `[${serviceName} -> ${entityName}] `
22
+ : '';
23
+
24
+ super(`${prefix}${message}`, { builder }, cause);
25
+ this.name = this.constructor.name;
26
+ }
27
+ }
@@ -0,0 +1,19 @@
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 DataAccessError from './data-access.error.js';
14
+
15
+ export default class SchemaError extends DataAccessError {
16
+ constructor(schema, message, cause) {
17
+ super(`[${schema.getModelName()}] ${message}`, { schema }, cause);
18
+ }
19
+ }
@@ -10,4 +10,6 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export default class ValidationError extends Error {}
13
+ import DataAccessError from './data-access.error.js';
14
+
15
+ export default class ValidationError extends DataAccessError {}
@@ -13,6 +13,7 @@
13
13
  import type { BaseCollection, BaseModel } from '../base';
14
14
 
15
15
  export interface ApiKey extends BaseModel {
16
+ getApiKeyId(): string;
16
17
  getDeletedAt(): string | undefined;
17
18
  getExpiresAt(): string | undefined;
18
19
  getHashedApiKey(): string;
@@ -32,6 +33,18 @@ export interface ApiKey extends BaseModel {
32
33
  }
33
34
 
34
35
  export interface ApiKeyCollection extends BaseCollection<ApiKey> {
35
- allByImsOrgIdAndImsUserId: (imsUserId: string, imsOrgId: string) => Promise<ApiKey[]>;
36
- findByHashedApiKey: (hashedApiKey: string) => Promise<ApiKey | null>;
36
+ allByHashedApiKey(hashedApiKey: string): Promise<ApiKey[]>;
37
+ allByHashedApiKeyAndUpdatedAt(hashedApiKey: string, updatedAt: string): Promise<ApiKey[]>;
38
+ allByImsOrgId(imsOrgId: string): Promise<ApiKey[]>;
39
+ allByImsOrgIdAndImsUserId(imsOrgId: string, imsUserId: string): Promise<ApiKey[]>;
40
+ allByImsOrgIdAndImsUserIdAndUpdatedAt(
41
+ imsOrgId: string, imsUserId: string, updatedAt: string
42
+ ): Promise<ApiKey[]>;
43
+ findByHashedApiKey(hashedApiKey: string): Promise<ApiKey | null>;
44
+ findByHashedApiKeyAndUpdatedAt(hashedApiKey: string, updatedAt: string): Promise<ApiKey | null>;
45
+ findByImsOrgId(imsOrgId: string): Promise<ApiKey | null>;
46
+ findByImsOrgIdAndImsUserId(imsOrgId: string, imsUserId: string): Promise<ApiKey | null>;
47
+ findByImsOrgIdAndImsUserIdAndUpdatedAt(
48
+ imsOrgId: string, imsUserId: string, updatedAt: string
49
+ ): Promise<ApiKey | null>;
37
50
  }
@@ -20,7 +20,31 @@ import BaseCollection from '../base/base.collection.js';
20
20
  * @extends BaseCollection
21
21
  */
22
22
  class AuditCollection extends BaseCollection {
23
- // add custom methods here
23
+ // create a copy of the audit as a LatestAudit entity
24
+ async _onCreate(item) {
25
+ const collection = this.entityRegistry.getCollection('LatestAuditCollection');
26
+ await collection.create(item.toJSON());
27
+ }
28
+
29
+ // of the created audits, find the latest per site and auditType
30
+ // and create a LatestAudit copy for each
31
+ async _onCreateMany(items) {
32
+ const collection = this.entityRegistry.getCollection('LatestAuditCollection');
33
+ const latestAudits = items.createdItems.reduce((acc, audit) => {
34
+ const siteId = audit.getSiteId();
35
+ const auditType = audit.getAuditType();
36
+ const auditedAt = audit.getAuditedAt();
37
+ const key = `${siteId}-${auditType}`;
38
+
39
+ if (!acc[key] || acc[key].getAuditedAt() < auditedAt) {
40
+ acc[key] = audit;
41
+ }
42
+
43
+ return acc;
44
+ }, {});
45
+
46
+ await collection.createMany(Object.values(latestAudits).map((audit) => audit.toJSON()));
47
+ }
24
48
  }
25
49
 
26
50
  export default AuditCollection;
@@ -26,7 +26,10 @@ Indexes Doc: https://electrodb.dev/en/modeling/indexes/
26
26
 
27
27
  const schema = new SchemaBuilder(Audit, AuditCollection)
28
28
  .addReference('belongs_to', 'Site', ['auditType', 'auditedAt'])
29
+ .addReference('has_one', 'LatestAudit', ['auditType'], { required: false })
29
30
  .addReference('has_many', 'Opportunities')
31
+ .allowUpdates(false)
32
+ .allowRemove(false)
30
33
  .addAttribute('auditResult', {
31
34
  type: 'any',
32
35
  required: true,
@@ -11,30 +11,35 @@
11
11
  */
12
12
 
13
13
  import type {
14
- BaseCollection, BaseModel, Opportunity, Site,
14
+ BaseCollection, BaseModel, LatestAudit, Opportunity, Site,
15
15
  } from '../index';
16
16
 
17
17
  export interface Audit extends BaseModel {
18
+ getAuditedAt(): number;
19
+ getAuditId(): string;
18
20
  getAuditResult(): object;
19
21
  getAuditType(): string;
20
- getAuditedAt(): number;
21
22
  getFullAuditRef(): string;
22
23
  getIsError(): boolean;
23
24
  getIsLive(): boolean;
25
+ getLatestAudit(): Promise<LatestAudit | null>;
26
+ getLatestAuditByAuditType(auditType: string): Promise<LatestAudit | null>;
24
27
  getOpportunities(): Promise<Opportunity[]>;
28
+ getOpportunitiesByUpdatedAt(updatedAt: string): Promise<Opportunity[]>;
29
+ getScores(): object | undefined;
25
30
  getSite(): Promise<Site>;
26
31
  getSiteId(): string;
27
- setAuditResult(auditResult: object): Audit;
28
- setAuditType(auditType: string): Audit;
29
- setAuditedAt(auditedAt: number): Audit;
30
- setFullAuditRef(fullAuditRef: string): Audit;
31
- setIsError(isError: boolean): Audit;
32
- setIsLive(isLive: boolean): Audit;
33
- setSiteId(siteId: string): Audit;
34
- toggleLive(): Audit;
35
32
  }
36
33
 
37
34
  export interface AuditCollection extends BaseCollection<Audit> {
38
35
  allBySiteId(siteId: string): Promise<Audit[]>;
39
- allBySiteAndType(siteId: string, auditType: string): Promise<Audit[]>;
36
+ allBySiteIdAndAuditType(siteId: string, auditType: string): Promise<Audit[]>;
37
+ allBySiteIdAndAuditTypeAndAuditedAt(
38
+ siteId: string, auditType: string, auditedAt: string
39
+ ): Promise<Audit[]>;
40
+ findBySiteId(siteId: string): Promise<Audit | null>;
41
+ findBySiteIdAndAuditType(siteId: string, auditType: string): Promise<Audit | null>;
42
+ findBySiteIdAndAuditTypeAndAuditedAt(
43
+ siteId: string, auditType: string, auditedAt: string
44
+ ): Promise<Audit | null>;
40
45
  }