-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user_vehicle.js
145 lines (136 loc) · 4.23 KB
/
create_user_vehicle.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const aws = require('aws-sdk');
const moment = require('moment');
const uuidv4 = require('uuid/v4');
let EventsTableName = process.env.dynamodb_events_table_name;
let UsersTableName = process.env.dynamodb_users_table_name;
let VehiclesTableName = process.env.dynamodb_vehicles_table_name;
function stagingOverwrite() {
EventsTableName = `Dev_${process.env.dynamodb_events_table_name}`;
UsersTableName = `Dev_${process.env.dynamodb_users_table_name}`;
VehiclesTableName = `Dev_${process.env.dynamodb_vehicles_table_name}`;
}
const dynamoDB = new aws.DynamoDB.DocumentClient();
function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
async function createVehicle(userId, alias, vehicleAttrs, vehicleIdentif, rawData) {
const vehicleId = `veh_${uuidv4()}`;
const eventId = `evt_${uuidv4()}`;
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss-04:00');
try {
await dynamoDB
.transactWrite({
TransactItems: [
{
Update: {
TableName: UsersTableName,
Key: {
user_id: userId
},
UpdateExpression: 'set vehicles = list_append(vehicles, :vehicleId), updated_at = :now',
ExpressionAttributeValues: {
':vehicleId': [vehicleId],
':now': timestamp
}
}
},
{
Put: {
TableName: VehiclesTableName,
Item: {
vehicle_id: vehicleId,
alias,
vehicle_attributes: {
type: vehicleAttrs.type,
model: vehicleAttrs.model,
seats: vehicleAttrs.seats,
color: vehicleAttrs.color,
brand: vehicleAttrs.brand
},
vehicle_verifications: {
identification: false
},
vehicle_identifications: {
type: vehicleIdentif.type,
identification: vehicleIdentif.identification,
country: vehicleIdentif.country
},
created_at: timestamp,
updated_at: timestamp
}
}
},
{
Put: {
TableName: EventsTableName,
Item: {
event_id: eventId,
user_id: userId,
resource_id: vehicleId,
resource: 'vehicle',
action: 'create',
event_data: rawData,
created_at: timestamp
}
}
}
]
})
.promise();
return vehicleId;
} catch (e) {
return false;
}
}
exports.handler = async (event) => {
if (event.requestContext.stage === 'staging') { stagingOverwrite(); }
const userId = event.requestContext.authorizer.user_id;
const body = JSON.parse(event.body);
const { alias } = body;
const vehicleAttrs = body.vehicle_attributes;
const vehicleIdentif = body.vehicle_identifications;
if (
!vehicleAttrs || !vehicleIdentif || isEmpty(vehicleAttrs) || isEmpty(vehicleIdentif)
|| !vehicleIdentif.type || !vehicleIdentif.identification || !vehicleIdentif.country
|| !vehicleAttrs.type || !vehicleAttrs.seats || !vehicleAttrs.color
|| !vehicleAttrs.brand || !vehicleAttrs.model
) {
return {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'create',
success: false,
resource: 'vehicle',
message: 'Missing attributes or identifications'
})
};
}
const vehicleId = await createVehicle(
userId, alias, vehicleAttrs, vehicleIdentif, body
);
if (vehicleId) {
return {
statusCode: 201,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(
{
action: 'create',
success: true,
resource: 'vehicle',
resource_id: vehicleId
}
)
};
}
return {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'create',
success: false,
resource: 'vehicle',
message: 'Request contains errors'
})
};
};