Skip to content

Commit

Permalink
add basic validation, fix argument to getSingleGeo
Browse files Browse the repository at this point in the history
relates #67 #58
  • Loading branch information
arrested-developer committed Oct 2, 2019
1 parent d08ee8d commit bdb302b
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/lambda/addLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ const getCurrentUserId = async () => {
export async function handler(event, context) {
// eslint-disable-next-line no-param-reassign
context.callbackWaitsForEmptyEventLoop = false;
if (event.httpMethod === 'POST') {
const location = JSON.parse(event.body);
try {
await connectToDatabase();
location.submittedBy = await getCurrentUserId();
location.geoLocation = await getSingleGeo(location);
const result = await addRentalRecord(location);
return {
statusCode: 200,
body: JSON.stringify(result),
};
} catch (err) {
if (event.httpMethod !== 'POST') {
return {
statusCode: 405,
};
}
try {
const rentalRecord = JSON.parse(event.body);
if (!rentalRecord.postcode) {
return {
statusCode: 500,
body: JSON.stringify({ error: 'Server error' }),
statusCode: 400,
body: { error: 'Location must include postcode' },
};
}
await connectToDatabase();
rentalRecord.submittedBy = await getCurrentUserId();
rentalRecord.geoLocation = await getSingleGeo(rentalRecord.postcode);
const result = await addRentalRecord(rentalRecord);
return {
statusCode: 200,
body: JSON.stringify(result),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: 'Server error' }),
};
}
return {
statusCode: 405,
};
}

0 comments on commit bdb302b

Please sign in to comment.