Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prisma/migrations/20261124120000_gate_zone/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "Gate" ADD COLUMN "zone" TEXT;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ model Gate {
event Event @relation(fields: [eventId], references: [id], onDelete: Cascade)
eventId String
name String
/// Optional on-chain zone identifier. When set, check-ins at this gate use `check_in_at_zone`.
zone String?
checkIns Ticket[]
createdAt DateTime @default(now())

Expand Down
11 changes: 10 additions & 1 deletion src/gates/dto/create-gate.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transform } from 'class-transformer';
import { IsString, MaxLength, MinLength } from 'class-validator';
import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator';

export class CreateGateDto {
@Transform(({ value }: { value: unknown }) =>
Expand All @@ -9,4 +9,13 @@ export class CreateGateDto {
@MinLength(1)
@MaxLength(256)
name!: string;

@IsOptional()
@Transform(({ value }: { value: unknown }) =>
typeof value === 'string' ? value.trim() : value,
)
@IsString()
@MinLength(1)
@MaxLength(256)
zone?: string;
}
4 changes: 3 additions & 1 deletion src/gates/gates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export class GatesService {
);
}

return this.prisma.gate.create({ data: { eventId, name: dto.name } });
return this.prisma.gate.create({
data: { eventId, name: dto.name, zone: dto.zone ?? null } as any,
});
}

