diff --git a/amplify/backend/api/wellbeeing/schema.graphql b/amplify/backend/api/wellbeeing/schema.graphql index 2b22f26..2a2bb02 100644 --- a/amplify/backend/api/wellbeeing/schema.graphql +++ b/amplify/backend/api/wellbeeing/schema.graphql @@ -1,8 +1,7 @@ type Reminder @model @auth(rules: [{ allow: owner }]) { id: ID! category: Category! - goal: Goal @belongsTo(fields: ["goalId"]) - goalId: ID @index(name: "byGoal") + goals: [Goal] @manyToMany(relationName: "GoalReminders") level: Level! @index( name: "remindersByLevel" @@ -21,7 +20,7 @@ type Goal @model @auth(rules: [{ allow: owner }]) { id: ID! title: String! category: Category! - reminders: [Reminder] @hasMany(indexName: "byGoal",fields: ["id"]) + reminders: [Reminder] @manyToMany(relationName: "GoalReminders") progress: Int! stepCount: Int dueDate: AWSDate! diff --git a/amplify/backend/function/S3Trigger9c40585d/src/index.js b/amplify/backend/function/S3Trigger9c40585d/src/index.js deleted file mode 100644 index 12e2a4e..0000000 --- a/amplify/backend/function/S3Trigger9c40585d/src/index.js +++ /dev/null @@ -1,6 +0,0 @@ -exports.handler = async function (event) { - console.log('Received S3 event:', JSON.stringify(event, null, 2)); - const bucket = event.Records[0].s3.bucket.name; - const key = event.Records[0].s3.object.key; - console.log(`Bucket: ${bucket}`, `Key: ${key}`); -}; \ No newline at end of file diff --git a/amplify/backend/function/S3Trigger9c40585d/src/package-lock.json b/amplify/backend/function/S3Trigger9c40585d/src/package-lock.json deleted file mode 100644 index 8598081..0000000 --- a/amplify/backend/function/S3Trigger9c40585d/src/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "S3Trigger9c40585d", - "version": "2.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "S3Trigger9c40585d", - "version": "2.0.0", - "license": "Apache-2.0" - } - } -} diff --git a/amplify/backend/function/S3Trigger9c40585d/src/package.json b/amplify/backend/function/S3Trigger9c40585d/src/package.json deleted file mode 100644 index 96e5b9e..0000000 --- a/amplify/backend/function/S3Trigger9c40585d/src/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "S3Trigger9c40585d", - "version": "2.0.0", - "description": "Lambda function generated by Amplify", - "main": "index.js", - "license": "Apache-2.0" -} diff --git a/amplify/backend/function/S3Trigger9c40585d/src/yarn.lock b/amplify/backend/function/S3Trigger9c40585d/src/yarn.lock deleted file mode 100644 index fb57ccd..0000000 --- a/amplify/backend/function/S3Trigger9c40585d/src/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - diff --git a/amplify/backend/function/S3Trigger9c40585d/amplify.state b/amplify/backend/function/uniqueValidationGoalReminderFunction/amplify.state similarity index 58% rename from amplify/backend/function/S3Trigger9c40585d/amplify.state rename to amplify/backend/function/uniqueValidationGoalReminderFunction/amplify.state index 2ba0f4c..0c3efe0 100644 --- a/amplify/backend/function/S3Trigger9c40585d/amplify.state +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/amplify.state @@ -1,6 +1,6 @@ { "pluginId": "amplify-nodejs-function-runtime-provider", "functionRuntime": "nodejs", - "defaultEditorFile": "src/index.js", - "useLegacyBuild": true + "useLegacyBuild": true, + "defaultEditorFile": "src/index.js" } \ No newline at end of file diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/custom-policies.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/custom-policies.json new file mode 100644 index 0000000..528c94f --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/custom-policies.json @@ -0,0 +1,6 @@ +[ + { + "Action": [], + "Resource": [] + } +] \ No newline at end of file diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/function-parameters.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/function-parameters.json new file mode 100644 index 0000000..568c0f3 --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/function-parameters.json @@ -0,0 +1,4 @@ +{ + "permissions": {}, + "lambdaLayers": [] +} \ No newline at end of file diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/parameters.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/parameters.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/parameters.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/amplify/backend/function/S3Trigger9c40585d/src/event.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/event.json similarity index 100% rename from amplify/backend/function/S3Trigger9c40585d/src/event.json rename to amplify/backend/function/uniqueValidationGoalReminderFunction/src/event.json diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/src/index.js b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/index.js new file mode 100644 index 0000000..a25e296 --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/index.js @@ -0,0 +1,69 @@ +/* Amplify Params - DO NOT EDIT + ENV + REGION +Amplify Params - DO NOT EDIT */ + +/** + * @type {import('@types/aws-lambda').APIGatewayProxyHandler} + */ + const AWS = require("aws-sdk"); + const dynamo = new AWS.DynamoDB.DocumentClient({ region: `us-east-1` }); + exports.handler = async (event) => { + + console.log("Processing event: %j", JSON.stringify(event)); + const GoalReminderObjectTableName = "GoalReminders-vumo7bou3fbg3fulexi2s26k7i-sandboxtwo"; + const IndexName = 'byGoal' + let goalIDs = [] + let reminderID = ''; + if (event.queryStringParameters && event.queryStringParameters.goalReminder) { + console.log("Received name: " + event.queryStringParameters.goalReminder); + goalIDs = event.queryStringParameters.goalReminder.goalIDs; + reminderID = event.queryStringParameters.goalReminder.reminderID; + }else{ + goalIDs = event.goalReminder.goalIDs + reminderID = event.goalReminder.reminderID; + } + + console.log("event params: ", goalIDs, reminderID); + + // Not working + // var queryParams = { + // TableName: GoalReminderObjectTableName, + // IndexName: IndexName, + // KeyConditionExpression: '#goalID = :vGoalID and #reminderID = :vReminderID', + // ExpressionAttributeValues: { ':vGoalID': goalID, ":vReminderID": reminderID }, + // ExpressionAttributeNames: { '#goalID': 'goalID', "#reminderID": 'reminderID' } + // }; + + // Not working + // var queryParams = { + // TableName: GoalReminderObjectTableName, + // KeyConditionExpression: 'goalID = :vGoalID and reminderID = :vReminderID', + // ExpressionAttributeValues: { ':vGoalID': {"S": goalID}, ":vReminderID": {"S": reminderID} } + // }; + let goalReminders = [] + goalIDs.forEach(async(gid) => { + + var queryParams = { + TableName: GoalReminderObjectTableName, + IndexName: IndexName, + KeyConditionExpression: 'goalID = :vGoalID', + ExpressionAttributeValues: { ':vGoalID': gid,':vReminderID': reminderID }, + FilterExpression: "reminderID = :vReminderID" + }; + const res = await dynamo.query(queryParams).promise(); + if (res.Items.length == 0) { + goalReminders.push({goalID: gid, reminderID: reminderID}); + } + }); + + + // const res = await dynamo.query(queryParams).promise(); + if (goalReminders.length > 0) { + return {statusCode: 200, message: "Success", goalReminders: goalReminders} + }else{ + return {statusCode: 400, message: "Already associated with same goal!", goalReminders: []} + } + + }; + \ No newline at end of file diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/src/package.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/package.json new file mode 100644 index 0000000..3976640 --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/package.json @@ -0,0 +1,10 @@ +{ + "name": "uniquevalidationgoalreminderfunction", + "version": "2.0.0", + "description": "Lambda function generated by Amplify", + "main": "index.js", + "license": "Apache-2.0", + "devDependencies": { + "@types/aws-lambda": "^8.10.92" + } +} diff --git a/amplify/backend/function/uniqueValidationGoalReminderFunction/src/yarn.lock b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/yarn.lock new file mode 100644 index 0000000..4d04f79 --- /dev/null +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/src/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/aws-lambda@^8.10.92": + version "8.10.104" + resolved "https://registry.yarnpkg.com/@types/aws-lambda/-/aws-lambda-8.10.104.tgz#01ec1d55a08bdf1201150d9ecae39ff7cca9ff4a" + integrity sha512-HXZJH8aBa06re9NCtFudOr21izYZycgXIVjd8vFBSNSf6Ca4GYD77TfKqwYgcDqv4olqj5KK+Ks7Xi5IXFbNGA== diff --git a/amplify/backend/function/S3Trigger9c40585d/S3Trigger9c40585d-cloudformation-template.json b/amplify/backend/function/uniqueValidationGoalReminderFunction/uniqueValidationGoalReminderFunction-cloudformation-template.json similarity index 89% rename from amplify/backend/function/S3Trigger9c40585d/S3Trigger9c40585d-cloudformation-template.json rename to amplify/backend/function/uniqueValidationGoalReminderFunction/uniqueValidationGoalReminderFunction-cloudformation-template.json index d600ed9..ecdcad3 100644 --- a/amplify/backend/function/S3Trigger9c40585d/S3Trigger9c40585d-cloudformation-template.json +++ b/amplify/backend/function/uniqueValidationGoalReminderFunction/uniqueValidationGoalReminderFunction-cloudformation-template.json @@ -2,12 +2,17 @@ "AWSTemplateFormatVersion": "2010-09-09", "Description": "{\"createdOn\":\"Linux\",\"createdBy\":\"Amplify\",\"createdWith\":\"9.2.1\",\"stackType\":\"function-Lambda\",\"metadata\":{}}", "Parameters": { - "env": { - "Type": "String" + "CloudWatchRule": { + "Type": "String", + "Default": "NONE", + "Description": " Schedule Expression" }, "deploymentBucketName": { "Type": "String" }, + "env": { + "Type": "String" + }, "s3Key": { "Type": "String" } @@ -30,16 +35,24 @@ "aws:asset:property": "Code" }, "Properties": { + "Code": { + "S3Bucket": { + "Ref": "deploymentBucketName" + }, + "S3Key": { + "Ref": "s3Key" + } + }, "Handler": "index.handler", "FunctionName": { "Fn::If": [ "ShouldNotCreateEnvResources", - "S3Trigger9c40585d", + "uniqueValidationGoalReminderFunction", { "Fn::Join": [ "", [ - "S3Trigger9c40585d", + "uniqueValidationGoalReminderFunction", "-", { "Ref": "env" @@ -53,6 +66,9 @@ "Variables": { "ENV": { "Ref": "env" + }, + "REGION": { + "Ref": "AWS::Region" } } }, @@ -63,15 +79,8 @@ ] }, "Runtime": "nodejs14.x", - "Timeout": 25, - "Code": { - "S3Bucket": { - "Ref": "deploymentBucketName" - }, - "S3Key": { - "Ref": "s3Key" - } - } + "Layers": [], + "Timeout": 25 } }, "LambdaExecutionRole": { @@ -80,12 +89,12 @@ "RoleName": { "Fn::If": [ "ShouldNotCreateEnvResources", - "S3Trigger9c40585dLambdaRole9c40585d", + "bewellLambdaRole24d31853", { "Fn::Join": [ "", [ - "S3Trigger9c40585dLambdaRole9c40585d", + "bewellLambdaRole24d31853", "-", { "Ref": "env" diff --git a/src/components/ReminderForm.vue b/src/components/ReminderForm.vue index 184d539..00fa25f 100644 --- a/src/components/ReminderForm.vue +++ b/src/components/ReminderForm.vue @@ -14,8 +14,14 @@ - + @@ -112,11 +118,22 @@ - + - + @@ -163,12 +180,14 @@ export default { reminder: { type: Object, }, + // goal: { // type: Object // }, }, data() { return { + goalIds: [], dialog: false, intervalId: 60000, valid: true, @@ -192,7 +211,7 @@ export default { ]), ...mapMutations(["SET_ASSOCIATED_GOAL"]), log(e) { - console.log(e) + console.log(e); }, freqStringToArray() { this.freqArr = this.reminder.frequency.split(",") @@ -217,7 +236,6 @@ export default { this.freqStringToArray() // this.SET_ASSOCIATED_GOAL(false) }, - async saveReminder() { if (!this.$refs.form.validate()) { return; @@ -236,14 +254,16 @@ export default { try { if (this.reminder.id) { await this.updateReminderById({ - id: this.reminder.id, - category: this.reminder.category, - level: this.reminder.level, - activity: this.reminder.activity, - frequency: this.reminder.frequency, - contentType: this.reminder.contentType, - time: this.reminder.time, - goalId: this.reminder.goalId, + reminder: { + id: this.reminder.id, + category: this.reminder.category, + level: this.reminder.level, + activity: this.reminder.activity, + frequency: this.reminder.frequency, + contentType: this.reminder.contentType, + time: this.reminder.time, + }, + goalIds: this.goalIds, }); } else { this.reminder.contentType = "Videos"; @@ -251,7 +271,7 @@ export default { this.reminder.level = this.userPrefLevel; // Call Vuex action to add reminder //console.log(this.reminder) - await this.addReminder(this.reminder); + await this.addReminder({reminder: this.reminder, goalIds: this.goalIds}); } // Close form and reset form values this.toggleReminderFormDialog(); diff --git a/src/graphql/extended_queries.js b/src/graphql/extended_queries.js index d2d180b..a32b054 100644 --- a/src/graphql/extended_queries.js +++ b/src/graphql/extended_queries.js @@ -11,17 +11,19 @@ export const extendedListGoals = /* GraphQL */ ` category reminders { items { - id - category - goalId - level - activity - frequency - contentType - time - createdAt - updatedAt - owner + goalReminderId: id + reminder { + id + category + level + activity + frequency + contentType + time + createdAt + updatedAt + owner + } } nextToken } @@ -41,4 +43,51 @@ export const extendedListGoals = /* GraphQL */ ` nextToken } } +`; + +export const extendedListReminders = /* GraphQL */ ` + query ListReminders( + $filter: ModelReminderFilterInput + $limit: Int + $nextToken: String + ) { + listReminders(filter: $filter, limit: $limit, nextToken: $nextToken) { + items { + id + category + goals { + items { + goalReminderId: id + goal { + id + title + category + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + } + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + nextToken + } + } `; \ No newline at end of file diff --git a/src/graphql/mutations.js b/src/graphql/mutations.js index c66b4aa..d5e0bc6 100644 --- a/src/graphql/mutations.js +++ b/src/graphql/mutations.js @@ -26,11 +26,8 @@ export const createReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -68,11 +65,8 @@ export const updateReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -110,11 +104,8 @@ export const deleteReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -1003,3 +994,153 @@ export const deleteCompetitionSubmission = /* GraphQL */ ` } } `; +export const createGoalReminders = /* GraphQL */ ` + mutation CreateGoalReminders( + $input: CreateGoalRemindersInput! + $condition: ModelGoalRemindersConditionInput + ) { + createGoalReminders(input: $input, condition: $condition) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; +export const updateGoalReminders = /* GraphQL */ ` + mutation UpdateGoalReminders( + $input: UpdateGoalRemindersInput! + $condition: ModelGoalRemindersConditionInput + ) { + updateGoalReminders(input: $input, condition: $condition) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; +export const deleteGoalReminders = /* GraphQL */ ` + mutation DeleteGoalReminders( + $input: DeleteGoalRemindersInput! + $condition: ModelGoalRemindersConditionInput + ) { + deleteGoalReminders(input: $input, condition: $condition) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; diff --git a/src/graphql/queries.js b/src/graphql/queries.js index 418472a..5698b55 100644 --- a/src/graphql/queries.js +++ b/src/graphql/queries.js @@ -23,11 +23,8 @@ export const getReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -64,7 +61,6 @@ export const listReminders = /* GraphQL */ ` updatedAt owner } - goalId level frequency contentType @@ -643,6 +639,97 @@ export const listCompetitionSubmissions = /* GraphQL */ ` } } `; +export const getGoalReminders = /* GraphQL */ ` + query GetGoalReminders($id: ID!) { + getGoalReminders(id: $id) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; +export const listGoalReminders = /* GraphQL */ ` + query ListGoalReminders( + $filter: ModelGoalRemindersFilterInput + $limit: Int + $nextToken: String + ) { + listGoalReminders(filter: $filter, limit: $limit, nextToken: $nextToken) { + items { + id + reminderID + goalID + reminder { + id + category + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + progress + stepCount + dueDate + isComplete + completedCount + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + nextToken + } + } +`; export const remindersByLevel = /* GraphQL */ ` query RemindersByLevel( $level: Level! @@ -677,7 +764,6 @@ export const remindersByLevel = /* GraphQL */ ` updatedAt owner } - goalId level frequency contentType diff --git a/src/graphql/subscriptions.js b/src/graphql/subscriptions.js index 3e6cb5c..d838ff3 100644 --- a/src/graphql/subscriptions.js +++ b/src/graphql/subscriptions.js @@ -23,11 +23,8 @@ export const onCreateReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -62,11 +59,8 @@ export const onUpdateReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -101,11 +95,8 @@ export const onDeleteReminder = /* GraphQL */ ` title isComplete } - createdAt - updatedAt - owner + nextToken } - goalId level frequency contentType @@ -895,3 +886,144 @@ export const onDeleteCompetitionSubmission = /* GraphQL */ ` } } `; +export const onCreateGoalReminders = /* GraphQL */ ` + subscription OnCreateGoalReminders($owner: String) { + onCreateGoalReminders(owner: $owner) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; +export const onUpdateGoalReminders = /* GraphQL */ ` + subscription OnUpdateGoalReminders($owner: String) { + onUpdateGoalReminders(owner: $owner) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; +export const onDeleteGoalReminders = /* GraphQL */ ` + subscription OnDeleteGoalReminders($owner: String) { + onDeleteGoalReminders(owner: $owner) { + id + reminderID + goalID + reminder { + id + category + goals { + nextToken + } + level + frequency + contentType + time + activity + createdAt + updatedAt + owner + } + goal { + id + title + category + reminders { + nextToken + } + progress + stepCount + dueDate + isComplete + completedCount + checklist { + title + isComplete + } + createdAt + updatedAt + owner + } + createdAt + updatedAt + owner + } + } +`; diff --git a/src/mixins/videos-mixin.js b/src/mixins/videos-mixin.js index 60f59c8..3725a7a 100644 --- a/src/mixins/videos-mixin.js +++ b/src/mixins/videos-mixin.js @@ -496,10 +496,10 @@ export default { let total = 0 if (reminders) { reminders.forEach((r) => { - //console.log(r) - let val = this.getCompletedActivities(this.capitalizeFirstLet((this.checkForFlex(r.category)).toLowerCase()), - this.checkForNA(r.level)).length / this.getActivities(this.capitalizeFirstLet((this.checkForFlex(r.category)).toLowerCase()), - this.checkForNA(r.level)).length + let reminder = r.reminder; + let val = this.getCompletedActivities(this.capitalizeFirstLet((this.checkForFlex(reminder.category)).toLowerCase()), + this.checkForNA(reminder.level)).length / this.getActivities(this.capitalizeFirstLet((this.checkForFlex(reminder.category)).toLowerCase()), + this.checkForNA(reminder.level)).length total += val }) return total / reminders.length * 100 diff --git a/src/store/modules/goal-store.js b/src/store/modules/goal-store.js index d2bcf22..f2c2921 100644 --- a/src/store/modules/goal-store.js +++ b/src/store/modules/goal-store.js @@ -3,6 +3,8 @@ import { createGoal } from "@/graphql/mutations"; import { updateGoal } from "@/graphql/mutations"; import { deleteGoal } from "@/graphql/mutations"; import { extendedListGoals } from "@/graphql/extended_queries"; +import { deleteGoalReminders } from "@/graphql/mutations"; + // import { getGoal } from "@/graphql/queries"; // import { listGoals } from "@/graphql/queries"; // import awsconfig from "@/aws-exports"; @@ -44,7 +46,18 @@ export default { }, async removeGoal({ commit, dispatch }, id) { try { - await API.graphql(graphqlOperation(deleteGoal, { input: id })); + await API.graphql(graphqlOperation(deleteGoal, { input: id })).then( + (res) => { + let goalReminders = res.data.deleteGoal.reminders.items; + goalReminders.map((item) => { + API.graphql( + graphqlOperation(deleteGoalReminders, { + input: { id: item.id }, + }) + ); + }); + } + ); dispatch("fetchGoals"); commit("SET_SNACKBAR", { show: true, diff --git a/src/store/modules/reminder-store.js b/src/store/modules/reminder-store.js index f9e5d0d..716db70 100644 --- a/src/store/modules/reminder-store.js +++ b/src/store/modules/reminder-store.js @@ -1,8 +1,13 @@ import { API, graphqlOperation } from "aws-amplify"; import { createReminder } from "@/graphql/mutations"; import { updateReminder } from "@/graphql/mutations"; -import { listReminders, getReminder } from "@/graphql/queries"; +import { createGoalReminders } from "@/graphql/mutations"; +// import { listReminders } from "@/graphql/queries"; +import { getReminder } from "@/graphql/queries"; import { deleteReminder } from "@/graphql/mutations"; +import { deleteGoalReminders } from "@/graphql/mutations"; +import { extendedListReminders } from "@/graphql/extended_queries"; +import axios from "axios"; export default { state: { @@ -10,13 +15,20 @@ export default { remindersOn: false, }, actions: { - async addReminder({ commit, dispatch }, reminder) { + async addReminder({ commit, dispatch }, { reminder, goalIds }) { commit("TOGGLE_SAVING", true); try { - await API.graphql( + console.log("addReminder", reminder, goalIds); + const res = await API.graphql( graphqlOperation(createReminder, { input: reminder }) ); dispatch("fetchReminders"); + + dispatch("associateGoalWithReminder", { + reminderId: res.data.createReminder.id, + goalIds: goalIds, + }); + commit("SET_SNACKBAR", { show: true, message: "Activity Reminder Successfully Added!", @@ -27,7 +39,99 @@ export default { } commit("TOGGLE_SAVING", false); }, - async updateReminderById({ commit, dispatch }, reminder) { + async associateGoalWithReminder({ commit, dispatch }, { reminderId, goalIds }) { + + // Create join table record to associate reminder with goal + // we need to make multiple request if there are multiple goals are associated + // e.g. if user has select 2 goals to associate then we need to make + // 2 join table request to associated. + if (goalIds && goalIds.length > 0) { + + await axios({ + method: "GET", + url: "https://f7ta76pehhv6ytuv5qewxrcame0zxdef.lambda-url.us-east-1.on.aws/", + headers: { 'content-type': 'application/json', 'Accept': 'application/json' }, + params: { + "goalReminder": { + "goalIDs": goalIds, + "reminderID": reminderId + } + }, + }).then((res) => { + let goalReminders = res.data.goalReminders + console.log( + "associateGoalWithReminder", + reminderId, goalIds, + goalReminders + ); + if(goalReminders.length > 0){ + goalReminders.forEach((gr) => { + API.graphql(graphqlOperation(createGoalReminders, { input: gr })); + }); + dispatch("fetchReminders"); + commit("SET_SNACKBAR", { + show: true, + message: "Activity Successfully associated with goal!", + color: "var(--mh-green)", + }); + } + }).catch((error) => { + if (error.response) { + commit("SET_SNACKBAR", { + show: true, + message: error.response.data.message, + color: "red", + }); + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + console.log('associateGoalWithReminder',error.response); + console.log('associateGoalWithReminder',error.response.status); + console.log('associateGoalWithReminder',error.response.headers); + } else if (error.request) { + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + console.log('associateGoalWithReminder',error.request); + } else { + // Something happened in setting up the request that triggered an Error + console.log('associateGoalWithReminder', error.message); + } + }); + + } + + // Create join table record to associate reminder with goal + // we need to make multiple request if there are multiple goals are associated + // e.g. if user has select 2 goals to associate then we need to make + // 2 join table request to associated. + // if (goalIds && goalIds.length > 0) { + // let goalReminders = []; + // goalIds.forEach((gid) => { + // let goalReminder = { + // reminderID: reminderId, + // goalID: gid, + // }; + // goalReminders.push(goalReminder); + // }); + // console.log( + // "associateGoalWithReminder", + // reminderId, goalIds, + // goalReminders + // ); + // goalReminders.forEach((gr) => { + // API.graphql(graphqlOperation(createGoalReminders, { input: gr })); + // }); + // dispatch("fetchReminders"); + // commit("SET_SNACKBAR", { + // show: true, + // message: "Activity Successfully associated with goal!", + // color: "var(--mh-green)", + // }); + // } + + + }, + async updateReminderById({ commit, dispatch }, { reminder, goalIds }) { commit("TOGGLE_SAVING", true); try { //console.log("updateReminderById", reminder); @@ -35,11 +139,12 @@ export default { graphqlOperation(updateReminder, { input: reminder }) ); dispatch("fetchReminders"); - /* commit("SET_SNACKBAR", { + dispatch("associateGoalWithReminder",{reminderId: reminder.id, goalIds: goalIds}); + commit("SET_SNACKBAR", { show: true, message: "Activity Reminder Successfully Updated!", color: "var(--mh-green)", - }); */ + }); } catch (error) { console.log(error); } @@ -47,7 +152,16 @@ export default { }, async removeReminder({ commit, dispatch }, id) { try { - await API.graphql(graphqlOperation(deleteReminder, { input: id })); + await API.graphql(graphqlOperation(deleteReminder, { input: id })).then( + (res) => { + let goalReminders = res.data.deleteReminder.goals.items; + goalReminders.map((item) => { + API.graphql( + graphqlOperation(deleteGoalReminders, { input: { id: item.id} }) + ); + }); + } + ); dispatch("fetchReminders"); commit("SET_SNACKBAR", { show: true, @@ -60,7 +174,7 @@ export default { }, async fetchReminders({ commit }) { try { - const res = await API.graphql(graphqlOperation(listReminders)); + const res = await API.graphql(graphqlOperation(extendedListReminders)); commit("SET_REMINDERS", res.data.listReminders.items); } catch (error) { console.log(error);