Skip to content

Commit

Permalink
HOTFIX: Arreglos menores tests + test de member
Browse files Browse the repository at this point in the history
  • Loading branch information
uo276436 committed May 2, 2022
1 parent 2e59b49 commit db418cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions restapi/controllers/orderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const addOrder = async (req: Request, res: Response): Promise<Response> =
newOrder.save();
return res.status(200).json(newOrder);
} catch (error) {
console.log(error.message);
return res.status(500).send(error);
}
}
Expand Down
55 changes: 31 additions & 24 deletions restapi/shippo/shippo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export async function calculaCostes(addressTo: Map<string, string>) {
}).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
Expand All @@ -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;
}
}
11 changes: 10 additions & 1 deletion restapi/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});

0 comments on commit db418cc

Please sign in to comment.