const User = require('./schemas/user');

const findByNumber = async (number) => {
	return await User.findOne({ number });
};
const findById = async (id) => {
	return await User.findById(id);
};
const createUser = async (body) => {
	const user = new User({ ...body });
	return await user.save();
};
const updateCode = async (id, code) => {
	return await User.updateOne({ _id: id }, { code });
};
const updateToken = async (id, token) => {
	return await User.updateOne({ _id: id }, { token });
};
const updateUser = async (id, obj) => {
	return await User.updateOne({ _id: id }, { ...obj });
};
const updateCredentials = async (id, body) => {
	return await User.findByIdAndUpdate({ _id: id }, { ...body }, { new: true });
};
const updateAvatar = async (id, avatarUrl) => {
	return await User.updateOne({ _id: id }, { avatarUrl });
};

module.exports = {
	findByNumber,
	createUser,
	updateCode,
	updateToken,
	updateUser,
	updateCredentials,
	updateAvatar,
	findById,
};
