mirror of
https://github.com/janishutz/libreevent.git
synced 2025-11-25 21:34:24 +00:00
update postHandler to accept ticket counts
This commit is contained in:
@@ -12,7 +12,8 @@ const db = require( '../db/db.js' );
|
|||||||
class POSTHandler {
|
class POSTHandler {
|
||||||
constructor () {
|
constructor () {
|
||||||
this.allSelectedSeats = { 'TestEvent2': [ { 'id': 'secAr1s1', 'component': 1 } ] };
|
this.allSelectedSeats = { 'TestEvent2': [ { 'id': 'secAr1s1', 'component': 1 } ] };
|
||||||
this.ticketTotals = {};
|
this.ticketTotals = { 'TestEvent2': { 'ticket1': 5, 'ticket2': 5 } };
|
||||||
|
this.settings = { 'maxTickets': 10 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add lang in the future
|
// Add lang in the future
|
||||||
@@ -29,19 +30,47 @@ class POSTHandler {
|
|||||||
if ( !this.allSelectedSeats[ data.eventID ] ) {
|
if ( !this.allSelectedSeats[ data.eventID ] ) {
|
||||||
this.allSelectedSeats[ data.eventID ] = [];
|
this.allSelectedSeats[ data.eventID ] = [];
|
||||||
}
|
}
|
||||||
if ( this.allSelectedSeats[ data.eventID ].includes( data.id ) ) {
|
|
||||||
reject( { 'code': 409, 'message': 'Seat already selected' } );
|
if ( this.allSelectedSeats[ data.eventID ].includes( data.id ) && !data.count ) {
|
||||||
|
reject( { 'code': 409, 'message': 'ERR_SEAT_SELECTED' } );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
transmit[ data.eventID ][ data.id ] = data;
|
||||||
|
// TODO: Respect max ticket count per user
|
||||||
|
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 } );
|
||||||
|
} else {
|
||||||
|
reject( { 'code': 409, 'message': this.ticketTotals[ data.eventID ] ?? 1 } );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reject( { 'code': 400, 'message': 'ERR_UNKNOWN_EVENT' } );
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component } );
|
this.allSelectedSeats[ data.eventID ].push( { 'id': data.id, 'component': data.component } );
|
||||||
transmit[ data.eventID ][ data.id ] = data;
|
}
|
||||||
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ), 'timestamp': new Date().toString() } ).then( () => {
|
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ), 'timestamp': new Date().toString() } ).then( () => {
|
||||||
resolve( 'ok' );
|
resolve( 'ok' );
|
||||||
} ).catch( error => {
|
} ).catch( () => {
|
||||||
reject( { 'code': 500, 'message': error } );
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
} );
|
} );
|
||||||
|
} else {
|
||||||
|
reject( { 'code': 418, 'message': 'ERR_TOO_MANY_TICKETS' } );
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} ).catch( error => {
|
} ).catch( () => {
|
||||||
reject( { 'code': 500, 'message': error } );
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
} );
|
} );
|
||||||
} else if ( call === 'deselectTicket' ) {
|
} else if ( call === 'deselectTicket' ) {
|
||||||
db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
|
db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
|
||||||
@@ -60,11 +89,11 @@ class POSTHandler {
|
|||||||
}
|
}
|
||||||
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ) } ).then( () => {
|
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ) } ).then( () => {
|
||||||
resolve( 'ok' );
|
resolve( 'ok' );
|
||||||
} ).catch( error => {
|
} ).catch( () => {
|
||||||
reject( { 'code': 500, 'message': error } );
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
} );
|
} );
|
||||||
} ).catch( error => {
|
} ).catch( () => {
|
||||||
reject( { 'code': 500, 'message': error } );
|
reject( { 'code': 500, 'message': 'ERR_DB' } );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|||||||
0
src/server/backend/db/data/events.json
Normal file
0
src/server/backend/db/data/events.json
Normal file
Reference in New Issue
Block a user