async listForEvent(userId: string, eventId: string) {
Expand Down
7 changes: 6 additions & 1 deletion src/scanner-devices/scanner-check-in.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ export class ScannerCheckInController {

/** #515 — an unsigned `check_in` for the device to sign with its own key. */
@Post(':ticketId/scanner-check-in/build')
buildCheckIn(@Param('ticketId') ticketId: string, @Req() req: DeviceRequest) {
buildCheckIn(
@Param('ticketId') ticketId: string,
@Body() dto: ConfirmCheckInDto,
@Req() req: DeviceRequest,
) {
return this.ticketsService.buildCheckInTxByDevice(
ticketId,
req.scannerDevice,
dto.gateId,
);
}

Expand Down
13 changes: 13 additions & 0 deletions src/stellar/stellar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
encodeBuyResale,
encodeCancelResale,
encodeCheckIn,
encodeCheckInAtZone,
encodeCreateEvent,
encodeIssueTicket,
encodeListForResale,
Expand Down Expand Up @@ -412,7 +413,19 @@ export class StellarService {
buildCheckInTx(params: {
organizerPublicKey: string;
chainTicketId: bigint;
zone?: string;
}): Promise<string> {
if (params.zone) {
return this.buildTx(
params.organizerPublicKey,
'check_in_at_zone',
encodeCheckInAtZone({
organizer: params.organizerPublicKey,
ticketId: params.chainTicketId,
zone: params.zone,
}),
);
}
return this.buildTx(
params.organizerPublicKey,
'check_in',
Expand Down
22 changes: 21 additions & 1 deletion src/stellar/ticketing-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xdr } from '@stellar/stellar-sdk';
import { Address, nativeToScVal, xdr } from '@stellar/stellar-sdk';
import { Spec } from '@stellar/stellar-sdk/contract';
import { SPEC } from './generated/ticketing/spec';
import type { EventParams } from './generated/ticketing/types';
Expand Down Expand Up @@ -97,6 +97,26 @@ export function encodeCheckIn(args: {
});
}

export function encodeCheckInAtZone(args: {
organizer: string;
ticketId: bigint;
zone: string;
}): xdr.ScVal[] {
try {
return contractArgs('check_in_at_zone', {
organizer: args.organizer,
ticket_id: args.ticketId,
zone: args.zone,
});
} catch {
return [
new Address(args.organizer).toScVal(),
nativeToScVal(args.ticketId, { type: 'u64' }),
nativeToScVal(args.zone, { type: 'string' }),
];
}
}

export function encodeRevokeTicket(args: {
organizer: string;
ticketId: bigint;
Expand Down
7 changes: 7 additions & 0 deletions src/tickets/dto/build-check-in.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsOptional, IsUUID } from 'class-validator';

export class BuildCheckInDto {
@IsOptional()
@IsUUID()
gateId?: string;
}
8 changes: 6 additions & 2 deletions src/tickets/tickets.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,12 @@ describe('TicketsController', () => {
describe('check-in', () => {
it('POST /:ticketId/check-in builds the tx', async () => {
service.buildCheckInTx.mockResolvedValue({ xdr: 'x' });
await http().post('/tickets/t1/check-in').set(AUTH).expect(201);
expect(service.buildCheckInTx).toHaveBeenCalledWith(uid, 't1');
await http()
.post('/tickets/t1/check-in')
.set(AUTH)
.send({ gateId: UUID })
.expect(201);
expect(service.buildCheckInTx).toHaveBeenCalledWith(uid, 't1', UUID);
});

it('POST /:ticketId/confirm-check-in forwards gate and reason', async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/tickets/tickets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { TransferTicketDto } from './dto/transfer-ticket.dto';
import { ConfirmTransferTicketDto } from './dto/confirm-transfer-ticket.dto';
import { ConfirmSignedTxDto } from './dto/confirm-signed-tx.dto';
import { ConfirmCheckInDto } from './dto/confirm-check-in.dto';
import { BuildCheckInDto } from './dto/build-check-in.dto';
import { ListForResaleDto } from './dto/list-for-resale.dto';
import { ConfirmListForResaleDto } from './dto/confirm-list-for-resale.dto';
import {
Expand Down Expand Up @@ -256,8 +257,9 @@ export class TicketsController {
buildCheckInTx(
@CurrentUser() user: CurrentUserPayload,
@Param('ticketId') ticketId: string,
@Body() dto: BuildCheckInDto,
) {
return this.ticketsService.buildCheckInTx(user.userId, ticketId);
return this.ticketsService.buildCheckInTx(user.userId, ticketId, dto.gateId);
}

@Post(':ticketId/confirm-check-in')
Expand Down
59 changes: 57 additions & 2 deletions src/tickets/tickets.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ describe('TicketsService', () => {
Array.isArray(cb) ? Promise.all(cb) : cb(prisma),
),
};
};
organizations = { assertMember: jest.fn().mockResolvedValue(undefined) };
stellar = {
gates = { assertBelongsToEvent: jest.fn().mockResolvedValue({ id: 'gate-1', eventId: 'event-1', zone: null }) };
(globalThis as any).__gatesMock = gates;
buildIssueTicketTx: jest.fn().mockResolvedValue('unsigned-xdr'),
buildPurchasePrimaryTx: jest.fn().mockResolvedValue('unsigned-xdr'),
buildTransferTicketTx: jest.fn().mockResolvedValue('unsigned-xdr'),
Expand All @@ -143,7 +145,7 @@ describe('TicketsService', () => {
submitSignedTransaction: jest
.fn()
.mockResolvedValue({ result: 7n, txHash: '0xabc' }),
verifyTicket: jest.fn(),
verifyTicket: jest.fn().mockResolvedValue({ status: 'Used' }),
decodeSignedInvocation: jest.fn(),
txHashOf: jest.fn().mockReturnValue('hash-1'),
getResaleFeeBps: jest.fn().mockResolvedValue(0),
Expand Down Expand Up @@ -176,6 +178,9 @@ describe('TicketsService', () => {
undefined,
undefined,
undefined,
(globalThis as any).__gatesMock as never,
undefined,
undefined,
qrTokens as unknown as QrTokenService,
);
});
Expand Down Expand Up @@ -2842,6 +2847,25 @@ describe('TicketsService', () => {
expect(stellar.submitSignedTransaction).toHaveBeenCalledWith(
'signed-xdr',
);
expect(stellar.verifyTicket).toHaveBeenCalledWith(7n);
});

it('stores VALID for multi-use device check-ins until exhausted', async () => {
mockSignedCall('check_in', [DEVICE_KEY, 7n]);
stellar.verifyTicket.mockResolvedValueOnce({ status: 'Valid' });
prisma.ticket.update.mockImplementation(({ data }) =>
Promise.resolve({ id: 'ticket-1', ...data }),
);

const result = await service.confirmCheckInByDevice(
'ticket-1',
'signed-xdr',
undefined,
undefined,
registered,
);

expect(result.status).toBe('VALID');
});

it('rejects an organizer-signed check-in from a device that has its own key', async () => {
Expand Down Expand Up @@ -2908,6 +2932,7 @@ describe('TicketsService', () => {
expect(stellar.submitSignedTransaction).toHaveBeenCalledWith(
'signed-xdr',
);
expect(stellar.verifyTicket).toHaveBeenCalledWith(7n);
const updateCall = prisma.ticket.update.mock.calls[0] as [
{ where: { id: string }; data: { status: string; checkedInAt: Date } },
];
Expand All @@ -2920,6 +2945,27 @@ describe('TicketsService', () => {
);
});

it('keeps multi-use tickets VALID until exhausted', async () => {
prisma.ticket.findUnique.mockResolvedValue(ticketRow);
stellar.verifyTicket.mockResolvedValueOnce({ status: 'Valid' });
prisma.ticket.update.mockImplementation(({ data }) =>
Promise.resolve({ id: 'ticket-1', ...data }),
);

const result = await service.confirmCheckIn(
'user-1',
'ticket-1',
'signed-xdr',
);

expect(prisma.ticket.update).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ status: 'VALID' }),
}),
);
expect(result.status).toBe('VALID');
});

it('does not update the ticket when submission fails', async () => {
prisma.ticket.findUnique.mockResolvedValue(ticketRow);
stellar.submitSignedTransaction.mockRejectedValueOnce(
Expand Down Expand Up @@ -3079,6 +3125,15 @@ describe('TicketsService', () => {
expect(stellar.buildBuyResaleTx).not.toHaveBeenCalled();
});

it('rejects when the buyer is the seller', async () => {
prisma.ticket.findUnique.mockResolvedValue(resaleTicketRow);

await expect(
service.buildBuyResaleTx('seller-1', 'ticket-1'),
).rejects.toBeInstanceOf(BadRequestException);
expect(stellar.buildBuyResaleTx).not.toHaveBeenCalled();
});

it('requires the buyer to exist', async () => {
prisma.ticket.findUnique.mockResolvedValue(resaleTicketRow);
prisma.user.findUnique.mockResolvedValue(null);
Expand Down
Loading