53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { HttpService } from '@nestjs/axios';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { AxiosResponse } from 'axios';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import { KeyHandoverPDFDataDto } from 'src/model/dto/key-handover.dto';
|
|
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
|
|
|
|
@Injectable()
|
|
export class PdfService {
|
|
|
|
private readonly STORAGESERVER = this.configService.get('STORAGE_HOST')
|
|
|
|
constructor(
|
|
private readonly configService: ConfigService,
|
|
private readonly httpService: HttpService,
|
|
private readonly handoverRepo: KeyHandoutRepository
|
|
) { }
|
|
|
|
public async generatePDF(dto: KeyHandoverPDFDataDto): Promise<{ pdf: Buffer, response: AxiosResponse}> {
|
|
const h = await this.handoverRepo.findOneByOrFail({ id: dto.handoverId });
|
|
const response = await this.post(`${this.STORAGESERVER}/pdf/keyhandover`, dto);
|
|
console.log(response)
|
|
if (response?.data == null) { return; }
|
|
h.pdfFormKey = response.data;
|
|
await this.handoverRepo.save(h);
|
|
return this.getPDFByHandoverKey(response.data)
|
|
|
|
}
|
|
|
|
public async getPDFByHandoverKey(key: string): Promise<{ pdf: Buffer, response: AxiosResponse}> {
|
|
return new Promise(resolve => {
|
|
this.httpService.get(`${this.STORAGESERVER}/pdf/keyhandover/${key}`, {
|
|
responseType: 'arraybuffer',
|
|
}).subscribe({
|
|
next: pdf => {
|
|
const pdfBuffer = Buffer.from(pdf.data);
|
|
resolve({ pdf: pdfBuffer, response: pdf})
|
|
},
|
|
error: e => console.log(e)
|
|
})
|
|
})
|
|
}
|
|
|
|
private async post(url: string, data: any): Promise<AxiosResponse> {
|
|
return new Promise(resolve => {
|
|
this.httpService.post(url, data).subscribe({
|
|
next: data => resolve(data)
|
|
})
|
|
})
|
|
}
|
|
}
|