Skip to content

Commit

Permalink
Update endpoints to allow forest data posting and fetching
Browse files Browse the repository at this point in the history
Endpoints, DTOs updated with 'type' parameter, to specify what kind of reports is being created/fetched
  • Loading branch information
Ignas-rgb committed Feb 2, 2024
1 parent 6382bc0 commit 82cddae
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ export class AdminController {
@Get('/reports')
async getAllReports(
@Query('isDeleted', ParseBoolPipe) isDeleted: boolean,
@Query('type') type: string,
): Promise<FullReportDto[]> {
return isDeleted
? await this.adminService.getAllDeletedReports()
: await this.adminService.getAllReports();
: await this.adminService.getAllReports(type);
}

@ApiOkResponse({
Expand Down
4 changes: 2 additions & 2 deletions src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class AdminService {
private readonly reportRepository: ReportRepository,
) {}

async getAllReports(): Promise<FullReportDto[]> {
const reports = await this.reportRepository.getAllReports();
async getAllReports(type: string): Promise<FullReportDto[]> {
const reports = await this.reportRepository.getAllReports(type);
return reports.map(AdminService.docToFullReport);
}

Expand Down
3 changes: 3 additions & 0 deletions src/report/dto/create-report.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class CreateReportDto {
@IsLatitude()
latitude: number;

@IsNotEmpty()
type: string;

@IsEmail()
email: string;

Expand Down
11 changes: 7 additions & 4 deletions src/report/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
ParseIntPipe,
Post,
Query,
UploadedFiles,
UseInterceptors,
} from '@nestjs/common';
Expand Down Expand Up @@ -46,17 +47,19 @@ export class ReportController {
type: [PublicReportDto],
})
@Get()
getAllPublicReports(): Promise<PublicReportDto[]> {
return this.reportService.getAllVisibleReports();
getAllPublicReports(@Query('type') type: string): Promise<PublicReportDto[]> {
return this.reportService.getAllVisibleReports(type);
}

@ApiOkResponse({
description: 'Report statistics have been successfully fetched',
type: ReportStatisticsDto,
})
@Get('/statistics')
getReportStatistics(): Promise<ReportStatisticsDto> {
return this.reportService.getReportStatistics();
getReportStatistics(
@Query('type') type: string,
): Promise<ReportStatisticsDto> {
return this.reportService.getReportStatistics(type);
}

@Get('/:refId')
Expand Down
8 changes: 4 additions & 4 deletions src/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ReportStatisticsDto } from './dto/report-statistics.dto';
export class ReportService {
constructor(private readonly reportRepository: ReportRepository) {}

async getAllVisibleReports(): Promise<PublicReportDto[]> {
const reports = await this.reportRepository.getVisibleReports();
async getAllVisibleReports(type: string): Promise<PublicReportDto[]> {
const reports = await this.reportRepository.getVisibleReports(type);
return reports.map(ReportService.docToPublicReport);
}

Expand All @@ -30,8 +30,8 @@ export class ReportService {
return ReportService.docToPublicReport(report);
}

async getReportStatistics(): Promise<ReportStatisticsDto> {
const stats = await this.reportRepository.getVisibleStatusCounts();
async getReportStatistics(type: string): Promise<ReportStatisticsDto> {
const stats = await this.reportRepository.getVisibleStatusCounts(type);
return ReportService.docToReportStatistics(stats);
}

Expand Down
13 changes: 7 additions & 6 deletions src/repositories/reports/report.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export class ReportRepository {
private cloudinary: CloudinaryService,
) {}

async getVisibleReports(): Promise<Report[]> {
async getVisibleReports(type: string): Promise<Report[]> {
return await this.reportModel
.find({ isVisible: true, isDeleted: false })
.find({ isVisible: true, isDeleted: false, type: type })

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
.sort({ reportDate: -1 })
.exec();
}

async getAllReports(): Promise<Report[]> {
async getAllReports(type: string): Promise<Report[]> {
return await this.reportModel
.find({ isDeleted: false })
.find({ isDeleted: false, type: type })

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
.sort({ reportDate: -1 })
.exec();
}
Expand All @@ -39,13 +39,14 @@ export class ReportRepository {
return await this.reportModel.findOne({ refId: refId }).exec();
}

async getVisibleStatusCounts(): Promise<any[]> {
async getVisibleStatusCounts(type: string): Promise<any[]> {
return await this.reportModel
.aggregate([
{
$match: {
isDeleted: false,
isVisible: true,
type: type,
},
},
{
Expand All @@ -72,7 +73,7 @@ export class ReportRepository {
const reportCount = reports.length;
const newReport = new this.reportModel({
name: createReport.name,
type: 'trash',
type: createReport.type,
refId: reportCount + 1,
comment: ' ',
reportLong: createReport.longitude,
Expand Down

0 comments on commit 82cddae

Please sign in to comment.