Skip to content

Commit

Permalink
Merge pull request #275 from agiledev-students-fall2023/task/229/int-…
Browse files Browse the repository at this point in the history
…create-issue-db

Task/229/int create issue db
  • Loading branch information
hasiburratul authored Nov 24, 2023
2 parents 0685744 + 01d882e commit 2956de3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
2 changes: 1 addition & 1 deletion back-end/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"nodemon": "^3.0.1",
"prettier": "^2.5.1"
}
}
}
77 changes: 34 additions & 43 deletions back-end/src/controllers/createIssueHandler.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
import { publicpath } from "../../app.js";
import Issue from '../../models/issueModel.js';
import { promises as fs } from 'fs';

export async function createIssueHandler(req, res) {
try {
const filePath = publicpath + '/json/mockapi.json';

// Assuming all the needed body parameters are provided correctly from the client side.
console.log(req.body);
const {
dateCreated, // Assuming this is sent from the client or generated here.
currentStatus, // This should have a default status if not provided by the client.
currentPriority // This should have a default priority if not provided by the client.
} = req.body;
// Read the existing JSON file
const fileContent = await fs.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
console.log(req.body);
const {
dateCreated,
currentStatus,
currentPriority
} = req.body;

const issueDateCreated = dateCreated || new Date().toLocaleDateString();
const issueTimeCreated = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: undefined, // Explicitly prevent seconds from showing
hour12: false // Use 24-hour format, change to true for 12-hour format if preferred
});
const attachments = req.files.map(file => file.filename);
const newIssue = {
index: jsonData.length + 1,
studentNetID: [req.params.studentNetID],
studentName: [req.body.studentName], // Now taking directly from req.body with validation
title: req.body.issueTitle,
description: req.body.issueDesc,
attachments: attachments,
departments: req.body.deptTagged.includes(',') ? req.body.deptTagged.split(',') : [req.body.deptTagged],
comments: [null],
dateCreated: issueDateCreated,
timeCreated: issueTimeCreated,
currentStatus: currentStatus || 'Open',
currentPriority: currentPriority || 'New'
};
const issueDateCreated = dateCreated || new Date().toLocaleDateString();
const issueTimeCreated = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false
});
const attachments = req.files.map(file => file.filename);
const lastIssue = await Issue.findOne().sort({ index: -1 });
const newIndex = lastIssue ? lastIssue.index + 1 : 1;

jsonData.push(newIssue);
const newIssue = new Issue ({
index: newIndex,
studentNetID: [req.params.studentNetID],
studentName: [req.body.studentName],
title: req.body.issueTitle,
description: req.body.issueDesc,
attachments: attachments,
departments: req.body.deptTagged.includes(',') ? req.body.deptTagged.split(',') : [req.body.deptTagged],
comments: [null],
dateCreated: issueDateCreated,
timeCreated: issueTimeCreated,
currentStatus:'Open',
currentPriority: 'New'
});

// Write the updated JSON data back to the file
await fs.writeFile(filePath, JSON.stringify(jsonData, null, 2)); // Corrected to use async/await
console.log('Issue created successfully and JSON file updated.');
console.log('Body:', req.body);
console.log('Params:', req.params);
try {
await newIssue.save();
console.log('Issue:', newIssue);
res.status(200).send('Issue created successfully');
} catch (error) {
// Handle errors
console.error('Error creating issue:', error);
res.status(500).json({ error: 'Internal Server Error' });
// res.status(500).json({ error: 'Internal Server Error' });
}
}

0 comments on commit 2956de3

Please sign in to comment.