Skip to content

Commit

Permalink
Merge pull request #738 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.42]
  • Loading branch information
one-community authored Feb 10, 2024
2 parents 3393512 + ce6d63d commit 7e3c59a
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 88 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const fs = require('fs');
const path = require('path');

const filename = 'BuildingUnits.json';
const currentFilePath = __filename;
const rootPath = path.resolve(path.dirname(currentFilePath), '../../../'); // Go up three levels to the root
const filepath = path.join(rootPath, filename);
const readFile = fs.readFile;
const writeFile = fs.writeFile;
const { readFile } = fs;
const { writeFile } = fs;

function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolType, EquipType) {
async function fetchMaterialTypes(req, res) {
Expand All @@ -22,7 +23,7 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp

const fetchInvUnitsFromJson = async (req, res) => {
try {
console.log(__dirname,filepath)
console.log(__dirname, filepath);
readFile(filepath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
Expand Down
156 changes: 97 additions & 59 deletions src/controllers/dashBoardController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const path = require('path');
const fs = require('fs/promises');
const mongoose = require('mongoose');
const dashboardhelper = require('../helpers/dashboardhelper')();
const emailSender = require('../utilities/emailSender');
const AIPrompt = require('../models/weeklySummaryAIPrompt');
/* eslint-disable quotes */
const path = require("path");
const fs = require("fs/promises");
const mongoose = require("mongoose");
const dashboardhelper = require("../helpers/dashboardhelper")();
const emailSender = require("../utilities/emailSender");
const AIPrompt = require("../models/weeklySummaryAIPrompt");
const User = require("../models/userProfile");

const dashboardcontroller = function () {
const dashboarddata = function (req, res) {
Expand All @@ -15,38 +17,72 @@ const dashboardcontroller = function () {
res.status(200).send(results);
});
};

// The Code below updates the time the AiPrompt was copied by the user - Sucheta
// eslint-disable-next-line space-before-blocks
const updateCopiedPrompt = function (req, res) {
return User.findOneAndUpdate(
{ _id: req.params.userId },
{ copiedAiPrompt: Date.now() },
{ new: true },
)
.then((user) => {
if (user) {
res.status(200).send('Copied AI prompt');
} else {
res.status(404).send({ message: 'User not found ' });
}
})
.catch((error) => {
res.status(500).send(error);
});
};
const getPromptCopiedDate = function (req, res) {
return User.findOne({ _id: req.params.userId }).then((user) => {
if (user) {
res.status(200).send({ message: user.copiedAiPrompt });
}
});
};
const updateAIPrompt = function (req, res) {
if (req.body.requestor.role === 'Owner') {
AIPrompt.findOneAndUpdate({ _id: 'ai-prompt' }, { ...req.body, aIPromptText: req.body.aIPromptText })
.then(() => {
res.status(200).send('Successfully saved AI prompt.');
}).catch(error => res.status(500).send(error));
if (req.body.requestor.role === "Owner") {
AIPrompt.findOneAndUpdate(
{ _id: "ai-prompt" },
{
...req.body,
aIPromptText: req.body.aIPromptText,
modifiedDatetime: Date.now(),
},
)
.then(() => {
res.status(200).send("Successfully saved AI prompt.");
})
.catch(error => res.status(500).send(error));
}
};

const getAIPrompt = function (req, res) {
AIPrompt.findById({ _id: 'ai-prompt' })
.then((result) => {
if (result) {
// If the GPT prompt exists, send it back.
res.status(200).send(result);
} else {
// If the GPT prompt does not exist, create it.
const defaultPrompt = {
_id: 'ai-prompt',
aIPromptText: "Please edit the following summary of my week's work. Make sure it is professionally written in 3rd person format.\nWrite it as only one paragraph. It must be only one paragraph. Keep it less than 500 words. Start the paragraph with 'This week'.\nMake sure the paragraph contains no links or URLs and write it in a tone that is matter-of-fact and without embellishment.\nDo not add flowery language, keep it simple and factual. Do not add a final summary sentence. Apply all this to the following:",
};
AIPrompt.create(defaultPrompt)
.then((newResult) => {
res.status(200).send(newResult);
})
.catch((creationError) => {
res.status(500).send(creationError);
});
}
})
.catch(error => res.status(500).send(error));
AIPrompt.findById({ _id: "ai-prompt" })
.then((result) => {
if (result) {
// If the GPT prompt exists, send it back.
res.status(200).send(result);
} else {
// If the GPT prompt does not exist, create it.
const defaultPrompt = {
_id: "ai-prompt",
aIPromptText:
"Please edit the following summary of my week's work. Make sure it is professionally written in 3rd person format.\nWrite it as only one paragraph. It must be only one paragraph. Keep it less than 500 words. Start the paragraph with 'This week'.\nMake sure the paragraph contains no links or URLs and write it in a tone that is matter-of-fact and without embellishment.\nDo not add flowery language, keep it simple and factual. Do not add a final summary sentence. Apply all this to the following:",
};
AIPrompt.create(defaultPrompt)
.then((newResult) => {
res.status(200).send(newResult);
})
.catch((creationError) => {
res.status(500).send(creationError);
});
}
})
.catch(error => res.status(500).send(error));
};

const monthlydata = function (req, res) {
Expand All @@ -60,7 +96,7 @@ const dashboardcontroller = function () {
if (!results || results.length === 0) {
const emptyresult = [
{
projectName: '',
projectName: "",
timeSpent_hrs: 0,
},
];
Expand Down Expand Up @@ -169,27 +205,27 @@ const dashboardcontroller = function () {

try {
emailSender(
'[email protected]',
"[email protected]",
`Bug Rport from ${firstName} ${lastName}`,
emailBody,
email,
);
res.status(200).send('Success');
res.status(200).send("Success");
} catch {
res.status(500).send('Failed');
res.status(500).send("Failed");
}
};

const suggestionData = {
suggestion: [
'Identify and remedy poor client and/or user service experiences',
'Identify bright spots and enhance positive service experiences',
'Make fundamental changes to our programs and/or operations',
'Inform the development of new programs/projects',
'Identify where we are less inclusive or equitable across demographic groups',
'Strengthen relationships with the people we serve',
"Identify and remedy poor client and/or user service experiences",
"Identify bright spots and enhance positive service experiences",
"Make fundamental changes to our programs and/or operations",
"Inform the development of new programs/projects",
"Identify where we are less inclusive or equitable across demographic groups",
"Strengthen relationships with the people we serve",
"Understand people's needs and how we can help them achieve their goals",
'Other',
"Other",
],
field: [],
};
Expand All @@ -212,7 +248,7 @@ const dashboardcontroller = function () {
<p>${args[0]}</p>
<b> &#9913; Suggestion:</b>
<p>${args[1]}</p>
${fieldaaray.length > 0 ? fieldaaray : ''}
${fieldaaray.length > 0 ? fieldaaray : ""}
<b> &#9913; Name of Suggester:</b>
<p>${args[3].firstName} ${args[3].lastName}</p>
<b> &#9913; Email of Suggester:</b>
Expand All @@ -239,17 +275,17 @@ const dashboardcontroller = function () {
);
try {
emailSender(
'[email protected]',
'A new suggestion',
"[email protected]",
"A new suggestion",
emailBody,
null,
null,
email,
null,
);
res.status(200).send('Success');
res.status(200).send("Success");
} catch {
res.status(500).send('Failed');
res.status(500).send("Failed");
}
};

Expand All @@ -258,40 +294,40 @@ const dashboardcontroller = function () {
if (suggestionData) {
res.status(200).send(suggestionData);
} else {
res.status(404).send('Suggestion data not found.');
res.status(404).send("Suggestion data not found.");
}
} catch (error) {
console.error('Error getting suggestion data:', error);
res.status(500).send('Internal Server Error');
console.error("Error getting suggestion data:", error);
res.status(500).send("Internal Server Error");
}
};

const editSuggestionOption = async (req, res) => {
try {
if (req.body.suggestion) {
if (req.body.action === 'add') {
if (req.body.action === "add") {
suggestionData.suggestion.unshift(req.body.newField);
}
if (req.body.action === 'delete') {
if (req.body.action === "delete") {
suggestionData.suggestion = suggestionData.suggestion.filter(
(item, index) => index + 1 !== +req.body.newField,
);
}
} else {
if (req.body.action === 'add') {
if (req.body.action === "add") {
suggestionData.field.unshift(req.body.newField);
}
if (req.body.action === 'delete') {
if (req.body.action === "delete") {
suggestionData.field = suggestionData.field.filter(
item => item !== req.body.newField,
);
}
}

res.status(200).send('success');
res.status(200).send("success");
} catch (error) {
console.error('Error editing suggestion option:', error);
res.status(500).send('Internal Server Error');
console.error("Error editing suggestion option:", error);
res.status(500).send("Internal Server Error");
}
};

Expand All @@ -307,6 +343,8 @@ const dashboardcontroller = function () {
getSuggestionOption,
editSuggestionOption,
sendMakeSuggestion,
updateCopiedPrompt,
getPromptCopiedDate,
};
};

Expand Down
6 changes: 4 additions & 2 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,11 @@ const userProfileController = function (UserProfile) {

// remove user from cache, it should be loaded next time
cache.removeCache(`user-${userId}`);
if (!key || value === undefined) return res.status(400).send({ error: 'Missing property or value' });
if (!key || value === undefined) {
return res.status(400).send({ error: 'Missing property or value' });
}

return UserProfile.findById(userId)
return UserProfile.findById(userId)
.then((user) => {
user.set({
[key]: value,
Expand Down
Loading

0 comments on commit 7e3c59a

Please sign in to comment.