Skip to content

Commit

Permalink
refactor: ♻️ Corrige algumas atualizações do projeto.
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianTavares committed Aug 30, 2023
1 parent 513333a commit 9924a4a
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 259 deletions.
65 changes: 65 additions & 0 deletions src/modules/@shared/domain/value-object/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import ValueObject from "../../../@shared/domain/value-object/value-object.interface"

export default class Address implements ValueObject {
_street: string = ""
_number: string = ""
_complement: string = ""
_city: string = ""
_state: string = ""
_zipCode: string = ""

constructor(street: string, number: string, complement: string, city: string, state: string, zipCode: string) {
this._street = street
this._number = number
this._complement = complement
this._city = city
this._state = state
this._zipCode = zipCode

}

get street(): string {
return this._street
}

get number(): string {
return this._number
}

get complement(): string {
return this._complement
}

get city(): string {
return this._city
}

get state(): string {
return this._state
}

get zipCode(): string {
return this._zipCode
}

validate() {
if (this._street.length === 0) {
throw new Error("Street is required")
}
if (this._number.length === 0) {
throw new Error("Number is required")
}
if (this._complement.length === 0) {
throw new Error("Complement is required")
}
if (this._city.length === 0) {
throw new Error("City is required")
}
if (this._state.length === 0) {
throw new Error("State is required")
}
if (this._zipCode.length === 0) {
throw new Error("Zip code is required")
}
}
}
54 changes: 32 additions & 22 deletions src/modules/client-adm/domain/client.entity.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
import AggregateRoot from "../../@shared/domain/entity/aggregate-root.interface";
import BaseEntity from "../../@shared/domain/entity/base.entity";
import Id from "../../@shared/domain/value-object/id.value-object";
import AggregateRoot from "../../@shared/domain/entity/aggregate-root.interface"
import BaseEntity from "../../@shared/domain/entity/base.entity"
import Address from "../../@shared/domain/value-object/address"
import Id from "../../@shared/domain/value-object/id.value-object"

type ClientProps = {
id?: Id;
name: string;
email: string;
address: string;
createdAt?: Date;
updatedAt?: Date;
};
id?: Id
name: string
email: string
document: string
address: Address
createdAt?: Date
updatedAt?: Date
}

export default class Client extends BaseEntity implements AggregateRoot {
private _name: string;
private _email: string;
private _address: string;

private _name: string
private _email: string
private _document: string
private _address: Address

constructor(props: ClientProps) {
super(props.id, props.createdAt, props.updatedAt);
this._name = props.name;
this._email = props.email;
this._address = props.address;
super(props.id, props.createdAt, props.updatedAt)
this._name = props.name
this._email = props.email
this._document = props.document
this._address = props.address
}

get name(): string {
return this._name;
return this._name
}

get email(): string {
return this._email;
return this._email
}

get address(): string {
return this._address;
get document(): string {
return this._document
}
}

get address(): Address {
return this._address
}

}
26 changes: 15 additions & 11 deletions src/modules/client-adm/facade/client-adm.facade.interface.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import Address from "../../@shared/domain/value-object/address"

export interface AddClientFacadeInputDto {
id?: string;
name: string;
email: string;
address: string;
id?: string
name: string
email: string
document: string
address: Address
}

export interface FindClientFacadeInputDto {
id: string;
id: string
}

export interface FindClientFacadeOutputDto {
id: string;
name: string;
email: string;
address: string;
createdAt: Date;
updatedAt: Date;
id: string
name: string
email: string
document: string
address: Address
createdAt: Date
updatedAt: Date
}

export default interface ClientAdmFacadeInterface {
Expand Down
138 changes: 83 additions & 55 deletions src/modules/client-adm/facade/client-adm.facade.spec.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,109 @@
import { Sequelize } from "sequelize-typescript";
import ClientAdmFacadeFactory from "../factory/client-adm.facade.factory";
import { ClientModel } from "../repository/client.model";
import ClientRepository from "../repository/client.repository";
import AddClientUseCase from "../usecase/add-client/add-client.usecase";
import FindClientUseCase from "../usecase/find-client/find-client.usecase";
import ClientAdmFacade from "./client-adm.facade";
import { Sequelize } from "sequelize-typescript"
import { ClientModel } from "../repository/client.model"
import ClientRepository from "../repository/client.repository"
import AddClientUseCase from "../usecase/add-client/add-client.usecase"
import ClientAdmFacade from "./client-adm.facade"
import ClientAdmFacadeFactory from "../factory/client-adm.facade.factory"
import Address from "../../@shared/domain/value-object/address"

describe("ClientAdmFacade test", () => {
let sequelize: Sequelize;

describe("Client Adm Facade test", () => {

let sequelize: Sequelize

beforeEach(async () => {
sequelize = new Sequelize({
dialect: "sqlite",
storage: ":memory:",
dialect: 'sqlite',
storage: ':memory:',
logging: false,
sync: { force: true },
});
sync: { force: true }
})

await sequelize.addModels([ClientModel]);
await sequelize.sync();
});
sequelize.addModels([ClientModel])
await sequelize.sync()
})

afterEach(async () => {
await sequelize.close();
});
await sequelize.close()
})

