-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreview.js
More file actions
67 lines (60 loc) · 1.79 KB
/
Copy pathreview.js
File metadata and controls
67 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
import { StatusCodes } from 'http-status-codes';
import { badRequestError } from '../errors/index.js';
const reviewSchema = new Schema({
rating: {
type: Number,
min: [1, 'Minimum rating is 1'],
max: [5, 'Maximum rating is 5'],
required: [true, 'Please provide rating'],
},
title: {
type: String,
required: [true, 'Please provide review title'],
maxlength: 100,
trim: true,
},
comment: {
type: String,
required: [true, 'Please enter a comment'],
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product',
required: true
}
},{ timestamps: true});
reviewSchema.index({product: 1, user: 1}, {unique: true});
reviewSchema.statics.calcAvgRating = async function(productID) {
const result = await this.aggregate([
{
$match: {product: productID},
},
{
$group: {
_id: null,
avgRating: {$avg: '$rating'},
totalRev: {$sum: 1}
}
},
]);
try {
await this.model('Product').findOneAndUpdate({_id: productID}, {avgRating: Math.round(result[0]?.avgRating || 0), noOfReviews: result[0]?.totalRev || 0})
} catch (err) {
console.log(err);
throw new badRequestError("Internal Server Error")
}
}
reviewSchema.post('save', async function() {
await this.constructor.calcAvgRating(this.product)
})
reviewSchema.post('remove', async function() {
await this.constructor.calcAvgRating(this.product)
})
export default mongoose.model('Review', reviewSchema);