Skip to content

Commit

Permalink
Modify AddLikeTermCommandHandler 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 459e577 commit 096b848
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ import CollaboratorRepository, {
import CollaboratorId from '@src/languages/domain/collaborator/collaboratorId';
import Collaborator from '@src/languages/domain/collaborator/collaborator';
import CollaboratorDoesNotExistsException from '@src/languages/domain/collaborator/collaboratorDoesNotExistsException';
import { IDENTITY_PROVIDER, IdentityProvider } from '@src/shared/domain/services/IdentityProvider';

@CommandHandler(AddLikeTermCommand)
export default class AddLikeTermCommandHandler implements ICommandHandler<AddLikeTermCommand> {
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: AddLikeTermCommand): Promise<void> {
const term = await this.getTerm(command.termId);
const collaborator = await this.getCollaborator(command.userId);

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

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

this.termRepository.save(term);

Expand Down
3 changes: 1 addition & 2 deletions src/languages/domain/term/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export default abstract class Term extends AggregateRoot {
this.record(new TermDeletedEvent(this.id.toString(), this.type.toString()));
}

addLike(userId: string, name: string, photo: string): void {
const termLikeId = Uuid.fromString(`${this.id.toString()}${userId}`).toString();
addLike(termLikeId: string, userId: string, name: string, photo: string): void {
const like = new TermLike(TermLikeId.of(termLikeId), CollaboratorId.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,11 +16,13 @@ import { CollaboratorRepositoryMock } from '@test/unit/languages/domain/collabor
import { CollaboratorMother } from '@test/unit/languages/domain/collaborator/collaboratorMother';
import { CollaboratorIdMother } from '@test/unit/languages/domain/collaborator/collaboratorIdMother';
import CollaboratorDoesNotExistsException from '@src/languages/domain/collaborator/collaboratorDoesNotExistsException';
import { IdentityProviderMock } from '@test/unit/shared/domain/services/IdentityProviderMock';

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

const COLLABORATOR_ID = '0a8008d5-ab68-4c10-8476-668b5b540e0f';
Expand All @@ -30,18 +32,20 @@ describe('Given a AddLikeTermCommandHandler to handle', () => {
const prepareDependencies = () => {
termRepository = new TermRepositoryMock();
collaboratorRepository = new CollaboratorRepositoryMock();
identityProvider = new IdentityProviderMock();
eventBus = new EventBusMock();
};

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

jest.useFakeTimers();
};

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

Expand Down Expand Up @@ -174,6 +178,7 @@ describe('Given a AddLikeTermCommandHandler to handle', () => {

function startScenario() {
command = AddLikeTermCommandMother.random({ termId: TERM_ID, userId: COLLABORATOR_ID });

term = WordMother.random({
id: TermIdMother.random(TERM_ID),
likes: [
Expand All @@ -188,6 +193,7 @@ describe('Given a AddLikeTermCommandHandler 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 @@ -229,6 +235,7 @@ describe('Given a AddLikeTermCommandHandler to handle', () => {
photo: PHOTO,
});

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

0 comments on commit 096b848

Please sign in to comment.