7fife-backend/models/user.model.js

134 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-03-07 13:01:44 +00:00
const mongoose = require("mongoose");
const constant = require('../util/constant');
const userSchema = new mongoose.Schema({
userName: {
type: String,
unique: true,
default: function () {
let username = 'guest' + Math.floor(Math.random() * 10000);
return username;
}
},
image: {
fileName: {
type: String,
},
fileAddress: {
type: String,
}
},
2024-03-11 12:29:54 +00:00
imageUrl:{
type: String
},
firstName:{
type: String,
// required: true
},
lastName:{
type: String,
// required: true
},
address:{
type: String,
},
mobileNo:{
type: Number
},
2024-03-07 13:01:44 +00:00
email: {
type: String,
required: true,
lowercase: true, // it will covert the email into the lower case and then store in the db,
minLength: 10, // anything less than 10 will fail
unique: true
},
password: {
type: String,
},
userTypes: {
type: String,
enum: [constant.userTypes.admin, constant.userTypes.customer],
default: constant.userTypes.customer
},
status: {
type: String,
2024-03-11 12:29:54 +00:00
enum: ['Active', 'Deactive'],
default: 'Active'
2024-03-07 13:01:44 +00:00
},
registerWith: {
type: String,
enum: [constant.registerWith.Email, constant.registerWith.google, constant.registerWith.facebook],
default: constant.registerWith.Email
},
playlist: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'PlayList'
},
favrioteSongs: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'Song',
},
mostPlayedSongs: {
type: Object,
ref: 'Song',
default: {}
},
following: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'Artist'
},
score: {
type: Number,
default: 0
},
otp : Number,
createdAt: {
// I want to default to a new date
type: Date,
immutable: true, // This will ensure the createdAt column is never updated but once in the start
default: () => {
return Date.now();
}
},
updatedAt: {
type: Date,
default: () => {
return Date.now();
}
}
})
userSchema.pre('deleteOne', async function (next) {
const userId = this.getFilter()['_id'];
const Artist = require('../models/artist.model');
const artist = await Artist.find({
followers: userId
})
const artistPromises = artist.map(artist => {
artist.followers.pull(userId);
return artist.save();
})
await Promise.all(artistPromises);
})
module.exports = mongoose.model("User", userSchema);