Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repository): implement undo soft delete feature #185

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/__tests__/unit/repository/soft-crud.repository.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('SoftCrudRepository', () => {
const customers = await repo.find();
expect(customers).to.have.length(3);
});

it('should find non soft deleted entries with and operator', async () => {
const customers = await repo.find({
where: {
Expand Down Expand Up @@ -793,6 +794,80 @@ describe('SoftCrudRepository', () => {
});
});

describe('undoSoftDelete', () => {
beforeEach(setupTestData);
afterEach(clearTestData);

it('should undo soft deleted entry by id', async () => {
await repo.undoSoftDeleteById(3);
const customer = await repo.findById(3);
const customers = await repo.find();
expect(customer.deleted).to.false();
expect(customers).to.have.length(4);
yeshamavani marked this conversation as resolved.
Show resolved Hide resolved
});

it('should check deletedOn flag is undefined after undo', async () => {
const softDeletedCustomer = await repo.findByIdIncludeSoftDelete(3);
expect(softDeletedCustomer.deletedOn).to.Date();
await repo.undoSoftDeleteById(3);
const customer = await repo.findById(3);
expect(customer.deletedOn).to.undefined();
});

it('should undo all soft deleted entries', async () => {
await repo.deleteAll();
await repo.undoSoftDeleteAll();
const customers = await repo.find();
expect(customers).to.have.length(4);
});

it('should undo soft deleted entries with and operator', async () => {
await repo.undoSoftDeleteAll({
and: [{email: 'alice@example.com'}, {id: 3}],
});
const customers = await repo.find({
where: {
and: [
{
email: 'alice@example.com',
},
{
id: 3,
},
],
},
});
expect(customers).to.have.length(1);
});

it('should undo soft deleted entries with or operator', async () => {
await repo.deleteAll({email: 'john@example.com'});
await repo.undoSoftDeleteAll({
or: [
{
email: 'john@example.com',
},
{
email: 'alice@example.com',
},
],
});
const customers = await repo.find({
where: {
or: [
{
email: 'john@example.com',
},
{
email: 'alice@example.com',
},
],
},
});
expect(customers).to.have.length(2);
});
});

describe('deleteAll', () => {
beforeEach(setupTestData);
afterEach(clearTestData);
Expand Down
31 changes: 29 additions & 2 deletions src/repositories/soft-crud.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import {Getter} from '@loopback/core';
import {
Condition,
DataObject,
DefaultCrudRepository,
Entity,
Filter,
juggler,
Where,
Condition,
juggler,
} from '@loopback/repository';
import {Count} from '@loopback/repository/src/common-types';
import {HttpErrors} from '@loopback/rest';
Expand Down Expand Up @@ -168,6 +168,33 @@ export abstract class SoftCrudRepository<
);
}

/**
* Method to perform undo the soft delete by Id.
* @param id
* @param options
*/
async undoSoftDeleteById(id: ID, options?: Options): Promise<void> {
await this.undoSoftDeleteAll({id} as Where<E>, options);
}

/**
* Method to perform undo all the soft deletes
* @param where
* @param options
*/
async undoSoftDeleteAll(where?: Where<E>, options?: Options): Promise<Count> {
const filter = new SoftFilterBuilder({where})
.imposeCondition({
deleted: true,
} as Condition<E>)
.build();
return super.updateAll(
{deleted: false, deletedOn: undefined},
filter.where,
options,
);
}

/**
* Method to perform hard delete of entries. Take caution.
* @param entity
Expand Down