From b784ef9123504f51c9f1de315cf7dc6692f4f227 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:10:39 +0300 Subject: [PATCH 1/6] Create DTO and transfer logic --- src/admin/admin.controller.ts | 12 ++++++ src/admin/admin.service.ts | 62 +++++++++++++++++++++++++++- src/admin/dto/index.ts | 1 + src/admin/dto/transfer-report.dto.ts | 42 +++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/admin/dto/transfer-report.dto.ts diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index a777273..e5c3dea 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -28,6 +28,7 @@ import { CreateDumpDto, FullDumpDto, FullReportDto, + TransferReportDto, UpdateDumpDto, UpdateReportDto, } from './dto'; @@ -113,6 +114,17 @@ export class AdminController { return report; } + @ApiOkResponse({ + description: 'Report has been successfully transferred', + type: TransferReportDto, + }) + @Post('/reports/transfer') + async transferReport( + @Body() transferReportDto: TransferReportDto, + ): Promise { + return await this.adminService.transferReport(transferReportDto); + } + @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..c14ba46 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,25 @@ 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 name = response.data.displayName; + // const email = response.data.mail; + // const payload = { email: email }; + // if (name == null || email == null) { + // return null; + // } + + return transferReportDto; + } + private static docToFullDump(dump: Dump): FullDumpDto { return new FullDumpDto( dump._id.toString(), @@ -136,4 +156,44 @@ export class AdminService { throw new Error(`Invalid report category: ${value}`); } } + + async sendTransferRequest( + transferReportDto: TransferReportDto, + ): Promise { + let returnValue: AxiosResponse | null = null; + const body = { + 'TL pranešimo ID': transferReportDto.refId, + Turinys: transferReportDto.name, + Platuma: transferReportDto.latitude, + Ilguma: transferReportDto.longitude, + Statusas: transferReportDto.status, + 'Data ir laikas': transferReportDto.reportDate, + 'Vykdytojo e-mail': transferReportDto.email, + }; + await axios + .post( + 'https://prod-90.westeurope.logic.azure.com:443/workflows/d31513e4f45c476b8062580dce0713b0/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=TPbFkxkCTVAAWPBEPefOkg2eJlbvzA1rW6e933CsId8', + { + "TL pranešimo ID": "13223", + "Turinys": "Testinis pranešimas", + "Platuma": "54.6872", + "Ilguma": "25.2797", + "Statusas":"gautas", + "Data ir laikas":"2024-04-23T10:35:06.246Z", + "Vykdytojo e-mail":"justas.tacionis@aad.am.lt" + }, + ) + .then((response) => { + console.log('ans'); + console.log(response.status); + console.log(response.data); + if (response.status == 200) { + console.log(response.data); + returnValue = response; + } else { + returnValue = null; + } + }); + return returnValue; + } } 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; + } +} From 11ec77428aa207c751056f1695357745c732c2f9 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Wed, 24 Apr 2024 16:16:43 +0300 Subject: [PATCH 2/6] Create report transfer logic with further updating to said report --- .env.example | 1 + src/admin/admin.controller.ts | 9 ++- src/admin/admin.service.ts | 67 ++++++++++--------- src/admin/dto/full-report.dto.ts | 16 +++++ src/repositories/reports/report.repository.ts | 55 +++++++++++++++ .../reports/schemas/report.schema.ts | 9 +++ 6 files changed, 125 insertions(+), 32 deletions(-) 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 e5c3dea..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, @@ -121,8 +122,12 @@ export class AdminController { @Post('/reports/transfer') async transferReport( @Body() transferReportDto: TransferReportDto, - ): Promise { - return await this.adminService.transferReport(transferReportDto); + ): Promise { + const transferReport = + await this.adminService.transferReport(transferReportDto); + if (!transferReport) + throw new InternalServerErrorException('Report transfer unsuccessful'); + return transferReport; } @ApiOkResponse({ diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index c14ba46..41b74ce 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -79,21 +79,25 @@ export class AdminService { async transferReport( transferReportDto: TransferReportDto, - ): Promise { + ): Promise { const response: AxiosResponse | null = await this.sendTransferRequest(transferReportDto); if (response == null) { - //return null; + return null; } - // const name = response.data.displayName; - // const email = response.data.mail; - // const payload = { email: email }; - // if (name == null || email == null) { - // return null; - // } + const controle = response.data[Object.keys(response.data)[0]]; + const controleId = response.data[Object.keys(response.data)[1]]; - return transferReportDto; + const report: Report | null = + await this.reportRepository.updateTransferReport( + transferReportDto.refId, + controle, + controleId, + transferReportDto.email, + ); + if (!report) return null; + return AdminService.docToFullReport(report); } private static docToFullDump(dump: Dump): FullDumpDto { @@ -119,8 +123,11 @@ export class AdminService { report.reportLong, report.reportLat, report.email, + report.controle, + report.controleId, report.isVisible, report.isDeleted, + report.isTransferred, report.comment, report.status, report.reportDate, @@ -160,39 +167,39 @@ export class AdminService { async sendTransferRequest( transferReportDto: TransferReportDto, ): Promise { - let returnValue: AxiosResponse | null = null; - const body = { + let returnValue = null; + const data = JSON.stringify({ 'TL pranešimo ID': transferReportDto.refId, Turinys: transferReportDto.name, - Platuma: transferReportDto.latitude, - Ilguma: transferReportDto.longitude, + Platuma: transferReportDto.latitude.toString(), + Ilguma: transferReportDto.longitude.toString(), Statusas: transferReportDto.status, - 'Data ir laikas': transferReportDto.reportDate, + '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 - .post( - 'https://prod-90.westeurope.logic.azure.com:443/workflows/d31513e4f45c476b8062580dce0713b0/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=TPbFkxkCTVAAWPBEPefOkg2eJlbvzA1rW6e933CsId8', - { - "TL pranešimo ID": "13223", - "Turinys": "Testinis pranešimas", - "Platuma": "54.6872", - "Ilguma": "25.2797", - "Statusas":"gautas", - "Data ir laikas":"2024-04-23T10:35:06.246Z", - "Vykdytojo e-mail":"justas.tacionis@aad.am.lt" - }, - ) + .request(config) .then((response) => { - console.log('ans'); - console.log(response.status); - console.log(response.data); if (response.status == 200) { - console.log(response.data); 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..c303395 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() + controle: string; + + @ApiProperty() + controleId: string; + @ApiProperty() @ToBoolean() isVisible: boolean; @@ -34,6 +40,10 @@ export class FullReportDto { @ToBoolean() isDeleted: boolean; + @ApiProperty() + @ToBoolean() + isTransferred: boolean; + @ApiProperty() comment: string; @@ -63,8 +73,11 @@ export class FullReportDto { longitude: number, latitude: number, email: string, + controle: string, + controleId: 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.controle = controle; + this.controleId = controleId; this.isVisible = isVisible; this.isDeleted = isDeleted; + this.isTransferred = isTransferred; this.comment = comment; this.status = status; this.reportDate = reportDate; diff --git a/src/repositories/reports/report.repository.ts b/src/repositories/reports/report.repository.ts index 954de09..c36cb4c 100644 --- a/src/repositories/reports/report.repository.ts +++ b/src/repositories/reports/report.repository.ts @@ -246,6 +246,61 @@ export class ReportRepository { return updatedReport; } + async updateTransferReport( + refId: string, + controle: string, + controleId: 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('controle', controle)); + historyEntry.edits.push(new HistoryEditsDto('controleId', controleId)); + historyEntry.edits.push(new HistoryEditsDto('isTransferred', 'true')); + if (historyEntry.edits.length != 0) { + await this.reportModel.findOneAndUpdate( + { + refId: refId, + }, + { + $push: { + historyData: historyEntry, + }, + }, + ); + } + } + + let updatedReport = null; + if (report != null) { + updatedReport = await this.reportModel + .findOneAndUpdate( + { + refId: { $eq: refId }, + }, + { + $set: { + controle: controle, + controleId: controleId, + 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..e222b45 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 }) + controle: string; + + @Prop({ type: String, required: false }) + controleId: 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; From 5c9b121f594f7484400c4d722fa1c67f36c61743 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:02:18 +0300 Subject: [PATCH 3/6] Update database query --- src/repositories/reports/report.repository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repositories/reports/report.repository.ts b/src/repositories/reports/report.repository.ts index c36cb4c..239cdcd 100644 --- a/src/repositories/reports/report.repository.ts +++ b/src/repositories/reports/report.repository.ts @@ -269,7 +269,7 @@ export class ReportRepository { if (historyEntry.edits.length != 0) { await this.reportModel.findOneAndUpdate( { - refId: refId, + refId: { $eq: refId }, }, { $push: { From 986b9346995a85883d980f2675a01ef7dded0c3d Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Tue, 7 May 2024 12:04:22 +0300 Subject: [PATCH 4/6] update variable naming --- src/admin/admin.service.ts | 13 +++++++------ src/repositories/reports/report.repository.ts | 14 ++++++++------ src/repositories/reports/schemas/report.schema.ts | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 41b74ce..8edb422 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -86,17 +86,18 @@ export class AdminService { return null; } - const controle = response.data[Object.keys(response.data)[0]]; - const controleId = response.data[Object.keys(response.data)[1]]; + 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, - controle, - controleId, + inspection, + inspectionId, transferReportDto.email, ); if (!report) return null; + return AdminService.docToFullReport(report); } @@ -123,8 +124,8 @@ export class AdminService { report.reportLong, report.reportLat, report.email, - report.controle, - report.controleId, + report.inspection, + report.inspectionId, report.isVisible, report.isDeleted, report.isTransferred, diff --git a/src/repositories/reports/report.repository.ts b/src/repositories/reports/report.repository.ts index 239cdcd..f504dc3 100644 --- a/src/repositories/reports/report.repository.ts +++ b/src/repositories/reports/report.repository.ts @@ -248,8 +248,8 @@ export class ReportRepository { async updateTransferReport( refId: string, - controle: string, - controleId: string, + inspection: string, + inspectionId: string, editorEmail: string, ): Promise { const report = await this.reportModel @@ -263,8 +263,10 @@ export class ReportRepository { edits: [], }; - historyEntry.edits.push(new HistoryEditsDto('controle', controle)); - historyEntry.edits.push(new HistoryEditsDto('controleId', controleId)); + 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( @@ -289,8 +291,8 @@ export class ReportRepository { }, { $set: { - controle: controle, - controleId: controleId, + inspection: inspection, + inspectionId: inspectionId, isTransferred: true, }, }, diff --git a/src/repositories/reports/schemas/report.schema.ts b/src/repositories/reports/schemas/report.schema.ts index e222b45..17aad9a 100644 --- a/src/repositories/reports/schemas/report.schema.ts +++ b/src/repositories/reports/schemas/report.schema.ts @@ -27,10 +27,10 @@ export class Report { email: string; @Prop({ type: String, required: false }) - controle: string; + inspection: string; @Prop({ type: String, required: false }) - controleId: string; + inspectionId: string; @Prop({ type: String, required: true }) comment: string; From f88d3bb3e3def8f3e621693d508de4a6517dfdb6 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:00:22 +0300 Subject: [PATCH 5/6] update naming and make method private --- src/admin/admin.service.ts | 2 +- src/admin/dto/full-report.dto.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 8edb422..5c69c6f 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -165,7 +165,7 @@ export class AdminService { } } - async sendTransferRequest( + private async sendTransferRequest( transferReportDto: TransferReportDto, ): Promise { let returnValue = null; diff --git a/src/admin/dto/full-report.dto.ts b/src/admin/dto/full-report.dto.ts index c303395..bb50705 100644 --- a/src/admin/dto/full-report.dto.ts +++ b/src/admin/dto/full-report.dto.ts @@ -27,10 +27,10 @@ export class FullReportDto { email: string; @ApiProperty() - controle: string; + inspection: string; @ApiProperty() - controleId: string; + inspectionId: string; @ApiProperty() @ToBoolean() @@ -73,8 +73,8 @@ export class FullReportDto { longitude: number, latitude: number, email: string, - controle: string, - controleId: string, + inspection: string, + inspectionId: string, isVisible: boolean, isDeleted: boolean, isTransferred: boolean, @@ -93,8 +93,8 @@ export class FullReportDto { this.longitude = longitude; this.latitude = latitude; this.email = email; - this.controle = controle; - this.controleId = controleId; + this.inspection = inspection; + this.inspectionId = inspectionId; this.isVisible = isVisible; this.isDeleted = isDeleted; this.isTransferred = isTransferred; From 540f06006766ebf3a7842a9138e5e3ca54dce774 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Mon, 10 Jun 2024 13:55:04 +0300 Subject: [PATCH 6/6] Update field properties --- src/admin/dto/full-report.dto.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/admin/dto/full-report.dto.ts b/src/admin/dto/full-report.dto.ts index bb50705..da5f014 100644 --- a/src/admin/dto/full-report.dto.ts +++ b/src/admin/dto/full-report.dto.ts @@ -26,11 +26,11 @@ export class FullReportDto { @ApiProperty() email: string; - @ApiProperty() - inspection: string; + @ApiProperty({ nullable: true }) + inspection?: string; - @ApiProperty() - inspectionId: string; + @ApiProperty({ nullable: true }) + inspectionId?: string; @ApiProperty() @ToBoolean() @@ -40,9 +40,9 @@ export class FullReportDto { @ToBoolean() isDeleted: boolean; - @ApiProperty() + @ApiProperty({ nullable: true }) @ToBoolean() - isTransferred: boolean; + isTransferred?: boolean; @ApiProperty() comment: string;