Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 137 #139

Merged
merged 8 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export class AccountRepository {

findAccountsByUserId(id: string) {
return database.account.findMany({
select: {
avatarUrl: true,
id: true,
socialMedia: {
select: {
id: true,
name: true,
},
},
},
where: {
userId: id,
},
Expand Down
12 changes: 9 additions & 3 deletions src/features/account/services/get-user-accounts-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AccountModel } from '@/features/account/models/account-model';
import type { AccountRepository } from '@/features/account/repositories/account-repository/account-repository';
import type { UserRepository } from '@/features/user/repositories/user-repository';
import { UserNotFound } from '@/shared/errors/user-not-found-error';
Expand All @@ -9,7 +8,14 @@ type Input = {
};

type Output = {
accounts: AccountModel[];
accounts: {
avatarUrl: string;
id: string;
socialMedia: {
id: number;
name: string;
} | null;
}[];
};

export class GetUserAccountsService implements Service<Input, Output> {
Expand All @@ -28,7 +34,7 @@ export class GetUserAccountsService implements Service<Input, Output> {
const accounts = await this.accountRepository.findAccountsByUserId(user.id);

return {
accounts: accounts,
accounts,
};
}
}
68 changes: 66 additions & 2 deletions src/features/user/controllers/user-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { randomUUID } from 'crypto';
import type { Request, Response } from 'express';
import { mock, mockDeep } from 'vitest-mock-extended';

import { GetUserAccountsService } from '@/features/account/services/get-user-accounts-service';
import { UserCreateService } from '@/features/user/services/user-create-service';
import { UserFindByIdService } from '@/features/user/services/user-find-by-id-service';
import { HttpError } from '@/shared/errors/http-error';
import { UserNotFound } from '@/shared/errors/user-not-found-error';
import { HttpStatusCode } from '@/shared/protocols/http-client';
import { accountRepositoryMock } from '@/shared/test-helpers/mocks/repositories/account-repository.mock';
import { userRepositoryMock } from '@/shared/test-helpers/mocks/repositories/user-repository.mock';

import { UserController } from './user-controller';
Expand All @@ -20,16 +24,25 @@ const makeSut = () => {
new UserFindByIdService(userRepositoryMock)
);

const mockGetUserAccountsService = mock<GetUserAccountsService>(
new GetUserAccountsService(userRepositoryMock, accountRepositoryMock)
);

const userController = new UserController(
mockUserCreateService,
mockUserFindByIdService
mockUserFindByIdService,
mockGetUserAccountsService
);

const req = mockDeep<Request>();
const res = mockDeep<Response>();
const res = {
json: vi.fn(),
status: vi.fn().mockReturnThis(),
} as unknown as Response;
const next = vi.fn();

return {
getUserAccountsService: mockGetUserAccountsService,
next,
req,
res,
Expand Down Expand Up @@ -132,4 +145,55 @@ describe('[Controllers] UserController', () => {
expect(next).toHaveBeenCalledWith(expect.any(Error));
});
});

describe('getAccounts', () => {
it('should call next with an error if user not found', async () => {
const { getUserAccountsService, next, req, res, userController } =
makeSut();

vi.spyOn(getUserAccountsService, 'execute').mockRejectedValueOnce(
new UserNotFound()
);

const uuid = randomUUID();
req.params = {
id: uuid,
};

await userController.getAccounts(req, res, next);

expect(next).toHaveBeenCalledWith(new Error('User not found'));
});

it('should return accounts from user', async () => {
const { getUserAccountsService, next, req, res, userController } =
makeSut();

const accounts = [
{
avatarUrl: 'avatar-url',
id: 'id1',
socialMedia: { id: 1, name: 'social-name' },
},
{
avatarUrl: 'avatar-url',
id: 'id2',
socialMedia: { id: 2, name: 'social-name' },
},
];
vi.spyOn(getUserAccountsService, 'execute').mockResolvedValueOnce({
accounts,
});

const uuid = randomUUID();
req.params = {
id: uuid,
};
await userController.getAccounts(req, res, next);

expect(res.status).toHaveBeenCalledWith(HttpStatusCode.ok);
expect(res.json).toHaveBeenCalledWith(accounts);
expect(next).not.toHaveBeenCalled();
});
});
});
12 changes: 7 additions & 5 deletions src/features/user/controllers/user-controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { RequestHandler } from 'express';

import type { GetUserAccountsService } from '@/features/account/services/get-user-accounts-service';
import type { UserCreateService } from '@/features/user/services/user-create-service';
import type { UserFindByIdService } from '@/features/user/services/user-find-by-id-service';
import {
Expand Down Expand Up @@ -30,11 +29,13 @@ export class UserController implements Controller {
}
};

getAccounts: RequestHandler = (req, res, next) => {
getAccounts: AsyncRequestHandler = async (req, res, next) => {
try {
const { id } = userFindByIdParamsSchema.parse(req.params);

return res.status(HttpStatusCode.ok).json({ id });
const { accounts } = await this.getUserAccountsService.execute({ id });

return res.status(HttpStatusCode.ok).json(accounts);
} catch (error) {
next(error);
}
Expand All @@ -56,6 +57,7 @@ export class UserController implements Controller {

constructor(
private serviceCreate: UserCreateService,
private serviceFindById: UserFindByIdService
private serviceFindById: UserFindByIdService,
private getUserAccountsService: GetUserAccountsService
) {}
}
10 changes: 9 additions & 1 deletion src/features/user/routes/user-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* istanbul ignore file -- @preserve */
import { AccountRepository } from '@/features/account/repositories/account-repository/account-repository';
import { GetUserAccountsService } from '@/features/account/services/get-user-accounts-service';
import { UserFindByIdService } from '@/features/user/services/user-find-by-id-service';
import { BcryptAdapter } from '@/shared/infra/crypto/bcrypt-adapter';

Expand All @@ -8,15 +10,21 @@ import { UserCreateService } from '../services/user-create-service';

export function userControllerFactory() {
const userRepository = new UserRepository();
const accountRepository = new AccountRepository();
const bcryptAdapter = new BcryptAdapter();
const userServiceCreate = new UserCreateService(
userRepository,
bcryptAdapter
);
const userServiceFindById = new UserFindByIdService(userRepository);
const getUserAccountsService = new GetUserAccountsService(
userRepository,
accountRepository
);
const userController = new UserController(
userServiceCreate,
userServiceFindById
userServiceFindById,
getUserAccountsService
);

return { userController };
Expand Down
Loading