From 7450113370dece3b5fa6f97c3686f13f6017f20f Mon Sep 17 00:00:00 2001 From: LunarFang416 Date: Fri, 22 Mar 2024 14:50:53 -0400 Subject: [PATCH] add funcs --- picea/backend/package-lock.json | 64 ++++++++++++++++++++++++++++++ picea/backend/package.json | 1 + picea/backend/serverless.yml | 16 ++++++++ picea/backend/services/DynamoDB.js | 55 +++++++++++++++++-------- picea/backend/services/chime.js | 43 ++++++++++++++++++++ 5 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 picea/backend/services/chime.js diff --git a/picea/backend/package-lock.json b/picea/backend/package-lock.json index 4052cb0..ba26db9 100644 --- a/picea/backend/package-lock.json +++ b/picea/backend/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@aws-sdk/client-chime-sdk-meetings": "^3.535.0", "aws-sdk": "^2.1583.0", "serverless": "^3.38.0" } @@ -104,6 +105,69 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, + "node_modules/@aws-sdk/client-chime-sdk-meetings": { + "version": "3.535.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-chime-sdk-meetings/-/client-chime-sdk-meetings-3.535.0.tgz", + "integrity": "sha512-n8sBuUcDXR2ZHfbfHQu6+BgsCBicUkg4T3efvTyiO7+04S8Kh7qH/AuNDwq/93tECkn2CjoC9/yF5N7ZJpQ+wg==", + "dependencies": { + "@aws-crypto/sha256-browser": "3.0.0", + "@aws-crypto/sha256-js": "3.0.0", + "@aws-sdk/client-sts": "3.535.0", + "@aws-sdk/core": "3.535.0", + "@aws-sdk/credential-provider-node": "3.535.0", + "@aws-sdk/middleware-host-header": "3.535.0", + "@aws-sdk/middleware-logger": "3.535.0", + "@aws-sdk/middleware-recursion-detection": "3.535.0", + "@aws-sdk/middleware-user-agent": "3.535.0", + "@aws-sdk/region-config-resolver": "3.535.0", + "@aws-sdk/types": "3.535.0", + "@aws-sdk/util-endpoints": "3.535.0", + "@aws-sdk/util-user-agent-browser": "3.535.0", + "@aws-sdk/util-user-agent-node": "3.535.0", + "@smithy/config-resolver": "^2.2.0", + "@smithy/core": "^1.4.0", + "@smithy/fetch-http-handler": "^2.5.0", + "@smithy/hash-node": "^2.2.0", + "@smithy/invalid-dependency": "^2.2.0", + "@smithy/middleware-content-length": "^2.2.0", + "@smithy/middleware-endpoint": "^2.5.0", + "@smithy/middleware-retry": "^2.2.0", + "@smithy/middleware-serde": "^2.3.0", + "@smithy/middleware-stack": "^2.2.0", + "@smithy/node-config-provider": "^2.3.0", + "@smithy/node-http-handler": "^2.5.0", + "@smithy/protocol-http": "^3.3.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "@smithy/url-parser": "^2.2.0", + "@smithy/util-base64": "^2.3.0", + "@smithy/util-body-length-browser": "^2.2.0", + "@smithy/util-body-length-node": "^2.3.0", + "@smithy/util-defaults-mode-browser": "^2.2.0", + "@smithy/util-defaults-mode-node": "^2.3.0", + "@smithy/util-endpoints": "^1.2.0", + "@smithy/util-middleware": "^2.2.0", + "@smithy/util-retry": "^2.2.0", + "@smithy/util-utf8": "^2.3.0", + "tslib": "^2.6.2", + "uuid": "^9.0.1" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-chime-sdk-meetings/node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@aws-sdk/client-cloudformation": { "version": "3.537.0", "resolved": "https://registry.npmjs.org/@aws-sdk/client-cloudformation/-/client-cloudformation-3.537.0.tgz", diff --git a/picea/backend/package.json b/picea/backend/package.json index be46ef2..0e9e965 100644 --- a/picea/backend/package.json +++ b/picea/backend/package.json @@ -9,6 +9,7 @@ "author": "", "license": "ISC", "dependencies": { + "@aws-sdk/client-chime-sdk-meetings": "^3.535.0", "aws-sdk": "^2.1583.0", "serverless": "^3.38.0" } diff --git a/picea/backend/serverless.yml b/picea/backend/serverless.yml index 59ad35c..10cc9cd 100644 --- a/picea/backend/serverless.yml +++ b/picea/backend/serverless.yml @@ -77,6 +77,14 @@ functions: path: storage method: get cors: true + dynamo: + handler: services/DynamoDB.getAllCounsellers + description: get counsellers + events: + - http: + path: counsellers + method: get + cors: true db: handler: services/db.database description: Database @@ -87,6 +95,14 @@ functions: - http: path: database method: get + meeting: + handler: services/chime.createMeetings + description: create meeting + events: + - http: + path: meeting + method: get + cors: true # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events diff --git a/picea/backend/services/DynamoDB.js b/picea/backend/services/DynamoDB.js index c744322..6e717b5 100644 --- a/picea/backend/services/DynamoDB.js +++ b/picea/backend/services/DynamoDB.js @@ -14,7 +14,7 @@ const meeting = "meeting" const counsellor = "counsellor" // Retrieve meeting from the meeting table by meeting title -const getMeeting = async (id) => { +module.exports.getMeeting = async (id) => { const result = await ddb.getItem({ TableName: meeting, Key: { @@ -27,7 +27,7 @@ const getMeeting = async (id) => { }; // Add meeting in the meeting table -const putMeeting = async (id, meetingInfo) => { +module.exports.putMeeting = async (id, meetingInfo) => { await ddb.putItem({ TableName: meeting, Item: { @@ -40,7 +40,7 @@ const putMeeting = async (id, meetingInfo) => { }; //returns a list of participants in the given meeting - const getMeetingParticipants = async (id, participantId) => { +module.exports.getMeetingParticipants = async (id, participantId) => { const result = await ddb.getItem({ TableName: meeting, Key: { @@ -53,7 +53,7 @@ const putMeeting = async (id, meetingInfo) => { }; //returns the id of the counsellor of the meeting - const getMeetingCounsellor = async (id, counsellorId) => { +module.exports.getMeetingCounsellor = async (id, counsellorId) => { const result = await ddb.getItem({ TableName: meeting, Key: { @@ -69,7 +69,7 @@ const putMeeting = async (id, meetingInfo) => { }; //add a participant to a meeting - const addParticipantToMeeting = async (meetingID, participantId) => { +module.exports.addParticipantToMeeting = async (meetingID, participantId) => { await ddb.UpdateItem({ TableName: meeting, Key: { @@ -85,7 +85,7 @@ const putMeeting = async (id, meetingInfo) => { }; - const addCounsellorToMeeting = async (meetingID, counsellorId) => { +module.exports.addCounsellorToMeeting = async (meetingID, counsellorId) => { await ddb.UpdateItem({ TableName: meeting, Key: { @@ -101,7 +101,7 @@ const putMeeting = async (id, meetingInfo) => { }; //return a list of counsellor_id that fits within the participants' desired avaliability - const findAvaliableCounsellor = async (participantStartTime) => { +module.exports.findAvaliableCounsellor = async (participantStartTime) => { await ddb.scan({ TableName: counsellor, FilterExpression: 'endTime > :startTime', // Define your condition using a numerical comparison operator @@ -112,7 +112,7 @@ const putMeeting = async (id, meetingInfo) => { return result.Item ? result.Item.counsellor_id : null; }; - const getCounsellorName = async (id) => { +module.exports.getCounsellorName = async (id) => { const result = await ddb.getItem({ TableName: counsellor, Key: { @@ -124,13 +124,34 @@ const putMeeting = async (id, meetingInfo) => { return result.Item ? JSON.parse(result.Item.name.SS) : null; }; -module.exports.database = async ( - id, - meetingInfo, - participantId, - counsellorId, - participantStartTime -) => { - getMeeting, putMeeting, getMeetingParticipants, getMeetingCounsellor, addParticipantToMeeting, - addCounsellorToMeeting, findAvaliableCounsellor, getCounsellorName + + export const scanTable = async (tableName) => { + const params = { + TableName: tableName, + }; + + const scanResults = []; + let items; + do{ + items = await documentClient.scan(params).promise(); + items.Items.forEach((item) => scanResults.push(item)); + params.ExclusiveStartKey = items.LastEvaluatedKey; + }while(typeof items.LastEvaluatedKey !== "undefined"); + + return scanResults; }; + +module.exports.getAllCounsellers = async () => { + return scanTable(counsellor); +} + +// module.exports.database = async ( +// id, +// meetingInfo, +// participantId, +// counsellorId, +// participantStartTime +// ) => { +// getMeeting, putMeeting, getMeetingParticipants, getMeetingCounsellor, addParticipantToMeeting, +// addCounsellorToMeeting, findAvaliableCounsellor, getCounsellorName +// }; diff --git a/picea/backend/services/chime.js b/picea/backend/services/chime.js new file mode 100644 index 0000000..61776eb --- /dev/null +++ b/picea/backend/services/chime.js @@ -0,0 +1,43 @@ +const { ChimeSDKMeetings } = require('@aws-sdk/client-chime-sdk-meetings'); + +const chimeSDKMeetings = new ChimeSDKMeetings({ region: 'us-east-1' }); + +function uuid() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + const r = (Math.random() * 16) | 0, + v = c === 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +} + +module.exports.createMeetings = async (event, context) => { + const title = 'default-meeting'; + const userId = 'default-user'; + + const request = { + ClientRequestToken: uuid(), + MediaRegion: 'us-east-1', + ExternalMeetingId: title.substring(0, 64), + }; + console.info('Creating new meeting before joining: ' + JSON.stringify(request)); + const meetingInfo = await chimeSDKMeetings.createMeeting(request); + console.log(meetingInfo) + + const { Attendee } = await chimeSDKMeetings.createAttendee({ + MeetingId: meetingInfo.Meeting.MeetingId, + ExternalUserId: userId, + }); + + let returnObject = { + statusCode: 200, + headers: { + "access-control-allow-origin": "*" + }, + body: JSON.stringify({ + meeting: meetingInfo, + attendee: Attendee + }) + }; + + return returnObject; +} \ No newline at end of file