Skip to content

Commit

Permalink
Changes and updates for dm's
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeBellTMH committed Oct 8, 2020
1 parent 2b84ddb commit 48334d8
Show file tree
Hide file tree
Showing 15 changed files with 15,730 additions and 15,827 deletions.
3 changes: 2 additions & 1 deletion amplify/backend/api/jcmobile/schema.graphql
Expand Up @@ -13,6 +13,7 @@ type User
@model
@auth(
rules: [
{ allow: private, provider:iam, operations:[read]}
{ allow: private, provider:userPools, operations:[read]}
{ allow: owner, ownerField: "owner" }
{ allow: groups, groups: ["verifiedUsers"], operations: [read] }
Expand All @@ -22,7 +23,7 @@ type User
id: ID!
given_name: String!
family_name: String!
email: String @auth(rules: [{ allow: owner, ownerField: "owner" }])
email: String @auth(rules: [{ allow: owner, ownerField: "owner" },{allow:groups,groups:["admin"],operations:[read]}])
phone: String @auth(rules: [{ allow: owner, ownerField: "owner" }])
owner: String
mainUserGroup:String
Expand Down
8 changes: 8 additions & 0 deletions amplify/backend/backend-config.json
Expand Up @@ -56,6 +56,14 @@
"providerPlugin": "awscloudformation",
"service": "Lambda",
"dependsOn": [
{
"category": "api",
"resourceName": "jcmobile",
"attributes": [
"GraphQLAPIIdOutput",
"GraphQLAPIEndpointOutput"
]
},
{
"category": "analytics",
"resourceName": "jcmobile",
Expand Down
@@ -1,5 +1,10 @@
{
"permissions": {
"api": {
"jcmobile": [
"read"
]
},
"analytics": {
"jcmobile": [
"create",
Expand Down
Expand Up @@ -7,6 +7,18 @@
"Default": "NONE",
"Description": " Schedule Expression"
},
"userPoolId": {
"Type": "String",
"Default": ""
},
"userPoolWebClientId": {
"Type": "String",
"Default": ""
},
"identityPoolId": {
"Type": "String",
"Default": ""
},
"env": {
"Type": "String"
},
Expand Down Expand Up @@ -70,6 +82,15 @@
},
"Environment": {
"Variables": {
"userPoolId": {
"Ref": "userPoolId"
},
"userPoolWebClientId": {
"Ref": "userPoolWebClientId"
},
"identityPoolId": {
"Ref": "identityPoolId"
},
"ENV": {
"Ref": "env"
},
Expand Down Expand Up @@ -103,7 +124,7 @@
"Timeout": "25",
"Code": {
"S3Bucket": "amplify-jc-mobile-beta-155138-deployment",
"S3Key": "amplify-builds/jcProcessDMAlerts-4d3477765a645a737551-build.zip"
"S3Key": "amplify-builds/jcProcessDMAlerts-772f503334715734376d-build.zip"
},
"Layers": []
}
Expand Down Expand Up @@ -283,6 +304,31 @@
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"appsync:Get*",
"appsync:List*"
],
"Resource": [
{
"Fn::Sub": [
"arn:aws:appsync:${region}:${account}:apis/${graph}/*",
{
"region": {
"Ref": "AWS::Region"
},
"account": {
"Ref": "AWS::AccountId"
},
"graph": {
"Ref": "apijcmobileGraphQLAPIIdOutput"
}
}
]
}
]
},
{
"Effect": "Allow",
"Action": [
Expand All @@ -296,42 +342,35 @@
],
"Resource": [
{
"Fn::Join": [
"",
[
"arn:aws:mobiletargeting:",
{
"Fn::Sub": [
"arn:aws:mobiletargeting:${region}:${account}:apps/${mobile}",
{
"region": {
"Ref": "analyticsjcmobileRegion"
},
":",
{
"account": {
"Ref": "AWS::AccountId"
},
":apps/",
{
"mobile": {
"Ref": "analyticsjcmobileId"
}
]
}
]
},
{
"Fn::Join": [
"",
[
"arn:aws:mobiletargeting:",
{
"Fn::Sub": [
"arn:aws:mobiletargeting:${region}:${account}:apps/${mobile}/*",
{
"region": {
"Ref": "analyticsjcmobileRegion"
},
":",
{
"account": {
"Ref": "AWS::AccountId"
},
":apps/",
{
"mobile": {
"Ref": "analyticsjcmobileId"
},
"/*"
]
}
}
]
}
]
Expand Down
102 changes: 51 additions & 51 deletions amplify/backend/function/jcProcessDMAlerts/src/index.js
@@ -1,10 +1,4 @@
/* Amplify Params - DO NOT EDIT
ANALYTICS_JCMOBILE_ID
ANALYTICS_JCMOBILE_REGION
API_JCMOBILE_GRAPHQLAPIENDPOINTOUTPUT
API_JCMOBILE_GRAPHQLAPIIDOUTPUT
ENV
REGION
/*
Amplify Params - DO NOT EDIT *//* Amplify Params - DO NOT EDIT
ANALYTICS_JCMOBILE_ID
ANALYTICS_JCMOBILE_REGION
Expand All @@ -21,56 +15,63 @@ const Amplify = require('aws-amplify');
global.fetch = require("node-fetch");
const queries = require('./queries')
const htmlToText = require('html-to-text');
const configSendAlerts = true


async function getRecipient(id) {
async function getUser(id) {
try {
// console.log(process.env)
var region = process.env.REGION
var API_JCMOBILE_GRAPHQLAPIENDPOINTOUTPUT = process.env.API_JCMOBILE_GRAPHQLAPIENDPOINTOUTPUT
Amplify.default.configure({
aws_appsync_graphqlEndpoint: API_JCMOBILE_GRAPHQLAPIENDPOINTOUTPUT,
aws_appsync_region: region,
aws_appsync_graphqlEndpoint: process.env.API_JCMOBILE_GRAPHQLAPIENDPOINTOUTPUT,
aws_appsync_region: process.env.region,
aws_appsync_authenticationType: "AMAZON_COGNITO_USER_POOLS",
Auth: {
mandatorySignIn: false,
region: region,
region: process.env.region,
userPoolId: process.env.userPoolId,
identityPoolRegion: region,
identityPoolRegion: process.env.region,
userPoolWebClientId: process.env.userPoolWebClientId,
identityPoolId: process.env.identityPoolId
}
})
console.log("Done config")
await Amplify.Auth.signIn("george.bell@themeetinghouse.com", "Tacobell#1")
await Amplify.Auth.signIn("george.bell@themeetinghouse.com", "")
console.log("Done login")
currentSession = await Amplify.Auth.currentSession()

const currentSession = await Amplify.Auth.currentSession()
Amplify.default.configure({
Authorization: currentSession.getIdToken().getJwtToken(),
})
console.log("Done Auth")
json = await Amplify.API.graphql({
query: queries.getUser,
variables: { limit: 20, filter: { profileState: { eq: "Complete" } }, nextToken: null },
authMode: "AMAZON_COGNITO_USER_POOLS"
});
console.log("Done List Users")
await Promise.all(json.data.listUsers.items.map(async (item) => {
console.log(item)
}))
try {
console.log("Done Auth")
const json = await Amplify.API.graphql({
query: queries.getUser,
variables: { id: id },
authMode: 'AMAZON_COGNITO_USER_POOLS'
});
console.log("Done Get Users")
const email = json.data.getUser.email
const name = json.data.getUser.given_name + " " + json.data.getUser.family_name
const alertConfig = json.data.getUser.alertConfig
return { email: email, name: name, alertConfig: alertConfig }
}
catch (json) {
if (json && json.data && json.data.getUser) {
const email = json.data.getUser.email
const name = json.data.getUser.given_name + " " + json.data.getUser.family_name
const alertConfig = json.data.getUser.alertConfig
return { email: email, name: name, alertConfig: alertConfig }
}
console.log({ "Error getting user": json })
return null
}
}
catch (e) {
console.log({ "ERROR:": e })
return null
}
}

const configSendAlerts = true
// Provide the full path to your config.json file.
async function sendEmail(recipient, message) {
async function sendEmail(recipient, message, name) {
console.log("Setting Up Email");
const sender = "Jesus Collective <donot-reply@jesuscollective.com>";
const subject = "Jesus Collective Message Alert";
const subject = "Jesus Collective DM from " + name;
const charset = "UTF-8";
console.log("Create SES");
var ses = new aws.SES();
Expand Down Expand Up @@ -106,9 +107,6 @@ async function sendEmail(recipient, message) {
}

}
function getRecipient(user) {
return null
}
function convertCommentFromJSONToHTML(text) {
try {
return stateToHTML(convertFromRaw(JSON.parse(text)))
Expand All @@ -127,22 +125,21 @@ function convertCommentFromJSONToTEXT(text) {
return null
}
}
function generateMessage(html, text) {
const body_text = "You have received a message on Jesus Collective\r\n" +
function generateMessage(html, text, name) {
const body_text = "You have received a message on Jesus Collective from " + name + "\r\n" +
text
+ "Please login to view it.";
+ "\r\nPlease login to view it.";

// The HTML body of the email.
const body_html = `<html>
<head></head>
<body>
<h1>Jesus Collective Message Alert</h1>
<p>You have received a direct message on Jesus Collective</p>`+
<p>You have received a direct message on Jesus Collective from `+ name + `</p>` +
html
+ `<a href='https://dev.jesuscollective.com/app/conversation?initialUserID=null&initialUserName=null'>Login</a></p>
</body>
</html>`;
console.log(body_text)
// console.log(body_html)
return { html: body_html, text: body_text }
}
Expand Down Expand Up @@ -171,17 +168,20 @@ async function Execute(event) {
const content = record.dynamodb.NewImage.content.S
const from = record.dynamodb.NewImage.userId.S
console.log("Starting Send Loop")
const fromInfo = await getUser(from)
await asyncForEach(recipients.filter(item => item != from), async (recipientID) => {
if (!messageRoomID.startsWith("course-")) {
console.log({ "Sending DM Alert to": recipientID })
const rec = await getRecipient(recipientID)
const recipient = "george.bell@themeetinghouse.com";
const html = convertCommentFromJSONToHTML(content)
const text = convertCommentFromJSONToTEXT(content)
if (html && text) {
const message = generateMessage(html, text)
const data = await sendEmail(recipient, message)
console.log(data)
console.log({ "Lookup user": recipientID })
const recipientInfo = await getUser(recipientID)
if (recipientInfo && recipientInfo.alertConfig.emailDirectMessage) {
console.log({ "Sending a DM to": recipientInfo })
const html = convertCommentFromJSONToHTML(content)
const text = convertCommentFromJSONToTEXT(content)
if (html && text) {
const message = generateMessage(html, text, fromInfo.name)
const data = await sendEmail(recipientInfo.email, message, fromInfo.name)
console.log(data)
}
}
}
})
Expand Down

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

Expand Up @@ -9,6 +9,7 @@
"aws-sdk": "^2.734.0",
"draft-js": "^0.11.7",
"draft-js-export-html": "^1.4.1",
"graphql-tag": "^2.11.0",
"html-to-text": "^5.1.1",
"node-fetch": "^2.6.1",
"react": "^16.13.1",
Expand Down

0 comments on commit 48334d8

Please sign in to comment.