Skip to content
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
2 changes: 1 addition & 1 deletion docs/insomnia.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_type":"export","__export_format":4,"__export_date":"2022-05-02T01:47:37.705Z","__export_source":"insomnia.desktop.app:v2022.2.1","resources":[{"_id":"req_cad0bada1e284ba2976efb0e393a909f","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651249698792,"created":1650255059765,"url":"localhost:3000/orders","name":"get orders","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{"type":"basic","useISO88591":false,"disabled":false,"username":"1","password":"senha"},"metaSortKey":-1650255059765,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_2c705df1dbcc47eabcc60bf90498a478","parentId":null,"modified":1649907991761,"created":1649907991761,"name":"New Document","description":"","scope":"design","_type":"workspace"},{"_id":"req_8b5826bdf37e4c32976e2e45a727119c","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455976995,"created":1650253425954,"url":"localhost:3000/order","name":"create order","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"restaurantId\": 1,\n \"clientId\": 1,\n \"payment\": 24.0\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_63b72947e1dc47c493557a05c2a6b730"}],"authentication":{},"metaSortKey":-1650253425954,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_16d8de8b5c894b2ab211dee134309cf7","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455977580,"created":1650253366994,"url":"localhost:3000/deliveryman","name":"create deliveryman","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Jose Cruz\",\n\t\"password\": \"senha\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_749c1e2b1f6a44e284e77fbfe06605ad"}],"authentication":{},"metaSortKey":-1650253366994,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_c188ea3e0164487bada3c59e422105e5","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455973682,"created":1650253304747,"url":"localhost:3000/restaurant","name":"create restaurant","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Bob's Madalena\",\n\t\"address\": \"Av. Eng. Abdias de Carvalho, 365 - Ilha do Retiro, Recife - PE, 50750-257\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_d22d284e543c43b9acc3c37540493e7f"}],"authentication":{},"metaSortKey":-1650253304747,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_254f144297f54edab16918b4d8581b45","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651456012214,"created":1649907991788,"url":"localhost:3000/client","name":"create client","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Bezaliel Silva\",\n\t\"address\": \"Rua Visconde de Barbacena, 329 - Varzea, Recife - PE\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_5954be099a9641828e1fd7983127c578"}],"authentication":{},"metaSortKey":-1649907991788,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_ad445f5f0b6d2ce1fe949db6bf9158952b37c855","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649908250482,"created":1649907991778,"name":"Base Environment","data":{},"dataPropertyOrder":{},"color":null,"isPrivate":false,"metaSortKey":1649907991778,"_type":"environment"},{"_id":"jar_ad445f5f0b6d2ce1fe949db6bf9158952b37c855","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649907991782,"created":1649907991782,"name":"Default Jar","cookies":[],"_type":"cookie_jar"},{"_id":"spc_092caef0a2b549b0bb0a74205f4ad63c","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1650253145428,"created":1649907991773,"fileName":"ESS","contents":"","contentType":"yaml","_type":"api_spec"},{"_id":"uts_13eae73b98704a449e0fb87cf730cb29","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649907991790,"created":1649907991790,"name":"Example Test Suite","_type":"unit_test_suite"}]}
{"_type":"export","__export_format":4,"__export_date":"2022-05-02T01:47:37.705Z","__export_source":"insomnia.desktop.app:v2022.2.1","resources":[{"_id":"req_cad0bada1e284ba2976efb0e393a909f","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651249698792,"created":1650255059765,"url":"localhost:3000/orders","name":"get orders","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{"type":"basic","useISO88591":false,"disabled":false,"username":"1","password":"senha"},"metaSortKey":-1650255059765,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_2c705df1dbcc47eabcc60bf90498a478","parentId":null,"modified":1649907991761,"created":1649907991761,"name":"New Document","description":"","scope":"design","_type":"workspace"},{"_id":"req_8b5826bdf37e4c32976e2e45a727119c","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455976995,"created":1650253425954,"url":"localhost:3000/order","name":"create order","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"restaurantId\": 1,\n \"clientId\": 1,\n \"payment\": 24.0\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_63b72947e1dc47c493557a05c2a6b730"}],"authentication":{},"metaSortKey":-1650253425954,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_16d8de8b5c894b2ab211dee134309cf7","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455977580,"created":1650253366994,"url":"localhost:3000/deliverer","name":"create deliverer","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Jose Cruz\",\n\t\"password\": \"senha\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_749c1e2b1f6a44e284e77fbfe06605ad"}],"authentication":{},"metaSortKey":-1650253366994,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_c188ea3e0164487bada3c59e422105e5","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651455973682,"created":1650253304747,"url":"localhost:3000/restaurant","name":"create restaurant","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Bob's Madalena\",\n\t\"address\": \"Av. Eng. Abdias de Carvalho, 365 - Ilha do Retiro, Recife - PE, 50750-257\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_d22d284e543c43b9acc3c37540493e7f"}],"authentication":{},"metaSortKey":-1650253304747,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_254f144297f54edab16918b4d8581b45","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1651456012214,"created":1649907991788,"url":"localhost:3000/client","name":"create client","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\": \"Bezaliel Silva\",\n\t\"address\": \"Rua Visconde de Barbacena, 329 - Varzea, Recife - PE\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_5954be099a9641828e1fd7983127c578"}],"authentication":{},"metaSortKey":-1649907991788,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_ad445f5f0b6d2ce1fe949db6bf9158952b37c855","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649908250482,"created":1649907991778,"name":"Base Environment","data":{},"dataPropertyOrder":{},"color":null,"isPrivate":false,"metaSortKey":1649907991778,"_type":"environment"},{"_id":"jar_ad445f5f0b6d2ce1fe949db6bf9158952b37c855","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649907991782,"created":1649907991782,"name":"Default Jar","cookies":[],"_type":"cookie_jar"},{"_id":"spc_092caef0a2b549b0bb0a74205f4ad63c","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1650253145428,"created":1649907991773,"fileName":"ESS","contents":"","contentType":"yaml","_type":"api_spec"},{"_id":"uts_13eae73b98704a449e0fb87cf730cb29","parentId":"wrk_2c705df1dbcc47eabcc60bf90498a478","modified":1649907991790,"created":1649907991790,"name":"Example Test Suite","_type":"unit_test_suite"}]}
46 changes: 23 additions & 23 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import express = require('express')
import bodyParser = require('body-parser')
import { RestaurantsService } from './src/restaurants-service'
import { ClientsService } from './src/clients-service'
import { DeliverymenService } from './src/deliverymen-service'
import { OrdersService } from './src/orders-service'
import { Order } from './src/order'
import { DeliveriesService } from './src/deliveries-service'
import { RestaurantsService } from './src/service/restaurants-service'
import { ClientsService } from './src/service/clients-service'
import { DeliverersService } from './src/service/deliverers-service'
import { OrdersService } from './src/service/orders-service'
import { Order } from './src/model/order'
import { DeliveriesService } from './src/service/deliveries-service'
import { Request, Response } from 'express-serve-static-core'
import { DeliveryMapper } from './src/delivery-mapper'
import { Action } from './src/delivery-action'
import { DeliveryMapper } from './src/mapper/delivery-mapper'
import { Action } from './src/model/delivery-action'

