diff --git a/.env.example b/.env.example index b846791..b3718ab 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,4 @@ CLOUDINARY_KEY=CLOUDINARY_KEY CLOUDINARY_SECRET=CLOUDINARY_SECRET JWT_SECRET=JWT_SECRET SENTRY_DSN=SENTRY_DSN +AADIS_URL=AADIS_URL \ No newline at end of file diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index a777273..63d8f57 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -2,6 +2,7 @@ import { Body, Controller, Get, + InternalServerErrorException, NotFoundException, Param, ParseBoolPipe, @@ -28,6 +29,7 @@ import { CreateDumpDto, FullDumpDto, FullReportDto, + TransferReportDto, UpdateDumpDto, UpdateReportDto, } from './dto'; @@ -113,6 +115,21 @@ export class AdminController { return report; } + @ApiOkResponse({ + description: 'Report has been successfully transferred', + type: TransferReportDto, + }) + @Post('/reports/transfer') + async transferReport( + @Body() transferReportDto: TransferReportDto, + ): Promise { + const transferReport = + await this.adminService.transferReport(transferReportDto); + if (!transferReport) + throw new InternalServerErrorException('Report transfer unsuccessful'); + return transferReport; + } + @ApiOkResponse({ description: 'All dumps have been successfully found', type: [FullDumpDto], diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 5a4c262..5c69c6f 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { FullReportDto } from './dto'; +import { FullReportDto, TransferReportDto } from './dto'; import { DumpRepository } from '../repositories/dumps/dump.repository'; import { ReportRepository } from '../repositories/reports/report.repository'; import { CreateDumpDto, FullDumpDto, UpdateDumpDto } from './dto'; @@ -15,6 +15,7 @@ import { StatusRecordsDto } from '../report/dto'; import { UpdateReportDto } from './dto'; import { Dump } from '../repositories/dumps/schemas'; import { ReportCategory } from '../common/dto/report-category'; +import axios, { AxiosResponse } from 'axios'; @Injectable() export class AdminService { @@ -76,6 +77,30 @@ export class AdminService { return AdminService.docToFullDump(dump); } + async transferReport( + transferReportDto: TransferReportDto, + ): Promise { + const response: AxiosResponse | null = + await this.sendTransferRequest(transferReportDto); + if (response == null) { + return null; + } + + const inspection = response.data[Object.keys(response.data)[0]]; + const inspectionId = response.data[Object.keys(response.data)[1]]; + + const report: Report | null = + await this.reportRepository.updateTransferReport( + transferReportDto.refId, + inspection, + inspectionId, + transferReportDto.email, + ); + if (!report) return null; + + return AdminService.docToFullReport(report); + } + private static docToFullDump(dump: Dump): FullDumpDto { return new FullDumpDto( dump._id.toString(), @@ -99,8 +124,11 @@ export class AdminService { report.reportLong, report.reportLat, report.email, + report.inspection, + report.inspectionId, report.isVisible, report.isDeleted, + report.isTransferred, report.comment, report.status, report.reportDate, @@ -136,4 +164,44 @@ export class AdminService { throw new Error(`Invalid report category: ${value}`); } } + + private async sendTransferRequest( + transferReportDto: TransferReportDto, + ): Promise { + let returnValue = null; + const data = JSON.stringify({ + 'TL pranešimo ID': transferReportDto.refId, + Turinys: transferReportDto.name, + Platuma: transferReportDto.latitude.toString(), + Ilguma: transferReportDto.longitude.toString(), + Statusas: transferReportDto.status, + 'Data ir laikas': transferReportDto.reportDate.toString(), + 'Vykdytojo e-mail': transferReportDto.email, + }); + + const config = { + method: 'post', + maxBodyLength: Infinity, + url: process.env['AADIS_URL'], + headers: { + 'Content-Type': 'application/json', + }, + data: data, + }; + + await axios + .request(config) + .then((response) => { + if (response.status == 200) { + returnValue = response; + } else { + returnValue = null; + } + }) + .catch((error) => { + console.log(error); + returnValue = null; + }); + return returnValue; + } } diff --git a/src/admin/dto/full-report.dto.ts b/src/admin/dto/full-report.dto.ts index 320240a..da5f014 100644 --- a/src/admin/dto/full-report.dto.ts +++ b/src/admin/dto/full-report.dto.ts @@ -26,6 +26,12 @@ export class FullReportDto { @ApiProperty() email: string; + @ApiProperty({ nullable: true }) + inspection?: string; + + @ApiProperty({ nullable: true }) + inspectionId?: string; + @ApiProperty() @ToBoolean() isVisible: boolean; @@ -34,6 +40,10 @@ export class FullReportDto { @ToBoolean() isDeleted: boolean; + @ApiProperty({ nullable: true }) + @ToBoolean() + isTransferred?: boolean; + @ApiProperty() comment: string; @@ -63,8 +73,11 @@ export class FullReportDto { longitude: number, latitude: number, email: string, + inspection: string, + inspectionId: string, isVisible: boolean, isDeleted: boolean, + isTransferred: boolean, comment: string, status: string, reportDate: Date, @@ -80,8 +93,11 @@ export class FullReportDto { this.longitude = longitude; this.latitude = latitude; this.email = email; + this.inspection = inspection; + this.inspectionId = inspectionId; this.isVisible = isVisible; this.isDeleted = isDeleted; + this.isTransferred = isTransferred; this.comment = comment; this.status = status; this.reportDate = reportDate; diff --git a/src/admin/dto/index.ts b/src/admin/dto/index.ts index fa35c74..c9e1cdf 100644 --- a/src/admin/dto/index.ts +++ b/src/admin/dto/index.ts @@ -3,3 +3,4 @@ export * from './full-dump.dto'; export * from './update-dump.dto'; export * from './full-report.dto'; export * from './update-report.dto'; +export * from './transfer-report.dto'; diff --git a/src/admin/dto/transfer-report.dto.ts b/src/admin/dto/transfer-report.dto.ts new file mode 100644 index 0000000..234e298 --- /dev/null +++ b/src/admin/dto/transfer-report.dto.ts @@ -0,0 +1,42 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class TransferReportDto { + @ApiProperty() + refId: string; + + @ApiProperty() + name: string; + + @ApiProperty({ format: 'double' }) + longitude: number; + + @ApiProperty({ format: 'double' }) + latitude: number; + + @ApiProperty() + status: string; + + @ApiProperty() + reportDate: Date; + + @ApiProperty() + email: string; + + constructor( + name: string, + refId: string, + longitude: number, + latitude: number, + status: string, + reportDate: Date, + email: string, + ) { + this.name = name; + this.refId = refId; + this.longitude = longitude; + this.latitude = latitude; + this.status = status; + this.reportDate = reportDate; + this.email = email; + } +} diff --git a/src/repositories/reports/report.repository.ts b/src/repositories/reports/report.repository.ts index 954de09..f504dc3 100644 --- a/src/repositories/reports/report.repository.ts +++ b/src/repositories/reports/report.repository.ts @@ -246,6 +246,63 @@ export class ReportRepository { return updatedReport; } + async updateTransferReport( + refId: string, + inspection: string, + inspectionId: string, + editorEmail: string, + ): Promise { + const report = await this.reportModel + .findOne({ refId: { $eq: refId } }) + .exec(); + + if (report != null) { + const historyEntry: HistoryDataDto = { + user: editorEmail, + date: new Date(), + edits: [], + }; + + historyEntry.edits.push(new HistoryEditsDto('inspection', inspection)); + historyEntry.edits.push( + new HistoryEditsDto('inspectionId', inspectionId), + ); + historyEntry.edits.push(new HistoryEditsDto('isTransferred', 'true')); + if (historyEntry.edits.length != 0) { + await this.reportModel.findOneAndUpdate( + { + refId: { $eq: refId }, + }, + { + $push: { + historyData: historyEntry, + }, + }, + ); + } + } + + let updatedReport = null; + if (report != null) { + updatedReport = await this.reportModel + .findOneAndUpdate( + { + refId: { $eq: refId }, + }, + { + $set: { + inspection: inspection, + inspectionId: inspectionId, + isTransferred: true, + }, + }, + ) + .exec(); + } + + return updatedReport; + } + async uploadImageToCloudinary(file: Express.Multer.File): Promise { const upload = await this.cloudinary.uploadImage(file).catch(() => { throw new BadRequestException('Invalid file type.'); diff --git a/src/repositories/reports/schemas/report.schema.ts b/src/repositories/reports/schemas/report.schema.ts index e5218a2..17aad9a 100644 --- a/src/repositories/reports/schemas/report.schema.ts +++ b/src/repositories/reports/schemas/report.schema.ts @@ -26,6 +26,12 @@ export class Report { @Prop({ type: String, required: true }) email: string; + @Prop({ type: String, required: false }) + inspection: string; + + @Prop({ type: String, required: false }) + inspectionId: string; + @Prop({ type: String, required: true }) comment: string; @@ -38,6 +44,9 @@ export class Report { @Prop({ type: Boolean, required: true, default: false }) isDeleted: boolean; + @Prop({ type: Boolean, required: false, default: false }) + isTransferred: boolean; + @Prop({ type: Date, required: true, default: Date.now() }) reportDate: Date;