Skip to content

Commit

Permalink
Criar service para apagar conta da rede social do usuário (#122)
Browse files Browse the repository at this point in the history
* feat: add models folder with account model

* feat: add a general BadRequestError

* feat: add DeleteUserAccountsService to account services
  • Loading branch information
melo-zip authored Jun 13, 2024
1 parent 6bd4331 commit 5a30845
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/features/account/models/account-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type AccountModel = {
avatarUrl: string;
id: string;
socialMediaId?: number;
userId?: string;
};
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);
}
}
15 changes: 15 additions & 0 deletions src/shared/errors/bad-request-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpStatusCode } from '../protocols/http-client.js';
import { HttpError } from './http-error.js';

export class BadRequestError extends HttpError {
constructor(public readonly message: string) {
super(HttpStatusCode.badRequest, message);
}

public toJSON() {
return {
code: HttpStatusCode.badRequest,
error: this.message,
};
}
}
12 changes: 12 additions & 0 deletions src/shared/errors/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BadRequestError } from './bad-request-error.js';
import { HttpError } from './http-error.js';
import { UserNotFound } from './user-not-found-error.js';
import { ValidationError } from './validation-error.js';
Expand Down Expand Up @@ -48,4 +49,15 @@ describe('[Errors]', () => {
});
});
});

describe('bad-request-error', () => {
it('should parse to json correctly', () => {
const error = new BadRequestError('error message');

expect(error.toJSON()).toStrictEqual({
code: 400,
error: 'error message',
});
});
});
});

0 comments on commit 5a30845

Please sign in to comment.