Skip to content

Commit

Permalink
Merge pull request #1307 from shemigam1/feat-testimonials_soft_delete
Browse files Browse the repository at this point in the history
Feat: testimonials soft delete
  • Loading branch information
TheCodeGhinux authored Mar 1, 2025
2 parents e0d6091 + a5bad36 commit 187cae5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/modules/testimonials/entities/testimonials.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { Column, Entity, ManyToOne, DeleteDateColumn } from 'typeorm';
import { AbstractBaseEntity } from '../../../entities/base.entity';
import { User } from '../../user/entities/user.entity';

Expand All @@ -12,4 +12,7 @@ export class Testimonial extends AbstractBaseEntity {

@Column({ nullable: false })
content: string;

@DeleteDateColumn()
deletedAt: Date;
}
3 changes: 2 additions & 1 deletion src/modules/testimonials/testimonials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class TestimonialsService {
const testimonial = await this.testimonialRepository.findOne({
where: { id: testimonialId },
relations: ['user'],
withDeleted: false,
});

if (!testimonial) {
Expand Down Expand Up @@ -155,7 +156,7 @@ export class TestimonialsService {
if (!testimonial) {
throw new CustomHttpException('Testimonial not found', HttpStatus.NOT_FOUND);
}
await this.testimonialRepository.remove(testimonial);
await this.testimonialRepository.softDelete(id);
return {
message: 'Testimonial deleted successfully',
status_code: HttpStatus.OK,
Expand Down
5 changes: 5 additions & 0 deletions src/modules/testimonials/tests/mocks/testimonials.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const testimonialsMock: Testimonial[] = [
content: 'Excellent Work!',
created_at: new Date(),
updated_at: new Date(),
deletedAt: null,
},
{
id: '2',
Expand All @@ -17,6 +18,7 @@ export const testimonialsMock: Testimonial[] = [
content: 'Excellent Work! Highly Recommend',
created_at: new Date(),
updated_at: new Date(),
deletedAt: null,
},
{
id: '3',
Expand All @@ -25,6 +27,7 @@ export const testimonialsMock: Testimonial[] = [
content: 'Organized and quality work!',
created_at: new Date(),
updated_at: new Date(),
deletedAt: null,
},
{
id: '4',
Expand All @@ -33,6 +36,7 @@ export const testimonialsMock: Testimonial[] = [
content: 'Excellent Work!',
created_at: new Date(),
updated_at: new Date(),
deletedAt: null,
},
{
id: '5',
Expand All @@ -41,5 +45,6 @@ export const testimonialsMock: Testimonial[] = [
content: 'Highly Recommend!',
created_at: new Date(),
updated_at: new Date(),
deletedAt: null,
},
];
8 changes: 4 additions & 4 deletions src/modules/testimonials/tests/testimonials.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ describe('TestimonialsService', () => {
mockTestimonial.id = testimonialId;

jest.spyOn(testimonialRepository, 'findOne').mockResolvedValue(mockTestimonial);
jest.spyOn(testimonialRepository, 'remove').mockResolvedValue(undefined);
jest.spyOn(testimonialRepository, 'softDelete').mockResolvedValue({ affected: 1 } as any);

const result = await service.deleteTestimonial(testimonialId);

expect(testimonialRepository.findOne).toHaveBeenCalledWith({ where: { id: testimonialId } });
expect(testimonialRepository.remove).toHaveBeenCalledWith(mockTestimonial);
expect(testimonialRepository.softDelete).toHaveBeenCalledWith(mockTestimonial.id);
expect(result).toEqual({
message: 'Testimonial deleted successfully',
status_code: HttpStatus.OK,
Expand All @@ -174,14 +174,14 @@ describe('TestimonialsService', () => {
const id = 'non_existent_id';

jest.spyOn(testimonialRepository, 'findOne').mockResolvedValue(null);
jest.spyOn(testimonialRepository, 'remove').mockImplementation(jest.fn());
jest.spyOn(testimonialRepository, 'softDelete').mockImplementation(jest.fn());

await expect(service.deleteTestimonial(id)).rejects.toThrow(
new CustomHttpException('Testimonial not found', HttpStatus.NOT_FOUND)
);

expect(testimonialRepository.findOne).toHaveBeenCalledWith({ where: { id } });
expect(testimonialRepository.remove).not.toHaveBeenCalled();
expect(testimonialRepository.softDelete).not.toHaveBeenCalled();
});
});
});

0 comments on commit 187cae5

Please sign in to comment.