Skip to content

Commit

Permalink
Merge pull request #149 from bcgov/loading-prod-data
Browse files Browse the repository at this point in the history
changes to code based on loading prod data
  • Loading branch information
mgtennant authored Apr 11, 2024
2 parents ae00025 + 335eb9f commit 7ab8dd5
Show file tree
Hide file tree
Showing 20 changed files with 6,982 additions and 3,589 deletions.
10,063 changes: 6,716 additions & 3,347 deletions backend/package-lock.json

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions backend/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,19 @@ export class AdminController {
});
}



@Get('preview-template/:id')
async previewTemplate(@Param('id') id: number, @Res() res) {
try {
const dtObject = await this.adminService.downloadTemplate(id);
const base64Data = dtObject.the_file;
const pdfBuffer = await this.adminService.convertDocxToPdfBuffer(base64Data);
const streamableFile = new stream.PassThrough();
streamableFile.end(pdfBuffer);
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=file.pdf',
});
streamableFile.pipe(res);
streamableFile.end(pdfBuffer);
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=file.pdf',
});
streamableFile.pipe(res);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
Expand Down
63 changes: 31 additions & 32 deletions backend/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as mammoth from 'mammoth';
import * as PDFDocument from 'pdfkit';
import * as pdf from 'html-pdf';


const axios = require('axios');
const FormData = require('form-data');

Expand Down Expand Up @@ -364,35 +363,35 @@ export class AdminService {
return header + csvRows.join('\n');
}

async convertDocxToPdfBuffer(base64Data: string): Promise<Buffer> {
// Decode base64 string
const buffer = Buffer.from(base64Data, 'base64');

// Write buffer to temporary file
const tempFilePath = './temp.docx';
fs.writeFileSync(tempFilePath, buffer);

// Convert DOCX to HTML
const { value } = await mammoth.convertToHtml({ path: tempFilePath });
const htmlContent = value;

// Set options for html-pdf
const options: pdf.CreateOptions = {
format: 'Letter', // Set the PDF format (e.g., 'A4', 'Letter', etc.)
base: `file://${__dirname}/`, // Set the base path for local file references
};

return new Promise<Buffer>((resolve, reject) => {
// Convert HTML to PDF
pdf.create(htmlContent, options).toBuffer((err, buffer) => {
if (err) {
reject(err);
} else {
// Remove temporary file
fs.unlinkSync(tempFilePath);
resolve(buffer);
}
});
});
}
async convertDocxToPdfBuffer(base64Data: string): Promise<Buffer> {
// Decode base64 string
const buffer = Buffer.from(base64Data, 'base64');

// Write buffer to temporary file
const tempFilePath = './temp.docx';
fs.writeFileSync(tempFilePath, buffer);

// Convert DOCX to HTML
const { value } = await mammoth.convertToHtml({ path: tempFilePath });
const htmlContent = value;

// Set options for html-pdf
const options: pdf.CreateOptions = {
format: 'Letter', // Set the PDF format (e.g., 'A4', 'Letter', etc.)
base: `file://${__dirname}/`, // Set the base path for local file references
};

return new Promise<Buffer>((resolve, reject) => {
// Convert HTML to PDF
pdf.create(htmlContent, options).toBuffer((err, buffer) => {
if (err) {
reject(err);
} else {
// Remove temporary file
fs.unlinkSync(tempFilePath);
resolve(buffer);
}
});
});
}
}
18 changes: 1 addition & 17 deletions backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import { DataSource } from 'typeorm';

