From 82337d18fc832aac7cda14a80121a6d360ae153c Mon Sep 17 00:00:00 2001 From: Troplo Date: Tue, 24 Nov 2020 00:25:52 +1100 Subject: [PATCH] Completed Friends/Relationships --- frontend/src/components/routes/User.vue | 142 +++++++++++++++++++++++- lib/errors.js | 4 + routes/relationship.js | 76 +++++++++++++ 3 files changed, 220 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/routes/User.vue b/frontend/src/components/routes/User.vue index efb1e3b..1aec6f4 100644 --- a/frontend/src/components/routes/User.vue +++ b/frontend/src/components/routes/User.vue @@ -56,10 +56,19 @@ Manage user
- + Remove Friend - + + Pending Friend Request + + + Accept Friend Request + + + Remove Friend Request + + Send friend request @@ -184,20 +193,149 @@ }) .catch(AjaxErrorHandler(this.$store)) }, + refreshFriend() { + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data) + }, + removeFriend () { + this.axios + .put(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'relationships/remove', { + friend: this.$route.params.username + }) + .then(() => { + this.refreshFriend() + this.description.loading = false + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data, this.refreshFriend()) + .catch(e => { + this.refreshFriend() + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) + }) + .catch(e => { + this.refreshFriend() + this.description.loading = false + + AjaxErrorHandler(this.$store)(e, error => { + this.description.error = error.message + }) + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data, this.refreshFriend()) + .catch(e => { + this.refreshFriend() + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) + }) + }, doRelationship () { this.axios .post(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'relationships/send', { friend: this.$route.params.username }) .then(() => { + this.refreshFriend() this.description.loading = false + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data) + .catch(e => { + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) }) .catch(e => { + this.refreshFriend() this.description.loading = false AjaxErrorHandler(this.$store)(e, error => { this.description.error = error.message }) + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data) + .catch(e => { + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) + }) + }, + doRelationshipAccept () { + this.axios + .put(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'relationships/accept', { + friend: this.$route.params.username + }) + .then(() => { + this.refreshFriend() + this.description.loading = false + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data) + .catch(e => { + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) + }) + .catch(e => { + this.refreshFriend() + this.description.loading = false + + AjaxErrorHandler(this.$store)(e, error => { + this.description.error = error.message + }) + this.axios + .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `relationships/${this.$route.params.username}`) + .then(res => this.relationship = res.data) + .catch(e => { + let invalidId = e.response.data.errors.find(error => { + return error.name === 'accountDoesNotExist' + }) + + if(invalidId) { + this.$store.commit('set404Page', true) + } else { + AjaxErrorHandler(this.$store)(e) + } + }) }) }, getIndexFromRoute (path) { diff --git a/lib/errors.js b/lib/errors.js index 50fd02a..3bca3b7 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -35,6 +35,10 @@ let Errors = { 'You have already sent a friend request to this person, or you are currently friends.', 400 ], + needToBeFriend: [ + 'You need to be friends with this person to do this action.', + 401 + ], teamDoesNotExist: [ 'This team does not exist.', 400 diff --git a/routes/relationship.js b/routes/relationship.js index d6367a8..6fcae22 100644 --- a/routes/relationship.js +++ b/routes/relationship.js @@ -39,6 +39,82 @@ router.post('/send', auth, async(req, res, next) => { } catch (err) { next(err) } }) +router.put('/accept', auth, async(req, res, next) => { + try { + if(req.body.friend !== undefined) { + let queryObj = { + where: {username: req.userData.username} + } + let user = await User.findOne(queryObj) + let queryObj2 = { + where: {username: req.body.friend} + } + let user2 = await User.findOne(queryObj2) + if (!user) { + throw Errors.unknown + } + if(!user2) { + throw Errors.accountDoesNotExist + } + let checkIfSent = await Relationship.findOne({ + where: {friend1Id: user.id, friend2Id: user2.id, type: 'pendingCanAccept'} + }) + if (!checkIfSent) { + throw Errors.needToBeFriend + } + let checkIfSent2 = await Relationship.findOne({ + where: {friend1Id: user2.id, friend2Id: user.id, type: 'pending'} + }) + checkIfSent.update({type: 'accepted'}) + checkIfSent2.update({type: 'accepted'}) + res.status(200) + res.json({success: true}) + } else { + res.status(400) + res.json({success: false}) + } + + } catch (err) { next(err) } +}) + +router.put('/remove', auth, async(req, res, next) => { + try { + if(req.body.friend !== undefined) { + let queryObj = { + where: {username: req.userData.username} + } + let user = await User.findOne(queryObj) + let queryObj2 = { + where: {username: req.body.friend} + } + let user2 = await User.findOne(queryObj2) + if (!user) { + throw Errors.unknown + } + if(!user2) { + throw Errors.accountDoesNotExist + } + let checkIfSent = await Relationship.findOne({ + where: {friend1Id: user.id, friend2Id: user2.id} + }) + if (!checkIfSent) { + throw Errors.needToBeFriend + } + let checkIfSent2 = await Relationship.findOne({ + where: {friend1Id: user2.id, friend2Id: user.id} + }) + checkIfSent.destroy() + checkIfSent2.destroy() + res.status(200) + res.json({success: true}) + } else { + res.status(400) + res.json({success: false}) + } + + } catch (err) { next(err) } +}) + router.get('/:username', auth, async(req, res, next) => { try { let queryObj = {