-
Notifications
You must be signed in to change notification settings - Fork 0
/
review_user_image.js
123 lines (110 loc) · 3.58 KB
/
review_user_image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const aws = require('aws-sdk');
const moment = require('moment');
const UsersTableName = process.env.dynamodb_users_table_name;
const dynamoDB = new aws.DynamoDB.DocumentClient();
const ses = new aws.SES();
async function getUser(userId, imageField) {
const params = {
TableName: UsersTableName,
Key: { user_id: userId },
ProjectionExpression: 'email, first_name, user_identifications'
};
const data = await dynamoDB.get(params).promise();
const [field, subfield] = imageField.split('.');
if (subfield) {
return [data.Item.email, data.Item.first_name, data.Item.user_identifications[field][subfield]];
}
return [data.Item.email, data.Item.first_name, data.Item.user_identifications[field]];
}
async function reviewImage(userId, imageField) {
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss');
const [field, subfield] = imageField.split('.');
const params = {
TableName: UsersTableName,
Key: {
user_id: userId
},
ExpressionAttributeNames: {},
ExpressionAttributeValues: { ':t': true, ':now': timestamp }
};
if (subfield) {
params.UpdateExpression = 'set user_verifications.#field.#subfield_checked = :t, updated_at = :now';
params.ExpressionAttributeNames['#field'] = field;
params.ExpressionAttributeNames['#subfield_checked'] = `${subfield}_checked`;
} else {
params.UpdateExpression = 'set user_verifications.#field_checked = :t, updated_at = :now';
params.ExpressionAttributeNames['#field_checked'] = `${field}_checked`;
}
const data = await dynamoDB.update(params).promise();
return data.Attributes;
}
async function notifyUser(recipientAddress, recipientName, imageField) {
const mapFields = {
selfie_image: 'selfie',
'identification.front': 'la parte frontal de tu carnet de identidad',
'identification.back': 'la parte trasera de tu carnet de identidad',
'driver_license.front': 'la parte frontal de tu licencia de conducir',
'driver_license.back': 'la parte trasera de tu licencia de conducir'
};
const params = {
Destination: {
ToAddresses: [recipientAddress]
},
Message: {
Subject: { Data: 'SALGODE - Imagen con problemas' },
Body: {
Text:
{
Data:
`Hola ${recipientName}!
La imagen que subiste como ${mapFields[imageField]} no ha podido ser verificada.
Por favor, sube una foto que no deje dudas al respecto.
Atentamente,
Equipo #SalgoDe`
}
}
},
Source: 'noreply@salgode.cl'
};
const result = await ses.sendEmail(params).promise();
return result;
}
function parseImageField(rawField) {
return rawField.replace('-', '.');
}
exports.handler = async (event) => {
const userId = event.queryStringParameters.user;
const imageFieldRaw = event.queryStringParameters.field;
const imageId = event.pathParameters.image;
const imageField = parseImageField(imageFieldRaw);
const [email, firstName, realImageId] = await getUser(userId, imageField);
if (realImageId !== imageId) {
return {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(
{
action: 'review',
success: false,
resource: 'image',
resource_id: imageId,
message: 'Wrong image id'
}
)
};
}
await reviewImage(userId, imageField);
await notifyUser(email, firstName, imageField);
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(
{
action: 'review',
success: true,
resource: 'image',
resource_id: imageId
}
)
};
};