@@ -8,6 +8,7 @@ var gutil = require('gulp-util');
88const path = require ( 'path' ) ;
99const CourseGroup = require ( '../models/courseGroup' ) ;
1010const CourseParticipant = require ( '../models/courseParticipant' ) ;
11+ const User = require ( 'users' ) . User ;
1112const mailer = require ( 'mailer' ) ;
1213const config = require ( 'config' ) ;
1314
@@ -38,7 +39,7 @@ module.exports = function() {
3839
3940 var participants = yield CourseParticipant . find ( {
4041 isActive : true ,
41- group : group . _id
42+ group : group . _id
4243 } ) . populate ( 'user' ) . exec ( ) ;
4344
4445 var recipients = participants
@@ -66,21 +67,33 @@ module.exports = function() {
6667 recipientsToSend = [ { email : args . test } ] ;
6768 }
6869
69- if ( recipientsToSend . length ) {
70+ var usersByEmail = { } ;
71+ for ( var i = 0 ; i < recipientsToSend . length ; i ++ ) {
72+ var recipient = recipientsToSend [ i ] ;
73+ var user = yield User . findOne ( { email : recipient . email } ) . exec ( ) ;
74+ if ( ! user ) {
75+ throw new Error ( "No user for email: " + recipient . email ) ;
76+ }
77+ usersByEmail [ recipient . email ] = user ;
78+ }
79+
80+ for ( var i = 0 ; i < recipientsToSend . length ; i ++ ) {
81+
82+ var recipient = recipientsToSend [ i ] ;
7083 yield * mailer . send ( {
7184 from : 'informer' ,
7285 templatePath : args . templatePath ,
73- to : recipientsToSend ,
86+ to : [ recipient ] ,
87+ user : usersByEmail [ recipient . email ] ,
88+ group : group ,
7489 subject : args . subject ,
75- label : args . test ? undefined : label ,
76- headers : {
77- Precedence : 'bulk' ,
78- 'List-ID' : `<${ group . slug } .group.list-id.javascript.ru>`
79- }
90+ label : args . test ? undefined : label
8091 } ) ;
8192
8293 gutil . log ( "Sent letter to " + JSON . stringify ( recipientsToSend ) ) ;
83- } else {
94+ }
95+
96+ if ( ! recipients . length ) {
8497 gutil . log ( `No recipients (was ${ recipients . length } before label exclusion)` ) ;
8598 }
8699
0 commit comments