Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions takeNotes-src/postUser/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const response = (statusCode, body, additionalHeaders) => ({
},
});

//logger helper
const logger = (valueName, value) => console.log(`${valueName}: ${value}`);

function isValidRequest(context, event) {
const body = JSON.parse(event.body);
return (
Expand All @@ -36,8 +39,8 @@ function isValidRequest(context, event) {
body.cognitoId !== null &&
body.email !== null &&
body.startDate !== null &&
body.endDate !== null
)
body.endDate !== null
);
}

let getDateFromISO = (date) => new Date(date);
Expand All @@ -50,8 +53,7 @@ let getNumWeeks = (start, end) => {

let getWeeks = (start, end) => {
let numWeeks = getNumWeeks(start, end);
console.log("numWeeks");
console.log(numWeeks);
logger("numweeks", numWeeks);
let dateString = new Date().toISOString();
let weeks = new Array(numWeeks);
for (var i = 0; i < numWeeks; i++) {
Expand All @@ -70,10 +72,7 @@ let getWeeks = (start, end) => {
* @return {object}
*/
let generateDoc = (attributes) => {
let [start, end] = [
attributes.startDate,
attributes.endDate
];
let [start, end] = [attributes.startDate, attributes.endDate];
let weeks = getWeeks(start, end);
return {
email: attributes.email,
Expand All @@ -82,7 +81,7 @@ let generateDoc = (attributes) => {
journal: {
weeks: weeks,
},
notes: [],
notes: {},
};
};

Expand All @@ -104,21 +103,17 @@ function addRecord(event) {
Item: itemBody,
ReturnValues: "ALL_OLD",
};
console.log("params");
console.log(params);
logger("params", params);

// Return the new object
return [docClient.put(params), params];
}

// Lambda Handler
exports.postUser = async (event, context, callback) => {
console.log("event");
console.log(event);
console.log("event type");
console.log(typeof(event));
console.log("callback");
console.log(callback);
logger("event", event);
logger("event type", typeof event);
logger("callback", callback);
if (!isValidRequest(context, event)) {
return response(400, { message: "Error: Invalid request" });
}
Expand All @@ -129,11 +124,10 @@ exports.postUser = async (event, context, callback) => {

return response(200, {
promise: dbPromise,
input: dbInput
input: dbInput,
});
} catch (err) {
console.log("err");
console.log(err.message);
logger("error", err.message);
return response(500, { message: err.message });
}
};
106 changes: 106 additions & 0 deletions takeNotes-src/putNotes/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
*.DS_Store
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
*.json

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
79 changes: 79 additions & 0 deletions takeNotes-src/putNotes/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// default imports
const AWS = require("aws-sdk");
const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
const { v4: uuidv4 } = require("uuid");

// environment variables
const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env;
const options = { region: REGION };
AWS.config.update({ region: REGION });

if (ENDPOINT_OVERRIDE !== "") {
options.endpoint = ENDPOINT_OVERRIDE;
}

const docClient = new AWS.DynamoDB.DocumentClient(options);

// response helper
const response = (statusCode, body, additionalHeaders) => ({
statusCode,
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
...additionalHeaders,
},
});

function isValidRequest(context, event) {
let isIdValid =
event !== null &&
event.pathParameters !== null &&
event.pathParameters.id !== null;

let body = event.body;
let isBodyValid = body !== null && body.notes !== null;

return isIdValid && isBodyValid;
}

function updateRecord(recordId, eventBody, noteIdx) {
let d = new Date();
console.log("record id: " + recordId + " eventBody: " + eventBody.notes);
const params = {
TableName: TABLE_NAME,
Key: {
id: recordId,
},
UpdateExpression: `set updated = :u, docBody.notes.#noteId = :n`,
ExpressionAttributeNames: { "#noteId": noteIdx },
ExpressionAttributeValues: {
":u": d.toISOString(),
":n": eventBody.notes,
},
ConditionExpression: "attribute_exists(docBody.notes.#noteId)",
ReturnValues: "ALL_NEW",
};
console.log("params: " + params);
return docClient.update(params);
}

// Lambda Handler
exports.putNotes = async (event, context, callback) => {
console.log("event: " + event);
console.log("body: " + event.body);
if (!isValidRequest(context, event)) {
return response(400, { message: "Error: Invalid request" });
}

try {
let data = await updateRecord(
event.pathParameters.id,
JSON.parse(event.body),
event.pathParameters.noteIdx
).promise();
return response(200, data);
} catch (err) {
return response(400, { message: err.message });
}
};
109 changes: 109 additions & 0 deletions takeNotes-src/putNotes/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading