Skip to content

Commit

Permalink
Always refetch participant when scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Jul 4, 2024
1 parent 2c896f6 commit c0d05a7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
14 changes: 11 additions & 3 deletions src/pages/scan/scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ describe('test handleParticipant()', () => {
} as any;
const errorModal = jest.fn();
const navigate = jest.fn();
(getParticipantByUuid as any).mockResolvedValue({
ok: true,
data: {id: 101, eventId: 42, regformId: 9999, checkinSecret: dummyParticipant.checkinSecret},
});
await expect(handleParticipant(data, errorModal, navigate, true)).resolves.not.toThrow();
expect(errorModal).toHaveBeenCalledWith({
title: 'The registration form of this participant does not exist',
Expand All @@ -124,12 +128,16 @@ describe('test handleParticipant()', () => {
} as any;
const errorModal = jest.fn();
const navigate = jest.fn();
(getParticipantByUuid as any).mockResolvedValue({
ok: true,
data: {id: 101, eventId: 42, regformId: 73, checkinSecret: dummyParticipant.checkinSecret},
});
await expect(handleParticipant(data, errorModal, navigate, true)).resolves.not.toThrow();
expect(errorModal).not.toHaveBeenCalledWith();
expect(errorModal).not.toHaveBeenCalled();
expect(navigate.mock.calls).toHaveLength(1);
expect(navigate).toHaveBeenCalledWith('/event/1/1/1', {
replace: true,
state: {autoCheckin: true},
state: {autoCheckin: true, fromScan: true},
});
});

Expand Down Expand Up @@ -238,7 +246,7 @@ describe('test handleParticipant()', () => {
expect(navigate.mock.calls).toHaveLength(1);
expect(navigate).toHaveBeenCalledWith('/event/1/1/1', {
replace: true,
state: {autoCheckin: true},
state: {autoCheckin: true, fromScan: true},
});
});
});
Expand Down
79 changes: 32 additions & 47 deletions src/pages/scan/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import {OAuth2Client, generateCodeVerifier} from '@badgateway/oauth2-client';
import {ErrorModalFunction} from '../../context/ModalContextProvider';
import db, {
addEventIfNotExists,
addParticipantIfNotExists,
addParticipant,
addRegformIfNotExists,
getParticipantByUuid,
getRegform,
getServer,
updateParticipant,
} from '../../db/db';
import {getParticipantByUuid as getParticipant} from '../../utils/client';
import {discoveryEndpoint, redirectUri} from '../Auth/utils';
Expand Down Expand Up @@ -92,10 +91,23 @@ export async function handleParticipant(
return;
}

const participant = await getParticipantByUuid(data.checkinSecret);
const response = await getParticipant({
serverId: server.id,
uuid: data.checkinSecret,
});

if (response.ok) {
const {id, eventId, regformId, ...rest} = response.data;
const event = await db.events.get({indicoId: eventId, serverId: server.id});
if (!event) {
errorModal({
title: 'The event of this participant does not exist',
content: 'Scan an event QR code first and try again.',
});
return;
}

if (participant) {
const regform = await getRegform(participant.regformId);
const regform = await db.regforms.get({indicoId: regformId, eventId: event.id});
if (!regform) {
errorModal({
title: 'The registration form of this participant does not exist',
Expand All @@ -104,50 +116,23 @@ export async function handleParticipant(
return;
}

const participantPage = `/event/${regform.eventId}/${regform.id}/${participant.id}`;
let participantId;
await db.transaction('readwrite', db.participants, async () => {
const participant = await db.participants.get({indicoId: id, regformId: regform.id});
if (participant) {
await updateParticipant(participant.id, response.data);
participantId = participant.id;
} else {
participantId = await addParticipant({indicoId: id, regformId: regform.id, ...rest});
}
});

const participantPage = `/event/${regform.eventId}/${regform.id}/${participantId}`;
navigate(participantPage, {
replace: true,
state: {autoCheckin},
state: {autoCheckin, fromScan: true},
});
} else {
const response = await getParticipant({
serverId: server.id,
uuid: data.checkinSecret,
});

if (response.ok) {
const {id, eventId, regformId, ...rest} = response.data;
const event = await db.events.get({indicoId: eventId, serverId: server.id});
if (!event) {
errorModal({
title: 'The event of this participant does not exist',
content: 'Scan an event QR code first and try again.',
});
return;
}

const regform = await db.regforms.get({indicoId: regformId, eventId: event.id});
if (!regform) {
errorModal({
title: 'The registration form of this participant does not exist',
content: 'Scan an event QR code first and try again.',
});
return;
}

const participantId = await addParticipantIfNotExists({
indicoId: id,
regformId: regform.id,
...rest,
});

const participantPage = `/event/${regform.eventId}/${regform.id}/${participantId}`;
navigate(participantPage, {
replace: true,
state: {autoCheckin},
});
} else {
handleError(response, 'Could not fetch participant data', errorModal);
}
handleError(response, 'Could not fetch participant data', errorModal);
}
}

0 comments on commit c0d05a7

Please sign in to comment.