Skip to content

Commit

Permalink
feat: implement update method in repository order
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCamargo31 committed Oct 26, 2023
1 parent 5264a21 commit 30833c7
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 33 deletions.
58 changes: 49 additions & 9 deletions src/domain/entity/order.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,76 @@
import { Customer } from './customer'
import { Order } from './order'
import { OrderItem } from './order-item'

describe('Order unit tests', () => {
it('should throw error when id is empty', () => {
expect(() => {
const order = new Order('', '123', [])
const order = new Order('', 'c1', [])
}).toThrowError('id is required')
})

it('should throw error when customerId is empty', () => {
expect(() => {
const order = new Order('123', '', [])
const order = new Order('o1', '', [])
}).toThrowError('customerId is required')
})

it('should throw error when customerId is empty', () => {
expect(() => {
const order = new Order('123', '123', [])
}).toThrowError('item qtd mus be greater than 0')
const order = new Order('o1', 'c1', [])
}).toThrowError('item qtd must be greater than 0')
})

it('should calculate total', () => {
const item1 = new OrderItem('123', 'product1', 100, '1', 2)
const item2 = new OrderItem('123', 'product2', 200, '2', 2)
const order = new Order('123', '123', [item1, item2])
const item1 = new OrderItem('i1', 'product1', 100, '1', 2)
const item2 = new OrderItem('i2', 'product2', 200, '2', 2)
const order = new Order('o1', 'c1', [item1, item2])
expect(order.total()).toBe(600)
})

it('should check if the item qtd is less or equal 0', () => {
expect(() => {
const item = new OrderItem('123', 'product1', 100, '1', 0)
const order = new Order('123', '123', [item])
const item = new OrderItem('i1', 'product1', 100, '1', 0)
const order = new Order('o1', 'c1', [item])
}).toThrowError('item quantity must be greater than 0')
})

it('should change customer', () => {
const item1 = new OrderItem('123', 'product1', 100, '1', 2)
const order = new Order('o1', 'c1', [item1])
expect(order.customerId).toBe('c1')

order.changeCustomer('c2')
expect(order.customerId).toBe('c2')
})

it('should add new item', () => {
const item1 = new OrderItem('123', 'product1', 100, '1', 2)
const order = new Order('o1', 'c1', [item1])
expect(order.items.length).toBe(1)

const item2 = new OrderItem('i2', 'product2', 200, '2', 2)
order.addItem(item2)
expect(order.items.length).toBe(2)
})

it('should remove item', () => {
const item1 = new OrderItem('123', 'product1', 100, '1', 2)
const item2 = new OrderItem('i2', 'product2', 200, '2', 2)
const order = new Order('o1', 'c1', [item1, item2])
expect(order.items.length).toBe(2)

order.removeItem(item1.id)
expect(order.items.length).toBe(1)
})

it('should throw error if remove all items', () => {
const item1 = new OrderItem('123', 'product1', 100, '1', 2)
const order = new Order('o1', 'c1', [item1])
expect(order.items.length).toBe(1)

expect(() => {
order.removeItem(item1.id)
}).toThrowError('item qtd must be greater than 0')
})
})
24 changes: 18 additions & 6 deletions src/domain/entity/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { OrderItem } from './order-item'

