From 139f6827ee6ff5ea236ba3a1c4c75236452d5b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Tue, 7 Jan 2025 10:59:09 +0100 Subject: [PATCH] fix: allByAuditType --- .../src/v2/models/base/base.collection.js | 3 +-- .../models/latest-audit/latest-audit.collection.js | 6 ++++++ .../src/v2/util/accessor.utils.js | 7 +++++-- .../latest-audit/latest-audit.collection.test.js | 14 ++++++++++++++ .../test/unit/v2/util/accessor.utils.test.js | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js b/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js index 35413967..88a583f5 100755 --- a/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js +++ b/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js @@ -284,8 +284,7 @@ class BaseCollection { * is not found. * @async */ - async allByIndexKeys(indexKeys, options = {}) { - const keys = { pk: entityNameToAllPKValue(this.entityName), ...indexKeys }; + async allByIndexKeys(keys, options = {}) { return this.#queryByIndexKeys(keys, options); } diff --git a/packages/spacecat-shared-data-access/src/v2/models/latest-audit/latest-audit.collection.js b/packages/spacecat-shared-data-access/src/v2/models/latest-audit/latest-audit.collection.js index 3c65e0c1..611ebf91 100755 --- a/packages/spacecat-shared-data-access/src/v2/models/latest-audit/latest-audit.collection.js +++ b/packages/spacecat-shared-data-access/src/v2/models/latest-audit/latest-audit.collection.js @@ -21,6 +21,12 @@ import { guardId, guardString } from '../../util/index.js'; * @extends AuditCollection */ class LatestAuditCollection extends BaseCollection { + async allByAuditType(auditType) { + guardString('auditType', auditType, this.entityName); + + return this.all({ auditType }); + } + async findById(siteId, auditType) { guardId('siteId', siteId, this.entityName); guardString('auditType', auditType, this.entityName); diff --git a/packages/spacecat-shared-data-access/src/v2/util/accessor.utils.js b/packages/spacecat-shared-data-access/src/v2/util/accessor.utils.js index 70e86d45..984232a0 100644 --- a/packages/spacecat-shared-data-access/src/v2/util/accessor.utils.js +++ b/packages/spacecat-shared-data-access/src/v2/util/accessor.utils.js @@ -103,6 +103,10 @@ export function createAccessor(config) { /* eslint-disable no-underscore-dangle }); } + if (context[name]) { + return; + } + const foreignKeys = { ...isNonEmptyObject(foreignKey) && { [foreignKey.name]: foreignKey.value }, }; @@ -150,9 +154,8 @@ export function createAccessor(config) { /* eslint-disable no-underscore-dangle ); } -export function createAccessors(configs, log) { +export function createAccessors(configs) { configs.forEach((config) => { createAccessor(config); - log.debug(`Created accessor ${config.name} for ${config.context.schema.getModelName()} to ${config.collection.schema.getModelName()}`); }); } diff --git a/packages/spacecat-shared-data-access/test/unit/v2/models/latest-audit/latest-audit.collection.test.js b/packages/spacecat-shared-data-access/test/unit/v2/models/latest-audit/latest-audit.collection.test.js index 9d69267d..8e93f257 100755 --- a/packages/spacecat-shared-data-access/test/unit/v2/models/latest-audit/latest-audit.collection.test.js +++ b/packages/spacecat-shared-data-access/test/unit/v2/models/latest-audit/latest-audit.collection.test.js @@ -60,6 +60,20 @@ describe('LatestAuditCollection', () => { }); }); + describe('allByAuditType', () => { + it('returns all latest audits by audit type', async () => { + const auditType = 'lhs-mobile'; + + instance.all = stub().resolves([mockRecord]); + + const audits = await instance.allByAuditType(auditType); + + expect(audits).to.be.an('array'); + expect(audits.length).to.equal(1); + expect(instance.all).to.have.been.calledWithExactly({ auditType }); + }); + }); + describe('findById', () => { it('finds latest audit by id', async () => { const siteId = '78fec9c7-2141-4600-b7b1-ea5c78752b91'; diff --git a/packages/spacecat-shared-data-access/test/unit/v2/util/accessor.utils.test.js b/packages/spacecat-shared-data-access/test/unit/v2/util/accessor.utils.test.js index 3ec7872c..21638700 100755 --- a/packages/spacecat-shared-data-access/test/unit/v2/util/accessor.utils.test.js +++ b/packages/spacecat-shared-data-access/test/unit/v2/util/accessor.utils.test.js @@ -109,6 +109,20 @@ describe('Accessor Utils', () => { /* eslint-disable no-underscore-dangle */ expect(mockContext._accessorCache).to.deep.equal({ a: 1 }); }); + + it('does not create accessor if context already has a function with the same name', async () => { + const config = { + collection: mockCollection, + context: { test: () => {} }, + name: 'test', + requiredKeys: ['test'], + }; + + createAccessor(config); + + expect(mockCollection.schema.getAttribute).to.not.have.been.called; + expect(mockCollection.findByIndexKeys).to.not.have.been.called; + }); }); describe('call accessor', () => {