-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(get_user): adapt to new user images structure
- Loading branch information
Franco Méndez
committed
Oct 27, 2019
1 parent
ec010d6
commit b3467d5
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const aws = require('aws-sdk'); | ||
|
||
const dynamoDB = new aws.DynamoDB.DocumentClient(); | ||
|
||
const UsersTableName = process.env.dynamodb_users_table_name; | ||
|
||
async function getUser(userId) { | ||
const params = { | ||
TableName: UsersTableName, | ||
Key: { | ||
user_id: userId | ||
}, | ||
ProjectionExpression: 'user_id, first_name, last_name, phone, user_identifications' | ||
}; | ||
const data = await dynamoDB.get(params).promise(); | ||
return data.Item; | ||
} | ||
|
||
function parseBody(result) { | ||
return { | ||
user_id: result.user_id, | ||
first_name: result.first_name, | ||
last_name: result.last_name, | ||
avatar: result.user_identifications.selfie_image, | ||
user_verifications: { | ||
phone: !!result.phone, | ||
identity: | ||
!!result.user_identifications.identification.front | ||
&& !!result.user_identifications.identification.back, | ||
driver_license: | ||
!!result.user_identifications.driver_license.front | ||
&& !!result.user_identifications.driver_license.back | ||
} | ||
}; | ||
} | ||
|
||
exports.handler = async (event) => { | ||
const userId = event.pathParameters.user; | ||
const result = await getUser(userId); | ||
if (result) { | ||
return { | ||
statusCode: 200, | ||
headers: { 'Access-Control-Allow-Origin': '*' }, | ||
body: JSON.stringify(parseBody(result)) | ||
}; | ||
} | ||
|
||
const responseBody = { | ||
message: 'User does not exist' | ||
}; | ||
return { | ||
statusCode: 422, | ||
headers: { 'Access-Control-Allow-Origin': '*' }, | ||
body: JSON.stringify(responseBody) | ||
}; | ||
}; |