export class Order {
private readonly _id: string
private readonly _customerId: string
private readonly _items: OrderItem[]=[]
private _customerId: string
private _items: OrderItem[]=[]
private readonly _total: number

constructor (id: string, customerId: string, items: OrderItem[]) {
Expand Down Expand Up @@ -34,16 +34,28 @@ export class Order {
throw new Error('customerId is required')
}
if (this._items.length === 0) {
throw new Error('item qtd mus be greater than 0')
}
if (this._items.length === 0) {
throw new Error('item qtd mus be greater than 0')
throw new Error('item qtd must be greater than 0')
}
if (this._items.some(item => item.quantity <= 0)) {
throw new Error('item quantity must be greater than 0')
}
}

changeCustomer (customerId: string): void {
this._customerId = customerId
this.validate()
}

addItem (item: OrderItem): void {
this._items.push(item)
this.validate()
}

removeItem (itemId: string): void {
this._items = this._items.filter(item => item.id !== itemId)
this.validate()
}

total (): number {
return this._items.reduce((acc, item) => acc + item.orderItemTotal(), 0)
}
Expand Down
208 changes: 191 additions & 17 deletions src/infrastructure/repository/order-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { OrderItem } from '../../domain/entity/order-item'
import { Order } from '../../domain/entity/order'
import OrderRepository from './order-repository'

const makeCustomer = (): Customer => {
const customer = new Customer('c1', 'customer 1')
const makeCustomer = (id: string, name: string): Customer => {
const customer = new Customer(id, name)
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP')
customer.changeAddress(address)
return customer
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('Order repository test', () => {

it('should create a new order', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
Expand Down Expand Up @@ -76,27 +76,201 @@ describe('Order repository test', () => {
})
})

it('should throw an error when calling the update method', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
await customerRepository.create(customer)
describe('update', () => {
it('should change order customer', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)
const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

const item = new OrderItem('i1', product.name, product.price, product.id, 2)
const order = new Order('o1', customer.id, [item])
const orderRepository = new OrderRepository()
const item = new OrderItem('i1', product.name, product.price, product.id, 2)
const order = new Order('o1', customer.id, [item])
const orderRepository = new OrderRepository()
await orderRepository.create(order)

void expect(async () => {
const orderModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item.id,
name: item.name,
price: item.price,
quantity: item.quantity,
order_id: order.id,
product_id: item.productId
}]
})

// change customer
const customer2 = makeCustomer('c2', 'customer 2')
await customerRepository.create(customer2)
order.changeCustomer(customer2.id)
await orderRepository.update(order)
}).rejects.toThrow('method not implemented')

const orderUpdatedModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderUpdatedModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer2.id,
total: order.total(),
items: [{
id: item.id,
name: item.name,
price: item.price,
quantity: item.quantity,
order_id: order.id,
product_id: item.productId
}]
})
})

it('should add a new item to order', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

const item = new OrderItem('i1', product.name, product.price, product.id, 2)
const order = new Order('o1', customer.id, [item])
const orderRepository = new OrderRepository()
await orderRepository.create(order)

const orderModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item.id,
name: item.name,
price: item.price,
quantity: item.quantity,
order_id: order.id,
product_id: item.productId
}]
})

// add new item
const item2 = new OrderItem('i2', product.name, product.price, product.id, 2)
order.addItem(item2)
await orderRepository.update(order)

const orderUpdatedModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderUpdatedModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item.id,
name: item.name,
price: item.price,
quantity: item.quantity,
order_id: order.id,
product_id: item.productId
}, {
id: item2.id,
name: item2.name,
price: item2.price,
quantity: item2.quantity,
order_id: order.id,
product_id: item2.productId
}]
})
})

it('should remove an item from the order', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

const item1 = new OrderItem('i1', product.name, product.price, product.id, 2)
const item2 = new OrderItem('i2', product.name, product.price, product.id, 2)

const order = new Order('o1', customer.id, [item1, item2])
const orderRepository = new OrderRepository()
await orderRepository.create(order)

const orderModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item1.id,
name: item1.name,
price: item1.price,
quantity: item1.quantity,
order_id: order.id,
product_id: item1.productId
}, {
id: item2.id,
name: item2.name,
price: item2.price,
quantity: item2.quantity,
order_id: order.id,
product_id: item2.productId
}]
})

// remove item
order.removeItem(item2.id)
await orderRepository.update(order)

const orderUpdatedModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderUpdatedModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item1.id,
name: item1.name,
price: item1.price,
quantity: item1.quantity,
order_id: order.id,
product_id: item1.productId
}]
})
})
})

it('should find a order', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
Expand All @@ -122,7 +296,7 @@ describe('Order repository test', () => {

it('should find all orders', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
const customer = makeCustomer('c1', 'customer 1')
await customerRepository.create(customer)

const productRepository = new ProductRepository()
Expand Down
Loading

0 comments on commit 30833c7

Please sign in to comment.