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

Bark beetle report #10

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ CLOUDINARY_KEY=CLOUDINARY_KEY
CLOUDINARY_SECRET=CLOUDINARY_SECRET
JWT_SECRET=JWT_SECRET
SENTRY_DSN=SENTRY_DSN
POSTMARK_API_TOKEN=POSTMARK_API_TOKEN
POSTMARK_TEMPLATE_ID=POSTMARK_TEMPLATE_ID
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"postmark": "^4.0.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand Down
52 changes: 52 additions & 0 deletions src/report/postmark.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable } from '@nestjs/common';
import * as postmark from 'postmark';
import { CreateReportDto } from './dto';
import { TemplatedMessage } from 'postmark';
import * as process from 'process';
import { MessageSendingResponse } from 'postmark/dist/client/models';

@Injectable()
export class PostmarkService {
private client: postmark.ServerClient;

constructor() {
this.client = new postmark.ServerClient(process.env['POSTMARK_API_TOKEN']!);
}

async sendBarkBeetleReport(
createReportDto: CreateReportDto,
images: Array<Express.Multer.File>,
to: string,
): Promise<MessageSendingResponse> {
function encodeToBase64(file: Express.Multer.File) {
return Buffer.from(file.buffer).toString('base64');
}

const attachments = [];

for (let i = 0; i < images.length; i++) {
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
const encodedImageContent = encodeToBase64(images[i]);
attachments.push({
Name: 'nuotrauka' + i + '.jpg',
Content: encodedImageContent,
ContentType: 'image/jpeg',
Copy link
Member

Choose a reason for hiding this comment

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

The image won't always be 'image/jpeg'. I would recommended to detect the file type and extension dynamically using a magic number approach. Utilize libraries like file-type (https://www.npmjs.com/package/file-type) or equivalent for detection.

ContentID: 'photo' + i,
});
}

const templatedMessage: TemplatedMessage = {
TemplateId: Number(process.env['POSTMARK_TEMPLATE_ID']),
From: 'neatsakyti@tvarkaulietuva.lt',
To: to,
TemplateModel: {
description: createReportDto.name,
longitude: createReportDto.longitude,
latitude: createReportDto.latitude,
email: createReportDto.email,
},
Attachments: attachments,
};

return this.client.sendEmailWithTemplate(templatedMessage);
}
}
25 changes: 24 additions & 1 deletion src/report/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ import { CreateReportDto } from './dto';
import { FilesInterceptor } from '@nestjs/platform-express';
import { ReportStatisticsDto } from './dto/report-statistics.dto';
import { ReportCategory } from '../common/dto/report-category';
import { PostmarkService } from './postmark.service';
import { Message, MessageSendingResponse } from 'postmark/dist/client/models';

@Controller('reports')
@ApiTags('reports')
export class ReportController {
constructor(private readonly reportService: ReportService) {}
constructor(
private readonly reportService: ReportService,
private readonly postmarkService: PostmarkService,
) {}

@ApiCreatedResponse({
description: 'New Report has been successfully created',
Expand All @@ -45,6 +50,24 @@ export class ReportController {
return this.reportService.createReport(createReportDto, images);
}

@ApiCreatedResponse({
description: 'New Report has been successfully sent',
type: Message,
})
@ApiConsumes('multipart/form-data')
@UseInterceptors(FilesInterceptor('images', 4))
@Post('/bark-beetle')
sendBarkBeetleReport(
@Body() createReportDto: CreateReportDto,
@UploadedFiles() images: Array<Express.Multer.File>,
): Promise<MessageSendingResponse> {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it's really good to return such response to public. It might contain some confidential information.

return this.postmarkService.sendBarkBeetleReport(
createReportDto,
images,
'benas.svedas@aad.am.lt',
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend to move email to environment variables.

);
}

@ApiOkResponse({
description: 'All visible reports have been successfully found',
type: [PublicReportDto],
Expand Down
3 changes: 2 additions & 1 deletion src/report/report.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { MongooseModule } from '@nestjs/mongoose';
import { ReportRepository } from '../repositories/reports/report.repository';
import { Report, ReportSchema } from '../repositories/reports/schemas';
import { CloudinaryModule } from '../cloudinary/cloudinary.module';
import { PostmarkService } from './postmark.service';

@Module({
imports: [
MongooseModule.forFeature([{ name: Report.name, schema: ReportSchema }]),
CloudinaryModule,
],
controllers: [ReportController],
providers: [ReportService, ReportRepository],
providers: [ReportService, ReportRepository, PostmarkService],
})
export class ReportModule {}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,13 @@ pluralize@8.0.0:
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==

postmark@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/postmark/-/postmark-4.0.2.tgz#0c0410ac427d85f2a43454360cf8d07e0fbda317"
integrity sha512-2zlCv+KVVQ0KoamXZHE7d+gXzLlr8tPE+PxQmtUaIZhbHzZAq4D6yH2b+ykhA8wYCc5ISodcx8U1aNLenXBs9g==
dependencies:
axios "^1.6.2"

prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
Expand Down
Loading