mirror of
https://github.com/janishutz/libreevent.git
synced 2025-11-25 13:24:24 +00:00
prepare to redo ticket selection api
This commit is contained in:
@@ -11,7 +11,7 @@ const db = require( '../db/db.js' );
|
|||||||
|
|
||||||
class POSTHandler {
|
class POSTHandler {
|
||||||
constructor () {
|
constructor () {
|
||||||
this.allSelectedSeats = { 'test2': [ { 'id': 'secAr1s1', 'component': 1 } ] };
|
this.allSelectedSeats = { 'test2': { 'secAr1s1': { 'id': 'secAr1s1', 'component': 1 } } };
|
||||||
this.ticketTotals = { 'test2': { 'ticket1': 5, 'ticket2': 5 } };
|
this.ticketTotals = { 'test2': { 'ticket1': 5, 'ticket2': 5 } };
|
||||||
this.settings = { 'maxTickets': 10 };
|
this.settings = { 'maxTickets': 10 };
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,7 @@ class POSTHandler {
|
|||||||
handleCall ( call, data, session ) {
|
handleCall ( call, data, session ) {
|
||||||
return new Promise( ( resolve, reject ) => {
|
return new Promise( ( resolve, reject ) => {
|
||||||
if ( call === 'reserveTicket' ) {
|
if ( call === 'reserveTicket' ) {
|
||||||
|
// TODO: Completely rework to optimize for speed.
|
||||||
db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
|
db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
|
||||||
let transmit = {};
|
let transmit = {};
|
||||||
if ( dat.length > 0 ) {
|
if ( dat.length > 0 ) {
|
||||||
@@ -27,54 +28,56 @@ class POSTHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( !this.allSelectedSeats[ data.eventID ] ) {
|
if ( !this.allSelectedSeats[ data.eventID ] ) {
|
||||||
this.allSelectedSeats[ data.eventID ] = [];
|
this.allSelectedSeats[ data.eventID ] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !transmit[ data.eventID ] ) {
|
if ( !transmit[ data.eventID ] ) {
|
||||||
transmit[ data.eventID ] = {};
|
transmit[ data.eventID ] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( this.allSelectedSeats[ data.eventID ].includes( data.id ) && !data.count ) {
|
if ( this.allSelectedSeats[ data.eventID ][ data.id ] && !data.count ) {
|
||||||
reject( { 'code': 409, 'message': 'ERR_SEAT_SELECTED' } );
|
reject( { 'code': 409, 'message': 'ERR_SEAT_SELECTED' } );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
transmit[ data.eventID ][ data.id ] = data;
|
db.getJSONDataSimple( 'booked', data.eventID ).then( booked => {
|
||||||
// TODO: Prevent seat selection if already taken (also if in booked!)
|
transmit[ data.eventID ][ data.id ] = data;
|
||||||
// TODO: Respect max ticket count per user
|
// TODO: Prevent seat selection if already taken (also if in booked!)
|
||||||
// TODO: maybe move to per event setting
|
// TODO: Respect max ticket count per user
|
||||||
let totalUserTickets = 0;
|
// TODO: maybe move to per event setting
|
||||||
for ( let event in transmit ) {
|
let totalUserTickets = 0;
|
||||||
for ( let ticket in transmit[ event ] ) {
|
for ( let event in transmit ) {
|
||||||
totalUserTickets += transmit[ event ][ ticket ][ 'count' ] ?? 1;
|
for ( let ticket in transmit[ event ] ) {
|
||||||
|
totalUserTickets += transmit[ event ][ ticket ][ 'count' ] ?? 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if ( totalUserTickets <= this.settings.maxTickets ) {
|
||||||
if ( totalUserTickets <= this.settings.maxTickets ) {
|
if ( data.count ) {
|
||||||
if ( data.count ) {
|
if ( this.ticketTotals[ data.eventID ] ) {
|
||||||
if ( this.ticketTotals[ data.eventID ] ) {
|
if ( data.count <= ( this.ticketTotals[ data.eventID ][ data.id.slice( 0, data.id.indexOf( '_' ) ) ] ?? 1 ) ) {
|
||||||
if ( data.count <= ( this.ticketTotals[ data.eventID ][ data.id.slice( 0, data.id.indexOf( '_' ) ) ] ?? 1 ) ) {
|
transmit[ data.eventID ][ data.id ][ 'count' ] = data.count;
|
||||||
transmit[ data.eventID ][ data.id ][ 'count' ] = data.count;
|
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component, 'count': data.count } );
|
||||||
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component, 'count': data.count } );
|
} else {
|
||||||
|
reject( { 'code': 409, 'message': this.ticketTotals[ data.eventID ] ?? 1 } );
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
reject( { 'code': 409, 'message': this.ticketTotals[ data.eventID ] ?? 1 } );
|
reject( { 'code': 400, 'message': 'ERR_UNKNOWN_EVENT' } );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
reject( { 'code': 400, 'message': 'ERR_UNKNOWN_EVENT' } );
|
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component } );
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ), 'timestamp': new Date().toString() } ).then( () => {
|
||||||
|
resolve( 'ok' );
|
||||||
|
} ).catch( error => {
|
||||||
|
console.error( error );
|
||||||
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
|
} );
|
||||||
} else {
|
} else {
|
||||||
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component } );
|
reject( { 'code': 418, 'message': 'ERR_TOO_MANY_TICKETS' } );
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ), 'timestamp': new Date().toString() } ).then( () => {
|
} );
|
||||||
resolve( 'ok' );
|
|
||||||
} ).catch( error => {
|
|
||||||
console.error( error );
|
|
||||||
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
|
||||||
} );
|
|
||||||
} else {
|
|
||||||
reject( { 'code': 418, 'message': 'ERR_TOO_MANY_TICKETS' } );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} ).catch( error => {
|
} ).catch( error => {
|
||||||
console.error( error );
|
console.error( error );
|
||||||
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
@@ -127,7 +130,7 @@ class POSTHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getReservedSeats ( event ) {
|
getReservedSeats ( event ) {
|
||||||
return this.allSelectedSeats[ event ];
|
return Object.values( this.allSelectedSeats[ event ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,6 @@
|
|||||||
"payments": "stripe",
|
"payments": "stripe",
|
||||||
"name": "libreevent",
|
"name": "libreevent",
|
||||||
"yourDomain": "http://localhost:8080",
|
"yourDomain": "http://localhost:8080",
|
||||||
"mailSender": "libreevent <info@libreevent.janishutz.com>"
|
"mailSender": "libreevent <info@libreevent.janishutz.com>",
|
||||||
|
"maxTickets": 3
|
||||||
}
|
}
|
||||||
@@ -24,11 +24,13 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<popups ref="popups" size="normal"></popups>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import sideCartView from '@/components/sideCartView.vue';
|
import sideCartView from '@/components/sideCartView.vue';
|
||||||
|
import popups from '@/components/notifications/popups.vue';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'noseatplan',
|
name: 'noseatplan',
|
||||||
@@ -37,6 +39,7 @@ export default {
|
|||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
sideCartView,
|
sideCartView,
|
||||||
|
popups,
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
@@ -68,6 +71,7 @@ export default {
|
|||||||
},
|
},
|
||||||
cartHandling ( operation, data, option ) {
|
cartHandling ( operation, data, option ) {
|
||||||
if ( operation === 'select' ) {
|
if ( operation === 'select' ) {
|
||||||
|
// TODO: Redo to optimize speed and server load
|
||||||
const options = {
|
const options = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: JSON.stringify( { 'id': data.id + '_' + option, 'component': data.category, 'ticketOption': option, 'eventID': this.event.eventID, 'count': ( this.cart[ this.event.eventID ] ? ( this.cart[ this.event.eventID ][ 'tickets' ][ data.id + '_' + option ] ? this.cart[ this.event.eventID ][ 'tickets' ][ data.id + '_' + option ][ 'count' ] : 0 ) : 0 ) + 1, 'category': data.category, 'name': 'Ticket ' + data.category + ' (' + this.event.ageGroups[ option ].name + ')' } ),
|
body: JSON.stringify( { 'id': data.id + '_' + option, 'component': data.category, 'ticketOption': option, 'eventID': this.event.eventID, 'count': ( this.cart[ this.event.eventID ] ? ( this.cart[ this.event.eventID ][ 'tickets' ][ data.id + '_' + option ] ? this.cart[ this.event.eventID ][ 'tickets' ][ data.id + '_' + option ][ 'count' ] : 0 ) : 0 ) + 1, 'category': data.category, 'name': 'Ticket ' + data.category + ' (' + this.event.ageGroups[ option ].name + ')' } ),
|
||||||
@@ -90,7 +94,11 @@ export default {
|
|||||||
}
|
}
|
||||||
} else if ( res.status === 409 ) {
|
} else if ( res.status === 409 ) {
|
||||||
setTimeout( () => {
|
setTimeout( () => {
|
||||||
this.$refs.popups.openPopup( 'Unfortunately, the seat you just tried to select was reserved by somebody else since the last time the seat plan was refreshed. Please select another one. We are sorry for the inconvenience.', {}, 'string' );
|
this.$refs.popups.openPopup( 'Unfortunately, the ticket you just tried to select was reserved by somebody else since the last time the free ticket counts were refreshed. Please select a ticket from another price category. We are sorry for the inconvenience.', {}, 'string' );
|
||||||
|
}, 300 );
|
||||||
|
} else if ( res.status === 418 ) {
|
||||||
|
setTimeout( () => {
|
||||||
|
this.$refs.popups.openPopup( 'You have reached the maximum amount of tickets allowed for a single purchase.', {}, 'string' );
|
||||||
}, 300 );
|
}, 300 );
|
||||||
}
|
}
|
||||||
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {
|
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {
|
||||||
|
|||||||
@@ -390,7 +390,7 @@
|
|||||||
}, 300 );
|
}, 300 );
|
||||||
} else if ( res.status === 418 ) {
|
} else if ( res.status === 418 ) {
|
||||||
setTimeout( () => {
|
setTimeout( () => {
|
||||||
this.$refs.popups.openPopup( 'We are sorry, but you have already selected the maximum amount of tickets you can buy.', {}, 'string' );
|
this.$refs.popups.openPopup( 'We are sorry, but you have already selected the maximum amount of tickets you can buy at once.', {}, 'string' );
|
||||||
}, 300 );
|
}, 300 );
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
@@ -457,8 +457,15 @@
|
|||||||
if ( res.status === 200 ) {
|
if ( res.status === 200 ) {
|
||||||
this.cart[ this.event.eventID ][ 'tickets' ][ 'ticket' + data.component + '_' + group ] = { 'displayName': 'Ticket ' + data.component + ' (' + this.event.ageGroups[ group ].name + ')', 'price': this.event.categories[ this.draggables[ data.component ].category ].price[ group ], 'id': 'ticket' + data.component + '_' + group, 'count': data.data[ group ], 'comp': data.component };
|
this.cart[ this.event.eventID ][ 'tickets' ][ 'ticket' + data.component + '_' + group ] = { 'displayName': 'Ticket ' + data.component + ' (' + this.event.ageGroups[ group ].name + ')', 'price': this.event.categories[ this.draggables[ data.component ].category ].price[ group ], 'id': 'ticket' + data.component + '_' + group, 'count': data.data[ group ], 'comp': data.component };
|
||||||
} else if ( res.status === 409 ) {
|
} else if ( res.status === 409 ) {
|
||||||
|
res.json().then( dat => {
|
||||||
|
this.cart[ this.event.eventID ][ 'tickets' ][ 'ticket' + data.component + '_' + group ] = { 'displayName': 'Ticket ' + data.component + ' (' + this.event.ageGroups[ group ].name + ')', 'price': this.event.categories[ this.draggables[ data.component ].category ].price[ group ], 'id': 'ticket' + data.component + '_' + group, 'count': dat.count, 'comp': data.component };
|
||||||
|
} );
|
||||||
setTimeout( () => {
|
setTimeout( () => {
|
||||||
this.$refs.popups.openPopup( 'Unfortunately, the seat you just tried to select was reserved by somebody else since the last time the seat plan was refreshed. Please select another one. We are sorry for the inconvenience.', {}, 'string' );
|
this.$refs.popups.openPopup( 'Unfortunately, you have selected more tickets than were still available. The maximum amount of tickets that are available have been selected for you automatically. We are sorry for the inconvenience.', {}, 'string' );
|
||||||
|
}, 300 );
|
||||||
|
} else if ( res.status === 418 ) {
|
||||||
|
setTimeout( () => {
|
||||||
|
this.$refs.popups.openPopup( 'We are sorry, but you have already selected the maximum amount of tickets you can buy at once.', {}, 'string' );
|
||||||
}, 300 );
|
}, 300 );
|
||||||
}
|
}
|
||||||
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {
|
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user