diff --git a/restapi/controllers/orderController.ts b/restapi/controllers/orderController.ts index 2f8354e..0fef25d 100644 --- a/restapi/controllers/orderController.ts +++ b/restapi/controllers/orderController.ts @@ -45,6 +45,7 @@ export const addOrder = async (req: Request, res: Response): Promise = newOrder.save(); return res.status(200).json(newOrder); } catch (error) { + console.log(error.message); return res.status(500).send(error); } } diff --git a/restapi/shippo/shippo.ts b/restapi/shippo/shippo.ts index a3fbe39..90c9fc1 100644 --- a/restapi/shippo/shippo.ts +++ b/restapi/shippo/shippo.ts @@ -31,10 +31,14 @@ export async function calculaCostes(addressTo: Map) { }).then((shipment: any, err: any) => { if (err != null) { console.log("Ha ocurrido un error al calcular los gastos de envio: " + err); + if(res[0]==undefined) + return 5; return getCosteEnvio(res[0].latitude, res[0].longitude); } if (shipment?.rates === undefined) { console.log("Ha ocurrido un error al calcular los gastos de envio: Se aplicará una tarifa estandar"); + if(res[0]==undefined) + return 5; return getCosteEnvio(res[0].latitude, res[0].longitude); } var costes = shipment?.rates[0].amount @@ -58,32 +62,35 @@ function getCosteEnvio(lat1: number, lon1: number) { neighbourhood: 'Manhattan Community Board 1', provider: 'openstreetmap' } + try{ + // https://www.geeksforgeeks.org/program-distance-two-points-earth/#:~:text=For%20this%20divide%20the%20values,is%20the%20radius%20of%20Earth. + // The math module contains a function + // named toRadians which converts from + // degrees to radians. + lon1 = lon1 * Math.PI / 180; + direccionBase.longitude = direccionBase.longitude * Math.PI / 180; + lat1 = lat1 * Math.PI / 180; + direccionBase.latitude = direccionBase.latitude * Math.PI / 180; - // https://www.geeksforgeeks.org/program-distance-two-points-earth/#:~:text=For%20this%20divide%20the%20values,is%20the%20radius%20of%20Earth. - // The math module contains a function - // named toRadians which converts from - // degrees to radians. - lon1 = lon1 * Math.PI / 180; - direccionBase.longitude = direccionBase.longitude * Math.PI / 180; - lat1 = lat1 * Math.PI / 180; - direccionBase.latitude = direccionBase.latitude * Math.PI / 180; + // Haversine formula + let dlon = direccionBase.longitude - lon1; + let dlat = direccionBase.latitude - lat1; + let a = Math.pow(Math.sin(dlat / 2), 2) + + Math.cos(lat1) * Math.cos(direccionBase.latitude) + * Math.pow(Math.sin(dlon / 2), 2); - // Haversine formula - let dlon = direccionBase.longitude - lon1; - let dlat = direccionBase.latitude - lat1; - let a = Math.pow(Math.sin(dlat / 2), 2) - + Math.cos(lat1) * Math.cos(direccionBase.latitude) - * Math.pow(Math.sin(dlon / 2), 2); + let c = 2 * Math.asin(Math.sqrt(a)); - let c = 2 * Math.asin(Math.sqrt(a)); + // Radius of earth in kilometers. Use 3956 + // for miles + let r = 6371; - // Radius of earth in kilometers. Use 3956 - // for miles - let r = 6371; - - // calculate the result - var kilometers = c * r; - var result = kilometers * 0.2; // 0,20€/km precio medio de kilometraje en España - console.log("Calculado el envío mediante la distancia: " + result + " €"); - return result; + // calculate the result + var kilometers = c * r; + var result = kilometers * 0.2; // 0,20€/km precio medio de kilometraje en España + console.log("Calculado el envío mediante la distancia: " + result + " €"); + return result; + }catch{ + return 5; + } } \ No newline at end of file diff --git a/restapi/tests/api.test.ts b/restapi/tests/api.test.ts index 7a60f7b..6ab563a 100644 --- a/restapi/tests/api.test.ts +++ b/restapi/tests/api.test.ts @@ -6,6 +6,7 @@ import cors from 'cors'; const productRoutes = require('../routes/productRoutes'); const userRoutes = require('../routes/userRoutes'); const orderRoutes = require('../routes/orderRoutes'); +const memberRoutes = require('../routes/memberRoutes'); const jwt = require("jsonwebtoken"); let app: Application; @@ -30,6 +31,7 @@ beforeAll(async () => { app.use('/api', userRoutes); app.use('/api', orderRoutes); app.use('/api', productRoutes); + app.use('/api', memberRoutes); server = app.listen(port, (): void => { console.log('Restapi server for testing listening on ' + port); @@ -110,7 +112,6 @@ function checkProductList(response: Response, tipo: string) { expect(response.statusCode).toBe(200); } function checkIfVegetarianList(response: Response) { - console.log(response.body); expect(response.text).not.toEqual('[]') expect(response.body[0]._vegetariano).toBe(true); expect(response.statusCode).toBe(200); @@ -239,3 +240,11 @@ describe('order', () => { expect(response.statusCode).toBe(200); }); }); +describe('members ', () => { + it('All products can be listed', async () => { + const response: Response = await request(app).get("/api/miembros"); + expect(response.text).not.toEqual('[]') + expect(response.statusCode).toBe(200); + expect(response.body.length).toBe(4); + }); +}); \ No newline at end of file