Skip to content

Commit 7daa769

Browse files
Merge pull request #2083 from OneCommunityGlobal/linh_backend_fix_faq_log_question_error
Linh - Fix FAQ unanswered question API false 500 and duplicate detection
2 parents 148613a + 7f7a359 commit 7daa769

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

src/controllers/faqController.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,28 @@ const logUnansweredFAQ = async function (req, res) {
151151
try {
152152
await verifyToken(req);
153153

154-
const { question } = req.body;
154+
const rawQuestion = req.body?.question;
155+
const normalizedQuestion = rawQuestion?.replaceAll(/\s+/g, ' ').trim();
156+
if (!normalizedQuestion) {
157+
return res.status(400).json({ message: 'Question is required' });
158+
}
155159
const createdBy = req.user.userid;
156160

157-
const existingQuestion = await UnansweredFAQ.findOne({ question });
161+
const escapedQuestion = normalizedQuestion.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
162+
const flexibleWhitespaceQuestion = escapedQuestion.replaceAll(/\s+/g, String.raw`\s+`);
163+
const duplicateQuestionPattern = new RegExp(
164+
String.raw`^\s*${flexibleWhitespaceQuestion}\s*$`,
165+
'i',
166+
);
167+
const existingQuestion = await UnansweredFAQ.findOne({
168+
question: { $regex: duplicateQuestionPattern },
169+
});
158170
if (existingQuestion) {
159171
return res.status(409).json({ message: 'This question has already been logged' });
160172
}
161173

162174
const newUnansweredFAQ = new UnansweredFAQ({
163-
question,
175+
question: normalizedQuestion,
164176
createdBy,
165177
createdAt: new Date().toISOString(),
166178
});
@@ -172,25 +184,30 @@ const logUnansweredFAQ = async function (req, res) {
172184
ownerEmails = [process.env.TEST_OWNER_EMAIL];
173185
console.log('Test mode enabled. Using test owner email:', ownerEmails);
174186
} else {
175-
const owners = await UserProfile.find({ role: 'owner' }).select('email');
187+
const owners = await UserProfile.find({ role: /^owner$/i }).select('email');
176188
ownerEmails = owners.map((owner) => owner.email);
177189

178190
if (ownerEmails.length === 0) {
179191
console.warn('No owner emails found in the database.');
180-
return res.status(500).json({ message: 'No owner emails found' });
181192
}
182-
console.log('Sending email to owners:', ownerEmails);
193+
if (ownerEmails.length > 0) {
194+
console.log('Sending email to owners:', ownerEmails);
195+
}
183196
}
184197

185198
const emailMessage = `
186199
<p>A new unanswered question has been logged:</p>
187-
<p><strong>Question:</strong> ${question}</p>
200+
<p><strong>Question:</strong> ${normalizedQuestion}</p>
188201
<p>Please review and add an answer if necessary.</p>
189202
`;
190203

191204
try {
192-
emailSender(ownerEmails, 'New Unanswered FAQ Logged', emailMessage);
193-
console.log('Email successfully sent.');
205+
if (ownerEmails.length > 0) {
206+
emailSender(ownerEmails, 'New Unanswered FAQ Logged', emailMessage);
207+
console.log('Email successfully sent.');
208+
} else {
209+
console.warn('Skipping owner notification email because no owner email was found.');
210+
}
194211
} catch (error) {
195212
console.error('Error sending email:', error);
196213
}

0 commit comments

Comments
 (0)