Skip to content

Commit

Permalink
Fixing missing elements in risk register. Minor type. Closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
davetaz committed Jul 30, 2024
1 parent 57982f6 commit 17f7af6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
27 changes: 26 additions & 1 deletion controllers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,29 @@ async function getCompletionState(projectId, schema) {
}
}

module.exports = { getUserProjects, getCompletionState, getUserProjectMetrics, addRiskScoreToProject };
async function getProjectOwner(project) {
try {
// Validate that the project object has an owner field
if (!project || !project.owner) {
throw new Error("Invalid project object or missing owner field");
}

// Find the owner user by ID
const owner = await User.findById(project.owner);
if (!owner) {
throw new Error("Owner not found");
}

// Return owner details
return {
id: owner._id,
name: owner.name,
email: owner.email
};
} catch (error) {
console.error("Error retrieving project owner:", error);
throw error; // Propagate the error to the caller
}
}

module.exports = { getUserProjects, getCompletionState, getUserProjectMetrics, addRiskScoreToProject, getProjectOwner };
11 changes: 10 additions & 1 deletion lib/docxBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ async function getImage(riskCounts) {
return buf;
}

async function buildDocx(project,metrics) {
async function buildDocx(project,metrics, owner) {
const donutChartData = await getImage(metrics.riskCounts);

try {
const { path: tempFilePath } = await tmp.file();

const doc = await docx.patchDocument(fs.readFileSync("./public/data/template.docx"), {
outputType: "nodebuffer",
features: {
updateFields: true,
},
patches: {
doctitle: {
type: docx.PatchType.PARAGRAPH,
Expand All @@ -103,6 +106,12 @@ async function buildDocx(project,metrics) {
new docx.TextRun(project.title)
]
},
author: {
type: docx.PatchType.PARAGRAPH,
children: [
new docx.TextRun(owner.name + " (" + owner.email + ")")
]
},
footertitle: {
type: docx.PatchType.PARAGRAPH,
children: [
Expand Down
3 changes: 2 additions & 1 deletion routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ router.get('/:id', ensureAuthenticated, checkProjectAccess, loadProject, async (
} else if (acceptHeader === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
// Respond with DOCX
project = await projectController.addRiskScoreToProject(project);
let owner = await projectController.getProjectOwner(project);
const userProjects = [];
userProjects.push(project);
let metrics = await projectController.getUserProjectMetrics(userProjects);
const tempFilePath = await buildDocx(project,metrics);
const tempFilePath = await buildDocx(project,metrics, owner);
const fileName = `${project.title.replace(/\s+/g, '_').trim()}.docx`;
//const buffer = await docx.Packer.toBuffer(doc);
res.set('Content-Disposition', `attachment; filename="${fileName}"`);
Expand Down
2 changes: 1 addition & 1 deletion views/pages/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
height: auto;
vertical-align: bottom;
padding-right: 10px;" src="/images/icons_arrow-blue-2d56bf3e.svg" alt=">"/>Select from example case studies:</h3>
<p> Learn how to conduct a Consequence Scan by working through one of our example case studies and identify consequences, determine risk and plan actions.</p>
<p> Learn how to conduct a Consequence Scan by working through one of our example case studies and identify consequences, determine risks and plan actions.</p>
</div>
<div>
<h3><img style="width: 30px;
Expand Down
4 changes: 4 additions & 0 deletions views/pages/project.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
});
document.getElementById("downloadDOCXButton").addEventListener("click", async () => {
try {
// Show an alert to inform users about updating the table of contents
alert("Once the document has downloaded, you will need to update the table of contents to correct page numbering and titles.");
// Fetch the document
const response = await fetch(`/project/${projectId}`, {
headers: {
"Accept": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Expand Down

0 comments on commit 17f7af6

Please sign in to comment.