1+ 'use strict' ;
2+
13const countries = require ( 'countries' ) ;
24const ImgurImage = require ( 'imgur' ) . ImgurImage ;
5+ const User = require ( 'users' ) . User ;
36const CourseFeedback = require ( '../models/courseFeedback' ) ;
7+ const CourseGroup = require ( '../models/courseGroup' ) ;
48const CourseParticipant = require ( '../models/courseParticipant' ) ;
59const _ = require ( 'lodash' ) ;
10+ const assert = require ( 'assert' ) ;
611
712exports . all = function * ( ) {
813
9- var group = this . locals . group = this . groupBySlug ;
14+ let group , teacher , participant , courseFeedback ;
1015
11- this . locals . title = "Отзыв\n" + group . title ;
16+ if ( ! this . user ) {
17+ this . throw ( 403 ) ;
18+ }
1219
13- this . locals . participant = this . participant ;
20+ if ( this . groupBySlug ) {
21+ group = this . groupBySlug ;
1422
15- this . locals . countries = countries . all ;
23+ participant = yield CourseParticipant . findOne ( {
24+ isActive : true ,
25+ group : group . _id ,
26+ user : this . user . _id
27+ } ) ;
28+
29+ if ( ! participant ) {
30+ this . throw ( 403 , "Оставлять отзыв могут только участники группы" ) ;
31+ }
1632
17- var courseFeedback = yield CourseFeedback . findOne ( {
18- participant : this . participant . _id
19- } ) . exec ( ) ;
33+ courseFeedback = yield CourseFeedback . findOne ( {
34+ participant : participant . _id
35+ } ) ;
2036
21- if ( ! courseFeedback ) {
37+ if ( courseFeedback ) {
38+ // if feedback exists already, there is another page to edit it
39+ this . redirect ( '/courses/feedback/edit/' + courseFeedback . number ) ;
40+ return ;
41+ }
42+
43+ teacher = yield User . findById ( group . teacher ) ;
2244 courseFeedback = new CourseFeedback ( {
45+ group : group . _id ,
46+ participant : participant . _id ,
2347 recommend : true ,
2448 isPublic : true ,
25- country : this . participant . country ,
26- photo : this . participant . photo ,
27- aboutLink : this . participant . aboutLink ,
28- city : this . participant . city ,
29- occupation : this . participant . occupation ,
30- userCache : this . user . id ,
31- teacherCache : group . teacher
49+ country : participant . country ,
50+ photo : participant . photo ,
51+ aboutLink : participant . aboutLink ,
52+ city : participant . city ,
53+ occupation : participant . occupation ,
54+ userCache : this . user . _id ,
55+ teacherCache : teacher . id
56+ } ) ;
57+
58+
59+ } else if ( this . params . feedbackNumber ) {
60+
61+ courseFeedback = yield CourseFeedback . findOne ( {
62+ number : this . params . feedbackNumber
3263 } ) ;
64+
65+ if ( ! courseFeedback ) {
66+ this . throw ( 404 ) ;
67+ }
68+
69+ participant = yield CourseParticipant . findById ( courseFeedback . participant ) ;
70+
71+ group = yield CourseGroup . findById ( courseFeedback . group ) ;
72+ teacher = yield User . findById ( group . teacher ) ;
73+
74+ if ( ! this . user . _id . equals ( participant . _id ) &&
75+ ! this . user . _id . equals ( teacher . _id ) &&
76+ ! this . isAdmin
77+ ) {
78+ this . throw ( 403 , 'Не хватает прав' ) ;
79+ }
80+
81+
3382 }
3483
84+ this . locals . title = "Отзыв\n" + group . title ;
85+
86+ assert ( participant ) ;
87+ assert ( teacher ) ;
88+ assert ( courseFeedback ) ;
89+ assert ( group ) ;
90+
91+ this . locals . participant = participant ;
92+ this . locals . group = group ;
93+ this . locals . teacher = teacher ;
94+ this . locals . courseFeedback = courseFeedback ;
95+ this . locals . countries = countries . all ;
96+
3597 if ( this . method == 'POST' ) {
36- var feedbackData = _ . pick ( this . request . body ,
98+ let feedbackData = _ . pick ( this . request . body ,
3799 'stars content country city isPublic recommend aboutLink occupation' . split ( ' ' )
38100 ) ;
39101
40- feedbackData . participant = this . participant . _id ;
41- feedbackData . group = group . _id ;
42102 feedbackData . recommend = Boolean ( + feedbackData . recommend ) ;
43103 feedbackData . isPublic = Boolean ( + feedbackData . isPublic ) ;
44104
@@ -63,7 +123,7 @@ exports.all = function*() {
63123
64124 this . body = this . render ( 'feedback/edit' , {
65125 errors : errors ,
66- form : courseFeedback
126+ courseFeedback : courseFeedback
67127 } ) ;
68128
69129 return ;
@@ -88,8 +148,6 @@ exports.all = function*() {
88148
89149 } else if ( this . method == 'GET' ) {
90150
91- this . locals . form = courseFeedback ;
92-
93151 this . body = this . render ( 'feedback/edit' ) ;
94152 }
95153
0 commit comments