Completed Friends/Relationships

This commit is contained in:
Troplo 2020-11-24 00:25:52 +11:00
parent f0923b0a60
commit 82337d18fc
3 changed files with 220 additions and 2 deletions

View File

@ -56,10 +56,19 @@
Manage user
</button>
</menu-button><br>
<b-button v-if="$store.state.experimentsStore.relationships && relationship && user.username !== $store.state.username && $store.state.username" class='is-danger button' icon-left="minus">
<b-button @click="removeFriend" v-if="$store.state.experimentsStore.relationships && relationship.type === 'accepted' && user.username !== $store.state.username && $store.state.username" class='is-danger button' icon-left="minus">
Remove Friend
</b-button>
<b-button :value="1" @click="doRelationship" v-if="$store.state.experimentsStore.relationships && user && !relationship && user.username !== $store.state.username && $store.state.username" class='is-info button' icon-left="plus">
<b-button @click="removeFriend" v-if="$store.state.experimentsStore.relationships && relationship.type === 'pending' && user.username !== $store.state.username && $store.state.username" class='is-warning button' icon-left="minus">
Pending Friend Request
</b-button>
<b-button @click="doRelationshipAccept" v-if="$store.state.experimentsStore.relationships && relationship.type === 'pendingCanAccept' && user.username !== $store.state.username && $store.state.username" class='is-success button' icon-left="plus">
Accept Friend Request
</b-button>
<b-button @click="removeFriend" v-if="$store.state.experimentsStore.relationships && relationship.type === 'pendingCanAccept' && user.username !== $store.state.username && $store.state.username" class='is-danger button' icon-left="minus">
Remove Friend Request
</b-button>
<b-button @click="doRelationship" v-if="$store.state.experimentsStore.relationships && user && relationship.type === 'notFriends' && user.username !== $store.state.username && $store.state.username" class='is-info button' icon-left="plus">
Send friend request
</b-button>
</div>
@ -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) {

View File

@ -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

View File

@ -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 = {