AI-Tennis-Coach/server/services/assessment.js
2025-02-11 11:23:59 +05:30

95 lines
2.8 KiB
JavaScript

import { Op } from "sequelize";
import { assessmentQuestion } from "../model/assessment.js";
import { Training } from "../model/training.js";
import { User } from "../model/user.js";
import { UserAnswers } from "../model/userAnswered.js";
export const createQuestions = async (payload) => {
try {
// Insert all questions at once using bulkCreate
const createdQuestions = await assessmentQuestion.bulkCreate(payload);
return createdQuestions; // Return the user object
} catch (err) {
console.error(err);
throw new Error('Error creating questions');
}
};
export const getQuestionById = async (id) => {
try {
const question = await assessmentQuestion.findByPk(id);
return question; // Return the user object
} catch (err) {
console.error(err);
throw new Error('Error getting questions');
}
};
export const updateQuestionById = async (question) => {
try {
return await question.save();
} catch (err) {
console.error(err);
throw new Error('Error getting questions');
}
};
// Function to get all questions
export const getAllQuestions = async () => {
try {
return await assessmentQuestion.findAll({
order: [['id', 'ASC']],
});
} catch (err) {
console.error('Error fetching questions:', err);
throw new Error('Error fetching questions'); // Throw an error if something goes wrong
}
};
export const addUserAnswersByUserId = async (payload) => {
try {
const upload = await UserAnswers.create(payload);
return upload;
} catch (error) {
console.error('Error adding res:', error);
throw new Error('Error adding res');
}
};
export const addUserDrillsByUserId = async (payload) => {
try {
const data = await Training.create(payload);
return data;
} catch (error) {
console.error('Error adding res drills:', error);
throw new Error('Error adding res drills');
}
};
export const getUserAnswersByUserId = async (userId) => {
try {
// Find the user and include their uploads
const user = await UserAnswers.findAll({
where: { user_id: userId }, // Filter by user ID
});
return user;
} catch (error) {
console.error('Error uploading file:', error);
throw new Error('Error uploading file');
}
};
export const getUserTrainingByUserId = async (userId) => {
try {
const userTraining = await Training.findOne({
where: { user_id: userId }, // Filter by user ID
order: [['createdAt', 'DESC']], // Order by createdAt in descending order
});
return userTraining;
} catch (error) {
console.error('Error Training:', error);
throw new Error('Error Training');
}
};