Skip to content
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

Update endpoints to allow forest data posting and fetching #2

Merged
merged 12 commits into from
Feb 13, 2024
10 changes: 7 additions & 3 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NotFoundException,
Param,
ParseBoolPipe,
ParseEnumPipe,
ParseIntPipe,
Post,
Put,
Expand All @@ -20,6 +21,7 @@ import {
ApiConsumes,
ApiNotFoundResponse,
ApiOkResponse,
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import {
Expand All @@ -32,6 +34,7 @@ import {
import { FilesInterceptor } from '@nestjs/platform-express';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { Request } from 'express';
import { ReportCategory } from '../common/dto/report-category';

@Controller('admin')
@ApiTags('admin')
Expand All @@ -44,13 +47,14 @@ export class AdminController {
description: 'All reports have been successfully found',
type: [FullReportDto],
})
@ApiQuery({ name: 'category', enum: ReportCategory, required: false })
@Get('/reports')
async getAllReports(
@Query('isDeleted', ParseBoolPipe) isDeleted: boolean,
@Query('category', new ParseEnumPipe(ReportCategory, { optional: true }))
category?: ReportCategory,
): Promise<FullReportDto[]> {
return isDeleted
? await this.adminService.getAllDeletedReports()
: await this.adminService.getAllReports();
return this.adminService.getAllReports(isDeleted, category);
}

@ApiOkResponse({
Expand Down
27 changes: 18 additions & 9 deletions src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HistoryEditsDto } from './dto/history-edits.dto';
import { StatusRecordsDto } from '../report/dto';
import { UpdateReportDto } from './dto';
import { Dump } from '../repositories/dumps/schemas';
import { ReportCategory } from '../common/dto/report-category';

@Injectable()
export class AdminService {
Expand All @@ -22,8 +23,14 @@ export class AdminService {
private readonly reportRepository: ReportRepository,
) {}

async getAllReports(): Promise<FullReportDto[]> {
const reports = await this.reportRepository.getAllReports();
async getAllReports(
isDeleted: boolean,
category?: ReportCategory,
): Promise<FullReportDto[]> {
const reports = await this.reportRepository.getAllReports(
isDeleted,
category,
);
return reports.map(AdminService.docToFullReport);
}

Expand All @@ -39,11 +46,6 @@ export class AdminService {
return AdminService.docToFullDump(dump);
}

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

async updateReport(
updateReport: UpdateReportDto,
images: Array<Express.Multer.File>,
Expand Down Expand Up @@ -78,7 +80,6 @@ export class AdminService {
return new FullDumpDto(
dump._id.toString(),
dump.name,
dump.type,
dump.reportLong,
dump.reportLat,
dump.isVisible,
Expand All @@ -93,7 +94,7 @@ export class AdminService {
return new FullReportDto(
report.refId,
report.name,
report.type,
AdminService.parseReportCategory(report.type),
report.refId,
report.reportLong,
report.reportLat,
Expand Down Expand Up @@ -127,4 +128,12 @@ export class AdminService {
private static docToStatusRecords(records: StatusRecords): StatusRecordsDto {
return new StatusRecordsDto(records.status, new Date(records.date));
}

private static parseReportCategory(value: string): ReportCategory {
if (Object.values(ReportCategory).includes(value as ReportCategory)) {
return value as ReportCategory;
} else {
throw new Error(`Invalid report category: ${value}`);
}
}
}
5 changes: 0 additions & 5 deletions src/admin/dto/full-dump.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export class FullDumpDto {
@ApiProperty()
name: string;

@ApiProperty()
type: string;

@ApiProperty({ format: 'double' })
longitude: number;

Expand All @@ -36,7 +33,6 @@ export class FullDumpDto {
constructor(
refId: string,
name: string,
type: string,
longitude: number,
latitude: number,
isVisible: boolean,
Expand All @@ -47,7 +43,6 @@ export class FullDumpDto {
) {
this.refId = refId;
this.name = name;
this.type = type;
this.longitude = longitude;
this.latitude = latitude;
this.isVisible = isVisible;
Expand Down
7 changes: 4 additions & 3 deletions src/admin/dto/full-report.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
import { HistoryDataDto } from './history-data.dto';
import { StatusRecordsDto } from './status-records.dto';
import { ToBoolean } from '../../common/transform/boolean.transform';
import { ReportCategory } from '../../common/dto/report-category';

export class FullReportDto {
@ApiProperty()
Expand All @@ -11,7 +12,7 @@ export class FullReportDto {
name: string;

@ApiProperty()
type: string;
category: ReportCategory;

@ApiProperty()
refId: string;
Expand Down Expand Up @@ -57,7 +58,7 @@ export class FullReportDto {
constructor(
_id: string,
name: string,
type: string,
category: ReportCategory,
refId: string,
longitude: number,
latitude: number,
Expand All @@ -74,7 +75,7 @@ export class FullReportDto {
) {
this._id = _id;
this.name = name;
this.type = type;
this.category = category;
this.refId = refId;
this.longitude = longitude;
this.latitude = latitude;
Expand Down
4 changes: 4 additions & 0 deletions src/common/dto/report-category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum ReportCategory {
trash = 'trash',
forest = 'forest',
}
5 changes: 0 additions & 5 deletions src/dumps/dto/dump.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ export class DumpDto {
@ApiProperty()
name: string;

@ApiProperty()
type: string;

@ApiProperty({ format: 'double' })
reportLong: number;

Expand All @@ -27,7 +24,6 @@ export class DumpDto {

constructor(
name: string,
type: string,
reportLong: number,
reportLat: number,
address: string,
Expand All @@ -36,7 +32,6 @@ export class DumpDto {
moreInformation: string,
) {
this.name = name;
this.type = type;
this.reportLong = reportLong;
this.reportLat = reportLat;
this.address = address;
Expand Down
2 changes: 1 addition & 1 deletion src/dumps/dump.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export class DumpService {
const dumps = await this.dumpRepository.getVisibleDumps();
return dumps.map(this.docToPublicDump);
}

private docToPublicDump(e: Dump): DumpDto {
return new DumpDto(
e.name,
e.type,
e.reportLong,
e.reportLat,
e.address,
Expand Down
6 changes: 5 additions & 1 deletion src/report/dto/create-report.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsLatitude, IsLongitude, IsNotEmpty } from 'class-validator';
import { IsEmail, IsEnum, IsLatitude, IsLongitude, IsNotEmpty } from 'class-validator';
import { ReportCategory } from '../../common/dto/report-category';

export class CreateReportDto {
@IsNotEmpty()
Expand All @@ -11,6 +12,9 @@ export class CreateReportDto {
@IsLatitude()
latitude: number;

@IsEnum(ReportCategory)
category: ReportCategory;

@IsEmail()
email: string;

Expand Down
7 changes: 4 additions & 3 deletions src/report/dto/public-report.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { StatusRecordsDto } from './status-records.dto';
import { ReportCategory } from '../../common/dto/report-category';

export class PublicReportDto {
@ApiProperty()
name: string;

@ApiProperty()
type: string;
category: ReportCategory;

@ApiProperty()
refId: string;
Expand Down Expand Up @@ -37,7 +38,7 @@ export class PublicReportDto {

constructor(
name: string,
type: string,
category: ReportCategory,
refId: string,
longitude: number,
latitude: number,
Expand All @@ -49,7 +50,7 @@ export class PublicReportDto {
statusRecords: StatusRecordsDto[],
) {
this.name = name;
this.type = type;
this.category = category;
this.refId = refId;
this.longitude = longitude;
this.latitude = latitude;
Expand Down
4 changes: 0 additions & 4 deletions src/report/dto/report-statistics.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ export class ReportStatisticsDto {
@ApiProperty({ type: 'integer' })
totalInvestigatedReports: number;
@ApiProperty({ type: 'integer' })
totalCleanedReports: number;
@ApiProperty({ type: 'integer' })
totalFalseReports: number;

constructor(
totalSentReports: number,
totalInInvestigationReports: number,
totalInvestigatedReports: number,
totalCleanedReports: number,
totalFalseReports: number,
) {
this.totalSentReports = totalSentReports;
this.totalInInvestigationReports = totalInInvestigationReports;
this.totalInvestigatedReports = totalInvestigatedReports;
this.totalCleanedReports = totalCleanedReports;
this.totalFalseReports = totalFalseReports;
}
}
20 changes: 16 additions & 4 deletions src/report/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
Get,
NotFoundException,
Param,
ParseEnumPipe,
ParseIntPipe,
Post,
Query,
UploadedFiles,
UseInterceptors,
} from '@nestjs/common';
Expand All @@ -15,12 +17,14 @@ import {
ApiCreatedResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { PublicReportDto } from './dto';
import { CreateReportDto } from './dto';
import { FilesInterceptor } from '@nestjs/platform-express';
import { ReportStatisticsDto } from './dto/report-statistics.dto';
import { ReportCategory } from '../common/dto/report-category';

@Controller('reports')
@ApiTags('reports')
Expand All @@ -45,18 +49,26 @@ export class ReportController {
description: 'All visible reports have been successfully found',
type: [PublicReportDto],
})
@ApiQuery({ name: 'category', enum: ReportCategory, required: false })
@Get()
getAllPublicReports(): Promise<PublicReportDto[]> {
return this.reportService.getAllVisibleReports();
getAllPublicReports(
@Query('category', new ParseEnumPipe(ReportCategory, { optional: true }))
category?: ReportCategory,
): Promise<PublicReportDto[]> {
return this.reportService.getAllVisibleReports(category);
}

@ApiOkResponse({
description: 'Report statistics have been successfully fetched',
type: ReportStatisticsDto,
})
@ApiQuery({ name: 'category', enum: ReportCategory, required: false })
@Get('/statistics')
getReportStatistics(): Promise<ReportStatisticsDto> {
return this.reportService.getReportStatistics();
getReportStatistics(
@Query('category', new ParseEnumPipe(ReportCategory, { optional: true }))
category?: ReportCategory,
): Promise<ReportStatisticsDto> {
return this.reportService.getReportStatistics(category);
}

@Get('/:refId')
Expand Down
Loading
Loading