Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refacto: tell dont ask #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23,225 changes: 13,358 additions & 9,867 deletions typescript/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"lint": "eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --fix",
"test": "jest"
"test": "jest",
"test:watch": "jest --watch"
},
"devDependencies": {
"@types/node": "^12.12",
Expand Down
8 changes: 4 additions & 4 deletions typescript/src/domain/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ class Category {
private taxPercentage: number;

public getName(): string {
return this.name;
return this.name;
}

public setName(name: string): void {
this.name = name;
this.name = name;
}

public getTaxPercentage(): number {
return this.taxPercentage;
return this.taxPercentage;
}

public setTaxPercentage(taxPercentage: number) {
this.taxPercentage = taxPercentage;
this.taxPercentage = taxPercentage;
}
}

Expand Down
95 changes: 71 additions & 24 deletions typescript/src/domain/Order.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import OrderItem from './OrderItem';
import { OrderStatus } from './OrderStatus';
import {OrderStatus} from './OrderStatus';
import ShippedOrdersCannotBeChangedException from '../exception/ShippedOrdersCannotBeChangedException';
import RejectedOrderCannotBeApprovedException from '../exception/RejectedOrderCannotBeApprovedException';
import ApprovedOrderCannotBeRejectedException from '../exception/ApprovedOrderCannotBeRejectedException';
import OrderCannotBeShippedTwiceException from '../exception/OrderCannotBeShippedTwiceException';
import OrderCannotBeShippedException from '../exception/OrderCannotBeShippedException';

class Order {
private total: number;
Expand All @@ -9,52 +14,94 @@ class Order {
private status: OrderStatus;
private id: number;

public getTotal(): number {
return this.total;
constructor() {
this.total = 0;
this.currency = 'EUR';
this.items = [];
this.tax = 0;
this.status = OrderStatus.CREATED;
this.id = Math.floor(Math.random() * 1000000);
}

public setTotal(total: number): void {
this.total = total;
public getTotal(): number {
return this.total;
}

public getCurrency(): string {
return this.currency;
return this.currency;
}

public setCurrency(currency: string): void {
this.currency = currency;
public getItems(): OrderItem[] {
return this.items;
}

public getItems(): OrderItem[] {
return this.items;
public getTax(): number {
return this.tax;
}

public setItems(items: OrderItem[]): void {
this.items = items;
public getStatus(): OrderStatus {
return this.status;
}

public getTax(): number {
return this.tax;
public setStatus(status: OrderStatus): void {
this.status = status;
}

public setTax(tax: number): void {
this.tax = tax;
public getId(): number {
return this.id;
}

public getStatus(): OrderStatus {
return this.status;
public addItem(item: OrderItem): void {
this.items.push(item);

this.total += item.getTaxedAmount();
this.tax += item.getTax();
}

public setStatus(status: OrderStatus): void {
this.status = status;
public approve(): void {
if (this.isShipped()) {
throw new ShippedOrdersCannotBeChangedException();
} else if (this.isRejected()) {
throw new RejectedOrderCannotBeApprovedException();
} else {
this.status = OrderStatus.APPROVED;
}
}

public getId(): number {
return this.id;
public reject(): void {
if (this.isShipped()) {
throw new ShippedOrdersCannotBeChangedException();
} else if (this.isApproved()) {
throw new ApprovedOrderCannotBeRejectedException();
} else {
this.status = OrderStatus.REJECTED;
}
}

public canBeShipped(): void {
if (this.isShipped()) {
throw new OrderCannotBeShippedTwiceException();
}

if (!this.isApproved()) {
throw new OrderCannotBeShippedException();
}
}

public ship(): void {
this.status = OrderStatus.SHIPPED;
}

public isRejected(): boolean {
return this.status === OrderStatus.REJECTED;
}

public isApproved(): boolean {
return this.status === OrderStatus.APPROVED;
}

public setId(id: number): void {
this.id = id;
public isShipped(): boolean {
return this.status === OrderStatus.SHIPPED;
}
}

Expand Down
28 changes: 12 additions & 16 deletions typescript/src/domain/OrderItem.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import Product from './Product';
import SellItemRequest from '../request/SellItemRequest';

class OrderItem {
private product: Product;
private readonly product: Product;
private quantity: number;
private taxedAmount: number;
private tax: number;
private readonly taxedAmount: number;
private readonly tax: number;

public getProduct(): Product {
return this.product;
constructor(product: Product, itemRequest: SellItemRequest) {
this.product = product;
this.quantity = itemRequest.getQuantity();
this.tax = product.getUnitaryTax() * this.quantity;
this.taxedAmount = product.getUnitaryTaxedAmount() * this.quantity;
}

public setProduct(product: Product): void {
this.product = product;
public getProduct(): Product {
return this.product;
}

public getQuantity(): number {
return this.quantity;
return this.quantity;
}

public setQuantity(quantity: number): void {
Expand All @@ -26,17 +30,9 @@ class OrderItem {
return this.taxedAmount;
}

public setTaxedAmount(taxedAmount: number): void {
this.taxedAmount = taxedAmount;
}

public getTax(): number {
return this.tax;
}

public setTax(tax: number): void {
this.tax = tax;
}
}

export default OrderItem;
Expand Down
8 changes: 8 additions & 0 deletions typescript/src/domain/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Product {
public setCategory(category: Category): void {
this.category = category;
}

public getUnitaryTax(): number {
return Math.round(this.getPrice() / 100 * this.getCategory().getTaxPercentage() * 100) / 100;
}

public getUnitaryTaxedAmount(): number {
return Math.round((this.getPrice() + this.getUnitaryTax()) * 100) / 100;
}
}

export default Product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class OrderShipmentRequest {
private orderId: number;

public setOrderId(orderId: number): void {
this.orderId = orderId;
this.orderId = orderId;
}

public getOrderId(): number {
return this.orderId;
return this.orderId;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ class SellItemRequest {
private productName: string;

public setQuantity(quantity: number): void{
this.quantity = quantity;
this.quantity = quantity;
}

public setProductName(productName: string): void {
this.productName = productName;
this.productName = productName;
}

public getQuantity(): number {
return this.quantity;
return this.quantity;
}

public getProductName(): string {
return this.productName;
return this.productName;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import SellItemRequest from "./SellItemRequest";
import SellItemRequest from './SellItemRequest';

class SellItemsRequest {
private requests: SellItemRequest[];

public setRequests(requests: SellItemRequest[]): void {
this.requests = requests;
this.requests = requests;
}

public getRequests(): SellItemRequest[] {
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/service/ShipmentService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Order from '../domain/Order';

export interface ShipmentService {
ship(order: Order): void;
ship(order: Order): void;
}
33 changes: 11 additions & 22 deletions typescript/src/useCase/OrderApprovalUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
import Order from '../domain/Order';
import { OrderStatus } from '../domain/OrderStatus';
import OrderRepository from '../repository/OrderRepository';
import ApprovedOrderCannotBeRejectedException from './ApprovedOrderCannotBeRejectedException';
import OrderApprovalRequest from './OrderApprovalRequest';
import RejectedOrderCannotBeApprovedException from './RejectedOrderCannotBeApprovedException';
import ShippedOrdersCannotBeChangedException from './ShippedOrdersCannotBeChangedException';
import OrderApprovalRequest from '../request/OrderApprovalRequest';

class OrderApprovalUseCase {
private readonly orderRepository: OrderRepository;

public constructor(orderRepository: OrderRepository){
this.orderRepository = orderRepository;
public constructor(orderRepository: OrderRepository) {
this.orderRepository = orderRepository;
}

public run(request: OrderApprovalRequest): void {
const order: Order = this.orderRepository.getById(request.getOrderId());
const order: Order = this.orderRepository.getById(request.getOrderId());

if (order.getStatus() === OrderStatus.SHIPPED) {
throw new ShippedOrdersCannotBeChangedException();
}
if (request.isApproved()) {
order.approve();
} else {
order.reject();
}

if (request.isApproved() && order.getStatus() === OrderStatus.REJECTED) {
throw new RejectedOrderCannotBeApprovedException();
}

if (!request.isApproved() && order.getStatus() === OrderStatus.APPROVED) {
throw new ApprovedOrderCannotBeRejectedException();
}

order.setStatus(request.isApproved() ? OrderStatus.APPROVED : OrderStatus.REJECTED);
this.orderRepository.save(order);
this.orderRepository.save(order);
}
}

export default OrderApprovalUseCase
export default OrderApprovalUseCase;
32 changes: 6 additions & 26 deletions typescript/src/useCase/OrderCreationUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Order from '../domain/Order';
import OrderItem from '../domain/OrderItem';
import { OrderStatus } from '../domain/OrderStatus';
import Product from '../domain/Product';
import OrderRepository from '../repository/OrderRepository';
import { ProductCatalog } from '../repository/ProductCatalog';
import SellItemsRequest from './SellItemsRequest';
import UnknownProductException from './UnknownProductException';
import {ProductCatalog} from '../repository/ProductCatalog';
import SellItemsRequest from '../request/SellItemsRequest';
import UnknownProductException from '../exception/UnknownProductException';

class OrderCreationUseCase {
private readonly orderRepository: OrderRepository;
Expand All @@ -18,33 +17,14 @@ class OrderCreationUseCase {

public run(request: SellItemsRequest): void {
const order: Order = new Order();
order.setStatus(OrderStatus.CREATED);
order.setItems([]);
order.setCurrency('EUR');
order.setTotal(0);
order.setTax(0);

for (const itemRequest of request.getRequests()) {
const product: Product = this.productCatalog.getByName(itemRequest.getProductName());
const product: Product = this.productCatalog.getByName(itemRequest.getProductName());

if (product === undefined) {
throw new UnknownProductException();
}
else {
const unitaryTax: number = Math.round(product.getPrice() / 100 * product.getCategory().getTaxPercentage() * 100) / 100;
const unitaryTaxedAmount: number = Math.round((product.getPrice() + unitaryTax) * 100) / 100;
const taxedAmount: number = Math.round(unitaryTaxedAmount * itemRequest.getQuantity() * 100) / 100;
const taxAmount: number = unitaryTax * itemRequest.getQuantity();

const orderItem: OrderItem = new OrderItem();
orderItem.setProduct(product);
orderItem.setQuantity(itemRequest.getQuantity());
orderItem.setTax(taxAmount);
orderItem.setTaxedAmount(taxedAmount);
order.getItems().push(orderItem);

order.setTotal(order.getTotal() + taxedAmount);
order.setTax(order.getTax() + taxAmount);
} else {
order.addItem(new OrderItem(product, itemRequest));
}
}

Expand Down
Loading