Skip to content

Commit

Permalink
Merge pull request #13 from AplinkosMinisterija/transfer-data-integra…
Browse files Browse the repository at this point in the history
…tion

Transfer data integration
  • Loading branch information
Ignas-rgb authored Jun 10, 2024
2 parents 0b64b58 + 3dc115d commit f7953c7
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ CLOUDINARY_KEY=CLOUDINARY_KEY
CLOUDINARY_SECRET=CLOUDINARY_SECRET
JWT_SECRET=JWT_SECRET
SENTRY_DSN=SENTRY_DSN
AADIS_URL=AADIS_URL
17 changes: 17 additions & 0 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Body,
Controller,
Get,
InternalServerErrorException,
NotFoundException,
Param,
ParseBoolPipe,
Expand All @@ -28,6 +29,7 @@ import {
CreateDumpDto,
FullDumpDto,
FullReportDto,
TransferReportDto,
UpdateDumpDto,
UpdateReportDto,
} from './dto';
Expand Down Expand Up @@ -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<FullReportDto> {
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],
Expand Down
70 changes: 69 additions & 1 deletion src/admin/admin.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -76,6 +77,30 @@ export class AdminService {
return AdminService.docToFullDump(dump);
}

async transferReport(
transferReportDto: TransferReportDto,
): Promise<FullReportDto | null> {
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(),
Expand All @@ -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,
Expand Down Expand Up @@ -136,4 +164,44 @@ export class AdminService {
throw new Error(`Invalid report category: ${value}`);
}
}

private async sendTransferRequest(
transferReportDto: TransferReportDto,
): Promise<AxiosResponse | null> {
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;
}
}
16 changes: 16 additions & 0 deletions src/admin/dto/full-report.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class FullReportDto {
@ApiProperty()
email: string;

@ApiProperty({ nullable: true })
inspection?: string;

@ApiProperty({ nullable: true })
inspectionId?: string;

@ApiProperty()
@ToBoolean()
isVisible: boolean;
Expand All @@ -34,6 +40,10 @@ export class FullReportDto {
@ToBoolean()
isDeleted: boolean;

@ApiProperty({ nullable: true })
@ToBoolean()
isTransferred?: boolean;

@ApiProperty()
comment: string;

Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/admin/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
42 changes: 42 additions & 0 deletions src/admin/dto/transfer-report.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
57 changes: 57 additions & 0 deletions src/repositories/reports/report.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,63 @@ export class ReportRepository {
return updatedReport;
}

async updateTransferReport(
refId: string,
inspection: string,
inspectionId: string,
editorEmail: string,
): Promise<Report | null> {
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<string> {
const upload = await this.cloudinary.uploadImage(file).catch(() => {
throw new BadRequestException('Invalid file type.');
Expand Down
9 changes: 9 additions & 0 deletions src/repositories/reports/schemas/report.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down

0 comments on commit f7953c7

Please sign in to comment.