prepare to redo ticket selection api

This commit is contained in:
2023-08-08 10:52:51 +02:00
parent ed991d3f57
commit 4d27fc8c48
4 changed files with 57 additions and 38 deletions

View File

@@ -11,7 +11,7 @@ const db = require( '../db/db.js' );
class POSTHandler {
constructor () {
this.allSelectedSeats = { 'test2': [ { 'id': 'secAr1s1', 'component': 1 } ] };
this.allSelectedSeats = { 'test2': { 'secAr1s1': { 'id': 'secAr1s1', 'component': 1 } } };
this.ticketTotals = { 'test2': { 'ticket1': 5, 'ticket2': 5 } };
this.settings = { 'maxTickets': 10 };
}
@@ -20,6 +20,7 @@ class POSTHandler {
handleCall ( call, data, session ) {
return new Promise( ( resolve, reject ) => {
if ( call === 'reserveTicket' ) {
// TODO: Completely rework to optimize for speed.
db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
let transmit = {};
if ( dat.length > 0 ) {
@@ -27,54 +28,56 @@ class POSTHandler {
}
if ( !this.allSelectedSeats[ data.eventID ] ) {
this.allSelectedSeats[ data.eventID ] = [];
this.allSelectedSeats[ data.eventID ] = {};
}
if ( !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' } );
return;
}
transmit[ data.eventID ][ data.id ] = data;
// TODO: Prevent seat selection if already taken (also if in booked!)
// TODO: Respect max ticket count per user
// TODO: maybe move to per event setting
let totalUserTickets = 0;
for ( let event in transmit ) {
for ( let ticket in transmit[ event ] ) {
totalUserTickets += transmit[ event ][ ticket ][ 'count' ] ?? 1;
db.getJSONDataSimple( 'booked', data.eventID ).then( booked => {
transmit[ data.eventID ][ data.id ] = data;
// TODO: Prevent seat selection if already taken (also if in booked!)
// TODO: Respect max ticket count per user
// TODO: maybe move to per event setting
let totalUserTickets = 0;
for ( let event in transmit ) {
for ( let ticket in transmit[ event ] ) {
totalUserTickets += transmit[ event ][ ticket ][ 'count' ] ?? 1;
}
}
}
if ( totalUserTickets <= this.settings.maxTickets ) {
if ( data.count ) {
if ( this.ticketTotals[ data.eventID ] ) {
if ( data.count <= ( this.ticketTotals[ data.eventID ][ data.id.slice( 0, data.id.indexOf( '_' ) ) ] ?? 1 ) ) {
transmit[ data.eventID ][ data.id ][ 'count' ] = data.count;
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component, 'count': data.count } );
if ( totalUserTickets <= this.settings.maxTickets ) {
if ( data.count ) {
if ( this.ticketTotals[ data.eventID ] ) {
if ( data.count <= ( this.ticketTotals[ data.eventID ][ data.id.slice( 0, data.id.indexOf( '_' ) ) ] ?? 1 ) ) {
transmit[ data.eventID ][ data.id ][ '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 {
reject( { 'code': 409, 'message': this.ticketTotals[ data.eventID ] ?? 1 } );
reject( { 'code': 400, 'message': 'ERR_UNKNOWN_EVENT' } );
return;
}
} else {
reject( { 'code': 400, 'message': 'ERR_UNKNOWN_EVENT' } );
return;
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component } );
}
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 {
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 => {
console.error( error );
reject( { 'code': 500, 'message': 'ERR_DB' } );
@@ -127,7 +130,7 @@ class POSTHandler {
}
getReservedSeats ( event ) {
return this.allSelectedSeats[ event ];
return Object.values( this.allSelectedSeats[ event ] );
}
}

View File

@@ -6,5 +6,6 @@
"payments": "stripe",
"name": "libreevent",
"yourDomain": "http://localhost:8080",
"mailSender": "libreevent <info@libreevent.janishutz.com>"
"mailSender": "libreevent <info@libreevent.janishutz.com>",
"maxTickets": 3
}

View File

@@ -24,11 +24,13 @@
</div>
</div>
</div>
<popups ref="popups" size="normal"></popups>
</div>
</template>
<script>
import sideCartView from '@/components/sideCartView.vue';
import popups from '@/components/notifications/popups.vue';
export default {
name: 'noseatplan',
@@ -37,6 +39,7 @@ export default {
},
components: {
sideCartView,
popups,
},
data () {
return {
@@ -68,6 +71,7 @@ export default {
},
cartHandling ( operation, data, option ) {
if ( operation === 'select' ) {
// TODO: Redo to optimize speed and server load
const options = {
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 + ')' } ),
@@ -90,7 +94,11 @@ export default {
}
} else if ( res.status === 409 ) {
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 );
}
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {

View File

@@ -390,7 +390,7 @@
}, 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.', {}, '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 );
}
} );
@@ -457,8 +457,15 @@
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 };
} 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( () => {
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 );
}
if ( Object.keys( this.cart[ this.event.eventID ][ 'tickets' ] ).length < 1 ) {