@Injectable()
export class AppService {
constructor(private dataSource: DataSource) {}

async initializeDb() {
const queryRunner = this.dataSource.createQueryRunner();
// Check if there are already provisions/groups/variants in the db
const [provisionCount] = await queryRunner.query('SELECT COUNT(*) FROM provision');
const [groupCount] = await queryRunner.query('SELECT COUNT(*) FROM provision_group');
const [documentType] = await queryRunner.query('SELECT COUNT(*) FROM document_type');
// If there is no data in any of the tables, run the SQL script
if (provisionCount.count == 0 && groupCount.count == 0 && documentType.count == 0) {
const sql = fs.readFileSync('./utils/db/init-db.sql', 'utf8');
await queryRunner.query(sql);
await queryRunner.release();
}
}
constructor() {}

getHello(): string {
return 'Hello Backend!';
Expand Down
2 changes: 1 addition & 1 deletion backend/src/document_data/document_data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export class DocumentDataService {
}

// helper function for getProvisionsByDocTypeIdAndDtid
async getDocumentDataVariables(document_type_id, dtid: number): Promise<DocumentDataVariable[]> {
async getDocumentDataVariables(document_type_id: number, dtid: number): Promise<DocumentDataVariable[]> {
const documentData: DocumentData = await this.documentDataRepository.findOne({
where: { dtid: dtid, document_type: { id: document_type_id } },
relations: ['document_data_provisions'],
Expand Down
3 changes: 0 additions & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { AppService } from './app.service';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -15,8 +14,6 @@ async function bootstrap() {

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
const appService = app.get(AppService);
// await appService.initializeDb();

await app.listen(process.env.ticdi_environment == 'DEVELOPMENT' ? 3001 : 3000);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ProvisionVariable } from './provision/entities/provision_variable.entit
import { DocumentTypeProvision } from './provision/entities/document_type_provision';

const config: TypeOrmModuleOptions = {
logging: true,
logging: ['error'],
type: 'postgres',
host: process.env.POSTGRESQL_HOST || 'localhost',
port: 5432,
Expand All @@ -30,7 +30,7 @@ const config: TypeOrmModuleOptions = {
DocumentType,
DocumentTypeProvision,
],
synchronize: true,
synchronize: false,
};

export default config;
9 changes: 5 additions & 4 deletions backend/src/provision/provision.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class ProvisionController {
return this.provisionService.findAllVariables();
}

@Get('variables-by-doc-type/:document_type_id')
findVariablesByDocType(@Param('document_type_id') document_type_id: number) {
return this.provisionService.findVariablesByDocType(+document_type_id);
}

@Post('add')
addProvision(
@Body()
Expand Down Expand Up @@ -149,10 +154,6 @@ export class ProvisionController {

@Post('update-manage-doc-type-provisions')
updateManageDocTypeProvisions(@Body() data: { document_type_id: number; provisions: ManageDocTypeProvision[] }) {
console.log('~~~~~~');
console.log(data.document_type_id);
console.log(data.provisions);
console.log('~~~~~~');
return this.provisionService.updateManageDocTypeProvisions(data.document_type_id, data.provisions);
}
}
24 changes: 23 additions & 1 deletion backend/src/provision/provision.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ export class ProvisionService {
});
}

async findVariablesByDocType(document_type_id: number): Promise<any[]> {
console.log('document_type_id: ' + document_type_id);
const variables = await this.provisionVariableRepository.find({
relations: ['provision'],
});
const documentTypeProvisions = await this.documentTypeProvisionRepository.find({
where: { associated: true },
relations: ['provision', 'document_type'],
});
const filteredDtps = documentTypeProvisions.filter((dtp) => dtp.document_type.id === document_type_id);
const docTypeProvisionIds = filteredDtps.map((dtp) => dtp.provision.id);
const associatedVariables = variables
.filter((variable) => docTypeProvisionIds.includes(variable.provision.id))
.map((variable) => {
return {
...variable,
provision_id: variable.provision.id,
};
});
return associatedVariables;
}

// old route to be deleted
async getProvisionsByDocumentTypeId(document_type_id: number): Promise<Provision[]> {
const provisions: Provision[] = await this.provisionRepository
Expand Down Expand Up @@ -279,7 +301,7 @@ export class ProvisionService {
update_timestamp: string;
};
const docTypeProvisions = await this.documentTypeProvisionRepository.find({
where: { document_type: { id: document_type_id } },
where: { document_type: { id: document_type_id }, associated: true },
relations: ['document_type', 'provision', 'provision_group', 'document_data_provisions'],
});
const provisions = await this.provisionRepository.find({ relations: ['provision_variables'] });
Expand Down
6 changes: 5 additions & 1 deletion backend/src/report/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Param,
UseGuards,
UseFilters,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { ProvisionJSON, SessionData, VariableJSON } from 'src/types';
import { TTLSService } from '../ttls/ttls.service';
Expand Down Expand Up @@ -50,6 +52,8 @@ export class ReportController {
console.log('callHttp failed');
console.log(err);
console.log(err.response.data);
const errorMessage = `Disposition Transaction not found with id ${dtid}`;
throw new HttpException(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
});
return response;
}
Expand Down Expand Up @@ -189,6 +193,6 @@ export class ReportController {

@Get()
getHealthCheck() {
return "";
return '';
}
}
4 changes: 1 addition & 3 deletions backend/src/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,14 +1272,12 @@ export class ReportService {
dtid: document.dtid,
version: templatesLookup[document.template_id].template_version,
file_name: templatesLookup[document.template_id].file_name,
updated_date: document.update_timestamp.toISOString().slice(0, 10),
updated_date: document && document.update_timestamp ? document.update_timestamp.toISOString().slice(0, 10) : '',
status: document.status,
active: templatesLookup[document.template_id].active_flag,
document_data_id: document.id,
document_type: document.document_type,
}));
console.log(combinedArray[0].updated_date);
// returns this: 2024-03-29T22:06:09.151Z

return combinedArray;
}
Expand Down
70 changes: 0 additions & 70 deletions backend/src/ttls/ttls.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,74 +346,4 @@ export class TTLSService {
console.log(error);
});
}

