diff --git a/backend/functions/add-dummy-data/app.js b/backend/functions/add-dummy-data/app.js new file mode 100644 index 00000000..d90a90da --- /dev/null +++ b/backend/functions/add-dummy-data/app.js @@ -0,0 +1,146 @@ +const dbConnection = require('dbConnection.js'); +const mysql = require('mysql'); + +/** + * Sample Lambda function which mocks the operation of buying a random number of shares for a stock. + * For demonstration purposes, this Lambda function does not actually perform any actual transactions. It simply returns a mocked result. + * + * @param {Object} event - Input event to the Lambda function + * @param {Object} context - Lambda Context runtime methods and attributes + * + * @returns {Object} object - Object containing details of the stock buying transaction + * + */ +exports.lambdaHandler = async (event, context) => { + const con = await dbConnection.connectDB( + process.env.DatabaseAddress, + 'user', + 'Password1234', + 'databaseAmazonianPrime', + ); + + const addUserQueries = [ + `INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (1, "John", "Doe", "johndoe@gmail.com", "Marketing", false)`, + `INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (2, "Alice", "Ather", "aather@gmail.com", null, false)`, + `INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (3, "Bob", "Carlson", "bobc321@gmail.com", "Management", true)` + ]; + + addUserQueries.forEach(async (queryString) => { + let insertUsers = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into Users'); + reject(err); + } + resolve(res); + }); + }); + }); + + const addListingsQueries = [ + `INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, Size, Brand, Colour, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (1, 1, "MCM Arm Chair", "Great condition.", 40.00, 2, "Amazon Merchandise", "1m", "Unknown", "Brown", "Used- Like New", "2023-03-01 12:00:00", true)`, + `INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, Size, Brand, Colour, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (2, 2, "Healthy Monstera", "Selling this very healthy monstera", 20.00, 1, "Garden & Outdoors", "2m", NULL, NULL, "New", "2023-03-01 12:00:00", true)`, + `INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, Size, Brand, Colour, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (3, 3, "Listing 3", "Listing 3 Description", 5.00, 10, "Miscellaneous", NULL, NULL, NULL, "Used Fair", "2023-03-10 12:00:00", false)` + ]; + + addListingsQueries.forEach(async (queryString) => { + let insertListing = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into Listings'); + reject(err); + } + resolve(res); + }); + }); + }); + + const addCountryQueries = [ + `INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Vanvouver", "BC", "100 St", "V0V 0V0", "Canada")`, + `INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Richmond", "BC", "1000 St", "V1V 1V1", "Canada")`, + `INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Toronto", "ON", "2000 St", "V2V 2V2", "Canada")` + ]; + + addCountryQueries.forEach(async (queryString) => { + let insertCountry = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into Country'); + reject(err); + } + resolve(res); + }); + }); + }); + + const addAddressQueries = [ + `INSERT INTO Address (AddressID, CityName, Province, StreetAddress) VALUES (1, "Vanvouver", "BC", "100 St")`, + `INSERT INTO Address (AddressID, CityName, Province, StreetAddress) VALUES (2, "Richmond", "BC", "1000 St")`, + `INSERT INTO Address (AddressID, CityName, Province, StreetAddress) VALUES (3, "Toronto", "ON", "2000 St")` + ]; + + addAddressQueries.forEach(async (queryString) => { + let insertAddress = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into Address'); + reject(err); + } + resolve(res); + }); + }); + }); + + const addPaymentDetailsQueries = [ + `INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (1, 1, 1, 1234123412341234, "1025", 100, "John Doe")`, + `INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (2, 2, 2, 4321432143214321, "0626", 200, "Alice Ather")`, + `INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (3, 3, 3, 1234567812345678, "0825", 300, "Bob Carlson")` + ]; + + addPaymentDetailsQueries.forEach(async (queryString) => { + let insertPaymentDetails = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into PaymentDetails'); + reject(err); + } + resolve(res); + }); + }); + }); + + const addBankingDetailsQueries = [ + `INSERT INTO BankingDetails (BankingID, UserID, AddressID, InstitutionNum, AccountNum, TransitNum, NameOnCard) VALUES (1, 1, 1, 001, 001, 001, "John Doe")`, + `INSERT INTO BankingDetails (BankingID, UserID, AddressID, InstitutionNum, AccountNum, TransitNum, NameOnCard) VALUES (2, 2, 2, 002, 002, 002, "Alice Ather")`, + `INSERT INTO BankingDetails (BankingID, UserID, AddressID, InstitutionNum, AccountNum, TransitNum, NameOnCard) VALUES (3, 3, 3, 003, 003, 003, "Bob Carlson")` + ]; + + addBankingDetailsQueries.forEach(async (queryString) => { + let insertBankingDetails = await new Promise((resolve, reject) => { + con.query(queryString, function (err, res) { + if (err) { + console.log('Error inserting dummy data into BankingDetails'); + reject(err); + } + resolve(res); + }); + }); + }); + + const getAllUsersQuery = `SELECT * FROM Users`; + + const getAllUsers = await new Promise((resolve, reject) => { + con.query(getAllUsersQuery, function (err, res) { + if (err) { + console.log("Couldn't get all Users from database!"); + reject(err); + } + resolve(res); + }); + }); + + return { + statusCode: 200, + body: JSON.stringify(getAllUsers), + }; +}; diff --git a/backend/functions/add-dummy-data/package.json b/backend/functions/add-dummy-data/package.json new file mode 100644 index 00000000..2cc8d607 --- /dev/null +++ b/backend/functions/add-dummy-data/package.json @@ -0,0 +1,20 @@ +{ + "name": "hello_world", + "version": "1.0.0", + "description": "hello world sample for NodeJS", + "main": "app.js", + "repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs", + "author": "SAM CLI", + "license": "MIT", + "dependencies": { + "axios": "^0.21.1", + "mysql": "^2.18.1" + }, + "scripts": { + "test": "mocha tests/unit/" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^9.1.4" + } +} diff --git a/backend/template.yaml b/backend/template.yaml index acb90d41..703bb4cc 100644 --- a/backend/template.yaml +++ b/backend/template.yaml @@ -611,6 +611,37 @@ Resources: Layers: - !Ref DBSharedLayer - !Ref NodeDependenciesLayer + + AddDummyFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: functions/add-dummy-data/ + Handler: app.lambdaHandler + Runtime: nodejs14.x + Architectures: + - x86_64 + Events: + Listings: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /api/user/dummy # Ensure api is proxied with /api/* + Method: post + VpcConfig: + SecurityGroupIds: [{ "Ref": "lambdaSecurityGroup" }] + SubnetIds: [{ "Ref": "PrivateSubnet1" }, { "Ref": "PrivateSubnet2" }] + Environment: + Variables: + DatabaseAddress: !GetAtt rdsDBInstance.Endpoint.Address + Policies: + - Statement: + - Sid: RDSLambdaConnectPolicy + Effect: Allow + Action: + - rds-db:connect + Resource: "*" + Layers: + - !Ref DBSharedLayer + - !Ref NodeDependenciesLayer DBSharedLayer: Type: AWS::Serverless::LayerVersion