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

Transfer data integration #13

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to pass the entire TransferReportDto, or would just the ReportID suffice for this method?

): 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
69 changes: 68 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,29 @@ 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 controle = response.data[Object.keys(response.data)[0]];
const controleId = response.data[Object.keys(response.data)[1]];

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 {
return new FullDumpDto(
dump._id.toString(),
Expand All @@ -99,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,
Expand Down Expand Up @@ -136,4 +163,44 @@ export class AdminService {
throw new Error(`Invalid report category: ${value}`);
}
}

async sendTransferRequest(
Ignas-rgb marked this conversation as resolved.
Show resolved Hide resolved
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging errors will be challenging since Sentry won't capture these errors, and you'll only find them only in Docker logs. To mitigate this, consider configuring axios validateStatus (as explained here: link) and consistently return AxiosResponse instead of AxiosResponse|null from this function. This way, if an error occurs, Sentry will capture it along with supplementary information such as the transferReportDto values and you won't need to deal with null case.

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()
controle: string;

@ApiProperty()
controleId: string;
Ignas-rgb marked this conversation as resolved.
Show resolved Hide resolved

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

@ApiProperty()
@ToBoolean()
isTransferred: boolean;

@ApiProperty()
comment: string;

Expand Down Expand Up @@ -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,
Expand All @@ -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;
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;
}
}
55 changes: 55 additions & 0 deletions src/repositories/reports/report.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,61 @@
return updatedReport;
}

async updateTransferReport(
refId: string,
controle: string,
controleId: 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('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,
},
Fixed Show fixed Hide fixed
{
$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<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 })
controle: string;

@Prop({ type: String, required: false })
controleId: 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
Loading