generated from AnderGI/typescript-ddd-esm-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/contexts/backoffice/clients/application/register/ClientRegistrar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Client from '../../domain/Client'; | ||
import ClientRepository from '../../domain/ClientRepository'; | ||
import RegisterClientCommand from './RegisterClientCommand'; | ||
|
||
export default class ClientRegistrar { | ||
constructor(private readonly repository: ClientRepository) {} | ||
async registar(command: RegisterClientCommand): Promise<void> { | ||
await Client.save(command)(this.repository); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/contexts/backoffice/clients/application/register/RegisterClientCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Command from '../../../../../shared/domain/command/Command'; | ||
|
||
type CreateClientPrimitives = { | ||
id: string; | ||
name: string; | ||
email: string; | ||
phone: string; | ||
company: string; | ||
position: string; | ||
}; | ||
export default class RegisterClientCommand implements Command { | ||
constructor(readonly data: CreateClientPrimitives) {} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/contexts/backoffice/clients/application/register/RegisterClientCommandHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Command from '../../../../../shared/domain/command/Command'; | ||
import CommandHandler from '../../../../../shared/domain/command/CommandHandler'; | ||
import ClientRegistrar from './ClientRegistrar'; | ||
import RegisterClientCommand from './RegisterClientCommand'; | ||
|
||
export default class RegisterClientCommandHandler implements CommandHandler<RegisterClientCommand> { | ||
constructor(private readonly registrar: ClientRegistrar) {} | ||
|
||
subscribedTo(): Command { | ||
return RegisterClientCommand; | ||
} | ||
|
||
async handle(command: RegisterClientCommand): Promise<void> { | ||
await this.registrar.registar(command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import RegisterClientCommand from '../application/register/RegisterClientCommand'; | ||
import ClientCompany from './ClientCompany'; | ||
import ClientEmail from './ClientEmail'; | ||
import ClientId from './ClientId'; | ||
import ClientName from './ClientName'; | ||
import ClientPhone from './ClientPhone'; | ||
import ClientPosition from './ClientPosition'; | ||
import ClientRepository from './ClientRepository'; | ||
import ClientSaver from './save/ClientSaver'; | ||
|
||
type CreateClientPrimitives = { | ||
id: string; | ||
name: string; | ||
email: string; | ||
phone: string; | ||
company: string; | ||
position: string; | ||
}; | ||
export default class Client { | ||
constructor( | ||
readonly id: ClientId, | ||
readonly name: ClientName, | ||
readonly email: ClientEmail, | ||
readonly phone: ClientPhone, | ||
readonly company: ClientCompany, | ||
readonly position: ClientPosition | ||
) {} | ||
|
||
public static create(_: CreateClientPrimitives): Client { | ||
return new Client( | ||
new ClientId(_.id), | ||
new ClientName(_.name), | ||
new ClientEmail(_.email), | ||
new ClientPhone(_.phone), | ||
new ClientCompany(_.company), | ||
new ClientPosition(_.position) | ||
); | ||
} | ||
|
||
public static fromPrimitives(_: CreateClientPrimitives): Client { | ||
return new Client( | ||
new ClientId(_.id), | ||
new ClientName(_.name), | ||
new ClientEmail(_.email), | ||
new ClientPhone(_.phone), | ||
new ClientCompany(_.company), | ||
new ClientPosition(_.position) | ||
); | ||
} | ||
|
||
static save(command: RegisterClientCommand) { | ||
return async (repository: ClientRepository): Promise<void> => { | ||
await ClientSaver.save(command, repository); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StringValueObject } from '../../../shared/domain/value-object/StringValueObject'; | ||
|
||
export default class ClientCompany extends StringValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StringValueObject } from '../../../shared/domain/value-object/StringValueObject'; | ||
|
||
export default class ClientEmail extends StringValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { UuidValueObject } from '../../../shared/domain/value-object/UuidValueObject'; | ||
|
||
export default class ClientId extends UuidValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StringValueObject } from '../../../shared/domain/value-object/StringValueObject'; | ||
|
||
export default class ClientName extends StringValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StringValueObject } from '../../../shared/domain/value-object/StringValueObject'; | ||
|
||
export default class ClientPhone extends StringValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StringValueObject } from '../../../shared/domain/value-object/StringValueObject'; | ||
|
||
export default class ClientPosition extends StringValueObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Client from './Client'; | ||
|
||
export default interface ClientRepository { | ||
save(_: Client): Promise<void>; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/contexts/backoffice/clients/domain/save/ClientSaver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import RegisterClientCommand from '../../application/register/RegisterClientCommand'; | ||
import Client from '../Client'; | ||
import ClientRepository from '../ClientRepository'; | ||
|
||
export default class ClientSaver { | ||
static async save(command: RegisterClientCommand, repository: ClientRepository): Promise<void> { | ||
const { data } = command; | ||
const client = Client.create({ | ||
id: data.id, | ||
name: data.name, | ||
email: data.email, | ||
phone: data.phone, | ||
company: data.company, | ||
position: data.position | ||
}); | ||
await repository.save(client); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/contexts/backoffice/clients/application/register/ClientRegistrar.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ClientRegistrar from '../../../../../../src/contexts/backoffice/clients/application/register/ClientRegistrar'; | ||
import RegisterClientCommandHandler from '../../../../../../src/contexts/backoffice/clients/application/register/RegisterClientCommandHandler'; | ||
import { ClientMother } from '../../domain/ClientMother'; | ||
import MockClientRepository from '../../domain/MockClientRepository'; | ||
import { RegisterClientCommandMother } from './RegisterClientCommandMother'; | ||
|
||
describe('ClientRegistrar', () => { | ||
describe('#register', () => { | ||
it('Should register a non existing client', () => { | ||
const command = RegisterClientCommandMother.random(); | ||
const repository = new MockClientRepository(); | ||
const client = ClientMother.fromCommand(command); | ||
const registar = new ClientRegistrar(repository); | ||
const handler = new RegisterClientCommandHandler(registar); | ||
|
||
handler.handle(command); | ||
|
||
repository.ensureSaveHasBeenCalledWith(client); | ||
}); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
tests/contexts/backoffice/clients/application/register/RegisterClientCommandMother.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as fs from 'faker'; | ||
|
||
import RegisterClientCommand from '../../../../../../src/contexts/backoffice/clients/application/register/RegisterClientCommand'; | ||
|
||
export class RegisterClientCommandMother { | ||
static random(): RegisterClientCommand { | ||
const primitives = { | ||
id: fs.datatype.uuid(), | ||
name: fs.name.firstName(), | ||
email: fs.internet.email(), | ||
phone: fs.phone.phoneNumber(), | ||
company: fs.company.companyName(), | ||
position: fs.name.jobType() | ||
}; | ||
|
||
return new RegisterClientCommand(primitives); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import RegisterClientCommand from '../../../../../src/contexts/backoffice/clients/application/register/RegisterClientCommand'; | ||
import Client from '../../../../../src/contexts/backoffice/clients/domain/Client'; | ||
|
||
export class ClientMother { | ||
static fromCommand(_: RegisterClientCommand): Client { | ||
const { data } = _; | ||
|
||
return Client.fromPrimitives({ | ||
id: data.id, | ||
name: data.name, | ||
email: data.email, | ||
phone: data.phone, | ||
company: data.company, | ||
position: data.position | ||
}); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/contexts/backoffice/clients/domain/MockClientRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Client from '../../../../../src/contexts/backoffice/clients/domain/Client'; | ||
import ClientRepository from '../../../../../src/contexts/backoffice/clients/domain/ClientRepository'; | ||
|
||
export default class MockClientRepository implements ClientRepository { | ||
private readonly mockSave: jest.Mock; | ||
constructor() { | ||
this.mockSave = jest.fn(); | ||
} | ||
|
||
async save(_: Client): Promise<void> { | ||
this.mockSave(_); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
ensureSaveHasBeenCalledWith(client: Client): void { | ||
expect(this.mockSave).toHaveBeenCalledWith(client); | ||
} | ||
} |