Skip to content

Commit

Permalink
kill me pls
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyRAV committed Sep 16, 2024
1 parent 22671f3 commit 8e472cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config/configureMiddlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const configureMiddlewares = (app) => {
app.use(express.json());
app.use(cors({
origin: function (origin, callback) {
const allowedOrigins = ['http://localhost:3000', 'https://www.intelliq.arc-solutions.xyz', 'https://www.intelliq.dev', 'https://www.beta.intelliq.dev', 'https://www.internal.intelliq.dev', 'https://intelliq-be-s1.azurewebsites.net', 'https://intelliq-be-s2.azurewebsites.net', 'https://intelliq-be-s3.azurewebsites.net'];
const allowedOrigins = ['http://localhost:3001', 'https://www.intelliq.arc-solutions.xyz', 'https://www.intelliq.dev', 'https://www.beta.intelliq.dev', 'https://www.internal.intelliq.dev', 'https://intelliq-be-s1.azurewebsites.net', 'https://intelliq-be-s2.azurewebsites.net', 'https://intelliq-be-s3.azurewebsites.net'];
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
Expand Down
52 changes: 26 additions & 26 deletions controllers/historyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,48 @@ const userHistory = async (req, res) => {
created_at: formatDate(quiz.created_at)
}));

const allQuizzes = await prisma.quizzes.findMany({
where: {user_id},
select: {
topics: true // Include topics in the selection
}
});
// const allQuizzes = await prisma.quizzes.findMany({
// where: {user_id},
// select: {
// topics: true // Include topics in the selection
// }
// });

// Fisher-Yates (Knuth) shuffle algorithm
const shuffleArray = (array) => {
for(let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// const shuffleArray = (array) => {
// for(let i = array.length - 1; i > 0; i--) {
// const j = Math.floor(Math.random() * (i + 1));
// [array[i], array[j]] = [array[j], array[i]];
// }
// }

// extract topics and flatten the array
const topics = allQuizzes.flatMap(quiz => quiz.topics.map(topic => topic.toLowerCase()));
// const topics = allQuizzes.flatMap(quiz => quiz.topics.map(topic => topic.toLowerCase()));

// count the occurrence of each topic
const topicCounts = topics.reduce((counts, topic) => {
counts[topic] = (counts[topic] || 0) + 1;
return counts;
}, {});
// const topicCounts = topics.reduce((counts, topic) => {
// counts[topic] = (counts[topic] || 0) + 1;
// return counts;
// }, {});

// sort the topics by their occurrence count in descending order
const sortedTopics = Object.entries(topicCounts).sort((a, b) => b[1] - a[1]);
// const sortedTopics = Object.entries(topicCounts).sort((a, b) => b[1] - a[1]);

// Log the top five topics along with their occurrence count
// const topFiveTopicsWithCount = sortedTopics.slice(0, 5).map(topic => `${topic[0]} x${topic[1]}`);
// console.log(topFiveTopicsWithCount); // Log the top five topics

// return the top five topics without their occurrence count
const topFiveTopics = sortedTopics.slice(0, 5).map(topic => ({
name: topic[0].split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' '),
topic_frequency: topic[1]
}));
// const topFiveTopics = sortedTopics.slice(0, 5).map(topic => ({
// name: topic[0].split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' '),
// topic_frequency: topic[1]
// }));

const totalCount = await prisma.quizzes.count({where: {user_id}});
shuffleArray(topFiveTopics)
console.log(topFiveTopics);
// const totalCount = await prisma.quizzes.count({where: {user_id}});
// shuffleArray(topFiveTopics)
// console.log(topFiveTopics);

res.json({quizzes: formattedQuizzes, totalCount, topFiveTopics});
res.json({quizzes: formattedQuizzes});
} catch (error) {
res.status(500).json({error: error.message});
}
Expand Down
16 changes: 12 additions & 4 deletions controllers/quizController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ const saveQuizResults = async (req, res) => {

try {
const {rawQuestions} = req.body;
let {quizTitle, questions, timeTaken, quizTopics} = rawQuestions;
quizTopics = quizTopics[0].split(',').map(topic => topic.trim());
let {quizTitle, questions, timeTaken} = rawQuestions;
// let {quizTitle, questions, timeTaken, quizTopics} = rawQuestions;
// quizTopics = quizTopics[0].split(',').map(topic => topic.trim());
// console.log(quizTopics)

// const createdQuiz = await prisma.quizzes.create({
// data: {
// user_id: user_id,
// quiz_title: quizTitle,
// total_time_taken: timeTaken,
// correct_answers_count: 0,
// topics: quizTopics
// }
// });
const createdQuiz = await prisma.quizzes.create({
data: {
user_id: user_id,
quiz_title: quizTitle,
total_time_taken: timeTaken,
correct_answers_count: 0,
topics: quizTopics
}
});

Expand Down Expand Up @@ -89,7 +98,6 @@ const saveQuizResults = async (req, res) => {
quiz_title: quizTitle,
timeTaken,
correctAnswersCount,
quizTopics,
questions: questions.map(question => ({
text: question.text,
correctAnswer: question.correctAnswer,
Expand Down

0 comments on commit 8e472cf

Please sign in to comment.