Skip to content

Commit

Permalink
Merge pull request #115 from mapeveri/feat/112-add-validations
Browse files Browse the repository at this point in the history
Added validations #112
  • Loading branch information
mapeveri authored May 4, 2024
2 parents 8211448 + c56452e commit dc6f16a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import { CommandHandler, ICommandHandler } from '@src/shared/domain/bus/commandBus/commandHandler';
import AddLikeTermCommand from '@src/languages/application/term/command/create/addLikeTermCommand';
import TermId from '@src/languages/domain/term/termId';
import UserId from '@src/languages/domain/user/userId';
import TermType from '@src/languages/domain/term/termType';
import { Inject } from '@src/shared/domain/injector/inject.decorator';
import TermRepository, { TERM_REPOSITORY } from '@src/languages/domain/term/termRepository';
import Term from '@src/languages/domain/term/term';
import TermDoesNotExistsException from '@src/languages/domain/term/termDoesNotExistsException';

@CommandHandler(AddLikeTermCommand)
export default class AddLikeTermCommandHandler implements ICommandHandler<AddLikeTermCommand> {
execute(_command: AddLikeTermCommand): Promise<any> {
constructor(@Inject(TERM_REPOSITORY) private readonly termRepository: TermRepository) {}

async execute(command: AddLikeTermCommand): Promise<any> {
const termId = TermId.of(command.termId);
TermType.of(command.type);
UserId.of(command.userId);

await this.getTerm(termId);

return Promise.resolve(undefined);
}

private async getTerm(termId: TermId): Promise<Term> {
const term = await this.termRepository.findById(termId);
if (!term) {
throw new TermDoesNotExistsException(termId.toString());
}

return term;
}
}
7 changes: 7 additions & 0 deletions src/languages/domain/term/termDoesNotExistsException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import NotFoundException from '@src/shared/domain/exceptions/notFoundException';

export default class TermDoesNotExistsException extends NotFoundException {
constructor(termId: string) {
super(`Term ${termId} does not exists`, 'term_does_not_exists');
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,97 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import AddLikeTermCommandHandler from '@src/languages/application/term/command/create/addLikeTermCommandHandler';
import AddLikeTermCommand from '@src/languages/application/term/command/create/addLikeTermCommand';
import { AddLikeTermCommandMother } from '@test/unit/languages/application/term/command/create/addLikeTermCommandMother';
import InvalidArgumentException from '@src/shared/domain/exceptions/invalidArgumentException';
import { TermRepositoryMock } from '@test/unit/languages/domain/term/termRepositoryMock';
import WordMother from '@test/unit/languages/domain/term/word/wordMother';
import TermDoesNotExistsException from '@src/languages/domain/term/termDoesNotExistsException';

describe('Given a AddLikeTermCommandHandler', () => {
const USER_ID = '0a8008d5-ab68-4c10-8476-668b5b540e0f';
const TERM_ID = '7abe3a96-d603-4e87-b69e-e9fb372294de';
const TERM_TYPE = 'word';

let termRepository: TermRepositoryMock;
let handler: AddLikeTermCommandHandler;
const command = new AddLikeTermCommand(TERM_ID, TERM_TYPE, USER_ID);

beforeEach(() => {
handler = new AddLikeTermCommandHandler();
termRepository = new TermRepositoryMock();

handler = new AddLikeTermCommandHandler(termRepository);

jest.useFakeTimers();
});

describe('When an user add a like to a term ', () => {
it('should add a new one', async () => {
await handler.execute(command);
describe('When the term id is invalid ', () => {
let command: AddLikeTermCommand;

function startScenario() {
command = AddLikeTermCommandMother.random({ termId: 'invalid' });
}

beforeEach(startScenario);

it('should thrown an exception', async () => {
await expect(handler.execute(command)).rejects.toThrowError(InvalidArgumentException);
});
});

describe('When the term type is invalid ', () => {
let command: AddLikeTermCommand;

function startScenario() {
command = AddLikeTermCommandMother.random({ type: 'invalid' });
}

beforeEach(startScenario);

it('should thrown an exception', async () => {
await expect(handler.execute(command)).rejects.toThrowError(Error);
});
});

describe('When the user id is invalid ', () => {
let command: AddLikeTermCommand;

function startScenario() {
command = AddLikeTermCommandMother.random({ userId: 'invalid' });
}

beforeEach(startScenario);

it('should thrown an exception', async () => {
await expect(handler.execute(command)).rejects.toThrowError(InvalidArgumentException);
});
});

describe('When the term does not exists ', () => {
let command: AddLikeTermCommand;

function startScenario() {
command = AddLikeTermCommandMother.random();
}

beforeEach(startScenario);

it('should thrown an exception', async () => {
await expect(handler.execute(command)).rejects.toThrowError(TermDoesNotExistsException);
});
});

describe('When an user add a like to a word term ', () => {
let command: AddLikeTermCommand;

function startScenario() {
command = AddLikeTermCommandMother.random({ termId: TERM_ID, userId: USER_ID, type: TERM_TYPE });
const term = WordMother.random();

termRepository.add(term);
}

beforeEach(startScenario);

it('should add a new one', async () => {
expect(await handler.execute(command)).toBeUndefined();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import faker from 'faker';
import AddLikeTermCommand from '@src/languages/application/term/command/create/addLikeTermCommand';
import { TermTypeEnum } from '@src/languages/domain/term/termType';

interface AddLikeTermCommandProps {
termId?: string;
type?: string;
userId?: string;
}

export class AddLikeTermCommandMother {
static random(props?: AddLikeTermCommandProps): AddLikeTermCommand {
const { termId, type, userId } = props ?? {};

return new AddLikeTermCommand(
termId ?? faker.datatype.uuid(),
type ?? TermTypeEnum.WORD,
userId ?? faker.datatype.uuid(),
);
}
}

0 comments on commit dc6f16a

Please sign in to comment.