Skip to content

Commit

Permalink
fix(mixin): forward options param to find and findById calls (#65)
Browse files Browse the repository at this point in the history
fixes the implementation of updateAll, deleteAll,
updateById and replaceById to forward the options params

GH-64
  • Loading branch information
shubhamp-sf authored May 12, 2023
1 parent 403e6bb commit f5d83cd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions .cz-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
{name: 'provider'},
{name: 'core'},
{name: 'maintenance'},
{name: 'mixin'},
],

appendBranchNameToCommitMessage: true,
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/unit/audit.mixin.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {consoleMessage} from '../acceptance/audit.mixin.acceptance';
import {
MockClass,
mockClassMethodCall,
optionsReceivedByParentRepository,
resetMethodCalls,
} from './fixtures/mockClass';
import {mockData, mockDataArray, resetMockData} from './fixtures/mockData';
Expand Down Expand Up @@ -249,6 +250,21 @@ describe('Audit Mixin', () => {
//check if super class method called
expect(mockClassMethodCall.updateById).to.be.true();
});

it("should forward the options param to base repository's findById method", async () => {
const options = {someTestKey: 'someTestValue'};
await returnedMixedClassInstance.updateById(
mockData.id,
{
itemName: 'replacedTestItemName',
description: 'replacedTestItemDescription',
},
options,
);

// check if findById received the options originally passed to mixined class
expect(optionsReceivedByParentRepository.findById).to.be.eql(options);
});
it('should update record and create appropriate Audit Log on calling updateById method', async () => {
const beforeMockData = Object.assign({}, mockData);

Expand Down Expand Up @@ -339,6 +355,21 @@ describe('Audit Mixin', () => {
expect(mockClassMethodCall.updateAll).to.be.true();
});

it("should forward the options param to base repository's find method", async () => {
const options = {someKey: 'someValue'};
await returnedMixedClassInstance.updateAll(
{
itemName: 'replacedTestItemName',
description: 'replacedTestItemDescription',
},
undefined,
options,
);

// check if find method received the options originally passed to mixined class
expect(optionsReceivedByParentRepository.find).to.be.eql(options);
});

it('should update records and create appropriate Audit Logs on calling updateAll method', async () => {
const beforeMockDataArray = mockDataArray.map(d => {
return d.toObject();
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/unit/fixtures/mockClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class MockClass extends DefaultCrudRepository<
filter?: FilterExcludingWhere<MockModel>,
options?: AnyObject,
): Promise<MockModel> {
optionsReceivedByParentRepository.findById = options;
return new Promise(resolve => {
const mockDataToReturn = Object.assign({}, mockData);
resolve(mockDataToReturn);
Expand Down Expand Up @@ -109,6 +110,7 @@ export class MockClass extends DefaultCrudRepository<
});
}
find(filter?: Filter<MockModel>, options?: AnyObject): Promise<MockModel[]> {
optionsReceivedByParentRepository.find = options;
return new Promise(resolve => {
const mockDataArrayToReturn: MockModel[] = [];
mockDataArray.forEach(data => {
Expand Down Expand Up @@ -158,6 +160,10 @@ export const mockClassMethodCall = {
replaceById: false,
updateAll: false,
};
export const optionsReceivedByParentRepository: {
findById?: AnyObject;
find?: AnyObject;
} = {};
export function resetMethodCalls() {
mockClassMethodCall.create = false;
mockClassMethodCall.createAll = false;
Expand Down
14 changes: 7 additions & 7 deletions src/mixins/audit.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export function AuditRepositoryMixin<
if (options?.noAudit) {
return super.updateAll(dataObject, where, options);
}
const toUpdate = await this.find({where});
const toUpdate = await this.find({where}, options);
const beforeMap = keyBy(toUpdate, d => d.getId());
const updatedCount = await super.updateAll(dataObject, where, options);
const updated = await this.find({where});
const updated = await this.find({where}, options);

if (this.getCurrentUser) {
const user = await this.getCurrentUser();
Expand Down Expand Up @@ -157,7 +157,7 @@ export function AuditRepositoryMixin<
if (options?.noAudit) {
return super.deleteAll(where, options);
}
const toDelete = await this.find({where});
const toDelete = await this.find({where}, options);
const beforeMap = keyBy(toDelete, d => d.getId());
const deletedCount = await super.deleteAll(where, options);

Expand Down Expand Up @@ -202,7 +202,7 @@ export function AuditRepositoryMixin<
if (options?.noAudit) {
return super.updateById(id, data, options);
}
const before = await this.findById(id);
const before = await this.findById(id, undefined, options);
// loopback repository internally calls updateAll so we don't want to create another log
if (options) {
options.noAudit = true;
Expand Down Expand Up @@ -250,9 +250,9 @@ export function AuditRepositoryMixin<
if (options?.noAudit) {
return super.replaceById(id, data, options);
}
const before = await this.findById(id);
const before = await this.findById(id, undefined, options);
await super.replaceById(id, data, options);
const after = await this.findById(id);
const after = await this.findById(id, undefined, options);

if (this.getCurrentUser) {
const user = await this.getCurrentUser();
Expand Down Expand Up @@ -287,7 +287,7 @@ export function AuditRepositoryMixin<
if (options?.noAudit) {
return super.deleteById(id, options);
}
const before = await this.findById(id);
const before = await this.findById(id, undefined, options);
await super.deleteById(id, options);

if (this.getCurrentUser) {
Expand Down

0 comments on commit f5d83cd

Please sign in to comment.