2025-05-21 22:13:09 +07:00

105 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, 'Пожалуйста, укажите ваше имя'],
trim: true,
},
email: {
type: String,
required: [true, 'Пожалуйста, укажите ваш email'],
unique: true,
lowercase: true,
trim: true,
match: [
/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/,
'Пожалуйста, укажите корректный email',
],
},
password: {
type: String,
required: [true, 'Пожалуйста, укажите пароль'],
minlength: [6, 'Пароль должен быть не менее 6 символов'],
select: false,
},
dateOfBirth: {
type: Date,
},
gender: {
type: String,
enum: ['male', 'female', 'other'],
},
bio: {
type: String,
trim: true,
maxlength: [500, 'Описание не должно превышать 500 символов'],
},
photos: [
{
url: String,
public_id: String, // Добавляем public_id
isProfilePhoto: { type: Boolean, default: false },
},
],
location: {
city: String,
country: String,
},
preferences: {
gender: { type: String, enum: ['male', 'female', 'other', 'any'], default: 'any' },
ageRange: {
min: { type: Number, default: 18 },
max: { type: Number, default: 99 },
},
},
isActive: {
type: Boolean,
default: true,
},
lastSeen: {
type: Date,
default: Date.now,
},
// --- НОВЫЕ ПОЛЯ ДЛЯ ЛАЙКОВ, ПРОПУСКОВ И МЭТЧЕЙ ---
liked: [{ // Массив ID пользователей, которых лайкнул текущий пользователь
type: mongoose.Schema.Types.ObjectId,
ref: 'User' // Ссылка на модель User
}],
passed: [{ // Массив ID пользователей, которых пропустил/дизлайкнул текущий пользователь
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
matches: [{ // Массив ID пользователей, с которыми у текущего пользователя взаимный лайк (мэтч)
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
// ----------------------------------------------------
},
{
timestamps: true,
}
);
// ... (Middleware pre('save') для хеширования пароля и метод matchPassword остаются без изменений) ...
userSchema.pre('save', async function (next) {
if (!this.isModified('password')) {
return next();
}
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
next();
});
userSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
const User = mongoose.model('User', userSchema);
module.exports = User;