Skip to content

Commit 1d0a1cc

Browse files
committed
Format report name in report service and controller
1 parent 263fe05 commit 1d0a1cc

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

Servers/controllers/reporting.ctrl.ts

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Request, Response } from "express";
22
import { STATUS_CODE } from "../utils/statusCode.utils";
33
import { uploadFile } from "../utils/fileUpload.utils";
4-
import { DefaultReportName } from "../models/reporting.model";
5-
import { getReportData, isAuthorizedUser } from '../services/reportService';
4+
import { getReportData, isAuthorizedUser, getFormattedReportName } from '../services/reportService';
65

76
import marked from 'marked';
87
const htmlDocx = require("html-to-docx");
@@ -31,28 +30,7 @@ export async function generateReports(
3130
const markdownDoc = await marked.parse(await markdownData); // markdown file
3231
const generatedDoc = await htmlDocx(markdownDoc); // convert markdown to docx
3332

34-
let defaultFileName;
35-
if (req.body.reportName === ''){
36-
switch(req.body.reportType) {
37-
case "Project risks report":
38-
defaultFileName = DefaultReportName.PROJECTRISK_REPORT;
39-
break;
40-
case "Vendors and risks report":
41-
defaultFileName = DefaultReportName.VENDOR_REPORT;
42-
break;
43-
case "Assessment tracker report":
44-
defaultFileName = DefaultReportName.ASSESSMENT_REPORT;
45-
break;
46-
case "Compliance tracker report":
47-
defaultFileName = DefaultReportName.COMPLIANCE_REPORT;
48-
break;
49-
default:
50-
defaultFileName = DefaultReportName.ALL_REPORT;
51-
}
52-
}else {
53-
defaultFileName = req.body.reportName;
54-
}
55-
33+
let defaultFileName = getFormattedReportName(req.body.reportName, req.body.reportType);
5634
const docFile = {
5735
originalname: `${defaultFileName}.docx`,
5836
buffer: generatedDoc,

Servers/services/reportService.ts

+47-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,52 @@
11
import { getProjectRisksReportQuery, getMembersByProjectIdQuery } from "../utils/reporting.utils";
2-
import { ReportType } from "../models/reporting.model";
2+
import { DefaultReportName, ReportType } from "../models/reporting.model";
33

44
interface reportBodyData {
55
projectTitle: string,
66
projectOwner: string
77
}
88

9+
/**
10+
* Format the report name
11+
* if request body includes report name, return the report name as user requested
12+
* If not, return as {type}_{YYYYMMDD}_{HHMMSS}
13+
*/
14+
export function getFormattedReportName(name: string, type: string) {
15+
let reportType;
16+
switch(type) {
17+
case ReportType.PROJECTRISK_REPORT:
18+
reportType = DefaultReportName.PROJECTRISK_REPORT;
19+
break;
20+
case ReportType.VENDOR_REPORT:
21+
reportType = DefaultReportName.VENDOR_REPORT;
22+
break;
23+
case ReportType.ASSESSMENT_REPORT:
24+
reportType = DefaultReportName.ASSESSMENT_REPORT;
25+
break;
26+
case ReportType.COMPLIANCE_REPORT:
27+
reportType = DefaultReportName.COMPLIANCE_REPORT;
28+
break;
29+
default:
30+
reportType = DefaultReportName.ALL_REPORT;
31+
}
32+
33+
const date = new Date();
34+
35+
const year = date.getFullYear();
36+
const month = String(date.getMonth() + 1).padStart(2, '0');
37+
const day = String(date.getDate()).padStart(2, '0');
38+
39+
const hour = String(date.getHours()).padStart(2, '0');
40+
const minute = String(date.getMinutes()).padStart(2, '0');
41+
const second = String(date.getSeconds()).padStart(2, '0');
42+
43+
if(name.length === 0 ) {
44+
return `${type}_${year}${month}${day}_${hour}${minute}${second}`;
45+
} else {
46+
return `${name}`;
47+
}
48+
}
49+
950
/*
1051
Get member lists by projectId
1152
Check whether the user belongs to current project
@@ -63,16 +104,16 @@ export async function getProjectRiskMarkdown (
63104
}
64105

65106
const projectRiskMD = `
66-
VerifyWise Project Risk Report
107+
VerifyWise project risk report
67108
========================
68109
69110
This report is generated by the VeriftyWise Project Risk. It aims to provide a way to demonstrate their claims about the risks of their AI systems.
70111
71-
- **Report Date :** ${new Date().toLocaleDateString()}
72-
- **Project :** ${data.projectTitle}
73-
- **Owner :** ${data.projectOwner}
112+
- **Report Date ** ${new Date().toLocaleDateString()}
113+
- **Project ** ${data.projectTitle}
114+
- **Owner ** ${data.projectOwner}
74115
75-
Project Risk Table
116+
Project risk table
76117
-------------
77118
| Risk Name | Owner | Severity | Likelihood | Mitigation Status | Risk Level | Target Date |
78119
|----|----|----|----|----|----|----|

0 commit comments

Comments
 (0)