-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.ts
More file actions
93 lines (81 loc) · 2.51 KB
/
Copy pathcomment.ts
File metadata and controls
93 lines (81 loc) · 2.51 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
import { formatFeedbackResponse } from '../shared/utils/responseUtils';
import { APIGatewayProxyEvent } from 'aws-lambda';
import {
createFeedback,
getAuthClient,
updateFeedback
} from '../shared/utils/googleSheetsUtils';
import { ComprehendClient } from '@aws-sdk/client-comprehend';
import { SSMClient } from '@aws-sdk/client-ssm';
import {
Comment,
Feedback,
FeedbackResponse,
FeedbackResponseStatusCodes
} from '../shared/types';
import { getSsmParam } from '../shared/utils/awsUtils';
const COMPREHEND_CLIENT = new ComprehendClient({ region: 'us-east-1' });
import { redactPii } from '../shared/utils/pii-redaction';
const SSM = new SSMClient();
export const handler = async (
event: APIGatewayProxyEvent
): Promise<FeedbackResponse> => {
try {
const { feedbackId, comment, pageURL, rating } = JSON.parse(
event.body as string
) as Comment;
if (comment == null) {
throw new Error('Submission is missing comment');
}
const redactedComment = await redactPii(comment.trim(), COMPREHEND_CLIENT);
const googleSheetsClientEmail = await getSsmParam(
SSM,
'/feedback-api/sheets-email'
);
const googleSheetsPrivateKey = await getSsmParam(
SSM,
'/feedback-api/sheets-private-key'
);
const sheetId = await getSsmParam(SSM, '/feedback-api/sheet-id');
const client = await getAuthClient(
googleSheetsClientEmail,
googleSheetsPrivateKey
);
if (feedbackId != null) {
const updatedId = await updateFeedback(
client,
sheetId,
feedbackId,
Feedback.Comment,
redactedComment
);
return formatFeedbackResponse(FeedbackResponseStatusCodes.Success, {
message: 'Success',
feedbackId: updatedId
});
} else if (pageURL != null && rating != null) {
const createdId = await createFeedback(
client,
sheetId,
pageURL,
rating,
redactedComment
);
return formatFeedbackResponse(FeedbackResponseStatusCodes.Success, {
message: 'Success',
feedbackId: createdId
});
} else {
throw Error(
'If submission is missing feedbackId, then it must include pageURL and rating.'
);
}
} catch (e) {
const message = e instanceof Error ? e.message : 'No further details';
// eslint-disable-next-line no-console
console.error(`Error: ${message}`);
return formatFeedbackResponse(FeedbackResponseStatusCodes.Error, {
message: `Failed to save comment: ${message}`
});
}
};