Skip to content

Commit

Permalink
Send notification of feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Jun 28, 2024
1 parent 1b5e2bf commit b0ebabb
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 6 deletions.
23 changes: 21 additions & 2 deletions node/src/handlers/post-chat-feedback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { wrap } = require("./middleware");
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns");

const Validator = require("jsonschema").Validator;

const feedbackSchema = {
Expand Down Expand Up @@ -56,6 +58,11 @@ const handler = wrap(async (event) => {
}
await uploadToS3(`${content.sentiment}/${content.context.ref}`, content);

await sendNotification(
`Chat feedback: ${content.sentiment} response`,
JSON.stringify(content, null, 2)
);

return {
statusCode: 200,
headers: { "content-type": "text/plain" },
Expand All @@ -76,7 +83,7 @@ const handler = wrap(async (event) => {
const uploadToS3 = async (key, body) => {
const client = new S3Client({});
const command = new PutObjectCommand({
Bucket: process.env.MEDIA_CONVERT_DESTINATION_BUCKET,
Bucket: process.env.CHAT_FEEDBACK_BUCKET,
Key: key,
Body: JSON.stringify(body, null, 2),
ContentType: "application/json",
Expand All @@ -85,4 +92,16 @@ const uploadToS3 = async (key, body) => {
return await client.send(command);
};

module.exports = { handler };
const sendNotification = async (subject, message) => {
const snsClient = new SNSClient({});
const command = new PublishCommand({
TopicArn: process.env.CHAT_FEEDBACK_TOPIC_ARN,
Subject: subject,
Message: message,
});
const response = await snsClient.send(command);
console.log(response);
return response;
};

module.exports = { handler, uploadToS3 };
174 changes: 174 additions & 0 deletions node/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@smithy/node-http-handler": "^2.5.0",
"@smithy/protocol-http": "^3.3.0",
"@smithy/signature-v4": "^2.3.0",
"aws-sdk-client-mock": "^4.0.1",
"axios": ">=0.21.1",
"cookie": "^0.5.0",
"debug": "^4.3.4",
Expand Down
2 changes: 1 addition & 1 deletion node/test/integration/post-chat-feedback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const chai = require("chai");
const expect = chai.expect;
chai.use(require("chai-http"));
const ApiToken = requireSource("api/api-token");
const { mockClient } = require("aws-sdk-client-mock");
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { mockClient } = require("aws-sdk-client-mock");

const { handler } = requireSource("handlers/post-chat-feedback");

Expand Down
2 changes: 1 addition & 1 deletion node/test/test-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TestEnvironment = {
NUSSO_BASE_URL: "https://nusso-base.com/",
NUSSO_API_KEY: "abc123",
WEBSOCKET_URI: "wss://thisisafakewebsocketapiurl",
MEDIA_CONVERT_DESTINATION_BUCKET: "test-mediaconvert-destination-bucket",
CHAT_FEEDBACK_BUCKET: "test-mediaconvert-destination-bucket",
};

for (const v in TestEnvironment) delete process.env[v];
Expand Down
19 changes: 17 additions & 2 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -705,22 +705,37 @@ Resources:
#* - !Ref apiDependencies
Environment:
Variables:
MEDIA_CONVERT_DESTINATION_BUCKET: !Ref MediaConvertDestinationBucket
CHAT_FEEDBACK_BUCKET: !Ref chatFeedbackBucket
CHAT_FEEDBACK_TOPIC_ARN: !Ref chatFeedbackTopic
Policies:
Version: 2012-10-17
Statement:
- Sid: BucketAccess
Effect: Allow
Action:
- s3:PutObject
Resource: !Sub "arn:aws:s3:::${MediaConvertDestinationBucket}/*"
Resource: !Sub "arn:aws:s3:::${chatFeedbackBucket}/*"
- Sid: TopicAccess
Effect: Allow
Action:
- sns:Publish
Resource: !Ref chatFeedbackTopic
Events:
PostApi:
Type: HttpApi
Properties:
ApiId: !Ref dcApi
Path: /chat-feedback
Method: POST
chatFeedbackBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Sub "${AWS::StackName}-chat-feedback"
chatFeedbackTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: DC Chat Feedback
TopicName: !Sub "${AWS::StackName}-chat-feedback"
defaultFunction:
Type: AWS::Serverless::Function
Properties:
Expand Down

0 comments on commit b0ebabb

Please sign in to comment.