Skip to content

Commit

Permalink
deleteUserVehicle lambda completed and uploaded
Browse files Browse the repository at this point in the history
Added documentation to all completed lambda functions
#minor
  • Loading branch information
Delaney H committed Apr 14, 2024
1 parent da2342b commit 28dcf61
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 135 deletions.
31 changes: 26 additions & 5 deletions backend-lambda/OAuthGetUrl/index.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { google } from 'googleapis'
/**
* @fileoverview This file contains a single AWS Lambda function handler for generating a Google OAuth URL.
* It uses the 'googleapis' library to interact with Google's OAuth2 API.
*
* @author dheffer
* @version 1.0.0
*/

// Importing required modules
import { google } from 'googleapis' // Google APIs library

/**
* AWS Lambda function handler for generating a Google OAuth URL.
*
* @param {Object} event - The incoming event.
* @param {Object} context - The context of the function.
* @returns {Object} The response object containing the generated URL.
*/
export const handler = async (event, context) => {
const url = getGoogleAuthUrl();

return { url };
};

/**
* Function to generate a Google OAuth URL.
*
* @returns {string} The generated Google OAuth URL.
*/
const getGoogleAuthUrl = () => {
const oauth = new google.auth.OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.OAUTH_CALLBACK_URL
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.OAUTH_CALLBACK_URL
);
return oauth.generateAuthUrl({
access_type: 'offline',
Expand All @@ -20,4 +41,4 @@ const getGoogleAuthUrl = () => {
'https://www.googleapis.com/auth/userinfo.profile',
],
});
};
};
63 changes: 52 additions & 11 deletions backend-lambda/deleteMaintenanceHistory/index.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,70 @@
import {MongoClient, ServerApiVersion} from "mongodb";
import 'dotenv/config';
import jwt from 'jsonwebtoken';
/**
* @fileoverview This file contains a single AWS Lambda function handler for deleting a vehicle's maintenance history.
* It uses MongoDB as a database and JWT for authentication.
*
* @author dheffer
* @version 1.0.0
*/

// Importing required modules
import {MongoClient, ServerApiVersion} from "mongodb"; // MongoDB driver
import 'dotenv/config'; // Module to load environment variables
import jwt from 'jsonwebtoken'; // Library to work with JSON Web Tokens

/**
* AWS Lambda function handler for deleting a vehicle's maintenance history.
*
* @param {Object} event - The incoming event.
* @param {Object} context - The context of the function.
* @returns {Promise<Object>} The response object with a status code and a body.
*/
export const handler = async (event, context) => {

// MongoDB connection string
const uri = process.env.MONGO_URI;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true

// Creating a new MongoDB client
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
}
});
);

// Extracting the 'Authorization' header and 'config_id' from the incoming event
const authorization = event.headers['Authorization'];
const config_id = event['queryStringParameters'].config_id;

// Parsing the body of the incoming event
const data = JSON.parse(event.body);

// Connecting to the MongoDB database
return client.connect()
.then(async () => {
// Extracting the JWT from the 'Authorization' header
const token = authorization.split(" ")[1];

// Verifying the JWT
const verified = jwt.verify(token, process.env.JWTSecret);

// If the JWT is invalid, return a 401 status code with a message
if (!verified) {
return {
statusCode: 401,
body: JSON.stringify({message: "Token invalid"})
};
}

// Decoding the JWT
const decoded = jwt.decode(token, process.env.JWTSecret);
console.log(decoded.email)

// Accessing the 'vehicleDB' database and 'user_vehicle_info' collection
const database = client.db("vehicleDB");
const garage = database.collection("user_vehicle_info");

// Updating a document in the 'user_vehicle_info' collection
return await garage.updateOne(
{
email: decoded.email,
Expand All @@ -47,14 +82,20 @@ export const handler = async (event, context) => {
}
);
}).then(res => {
// Logging the result of the update operation and returning it as a JSON string with a 200 status code
console.log(res);
return {
statusCode: 200,
body: JSON.stringify({message: res})
};
}).catch(err => {
// If any error occurs during the process, return a 500 status code with an error message
return {
statusCode: 500,
body: JSON.stringify({ message: `Internal Server Error: ${err.message}` })
};
}).finally(() => client.close());
}).finally(() => {
// Closing the database connection
client.close();
});
};
110 changes: 92 additions & 18 deletions backend-lambda/deleteUserVehicle/index.mjs
Original file line number Diff line number Diff line change
@@ -1,45 +1,119 @@
import {MongoClient, ServerApiVersion} from "mongodb";
import 'dotenv/config';
/**
* @fileoverview This file contains a single AWS Lambda function handler for deleting a user's vehicle.
* It uses MongoDB as a database and JWT for authentication.
*
* @author dheffer
* @version 1.0.0
*/

