Skip to content

Commit 5a30845

Browse files
authored
Criar service para apagar conta da rede social do usuário (#122)
* feat: add models folder with account model * feat: add a general BadRequestError * feat: add DeleteUserAccountsService to account services
1 parent 6bd4331 commit 5a30845

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

pnpm-lock.yaml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type AccountModel = {
2+
avatarUrl: string;
3+
id: string;
4+
socialMediaId?: number;
5+
userId?: string;
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { AccountMock } from '@/shared/test-helpers/mocks/account.mock.js';
3+
import { DeleteUserAccountsService } from './delete-user-accounts-service.js';
4+
import { BadRequestError } from '@/shared/errors/bad-request-error.js';
5+
6+
const makeSut = () => {
7+
const accountRepository = {
8+
deleteAccountsBySocialMediaId: vi.fn(),
9+
getAccounts: vi.fn(),
10+
};
11+
const deleteUserAccountsService = new DeleteUserAccountsService(
12+
accountRepository
13+
);
14+
15+
return { accountRepository, deleteUserAccountsService };
16+
};
17+
18+
describe('DeleteUserAccountsService', () => {
19+
it('deletes accounts by social media id', async () => {
20+
const { accountRepository, deleteUserAccountsService } = makeSut();
21+
const account = AccountMock.create();
22+
23+
await deleteUserAccountsService.execute(account);
24+
25+
expect(
26+
accountRepository.deleteAccountsBySocialMediaId
27+
).toHaveBeenCalledWith(account.socialMediaId);
28+
});
29+
30+
it('throws BadRequestError if socialMediaId is not provided', async () => {
31+
const { deleteUserAccountsService } = makeSut();
32+
const account = AccountMock.create({ socialMediaId: undefined });
33+
34+
await expect(deleteUserAccountsService.execute(account)).rejects.toThrow(
35+
BadRequestError
36+
);
37+
});
38+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Service } from '@/shared/protocols/service.js';
2+
import type { AccountRepository } from '../repositories/account-repository/account-repository.js';
3+
import type { AccountModel } from '../models/account-model.js';
4+
import { BadRequestError } from '@/shared/errors/bad-request-error.js';
5+
6+
export class DeleteUserAccountsService implements Service<AccountModel> {
7+
constructor(private readonly accountRepository: AccountRepository) {}
8+
9+
async execute({ socialMediaId }: AccountModel) {
10+
if (!socialMediaId) throw new BadRequestError('undefined social media id');
11+
await this.accountRepository.deleteAccountsBySocialMediaId(socialMediaId);
12+
}
13+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { HttpStatusCode } from '../protocols/http-client.js';
2+
import { HttpError } from './http-error.js';
3+
4+
export class BadRequestError extends HttpError {
5+
constructor(public readonly message: string) {
6+
super(HttpStatusCode.badRequest, message);
7+
}
8+
9+
public toJSON() {
10+
return {
11+
code: HttpStatusCode.badRequest,
12+
error: this.message,
13+
};
14+
}
15+
}

src/shared/errors/error.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BadRequestError } from './bad-request-error.js';
12
import { HttpError } from './http-error.js';
23
import { UserNotFound } from './user-not-found-error.js';
34
import { ValidationError } from './validation-error.js';
@@ -48,4 +49,15 @@ describe('[Errors]', () => {
4849
});
4950
});
5051
});
52+
53+
describe('bad-request-error', () => {
54+
it('should parse to json correctly', () => {
55+
const error = new BadRequestError('error message');
56+
57+
expect(error.toJSON()).toStrictEqual({
58+
code: 400,
59+
error: 'error message',
60+
});
61+
});
62+
});
5163
});

0 commit comments

Comments
 (0)