Skip to content

Commit

Permalink
feat: add DeleteUserAccountsService to account services
Browse files Browse the repository at this point in the history
  • Loading branch information
melo-zip committed Jun 13, 2024
1 parent 962885a commit 7b0801c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/features/account/services/delete-user-accounts-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it, vi } from 'vitest';
import { AccountMock } from '@/shared/test-helpers/mocks/account.mock.js';
import { DeleteUserAccountsService } from './delete-user-accounts-service.js';
import { BadRequestError } from '@/shared/errors/bad-request-error.js';

const makeSut = () => {
const accountRepository = {
deleteAccountsBySocialMediaId: vi.fn(),
getAccounts: vi.fn(),
};
const deleteUserAccountsService = new DeleteUserAccountsService(
accountRepository
);

return { accountRepository, deleteUserAccountsService };
};

describe('DeleteUserAccountsService', () => {
it('deletes accounts by social media id', async () => {
const { accountRepository, deleteUserAccountsService } = makeSut();
const account = AccountMock.create();

await deleteUserAccountsService.execute(account);

expect(
accountRepository.deleteAccountsBySocialMediaId
).toHaveBeenCalledWith(account.socialMediaId);
});

it('throws BadRequestError if socialMediaId is not provided', async () => {
const { deleteUserAccountsService } = makeSut();
const account = AccountMock.create({ socialMediaId: undefined });

await expect(deleteUserAccountsService.execute(account)).rejects.toThrow(
BadRequestError
);
});
});
13 changes: 13 additions & 0 deletions src/features/account/services/delete-user-accounts-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Service } from '@/shared/protocols/service.js';
import type { AccountRepository } from '../repositories/account-repository/account-repository.js';
import type { AccountModel } from '../models/account-model.js';
import { BadRequestError } from '@/shared/errors/bad-request-error.js';

export class DeleteUserAccountsService implements Service<AccountModel> {
constructor(private readonly accountRepository: AccountRepository) {}

async execute({ socialMediaId }: AccountModel) {
if (!socialMediaId) throw new BadRequestError('undefined social media id');
await this.accountRepository.deleteAccountsBySocialMediaId(socialMediaId);
}
}

0 comments on commit 7b0801c

Please sign in to comment.