Skip to content

Commit

Permalink
fix: allByAuditType
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Jan 7, 2025
1 parent 13e221e commit 139f682
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
};
Expand Down Expand Up @@ -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()}`);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 139f682

Please sign in to comment.