// // TODO
// async generateNFRReport(prdid: number, templateId: number, variables: any, username: string) {
// const url = `${hostname}:${port}/document-data/view/${prdid}`;
// const templateUrl = `${hostname}:${port}/document-template/find-one/${templateId}`;
// const logUrl = `${hostname}:${port}/print-request-log/`;
// // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// // TODO get the view for specified prdid from the new NFR entity
// const data = await axios
// .get(url, {
// headers: {
// 'Content-Type': 'application/json',
// },
// })
// .then((res) => {
// return res.data;
// });
// if (data.InspectionDate) {
// data['InspectionDate'] = this.formatInspectedDate(data.InspectionDate);
// }
// // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// // get the document template
// const documentTemplateObject: { id: number; the_file: string } = await axios.get(templateUrl).then((res) => {
// return res.data;
// });

// // log the request
// await axios.post(logUrl, {
// document_template_id: templateId,
// print_request_detail_id: prdid,
// dtid: data.DTID,
// request_app_user: username,
// request_json: JSON.stringify(data),
// });

// const cdogsToken = await this.callGetToken();
// let bufferBase64 = documentTemplateObject.the_file;
// const md = JSON.stringify({
// data,
// formatters:
// '{"myFormatter":"_function_myFormatter|function(data) { return data.slice(1); }","myOtherFormatter":"_function_myOtherFormatter|function(data) {return data.slice(2);}"}',
// options: {
// cacheReport: false,
// convertTo: 'docx',
// overwrite: true,
// reportName: 'nfr-report',
// },
// template: {
// content: `${bufferBase64}`,
// encodingType: 'base64',
// fileType: 'docx',
// },
// });

// const conf = {
// method: 'post',
// url: process.env.cdogs_url,
// headers: {
// Authorization: `Bearer ${cdogsToken}`,
// 'Content-Type': 'application/json',
// },
// responseType: 'arraybuffer',
// data: md,
// };
// const ax = require('axios');
// const response = await ax(conf).catch((error) => {
// console.log(error.response);
// });
// return response.data;
// }
}
9 changes: 8 additions & 1 deletion frontend/src/app/common/manage-provisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ export const getVariables = async (): Promise<Variable[]> => {
const url = `${config.API_BASE_URL}/provision/variables`;
const getParameters = api.generateApiParameters(url);
const response: Variable[] = await api.get<Variable[]>(getParameters);
console.log('getVariables response');
return response;
};

export const getVariablesByDocType = async (document_type_id: number): Promise<Variable[]> => {
const url = `${config.API_BASE_URL}/provision/variables-by-doc-type/${document_type_id}`;
const getParameters = api.generateApiParameters(url);
const response: Variable[] = await api.get<Variable[]>(getParameters);
console.log('getVariablesByDocType response');
console.log(response);
return response;
};
Expand Down
Loading

0 comments on commit 7ab8dd5

Please sign in to comment.