const app = express()

Expand All @@ -24,9 +24,9 @@ app.use(bodyParser.json())

const restaurantService: RestaurantsService = new RestaurantsService()
const clientsService: ClientsService = new ClientsService()
const deliverymenService: DeliverymenService = new DeliverymenService()
const deliverersService: DeliverersService = new DeliverersService()
const ordersService: OrdersService = new OrdersService()
const deliveriesService: DeliveriesService = new DeliveriesService(ordersService, deliverymenService)
const deliveriesService: DeliveriesService = new DeliveriesService(ordersService, deliverersService)
const deliveryMapper: DeliveryMapper = new DeliveryMapper()

function extractCredentials (req: Request, res: Response): string[] {
Expand Down Expand Up @@ -58,21 +58,21 @@ app.post('/client', function (req, res) {
return res.send(clientsService.add(req.body))
})

app.post('/deliveryman', function (req, res) {
return res.send(deliverymenService.add(req.body))
app.post('/deliverer', function (req, res) {
return res.send(deliverersService.add(req.body))
})

app.post('/order', function (req, res) {
try {
const orderId = req.body.id
const client = clientsService.getById(req.body.clientId)
const restaurant = restaurantService.getById(req.body.restaurantId)
const deliveryman = req.body.deliverymanId
const deliverer = req.body.delivererId
const payment = Number(req.body.payment)
if (!client) throw Error('invalid client')
if (!restaurant) throw Error('invalid restaurant')
const order = ordersService.add(<Order>{ id: orderId, restaurant: restaurant, client: client, payment: payment })
deliveriesService.addOrder(order.id, deliveryman)
deliveriesService.addOrder(order.id, deliverer)
return res.send(order)
} catch (e) {
return res.status(500).send(e)
Expand All @@ -82,9 +82,9 @@ app.post('/order', function (req, res) {
app.get('/order/:orderId', function (req, res) {
try {
const [username, password] = extractCredentials(req, res)
deliverymenService.auth(Number(username), password)
deliverersService.auth(Number(username), password)

const delivery = deliveriesService.byDeliveryman(Number(username)).find(({ order }) => req.params.orderId == order.id)
const delivery = deliveriesService.byDeliverer(Number(username)).find(({ order }) => req.params.orderId == order.id)
return res.send(deliveryMapper.toJson(delivery))
} catch (e) {
if (e.message == 'auth failed') {
Expand All @@ -97,7 +97,7 @@ app.get('/order/:orderId', function (req, res) {
app.get('/order/:orderId/:action', function (req, res) {
try {
const [username, password] = extractCredentials(req, res)
deliverymenService.auth(Number(username), password)
deliverersService.auth(Number(username), password)

const delivery = deliveriesService.takeAction(Number(username), Number(req.params.orderId), <Action>(req.params.action))
return res.send(deliveryMapper.toJson(delivery))
Expand All @@ -112,7 +112,7 @@ app.get('/order/:orderId/:action', function (req, res) {
app.post('/evaluation', function (req, res) {
try {
const [username, password] = extractCredentials(req, res)
deliverymenService.auth(Number(username), password)
deliverersService.auth(Number(username), password)
const delivery = deliveriesService.evaluateOrder(Number(username), Number(req.body.id), Number(req.body.restaurantScore), Number(req.body.clientScore))
return res.send(deliveryMapper.toJson(delivery))
} catch (e) {
Expand All @@ -126,9 +126,9 @@ app.post('/evaluation', function (req, res) {
app.get('/orders/', function (req, res) {
try {
const [username, password] = extractCredentials(req, res)
deliverymenService.auth(Number(username), password)
deliverersService.auth(Number(username), password)

const ans = deliveriesService.byDeliveryman(Number(username)).map(delivery => deliveryMapper.toJsonMinimal(delivery))
const ans = deliveriesService.byDeliverer(Number(username)).map(delivery => deliveryMapper.toJsonMinimal(delivery))
return res.status(200).send(ans)
} catch (e) {
if (e.message == 'auth failed') {
Expand All @@ -141,9 +141,9 @@ app.get('/orders/', function (req, res) {
app.get('/user/', function (req, res) {
try {
const [username, password] = extractCredentials(req, res)
deliverymenService.auth(Number(username), password)
const deliveryman = deliverymenService.getById(Number(username))
return res.status(200).send({ name: deliveryman.name, wallet: deliveryman.wallet })
deliverersService.auth(Number(username), password)
const deliverer = deliverersService.getById(Number(username))
return res.status(200).send({ name: deliverer.name, wallet: deliverer.wallet })
} catch (e) {
if (e.message == 'auth failed') {
return res.status(401).send(e)
Expand Down
4 changes: 2 additions & 2 deletions server/spec/clients-services.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'jasmine'
import { Client } from '../src/client'
import { ClientsService } from '../src/clients-service'
import { Client } from '../src/model/client'
import { ClientsService } from '../src/service/clients-service'

describe('O servico de clientes', () => {
let clientsService: ClientsService
Expand Down
39 changes: 39 additions & 0 deletions server/spec/deliverers-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'jasmine'
import { Deliverer } from '../src/model/deliverer'
import { DeliverersService } from '../src/service/deliverers-service'

describe('O servico de entregadores', () => {
let deliverersService: DeliverersService
const deliverer1 = <Deliverer> {
name: 'Gabriel Mendes'
}
const deliverer2 = <Deliverer> {
name: 'José Cruz'
}

beforeEach(() => deliverersService = new DeliverersService())

it('é inicialmente vazio', () => {
expect(deliverersService.deliverers.length).toBe(0)
})

it('cadastra entregadores corretamente', () => {
deliverersService.add(deliverer1)
deliverersService.add(deliverer2)

expect(deliverersService.deliverers.length).toBe(2)
const result = deliverersService.getById(2)
expect(result.id).toBe(2)
expect(result.name).toBe(deliverer2.name)
expect(result.wallet).toBe(0.0)
})

it('atualiza saldo da carteira corretamente', () => {
deliverersService.add(deliverer1)
deliverersService.add(deliverer2)

deliverersService.getById(2).addBalance(5.0)
deliverersService.getById(2).addBalance(7.0)
expect(deliverersService.getById(2).wallet).toBe(12.0)
})
})
Loading