// Importing required modules
import {MongoClient, ServerApiVersion} from "mongodb"; // MongoDB driver
import 'dotenv/config'; // Module to load environment variables
import jwt from 'jsonwebtoken'; // Library to work with JSON Web Tokens

/**
* AWS Lambda function handler for deleting a user's vehicle.
*
* @param {Object} event - The incoming event.
* @param {Object} context - The context of the function.
* @returns {Promise<Object>} The response object with a status code and a body.
*/
export const handler = async (event, context) => {

// MongoDB connection string
const uri = process.env.MONGO_URI;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true

// Creating a new MongoDB client
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
}
});
const config_id = event['config_id'];
const email = event.headers['email'];
);

// Extracting the 'Authorization' header and 'config_id' from the incoming event
const authorization = event.headers['Authorization'];
const config_id = event['queryStringParameters'].config_id;

// Connecting to the MongoDB database
return client.connect()
.then(async () => {
// Extracting the JWT from the 'Authorization' header
const token = authorization.split(" ")[1];

// Verifying the JWT
const verified = jwt.verify(token, process.env.JWTSecret);

// If the JWT is invalid, return a 401 status code with a message
if (!verified) {
return {
statusCode: 401,
body: JSON.stringify({message: "Token invalid"})
};
}

// Decoding the JWT
const decoded = jwt.decode(token, process.env.JWTSecret);

// Accessing the 'vehicleDB' database and 'user_garage' collection
const database = client.db("vehicleDB");
const garage = database.collection("user_garage");
await garage.updateOne(
{ email: email },
{ $pull: { vehicle_config_ids: config_id } }

// Updating a document in the 'user_garage' collection
return await garage.updateOne(
{email: decoded.email},
{$pull: {vehicle_config_ids: config_id}}
);
}).then(res => {
// Logging the result of the update operation and returning it as a JSON string with a 200 status code
console.log(res);
return {
statusCode: 200,
body: JSON.stringify({message: `Vehicle with config ${config_id} deleted from garage`})
body: JSON.stringify({message: res})
};
}).then(async () => {
// Extracting the JWT from the 'Authorization' header
const token = authorization.split(" ")[1];

// Verifying the JWT
const verified = jwt.verify(token, process.env.JWTSecret);

// If the JWT is invalid, return a 401 status code with a message
if (!verified) {
return {
statusCode: 401,
body: JSON.stringify({message: "Token invalid"})
};
}

// Decoding the JWT
const decoded = jwt.decode(token, process.env.JWTSecret);

// Accessing the 'vehicleDB' database and 'user_vehicle_info' collection
const database = client.db("vehicleDB");
const vehicInfo = database.collection("user_vehicle_info");
await vehicInfo.deleteOne(
{ email: email, config_id: config_id }

// Deleting a document in the 'user_vehicle_info' collection
return await vehicInfo.deleteOne(
{email: decoded.email, config_id: config_id}
);
}).then(res => {
// Logging the result of the delete operation and returning it as a JSON string with a 200 status code
console.log(res);
return {
statusCode: 200,
body: JSON.stringify({message: `Vehicle with config ${config_id} deleted from garage`})
body: JSON.stringify({message: res})
};
}).catch(err => {
// If any error occurs during the process, return a 500 status code with an error message
return {
statusCode: 500,
body: JSON.stringify({ message: `Internal Server Error: ${err.message}` })
};
}).finally(() => client.close());
}).finally(() => {
// Closing the database connection
client.close();
});
};
1 change: 1 addition & 0 deletions backend-lambda/deleteUserVehicle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.5.0"
}
}
Loading

0 comments on commit 28dcf61

Please sign in to comment.