it("should create a client", async () => {
const repository = new ClientRepository();
const addUsecase = new AddClientUseCase(repository);

const repository = new ClientRepository()
const addUsecase = new AddClientUseCase(repository)
const facade = new ClientAdmFacade({
addUsecase: addUsecase,
findUsecase: undefined,
});
})

const input = {
id: "1",
name: "Client 1",
email: "[email protected]",
address: "Address 1",
};
name: "Lucian",
email: "[email protected]",
document: "1234-5678",
address: new Address(
"Rua 123",
"99",
"Casa Verde",
"Criciúma",
"SC",
"88888-888",
)
}

await facade.add(input);
await facade.add(input)

const client = await ClientModel.findOne({ where: { id: "1" } });
const client = await ClientModel.findOne({ where: { id: "1" } })

expect(client).toBeDefined();
expect(client.name).toBe(input.name);
expect(client.email).toBe(input.email);
expect(client.address).toBe(input.address);
});
expect(client).toBeDefined()
expect(client.id).toBe(input.id)
expect(client.name).toBe(input.name)
expect(client.email).toBe(input.email)
expect(client.document).toBe(input.document)
expect(client.street).toBe(input.address.street)
})

it("should find a client", async () => {
// const repository = new ClientRepository();
// const findUsecase = new FindClientUseCase(repository);
// const addUsecase = new AddClientUseCase(repository);

// const repository = new ClientRepository()
// const addUsecase = new AddClientUseCase(repository)
// const findUseCase = new FindClientUseCase(repository)
// const facade = new ClientAdmFacade({
// addUsecase: addUsecase,
// findUsecase: findUsecase,
// });
// addUseCase: addUsecase,
// findUseCase: findUseCase
// })

const facade = ClientAdmFacadeFactory.create();
const facade = ClientAdmFacadeFactory.create()

const input = {
id: "1",
name: "Client 1",
email: "[email protected]",
address: "Address 1",
};

await facade.add(input);

const client = await facade.find({ id: "1" });

expect(client).toBeDefined();
expect(client.id).toBe(input.id);
expect(client.name).toBe(input.name);
expect(client.email).toBe(input.email);
expect(client.address).toBe(input.address);
});
});
name: "Lucian",
email: "[email protected]",
document: "1234-5678",
address: new Address(
"Rua 123",
"99",
"Casa Verde",
"Criciúma",
"SC",
"88888-888"
)
}

await facade.add(input)

const client = await facade.find({ id: "1" })

expect(client).toBeDefined()
expect(client.id).toBe(input.id)
expect(client.name).toBe(input.name)
expect(client.email).toBe(input.email)
expect(client.document).toBe(input.document)
expect(client.address.street).toBe(input.address.street)
expect(client.address.number).toBe(input.address.number)
expect(client.address.complement).toBe(input.address.complement)
expect(client.address.city).toBe(input.address.city)
expect(client.address.state).toBe(input.address.state)
expect(client.address.zipCode).toBe(input.address.zipCode)
})
})
36 changes: 27 additions & 9 deletions src/modules/client-adm/repository/client.model.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import { Column, Model, PrimaryKey, Table } from "sequelize-typescript";

@Table({
tableName: "clients",
timestamps: false,
tableName: 'client',
timestamps: false
})
export class ClientModel extends Model {
@PrimaryKey
@Column({ allowNull: false })
id: string;
id: string

@Column({ allowNull: false })
name: string;
name: string

@Column({ allowNull: false })
email: string;
email: string

@Column({ allowNull: false })
address: string;
document: string

@Column({ allowNull: false })
createdAt: Date;
street: string

@Column({ allowNull: false })
updatedAt: Date;
}
number: string

@Column({ allowNull: true })
complement: string

@Column({ allowNull: false })
city: string

@Column({ allowNull: false })
state: string

@Column({ allowNull: false })
zipcode: string

@Column({ allowNull: false })
createdAt: Date

@Column({ allowNull: false })
updatedAt: Date
}
Loading

0 comments on commit 9924a4a

Please sign in to comment.