Skip to content

Commit

Permalink
refactor: change file names and apply linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Méndez committed Oct 23, 2019
1 parent 1013d2d commit 730223a
Show file tree
Hide file tree
Showing 17 changed files with 715 additions and 349 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = {
sourceType: 'module'
},
rules: {
'comma-dangle': ['error', 'never']
'comma-dangle': ['error', 'never'],
'import/no-unresolved': 0
},
settings: {
'import/core-modules': [ 'aws-sdk' ]
Expand Down
77 changes: 0 additions & 77 deletions UploadToS3.js

This file was deleted.

15 changes: 8 additions & 7 deletions accept_slot.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const aws = require('aws-sdk');

const dynamoDB = new aws.DynamoDB.DocumentClient();

async function getTripFromSlot(slotId) {
let params = {
const params = {
TableName: process.env.dynamodb_table_name_slots,
Key: {
slot_id: slotId
},
ProjectionExpression: 'trip_id'
};
let data = await dynamoDB.get(params).promise();
const data = await dynamoDB.get(params).promise();
return data.Item.trip_id;
}

Expand Down Expand Up @@ -54,13 +55,13 @@ async function acceptSlot(tripId, slotId, slotStatus) {
}
}

exports.handler = async event => {
let slotId = event.slot_id;
let slotStatus = event.slot_status;
exports.handler = async (event) => {
const slotId = event.slot_id;
const slotStatus = event.slot_status;

let tripId = await getTripFromSlot(slotId);
const tripId = await getTripFromSlot(slotId);

let result = {
const result = {
accepted: await acceptSlot(tripId, slotId, slotStatus)
};

Expand Down
3 changes: 1 addition & 2 deletions create_slot.js → create_trip_reservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ async function createSlot(tripId, userId, spotId) {
.promise();
return true;
} catch (e) {
console.log(e);
console.error(e);
return false;
}
}

exports.handler = async (event) => {
console.log(event.body);
const body = JSON.parse(event.body);
const tripId = event.pathParameters.trip;
const userId = body.user_id;
Expand Down
110 changes: 55 additions & 55 deletions create_user.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const aws = require('aws-sdk');

const dynamoDB = new aws.DynamoDB.DocumentClient();
const uuidv4 = require('uuid/v4');
const moment = require('moment');
const bcrypt = require('bcryptjs');

function hashPassword(userPassword) {
let Salt = bcrypt.genSaltSync(15);
const Salt = bcrypt.genSaltSync(15);
return bcrypt.hashSync(userPassword, Salt);
}

async function checkEmail(userEmail) {
let params = {
const params = {
TableName: process.env.dynamodb_table_name,
IndexName: process.env.dynamodb_index_name,
ProjectionExpression: 'user_id, email',
Expand All @@ -19,7 +20,7 @@ async function checkEmail(userEmail) {
':email': userEmail
}
};
let data = await dynamoDB.query(params).promise();
const data = await dynamoDB.query(params).promise();
return data.Count;
}

Expand All @@ -34,7 +35,7 @@ async function createUser(
identificationImages,
createdAt
) {
let params = {
const params = {
TableName: process.env.dynamodb_table_name,
Item: {
user_id: userId,
Expand All @@ -55,70 +56,69 @@ async function createUser(
updated_at: createdAt
}
};
let data = await dynamoDB.put(params).promise();
const data = await dynamoDB.put(params).promise();
return data;
}

exports.handler = async event => {
let body = JSON.parse(event.body);
let userEmail = body.email;
let userPassword = body.password;
let firstName = body.first_name;
let lastName = body.last_name;
let userPhone = body.phone;
let identificationImages = body.user_identifications;
exports.handler = async (event) => {
const body = JSON.parse(event.body);
const userEmail = body.email;
const userPassword = body.password;
const firstName = body.first_name;
const lastName = body.last_name;
const userPhone = body.phone;
const identificationImages = body.user_identifications;

let emailIsUsed = await checkEmail(userEmail);
const emailIsUsed = await checkEmail(userEmail);

if (emailIsUsed > 0) {
let responseBody = {
const responseBody = {
message: 'Email has already been used'
};
return {
statusCode: 409,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(responseBody)
};
} else {
let userId = 'usr_' + uuidv4();
let bearerToken = uuidv4();
let createdAt = moment().format('YYYY-MM-DDTHH:mm:ss-04:00');
let passwordHash = hashPassword(userPassword);
await createUser(
userId,
bearerToken,
userEmail,
passwordHash,
firstName,
lastName,
userPhone,
identificationImages,
createdAt
);
let responseBody = {
message: 'User has been created',
user: {
user_id: userId,
email: userEmail,
bearer_token: bearerToken,
first_name: firstName,
last_name: lastName,
phone: userPhone,
user_identifications: {
identification_image_front:
}
const userId = `usr_${uuidv4()}`;
const bearerToken = uuidv4();
const createdAt = moment().format('YYYY-MM-DDTHH:mm:ss-04:00');
const passwordHash = hashPassword(userPassword);
await createUser(
userId,
bearerToken,
userEmail,
passwordHash,
firstName,
lastName,
userPhone,
identificationImages,
createdAt
);
const responseBody = {
message: 'User has been created',
user: {
user_id: userId,
email: userEmail,
bearer_token: bearerToken,
first_name: firstName,
last_name: lastName,
phone: userPhone,
user_identifications: {
identification_image_front:
identificationImages.identification_image_front,
identification_image_back:
identification_image_back:
identificationImages.identification_image_back,
selfie_image: identificationImages.selfie_image
},
created_at: createdAt,
updated_at: createdAt
}
};
return {
statusCode: 201,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(responseBody)
};
}
selfie_image: identificationImages.selfie_image
},
created_at: createdAt,
updated_at: createdAt
}
};
return {
statusCode: 201,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(responseBody)
};
};
59 changes: 28 additions & 31 deletions goToNextStop.js → forward_trip_place.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
const AWS = require('aws-sdk');
var dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports.handler = function(event, context, callback) {
var timestamp = new Date().getTime();
const dynamoDb = new AWS.DynamoDB.DocumentClient();

var tripId = event.pathParameters.trip;
const MissingIdOnRequestError = {
statusCode: 400,
message: 'Id de trip no especificada en request'
};

const TripInValidState = {
statusCode: 400,
message: 'Viaje no se encuentra en curso'
};
const NotFound = {
statusCode: 404,
message: 'No se ha encontrado lo que buscas'
};
const InternalServerError = {
statusCode: 503,
message: 'Algo inesperado acaba de pasar... gracias por intentar más tarde'
};

module.exports.handler = function forwardTripPlace(event, context, callback) {
const timestamp = new Date().getTime();

const tripId = event.pathParameters.trip;
if (!tripId) return callback(null, MissingIdOnRequestError);

var getParams = {
const getParams = {
TableName: 'SalgoDe_Trips',
Key: {
trip_id: tripId
Expand All @@ -26,28 +45,28 @@ module.exports.handler = function(event, context, callback) {
return callback(null, TripInValidState);
}
getData.Item.next_stop = getData.Item.next_stop || 0;
getData.Item.next_stop = getData.Item.next_stop + 1;
getData.Item.next_stop += 1;
getData.Item.updatedAt = timestamp;

if (getData.Item.next_stop === getData.Item.route_points.length) {
getData.Item.trip_status = 'finished';
}

var putParams = {
const putParams = {
TableName: 'SalgoDe_Trips',
Key: {
trip_id: tripId
},
Item: getData.Item
};

return dynamoDb.put(putParams, error => {
return dynamoDb.put(putParams, (error) => {
if (error) {
console.error(error);
return callback(null, InternalServerError);
}

var response = {
const response = {
statusCode: 200
};
return callback(null, response);
Expand All @@ -58,25 +77,3 @@ module.exports.handler = function(event, context, callback) {
function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}

var MissingIdOnRequestError = {
statusCode: 400,
message: 'Id de trip no especificada en request'
};

var TripInValidState = {
statusCode: 400,
message: 'Viaje no se encuentra en curso'
};
var Unauthorized = {
statusCode: 401,
message: 'No estás autorizado para esto'
};
var NotFound = {
statusCode: 404,
message: 'No se ha encontrado lo que buscas'
};
var InternalServerError = {
statusCode: 503,
message: 'Algo inesperado acaba de pasar... gracias por intentar más tarde'
};
Loading

0 comments on commit 730223a

Please sign in to comment.