Generate ticket, dashboard, excess luggage rate updates

This commit is contained in:
Stephanos A
2026-07-16 08:41:35 +03:00
parent 2d2fcfbda9
commit b2b31f30bb
16 changed files with 585 additions and 315 deletions

View File

@@ -1,14 +1,23 @@
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { Controller, Get, Param, SetMetadata, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { DashboardService } from './dashboard.service';
import { JwtGuard } from '../../common/jwt.guard';
import { PassengerAdmin } from '../../common/passenger-guards';
@ApiTags('Dashboard')
@Controller('dashboard')
@UseGuards(JwtGuard)
@ApiBearerAuth('JWT-auth')
export class DashboardController {
constructor(private service: DashboardService) {}
@Get(':passengerId') @ApiOperation({ summary: 'Get home dashboard aggregate for passenger' })
@Get('backoffice-stats')
@PassengerAdmin()
@ApiBearerAuth('IAM-auth')
@ApiOperation({ summary: 'Backoffice summary: totals and revenue by currency' })
getBackofficeStats() { return this.service.getBackofficeStats(); }
@Get(':passengerId')
@UseGuards(JwtGuard)
@ApiBearerAuth('JWT-auth')
@ApiOperation({ summary: 'Get home dashboard aggregate for passenger' })
getHomeDashboard(@Param('passengerId') id: string) { return this.service.getHomeDashboard(id); }
}

View File

@@ -10,6 +10,57 @@ export class DashboardService {
@InjectDataSource() private dataSource: DataSource,
) {}
async getBackofficeStats() {
const [totalBookings, totalPackageBookings, totalTickets, totalPassengers, revenueRows, packageRevenueRows] =
await Promise.all([
this.prisma.booking.count(),
this.prisma.booking.count({ where: { packageId: { not: null } } }),
this.prisma.ticket.count(),
this.prisma.passenger.count(),
this.prisma.$queryRaw<{ currency: string; total: bigint }[]>`
SELECT
COALESCE("displayCurrency"::text, "currency"::text) AS currency,
SUM(COALESCE("displayTotalMinor", "totalMinor")) AS total
FROM passenger."Booking"
WHERE status IN ('CONFIRMED', 'BOARDED')
AND "packageId" IS NULL
AND id IN (SELECT "bookingId" FROM passenger."PaymentIntent" WHERE status = 'SUCCEEDED')
GROUP BY COALESCE("displayCurrency"::text, "currency"::text)
`,
this.prisma.$queryRaw<{ currency: string; total: bigint }[]>`
SELECT
COALESCE("displayCurrency"::text, "currency"::text) AS currency,
SUM(COALESCE("displayTotalMinor", "totalMinor")) AS total
FROM passenger."Booking"
WHERE status IN ('CONFIRMED', 'BOARDED')
AND "packageId" IS NOT NULL
AND id IN (SELECT "bookingId" FROM passenger."PaymentIntent" WHERE status = 'SUCCEEDED')
GROUP BY COALESCE("displayCurrency"::text, "currency"::text)
`,
]);
const totalPackageTickets = await this.prisma.ticket.count({
where: { booking: { packageId: { not: null } } },
});
const toMap = (rows: { currency: string; total: bigint }[]) =>
Object.entries(
rows.reduce((m, r) => { m[r.currency] = Number(r.total); return m; }, {} as Record<string, number>),
).map(([currency, totalMinor]) => ({ currency, totalMinor }));
return {
totalBookings,
totalPackageBookings,
totalNormalBookings: totalBookings - totalPackageBookings,
totalTickets,
totalPackageTickets,
totalNormalTickets: totalTickets - totalPackageTickets,
totalPassengers,
revenueByCurrency: toMap(revenueRows),
packageRevenueByCurrency: toMap(packageRevenueRows),
};
}
async getHomeDashboard(passengerId: string) {
const now = new Date();
const [passenger, upcomingBooking, wallet, promos, weatherAlerts, stationSignals, savedRoutes] = await Promise.all([