This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
qoverflow/server/db/models/Answer.js
“spicecat” bda650f728 update
2022-08-18 15:13:29 -04:00

27 lines
1.1 KiB
JavaScript

const mongoose = require('mongoose');
const calculateAnswerBadges = require('../../utils/badges/answerBadges');
const User = require('./User');
const Answer = mongoose.Schema(
{
question_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Question' },
answer_id: { type: String, required: true },
creator: { type: String, required: true },
text: { type: String, required: true },
upvotes: { type: Number, required: true, default: 0 },
downvotes: { type: Number, required: true, default: 0 },
accepted: { type: Boolean, required: true, default: false },
createdAt: { type: Date, required: true },
lastCommentFetch: { type: Date, default: new Date(0) },
},
{ timestamps: { createdAt: false, updatedAt: true } }
);
Answer.post('findOneAndUpdate', (doc) => {
if (doc) {
const badges = calculateAnswerBadges(doc.upvotes - doc.downvotes);
User.findOneAndUpdate({ username: doc.creator }, { $addToSet: { tags: { $each: badges } } });
}
});
module.exports = mongoose.model('Answer', Answer);