Skip to content

Commit

Permalink
Modify DislikeTermCommandHandler to use IdentityProvider #181
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Jan 18, 2025
1 parent 096b848 commit a770407
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ import CollaboratorRepository, {
import Collaborator from '@src/languages/domain/collaborator/collaborator';
import CollaboratorId from '@src/languages/domain/collaborator/collaboratorId';
import CollaboratorDoesNotExistsException from '@src/languages/domain/collaborator/collaboratorDoesNotExistsException';
import { IDENTITY_PROVIDER, IdentityProvider } from '@src/shared/domain/services/IdentityProvider';

@CommandHandler(DislikeTermCommand)
export default class DislikeTermCommandHandler implements ICommandHandler<DislikeTermCommand> {
constructor(
@Inject(TERM_REPOSITORY) private readonly termRepository: TermRepository,
@Inject(COLLABORATOR_REPOSITORY) private readonly collaboratorRepository: CollaboratorRepository,
@Inject(IDENTITY_PROVIDER) private readonly identityProvider: IdentityProvider,
@Inject(ASYNC_EVENT_BUS) private readonly eventBus: EventBus,
) {}

async execute(command: DislikeTermCommand): Promise<void> {
const term = await this.getTerm(command.termId);
const collaborator = await this.getCollaborator(command.userId);

term.dislike(command.userId, collaborator.getName(), collaborator.getPhoto());
const userId = command.userId;
const id = this.identityProvider.generateFromValue(`${term.getId().toString()}${userId}`);

term.dislike(id, userId, collaborator.getName(), collaborator.getPhoto());

this.termRepository.save(term);

Expand Down
4 changes: 1 addition & 3 deletions src/languages/domain/term/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TermLike from '@src/languages/domain/term/termLike';
import TermLikeAddedEvent from '@src/languages/domain/term/termLikeAddedEvent';
import TermDislikedEvent from '@src/languages/domain/term/termDislikedEvent';
import TermLikeId from '@src/languages/domain/term/termLikeId';
import { Uuid } from '@src/shared/domain/valueObjects/uuid';
import TermDeletedEvent from './termDeletedEvent';
import CollaboratorId from '@src/languages/domain/collaborator/collaboratorId';

Expand Down Expand Up @@ -63,8 +62,7 @@ export default abstract class Term extends AggregateRoot {
this.record(new TermLikeAddedEvent(this.id.toString(), termLike.userId, termLike.name, termLike.photo));
}

dislike(userId: string, name: string, photo: string): void {
const termLikeId = Uuid.fromString(`${this.id.toString()}${userId}`).toString();
dislike(termLikeId: string, userId: string, name: string, photo: string): void {
const like = new TermLike(TermLikeId.of(termLikeId), UserId.of(userId), this.id, name, photo);

if (!this.hasLike(like)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import { CollaboratorRepositoryMock } from '@test/unit/languages/domain/collabor
import CollaboratorDoesNotExistsException from '@src/languages/domain/collaborator/collaboratorDoesNotExistsException';
import { CollaboratorIdMother } from '@test/unit/languages/domain/collaborator/collaboratorIdMother';
import { CollaboratorMother } from '@test/unit/languages/domain/collaborator/collaboratorMother';
import { IdentityProviderMock } from '@test/unit/shared/domain/services/IdentityProviderMock';

describe('Given a DislikeTermCommandHandler to handle', () => {
let termRepository: TermRepositoryMock;
let collaboratorRepository: CollaboratorRepositoryMock;
let identityProvider: IdentityProviderMock;
let eventBus: EventBusMock;
let handler: DislikeTermCommandHandler;

Expand All @@ -30,18 +32,20 @@ describe('Given a DislikeTermCommandHandler to handle', () => {
const prepareDependencies = () => {
termRepository = new TermRepositoryMock();
collaboratorRepository = new CollaboratorRepositoryMock();
identityProvider = new IdentityProviderMock();
eventBus = new EventBusMock();
};

const initHandler = () => {
handler = new DislikeTermCommandHandler(termRepository, collaboratorRepository, eventBus);
handler = new DislikeTermCommandHandler(termRepository, collaboratorRepository, identityProvider, eventBus);

jest.useFakeTimers();
};

const clean = () => {
termRepository.clean();
collaboratorRepository.clean();
identityProvider.clean();
eventBus.clean();
};

Expand Down Expand Up @@ -188,6 +192,7 @@ describe('Given a DislikeTermCommandHandler to handle', () => {
});
const collaborator = CollaboratorMother.random({ id: CollaboratorIdMother.random(COLLABORATOR_ID) });

identityProvider.add(TERM_LIKE_ID);
termRepository.add(term);
collaboratorRepository.add(collaborator);
}
Expand Down Expand Up @@ -230,6 +235,7 @@ describe('Given a DislikeTermCommandHandler to handle', () => {
});
const collaborator = CollaboratorMother.random({ id: CollaboratorIdMother.random(COLLABORATOR_ID) });

identityProvider.add(TERM_LIKE_ID);
termRepository.add(term);
collaboratorRepository.add(collaborator);
}
Expand Down

0 comments on commit a770407

Please sign in to comment.