Skip to content

Commit

Permalink
fix(repository): added checks for fields passed as an array in case o…
Browse files Browse the repository at this point in the history
…f findbyid (#122)

added checks for fields passed as an array in function findById.

GH-117
  • Loading branch information
prernagp committed Feb 14, 2023
1 parent d125f02 commit 2a66e59
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/__tests__/unit/repository/soft-crud.repository.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ describe('SoftCrudRepository', () => {
expect(e.message).to.be.equal('EntityNotFound');
}
});
it('should not return soft deleted entry by id, without using deleted in fields filter(fields fileter is passed as array)', async () => {
try {
await repo.findById(3, {
fields: ['id', 'email'],
});
fail();
} catch (e) {
expect(e.message).to.be.equal('EntityNotFound');
}
});
it('should return requested fields only when not using deleted in fields filter', async () => {
const customer = await repo.findById(4, {
fields: {
Expand All @@ -385,6 +395,18 @@ describe('SoftCrudRepository', () => {
});
expect(customer).to.have.property('deleted');
});
it('should return requested fields only when not using deleted in fields filter array', async () => {
const customer = await repo.findById(4, {
fields: ['id', 'email'],
});
expect(customer).to.not.have.property('deleted');
});
it('should return requested fields matched with fields filter array', async () => {
const customer = await repo.findById(4, {
fields: ['id', 'email', 'deleted'],
});
expect(customer).to.have.property('deleted');
});
});

describe('findByIdIncludeSoftDelete', () => {
Expand Down
44 changes: 36 additions & 8 deletions src/repositories/soft-crud.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,46 @@ export abstract class SoftCrudRepository<
[pk]: id,
} as Condition<T>;
}
const finalFilter: Filter<T> = {};
Object.assign(finalFilter, filter);
let finalFilter: Filter<T> = {};
//In case of array of fields, we need to copy the array
// by value and not by reference
finalFilter = {
...filter,
fields:
filter?.fields && Array.isArray(filter.fields)
? [...filter.fields]
: filter.fields,
};
if (finalFilter.fields) {
finalFilter.fields = {
...finalFilter.fields,
deleted: true,
};
if (Array.isArray(finalFilter.fields)) {
const fields = finalFilter.fields as Extract<
keyof SoftDeleteEntity,
string
>[];
if (!fields.includes('deleted')) {
fields.push('deleted');
}
} else {
finalFilter.fields = {
...finalFilter.fields,
deleted: true,
};
}
}
const entity = await super.findById(id, finalFilter, options);
if (entity && !entity.deleted) {
if (filter.fields && !(filter.fields as AnyObject).deleted) {
delete entity.deleted;
if (filter.fields) {
if (Array.isArray(filter.fields)) {
const temp = filter.fields as Extract<
keyof SoftDeleteEntity,
string
>[];
if (!temp.includes('deleted')) {
delete entity.deleted;
}
} else if (!(filter.fields as AnyObject).deleted) {
delete entity.deleted;
}
}
return entity;
} else {
Expand Down

0 comments on commit 2a66e59

Please sign in to comment.