-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (91 loc) · 3.69 KB
/
index.js
File metadata and controls
98 lines (91 loc) · 3.69 KB
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
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB();
exports.handler = function (event, context, callback) {
let cognitoSub = event.cognitoSub;
let cognitoUser
if (!cognitoSub){
cognitoUser = decodeJWT(event.jwtToken);
cognitoSub = cognitoUser.sub
}
const donationId = event.donationId;
const newStatus = event.newStatus;
if (!(newStatus === 'contact-shown' || newStatus === 'contacted' || newStatus === 'given')){
callback('forbidden 1');
}
const userid = event.pid;
if (userid === cognitoSub){
dynamo.getItem({
TableName: 'TI-TargetedDonations',
AttributesToGet: ['Status', 'User', 'LocId'],
Key : {
'DonationId' : {
S: donationId
}
}
}, function (err, data) {
if (err===null){
if (data.Item.Status.S === 'accepted' || data.Item.Status.S === 'contact-shown' || data.Item.Status.S === 'contacted' || data.Item.Status.S === 'given'){
dynamo.getItem({
TableName: 'TI-PUser-Loc',
Key: {
PUser: {
S: userid
},
LocId: {
S: 'self'
}
}
}, function (err,userloc) {
if (err === null){
// TODO add lastContacted , accepted, given
if (userloc.Item.Location.S === data.Item.LocId.S){
dynamo.updateItem({
TableName: 'TI-TargetedDonations',
Key: {
'DonationId' : {
S: donationId
}
},
UpdateExpression: 'set #S = :newStatus',
ExpressionAttributeValues: {
':newStatus' : { S: newStatus}
},
ExpressionAttributeNames: {
"#S": "Status"
}
}, (err,res)=>{
if (err) {
callback(err);
} else {
callback(null,{ status: newStatus})
}
})
} else {
callback('forbidden 5')
}
} else {
callback('forbidden 4');
}
})
} else {
callback('forbidden 2');
}
} else{
callback('can not get');
}
})
} else{
callback('forbidden 3')
}
}
function decodeJWT(jwt){
if (!jwt) return {};
const buff = Buffer.from(jwt, 'base64');
const text = buff.toString('utf-8');
let firstObj = text.indexOf('}')+1; // address : { formated:
let second = text.indexOf('}',firstObj)+1;
second = text.indexOf('}',second)+1;
const wHeader = text.substring(firstObj, second);
console.log(wHeader);
return JSON.parse(wHeader)
}