-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotation.js
More file actions
97 lines (88 loc) · 3.59 KB
/
notation.js
File metadata and controls
97 lines (88 loc) · 3.59 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const Sequelize = require("sequelize");
const Promise = require("bluebird");
module.exports = (sequelize, DataTypes) => {
let Notation = sequelize.define('Notation', {
// no need for more ; as notes are usually between 0 and 5
note: {
type: DataTypes.DECIMAL(3, 2),
allowNull: false
}
}, {
// https://sequelize.org/master/manual/models-definition.html#configuration
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false
});
// common function for hooks
function exercise_metrics_hookFct(kind) {
return function (notation, options) {
// find the Exercise_Metrics related to this exercise with the AVG computed
return Promise.all([
// Find exercise metrics for given exercise
notation
.sequelize
.models
.Exercise_Metrics
.findAll({
attributes: ["id", "vote_count"],
where: {
exercise_id: notation.exercise_id
},
transaction: options.transaction
}),
// Find the new avg vote score
notation
.sequelize
.models
.Notation
.findAll({
attributes: [
[Sequelize.fn("AVG", Sequelize.col("note")), "vote_score"]
],
where: {
exercise_id: notation.exercise_id
},
group: ["exercise_id"],
transaction: options.transaction
})
]).then(
([[exercise_metrics], [result_in_db] ]) => {
// to handle both cases : the first notation created / other cases
const computed_metadata = {
vote_count: (kind === "insert")
? exercise_metrics.get("vote_count") + 1
: exercise_metrics.get("vote_count"),
vote_score: (result_in_db !== undefined)
? result_in_db.get("vote_score")
// by default, only one note was inserted for this exercise
// it should never happen but who knows what could happen ...
: /* istanbul ignore next */ notation.note
};
return notation
.sequelize
.models
.Exercise_Metrics
.update({
vote_count: computed_metadata.vote_count,
avg_vote_score: computed_metadata.vote_score
}, {
where: {id: exercise_metrics.id},
transaction: options.transaction
});
});
}
}
// after inserting / updating a row , we must update the related Exercise_Metrics
// warning for seeding I must use that : https://github.com/sequelize/sequelize/issues/5053
Notation.addHook(
"afterCreate",
"exercise_metrics_created",
exercise_metrics_hookFct("insert")
);
Notation.addHook(
"afterUpdate",
"exercise_metrics_updated",
exercise_metrics_hookFct("update")
);
return Notation;
};