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

157 lines
4.4 KiB
JavaScript

import { Op } from "sequelize";
import { User } from "../model/user.js";
import { UserUploads } from "../model/userUpload.js";
export const countUsers = async () => {
try {
const count = await User.count();
return count;
} catch (error) {
throw new Error('Error counting users');
}
};
// Function to get a user by email
export const getUserByEmail = async (email) => {
try {
const user = await User.findOne({ where: { email } });
return user; // Return the user object
} catch (err) {
console.error(err);
throw new Error('Error while checking user by email');
}
};
export const getUserByFilter = async (payload) => {
try {
const user = await User.findOne({ where: payload });
return user; // Return the user object
} catch (err) {
console.error(err);
throw new Error('Error while checking user by filter');
}
};
// Function to create a new user
export const createUser = async (payload) => {
try {
const user = await User.create(payload);
return user; // Return the created user
} catch (err) {
console.error(err);
throw new Error('Error while creating user');
}
};
// Function to get a user by ID (for example, to fetch profile data)
export const getUserById = async (id) => {
try {
const user = await User.findByPk(id); // Find the user by primary key (ID)
return user; // Return the user data
} catch (err) {
console.error(err);
throw new Error('Error while fetching user by ID');
}
};
export const countUserUploadsByUserId = async (userId) => {
try {
const count = await UserUploads.count({
where: {
user_id: userId
}
});
console.log(`Total uploads for user ID ${userId}: ${count}`);
return count;
} catch (error) {
console.error('Error counting user uploads:', error);
throw new Error('Error counting user uploads');
}
};
// Update user profile
export const updateUserProfile = async (id, updatedFields) => {
try {
const user = await User.findByPk(id);
// Dynamically update the fields based on the input
await user.update(updatedFields); // Sequelize handles the query and update
return user; // Return the updated user
} catch (err) {
console.error(err);
throw new Error('Error while updating user profile');
}
};
export const createUpload = async (payload) => {
try {
const upload = await UserUploads.create(payload);
return upload;
} catch (error) {
console.error('Error uploading file:', error);
throw new Error('Error uploading file');
}
};
export const getUserUpload = async (userId) => {
try {
// Find the user and include their uploads
const user = await User.findOne({
where: { id: userId }, // Filter by user ID
include: [
{
model: UserUploads, // Include the user uploads
as: 'uploads', // Alias defined in associations
}
]
});
return user;
} catch (error) {
console.error('Error uploading file:', error);
throw new Error('Error uploading file');
}
};
export const getUploadVideoById = async (payload) => {
try {
const userData = await UserUploads.findOne({ where: payload });
return userData;
} catch (error) {
console.error('Error file:', error);
throw new Error('Error file');
}
};
export const getUserUploadByDateFilter = async (userId, startDate, endDate) => {
try {
// Find the user and include their uploads
const user = await User.findOne({
where: { id: userId }, // Filter by user ID
include: [
{
model: UserUploads, // Include the user uploads
as: 'uploads', // Alias defined in associations
where: {
upload_time: {
[Op.between]: [startDate, endDate], // Filter uploads within the current week
}
}
}
]
});
return user;
} catch (error) {
console.error('Error fetching user uploads:', error);
throw new Error('Error fetching user uploads');
}
};