Skip to content

Commit

Permalink
fix: modify to ensure softDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
jayudoye committed Mar 1, 2025
1 parent d96c317 commit 437f8a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/modules/squeeze/squeeze.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { CustomHttpException } from '@shared/helpers/custom-http-filter';
export class SqueezeService {
constructor(
@InjectRepository(Squeeze)
private readonly SqueezeRepository: Repository<Squeeze>
private readonly squeezeRepository: Repository<Squeeze>
) {}

async create(createSqueezeDto: SqueezeRequestDto) {
try {
const mapNewSqueeze = CreateSqueezeMapper.mapToEntity(createSqueezeDto);

const existingSqueeze = await this.SqueezeRepository.findOne({
const existingSqueeze = await this.squeezeRepository.findOne({
where: {
email: mapNewSqueeze.email,
},
Expand All @@ -41,10 +41,10 @@ export class SqueezeService {
});
}

const newSqueeze = this.SqueezeRepository.create({
const newSqueeze = this.squeezeRepository.create({
...mapNewSqueeze,
});
await this.SqueezeRepository.save(newSqueeze);
await this.squeezeRepository.save(newSqueeze);
const mappedResponse = SqueezeMapper.mapToResponseFormat(newSqueeze);
return {
status: 'success',
Expand All @@ -63,7 +63,7 @@ export class SqueezeService {

async updateSqueeze(updateDto: UpdateSqueezeDto) {
try {
const squeeze = await this.SqueezeRepository.findOneBy({ email: updateDto.email });
const squeeze = await this.squeezeRepository.findOneBy({ email: updateDto.email });

if (!squeeze) {
throw new NotFoundException({
Expand All @@ -80,7 +80,7 @@ export class SqueezeService {
}

Object.assign(squeeze, updateDto);
const updatedSqueeze = await this.SqueezeRepository.save(squeeze);
const updatedSqueeze = await this.squeezeRepository.save(squeeze);
return updatedSqueeze;
} catch (err) {
if (this.isInstanceOfAny(err, [ForbiddenException, NotFoundException])) {
Expand All @@ -99,7 +99,7 @@ export class SqueezeService {
}

async deleteSqueeze(squeezeId: string, authenticatedSqueezeId: string): Promise<any> {
const squeeze = await this.SqueezeRepository.findOne({
const squeeze = await this.squeezeRepository.findOne({
where: { id: squeezeId },
});

Expand All @@ -111,7 +111,7 @@ export class SqueezeService {
throw new CustomHttpException('You are not authorized to delete this squeeze', HttpStatus.UNAUTHORIZED);
}

await this.SqueezeRepository.delete(squeezeId);
await this.squeezeRepository.softDelete(squeezeId);

return {
status: 'success',
Expand Down
6 changes: 3 additions & 3 deletions src/modules/squeeze/tests/squeeze.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('SqueezeService', () => {
findOne: jest.fn(),
findAndCount: jest.fn(),
count: jest.fn(),
delete: jest.fn(),
softDelete: jest.fn(),
};

beforeEach(async () => {
Expand Down Expand Up @@ -116,14 +116,14 @@ describe('SqueezeService', () => {
const squeezeToDelete = { id: squeezeId, email: '[email protected]' };

mockSqueezeRepository.findOne.mockResolvedValueOnce(squeezeToDelete);
mockSqueezeRepository.delete.mockResolvedValueOnce({ affected: 1 });
mockSqueezeRepository.softDelete.mockResolvedValueOnce({ affected: 1 });

const result = await service.deleteSqueeze(squeezeId, authenticatedSqueezeId);

expect(result.status).toBe('success');
expect(result.message).toBe('Deletion in progress');
expect(mockSqueezeRepository.findOne).toHaveBeenCalledWith({ where: { id: squeezeId } });
expect(mockSqueezeRepository.delete).toHaveBeenCalledWith(squeezeId);
expect(mockSqueezeRepository.softDelete).toHaveBeenCalledWith(squeezeId);
});

it('should throw an error if squeeze is not found', async () => {
Expand Down

0 comments on commit 437f8a5

Please sign in to comment.