Skip to content

Instantly share code, notes, and snippets.

@corbifex
Created December 14, 2020 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corbifex/9927d5819b11407e88256c7427bc74fb to your computer and use it in GitHub Desktop.
Save corbifex/9927d5819b11407e88256c7427bc74fb to your computer and use it in GitHub Desktop.
SoldTicket reducer
const soldTicket = async ({params, stateStore}) => {
const { eventId, typeId } = params;
const registeredEventsBuffer = await stateStore.chain.get(
CHAIN_STATE_EVENTS
);
if (!registeredEventsBuffer) {
throw new Error('No events found');
}
const registeredEvents = codec.decode(
eventSchema,
registeredEventsBuffer
);
const eventIndex = registeredEvents.events.findIndex(e =>e.id.toString('hex') === eventId);
if (eventIndex === -1) {
throw new Error('Event not found');
}
const typeIndex = registeredEvents.events[eventIndex].ticketData.findIndex(t => t.id === typeId);
if (typeIndex === -1) {
throw new Error('Ticket type not found');
}
registeredEvents.events[eventIndex].ticketData[typeIndex].sold++;
await setAllEvents(stateStore, registeredEvents.events);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment