-
Notifications
You must be signed in to change notification settings - Fork 0
R18 and R19 - Report generation #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { ReportController } from './reports.controller'; | ||
|
|
||
| describe('ReportsController', () => { | ||
| let controller: ReportController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [ReportController], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<ReportController>(ReportController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { Controller, Post, Get, Param, Req, UseGuards } from '@nestjs/common'; | ||
| import { ReportService } from './reports.service'; | ||
| import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; | ||
| import { RolesGuard } from '../auth/guards/roles.guard'; | ||
| import { Roles } from '../auth/decorators/roles.decorator'; | ||
| import { Role } from '@prisma/client'; | ||
|
|
||
| @Controller() | ||
| @UseGuards(JwtAuthGuard, RolesGuard) | ||
| // 🔐 Role-based access control for report endpoints | ||
| export class ReportController { | ||
| constructor(private service: ReportService) {} | ||
|
|
||
| /** | ||
| * R18 – Generate Event Report | ||
| * Only Organizer can request report for their own event | ||
| */ | ||
| @Post(':id/report/generate') | ||
| @Roles(Role.ORG) | ||
| create(@Param('id') id: string, @Req() req) { | ||
| return this.service.createReport(+id, req.user.sub); | ||
| } | ||
|
|
||
| /** | ||
| * R18– View report status and results | ||
| * Organizer can view own reports | ||
| * Admin can view any report | ||
| */ | ||
| @Get('reports/:id') | ||
| get(@Param('id') id: string, @Req() req) { | ||
| return this.service.getReport(+id, req.user); | ||
| } | ||
|
|
||
| /** | ||
| * – Admin can view all reports | ||
| */ | ||
| @Get('reports') | ||
| @Roles(Role.ADMIN) | ||
| getAll() { | ||
| return this.service.getAllReports(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { ReportService } from './reports.service'; | ||
| import { ReportController } from './reports.controller'; | ||
| import { PrismaModule } from '../prisma/prisma.module'; | ||
|
|
||
| @Module({}) | ||
| @Module({ | ||
| imports: [PrismaModule], | ||
| controllers: [ReportController], | ||
| providers: [ReportService], | ||
| }) | ||
| export class ReportsModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { ReportService } from './reports.service'; | ||
|
|
||
| describe('ReportsService', () => { | ||
| let service: ReportService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [ReportService], | ||
| }).compile(); | ||
|
|
||
| service = module.get<ReportService>(ReportService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { | ||
| Injectable, | ||
| NotFoundException, | ||
| ForbiddenException, | ||
| BadRequestException, | ||
| } from '@nestjs/common'; | ||
| import { PrismaService } from '../prisma/prisma.service'; | ||
| import { ReportStatus } from '@prisma/client'; | ||
|
|
||
| @Injectable() | ||
| export class ReportService { | ||
| constructor(private prisma: PrismaService) {} | ||
|
|
||
| /** | ||
| * R18 – Create report request | ||
| * - Validates event existence | ||
| * - Validates organizer ownership | ||
| * - Initializes report as PENDING | ||
| */ | ||
| async createReport(eventId: number, organizerId: number) { | ||
| const event = await this.prisma.event.findUnique({ | ||
| where: { event_id: eventId }, | ||
| }); | ||
|
|
||
| if (!event) throw new NotFoundException('Event not found'); | ||
|
|
||
| // R18 – Ensure organizer owns the event | ||
| if (event.organizer_id !== organizerId) | ||
| throw new ForbiddenException('You do not own this event'); | ||
|
|
||
| // Prevent duplicate reports (optional improvement) | ||
| const existing = await this.prisma.report.findFirst({ | ||
| where: { | ||
| event_id: eventId, | ||
| status: { in: ['PENDING', 'IN_PROGRESS'] }, | ||
| }, | ||
| }); | ||
|
|
||
| if (existing) throw new BadRequestException('Report already in progress'); | ||
|
|
||
| const report = await this.prisma.report.create({ | ||
| data: { | ||
| event_id: eventId, | ||
| organizer_id: organizerId, | ||
| status: ReportStatus.PENDING, | ||
| progress_percent: 0, | ||
| }, | ||
| }); | ||
|
|
||
| // R19 – Start simulated background processing | ||
| this.processReport(report.report_id); | ||
|
|
||
| return report; | ||
| } | ||
|
|
||
| /** | ||
| * R19 – Simulated async processing | ||
| * - Updates progress | ||
| * - Computes analytics | ||
| * - Stores JSON result | ||
| */ | ||
| private async processReport(reportId: number) { | ||
| // Move to IN_PROGRESS | ||
| await this.prisma.report.update({ | ||
| where: { report_id: reportId }, | ||
| data: { status: ReportStatus.IN_PROGRESS }, | ||
| }); | ||
|
|
||
| for (let percent = 10; percent <= 100; percent += 10) { | ||
| await new Promise((resolve) => setTimeout(resolve, 1500)); | ||
|
|
||
| await this.prisma.report.update({ | ||
| where: { report_id: reportId }, | ||
| data: { progress_percent: percent }, | ||
| }); | ||
| } | ||
|
|
||
| // Fetch registrations to calculate analytics | ||
| const report = await this.prisma.report.findUnique({ | ||
| where: { report_id: reportId }, | ||
| include: { | ||
| event: { | ||
| include: { registrations: true }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!report) return; // Prevent null access | ||
|
|
||
| const total = report.event.registrations.length; | ||
|
|
||
| const confirmed = report.event.registrations.filter( | ||
| (r) => r.status === 'CONFIRMED', | ||
| ).length; | ||
|
|
||
| const cancelled = report.event.registrations.filter( | ||
| (r) => r.status === 'CANCELLED', | ||
| ).length; | ||
|
|
||
| const occupancyRate = | ||
| report.event.capacity > 0 | ||
| ? Math.round((confirmed / report.event.capacity) * 100) | ||
| : 0; | ||
|
|
||
| await this.prisma.report.update({ | ||
| where: { report_id: reportId }, | ||
| data: { | ||
| status: ReportStatus.DONE, | ||
| result_data: { | ||
| total_registrations: total, | ||
| confirmed_registrations: confirmed, | ||
| cancelled_registrations: cancelled, | ||
| capacity: report.event.capacity, | ||
| occupancy_rate_percent: occupancyRate, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * R35 + R36 – View report status + result | ||
| * - Organizer can view own | ||
| * - Admin can view any | ||
| */ | ||
| async getReport(reportId: number, user: any) { | ||
| const report = await this.prisma.report.findUnique({ | ||
| where: { report_id: reportId }, | ||
| }); | ||
|
|
||
| if (!report) throw new NotFoundException('Report not found'); | ||
|
|
||
| // R37 – Access control validation | ||
| if (user.role !== 'ADMIN' && report.organizer_id !== user.sub) { | ||
| throw new ForbiddenException('Access denied'); | ||
| } | ||
|
|
||
| return report; | ||
| } | ||
|
|
||
| /** | ||
| * R37 – Admin can view all reports | ||
| */ | ||
| async getAllReports() { | ||
| return this.prisma.report.findMany({ | ||
| include: { event: true }, | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the same issue