Skip to content

Commit

Permalink
debug and implement update and post accommodation through api
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenchee97 committed Oct 26, 2024
1 parent 448dbaa commit 50f500c
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 57 deletions.
21 changes: 21 additions & 0 deletions amplify/backend/backend-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
"providerPlugin": "awscloudformation",
"service": "Lambda"
},
"generateEncryptionKeyAPI": {
"build": true,
"providerPlugin": "awscloudformation",
"service": "Lambda"
},
"geocodingHandler": {
"build": true,
"providerPlugin": "awscloudformation",
Expand Down Expand Up @@ -144,6 +149,22 @@
}
]
},
"AMPLIFY_function_generateEncryptionKeyAPI_deploymentBucketName": {
"usedBy": [
{
"category": "function",
"resourceName": "generateEncryptionKeyAPI"
}
]
},
"AMPLIFY_function_generateEncryptionKeyAPI_s3Key": {
"usedBy": [
{
"category": "function",
"resourceName": "generateEncryptionKeyAPI"
}
]
},
"AMPLIFY_function_geocodingHandler_deploymentBucketName": {
"usedBy": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pluginId": "amplify-nodejs-function-runtime-provider",
"functionRuntime": "nodejs",
"useLegacyBuild": true,
"defaultEditorFile": "src\\index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"Action": [],
"Resource": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lambdaLayers": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "{\"createdOn\":\"Windows\",\"createdBy\":\"Amplify\",\"createdWith\":\"12.13.0\",\"stackType\":\"function-Lambda\",\"metadata\":{\"whyContinueWithGen1\":\"\"}}",
"Parameters": {
"CloudWatchRule": {
"Type": "String",
"Default": "NONE",
"Description": " Schedule Expression"
},
"deploymentBucketName": {
"Type": "String"
},
"env": {
"Type": "String"
},
"s3Key": {
"Type": "String"
}
},
"Conditions": {
"ShouldNotCreateEnvResources": {
"Fn::Equals": [
{
"Ref": "env"
},
"NONE"
]
}
},
"Resources": {
"LambdaFunction": {
"Type": "AWS::Lambda::Function",
"Metadata": {
"aws:asset:path": "./src",
"aws:asset:property": "Code"
},
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "deploymentBucketName"
},
"S3Key": {
"Ref": "s3Key"
}
},
"Handler": "index.handler",
"FunctionName": {
"Fn::If": [
"ShouldNotCreateEnvResources",
"generateEncryptionKeyAPI",
{
"Fn::Join": [
"",
[
"generateEncryptionKeyAPI",
"-",
{
"Ref": "env"
}
]
]
}
]
},
"Environment": {
"Variables": {
"ENV": {
"Ref": "env"
},
"REGION": {
"Ref": "AWS::Region"
}
}
},
"Role": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
},
"Runtime": "nodejs18.x",
"Layers": [],
"Timeout": 25
}
},
"LambdaExecutionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": {
"Fn::If": [
"ShouldNotCreateEnvResources",
"unirentLambdaRolef4eecc0c",
{
"Fn::Join": [
"",
[
"unirentLambdaRolef4eecc0c",
"-",
{
"Ref": "env"
}
]
]
}
]
},
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
}
}
},
"lambdaexecutionpolicy": {
"DependsOn": [
"LambdaExecutionRole"
],
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "lambda-execution-policy",
"Roles": [
{
"Ref": "LambdaExecutionRole"
}
],
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": {
"Fn::Sub": [
"arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*",
{
"region": {
"Ref": "AWS::Region"
},
"account": {
"Ref": "AWS::AccountId"
},
"lambda": {
"Ref": "LambdaFunction"
}
}
]
}
}
]
}
}
}
},
"Outputs": {
"Name": {
"Value": {
"Ref": "LambdaFunction"
}
},
"Arn": {
"Value": {
"Fn::GetAtt": [
"LambdaFunction",
"Arn"
]
}
},
"Region": {
"Value": {
"Ref": "AWS::Region"
}
},
"LambdaExecutionRole": {
"Value": {
"Ref": "LambdaExecutionRole"
}
},
"LambdaExecutionRoleArn": {
"Value": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
24 changes: 24 additions & 0 deletions amplify/backend/function/generateEncryptionKeyAPI/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const AWS = require("aws-sdk");
const kms = new AWS.KMS();

AWS.config.update({ region: "ap-southeast-1" });

exports.handler = async (event) => {
console.log(`EVENT: ${JSON.stringify(event)}`);
try {
const dekParams = { KeyId: keyId, KeySpec: "AES_256" };
const dekResponse = await kms.generateDataKey(dekParams).promise();
const plaintextDek = dekResponse.Plaintext.toString("base64");
console.log("plaintextDek: " + plaintextDek);
return {
statusCode: 200,
body: JSON.stringify({ dek: plaintextDek })
};
} catch (error) {
console.error("Error generating DEK:", error);
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
}
};
10 changes: 10 additions & 0 deletions amplify/backend/function/generateEncryptionKeyAPI/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "generateencryptionkeyapi",
"version": "2.0.0",
"description": "Lambda function generated by Amplify",
"main": "index.js",
"license": "Apache-2.0",
"devDependencies": {
"@types/aws-lambda": "^8.10.92"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/aws-lambda@^8.10.92":
version "8.10.145"
resolved "https://registry.yarnpkg.com/@types/aws-lambda/-/aws-lambda-8.10.145.tgz#b2d31a987f4888e5553ff1819f57cafa475594d9"
integrity sha512-dtByW6WiFk5W5Jfgz1VM+YPA21xMXTuSFoLYIDY0L44jDLLflVPtZkYuu3/YxpGcvjzKFBZLU+GyKjR0HOYtyw==
16 changes: 9 additions & 7 deletions amplify/backend/function/recommendationHandler/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,24 @@ app.post("/recommendation", async function (req, res) {
longitude: req.body.coords.longitude
};

// TODO: Check the structure of address and find out how to decrpyt data
for (let i = 0; i < data.length; i++) {

const address = JSON.parse(data[i].address);
console.log(`address[${i}]: ` + JSON.stringify(address, null, 2));

const addressCoords = {
latitude: address.geo.geometry.location.lat,
longitude: address.geo.geometry.location.lng
};
// const addressCoords = {
// latitude: address.geo.geometry.location.lat,
// longitude: address.geo.geometry.location.lng
// };

const jsonAddress = JSON.parse(address);

console.log(`userCoords[${i}]: ` + JSON.stringify(userCoords, null, 2));
console.log(`addressCoords[${i}]: ` + JSON.stringify(userCoords, null, 2));
console.log(`jsonAddress.geo[${i}]: ` + JSON.stringify(jsonAddress.geo, null, 2));

// const dist = geolib.getDistance(req.body.coords, address.geo);
const dist = geolib.getDistance(userCoords, addressCoords);
// const dist = geolib.getDistance(userCoords, addressCoords);
const dist = geolib.getDistance(userCoords, jsonAddress.geo);
console.log(`dist[${i}]: ` + dist);

map.set(data[i], dist);
Expand Down
7 changes: 7 additions & 0 deletions amplify/backend/types/amplify-dependent-resources-ref.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export type AmplifyDependentResourcesAttributes = {
"Name": "string",
"Region": "string"
},
"generateEncryptionKeyAPI": {
"Arn": "string",
"LambdaExecutionRole": "string",
"LambdaExecutionRoleArn": "string",
"Name": "string",
"Region": "string"
},
"geocodingHandler": {
"Arn": "string",
"LambdaExecutionRole": "string",
Expand Down
6 changes: 5 additions & 1 deletion amplify/team-provider-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
},
"recommendationHandler": {
"deploymentBucketName": "amplify-unirent-staging-195410-deployment",
"s3Key": "amplify-builds/recommendationHandler-726e587555504a4e3555-build.zip"
"s3Key": "amplify-builds/recommendationHandler-2f4134666c38362f412f-build.zip"
},
"generateEncryptionKeyAPI": {
"deploymentBucketName": "amplify-unirent-staging-195410-deployment",
"s3Key": "amplify-builds/generateEncryptionKeyAPI-4c544d3658454135734e-build.zip"
}
},
"storage": {
Expand Down
3 changes: 2 additions & 1 deletion src/api/GoogleMapsAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export async function getGeocode(request) {

try {
const response = await API.post(apiName, path, myInit);
return response;
console.log(response.geometry.location)
return response.geometry.location;
} catch (error) {
console.error("Error in google maps API: " + error);
}
Expand Down
Loading

0 comments on commit 50f500c

Please sign in to comment.