31 lines
639 B
JavaScript
31 lines
639 B
JavaScript
const mongoose = require("mongoose");
|
||
const Schema = mongoose.Schema;
|
||
|
||
const UserSchema = new Schema(
|
||
{
|
||
_id: {
|
||
type: Number,
|
||
required: true,
|
||
},
|
||
anixartId: {
|
||
type: Number,
|
||
required: true,
|
||
}
|
||
},
|
||
{ versionKey: false }
|
||
);
|
||
|
||
// Возвращает пользователя и создаёт его, если нет
|
||
UserSchema.statics.firstOrCreate = async function(tgUser, anixartId) {
|
||
let user = await this.findById(tgUser.id);
|
||
if (!user && !tgUser.is_bot) {
|
||
user = await new this({
|
||
_id: tgUser.id,
|
||
anixartId: anixartId
|
||
}).save();
|
||
}
|
||
|
||
return user;
|
||
};
|
||
|
||
module.exports = mongoose.model("users", UserSchema); |