Check if item owned API

This commit is contained in:
Troplo 2020-11-18 01:04:25 +11:00
parent 5aa0656714
commit c2d1857093
4 changed files with 65 additions and 61 deletions

View File

@ -128,7 +128,7 @@ blockquote {
<div class='route_container section route_container--settings'>
<div class='column box'>
<tab-view
:tabs='["General", "Account", "Privacy", "Experiments", "TCreate (DEV)", "About"]'
:tabs='["General", "Account", "Privacy", "Experiments", "About"]'
v-model="showSettingTab"
padding='true'
slot='main'
@ -399,61 +399,6 @@ blockquote {
</center>
</div>
</template>
<template slot='TCreate (DEV)'>
<p style='margin-top: 0;'>
Sign up to {{name}} today!
<br/>It's fast and easy.
</p>
<br/>
<form @submit.prevent='createAccount'>
<fancy-input
ref='input'
v-model='tcreate.username'
:label-position="kavelabelPosition"
:error='tcreate.errors.username'
placeholder='Username'
width='100%'
>
</fancy-input>
<fancy-input
v-model='tcreate.name'
:label-position="kavelabelPosition"
:error='tcreate.errors.name'
placeholder='Name'
width='100%'
type="email"
>
</fancy-input>
By pressing the "Register" button, you agree to the Kaverti <router-link to="/legal/tos" v-on:click.native="closeAccountModal">Terms of Service</router-link>, and have read the <router-link to="/legal/privacy" v-on:click.native="closeAccountModal">Privacy Policy</router-link>.
<div style='margin-top: 0.5rem;'>
<b-button
class='is-info'
style="width: 55%"
:loading='tcreate.loading'
@click='createTeam'
v-if='!$store.state.meta.RegistrationsDisabled'
>
Register
</b-button>
<b-button
class='is-info disabled'
disabled="disabled"
style="width: 55%"
:loading='signup.loading'
@click='closeAccountModal'
v-if='$store.state.meta.RegistrationsDisabled'
>
Disabled
</b-button>
&nbsp;
<b-button style="float: right" class="is-danger-passive" @click='closeAccountModal'>
Cancel
</b-button>
</div>
</form>
</template>
</tab-view>
</div>
</div>
@ -470,7 +415,7 @@ blockquote {
:hide-header="true"
>
<tab-view
:tabs='["Register", "Login", "Recovery", "TCreate"]'
:tabs='["Register", "Login", "Recovery"]'
v-model="showAccountTab"
padding='true'
slot='main'

View File

@ -39,14 +39,17 @@
<p class="limit">
{{user.description}}
<br>
Team founded at {{user.createdAt | formatDate}}
Asset uploaded at {{user.createdAt | formatDate}}
</p>
</div>
<div class="column" style="float: right">
<p v-if="!user.saleEnabled" style="float: right;">This item costs <b>{{user.price}} Koins</b></p>
<p v-if="user.saleEnabled" style="float: right;">This item costs <b class="onsale">{{user.price}}</b><b> {{user.salePrice}} Koins</b></p><br>
<b-button @click="buyItem()" style="float: right;">Buy item</b-button>
<b-tooltip v-if="user.limited" style="float: right;" label="You own this item, if you don't want it anymore, you can sell it and put it on the market, this is only available for Limited Edition items">
<b-button @click="buyItem()" v-if="!purchased && !loading && !failStatus" style="float: right;">Buy item</b-button>
<b-button disabled v-if="loading && !failStatus" style="float: right;">Loading purchase status</b-button>
<b-button disabled v-if="purchased && !loading && !failStatus" style="float: right;">Item Owned</b-button>
<b-button disabled v-if="failStatus" style="float: right;">Failed to load status</b-button>
<b-tooltip v-if="user.limited && purchased && !loading && !failStatus" style="float: right;" label="You own this item, if you don't want it anymore, you can sell it and put it on the market, this is only available for Limited Edition items">
<b-button @click="sellItem()" style="float: right;">Sell item</b-button>
</b-tooltip>
</div>
@ -73,6 +76,7 @@
<script>
import MenuButton from '../MenuButton'
import AjaxErrorHandler from "@/assets/js/errorHandler";
export default {
name: 'MarketplaceItem',
@ -89,7 +93,13 @@ export default {
selected: 0,
username: "unknown",
buy: {
loading: false
},
user: null,
purchased: false,
loading: true,
failStatus: false,
savings: 0,
}
},
@ -115,6 +125,19 @@ export default {
}
},
methods: {
buyItem () {
this.axios.get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'marketplace/purchase/' + this.user.id)
.then(() => {
this.buy.loading = false
this.axios
.get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `marketplace/check/${this.$route.params.id}`)
.then(res => this.purchased = res.data.purchased, this.loading = false)
}).catch(e => {
this.buy.loading = false
AjaxErrorHandler(this.$store)(e)
})
},
getPercentageChange() {
const decreaseValue = this.user.price - this.user.salePrice;
@ -148,6 +171,9 @@ export default {
this.resetFetchData()
this.selected = this.getIndexFromRoute(this.$route.path)
this.fetchData()
this.axios
.get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `marketplace/check/${this.$route.params.id}`)
.then(res => this.purchased = res.data.purchased, this.loading = false)
}
}
</script>

View File

@ -132,7 +132,6 @@ module.exports = (sequelize, DataTypes) => {
classMethods: {
associate (models) {
Item.belongsTo(models.ItemCategory)
Item.belongsTo(models.Inventory)
Item.belongsTo(models.User, {through: 'User'})
},
includeOptions (from, limit) {

View File

@ -108,11 +108,15 @@ router.get('/purchase/:id', auth, async(req, res, next) => {
let queryObj = {
where: {id: req.params.id},
}
let queryObj3 = {
where: {UserId: req.userData.UserId, ItemId: req.params.id},
}
let queryObj2 = {
where: {username: req.userData.username},
}
let marketplace = await Item.findOne(queryObj)
let user = await User.findOne(queryObj2)
let inventory = await Inventory.findOne(queryObj3)
if (!marketplace) throw Errors.invalidItem
if (user.koins < marketplace.price && !marketplace.saleEnabled) {
throw Errors.insufficientKoins
@ -123,6 +127,12 @@ router.get('/purchase/:id', auth, async(req, res, next) => {
if (marketplace.offSale) {
throw Errors.offSale
}
if(inventory && !marketplace.limited) {
throw Errors.itemOwned
}
if(inventory && marketplace.limited) {
throw Errors.itemOwnedLimited
}
if(marketplace.saleEnabled) {
const UserId = user.id
const priceTotal = marketplace.salePrice
@ -146,4 +156,28 @@ router.get('/purchase/:id', auth, async(req, res, next) => {
} catch (err) { next(err) }
})
router.get('/check/:id', auth, async(req, res, next) => {
try {
let queryObj = {
where: {id: req.params.id},
}
let queryObj3 = {
where: {UserId: req.userData.UserId, ItemId: req.params.id},
}
let inventory = await Inventory.findOne(queryObj3)
let marketplace = await Item.findOne(queryObj)
if(marketplace) {
if (inventory) {
res.status(200)
res.json({purchased: true})
} else {
res.status(200)
res.json({purchased: false})
}
} else {
throw Errors.invalidItem
}
} catch (err) { next(err) }
})
module.exports = router