'use strict';

var Promise      = require('bluebird');
var Handlebars   = require('handlebars');
var Notification = require('../../api/models/Notification');

var Users = require('../../api/collections/Users');
var users = new Users();

module.exports = function(router, resources) {
	resources.actions.notify = function(conn, project, options) {
		var roleIds = options.role_ids || [];

		// get all project assignments
		return Promise.all(Object.keys(project.assignments.data).map(function(userId) {
			var assignment = project.assignments.data[userId];

			// only notify specified roles
			if(roleIds.indexOf(assignment.role_id) === -1)
				return;

			// create a notification
			return Notification.create(conn, {
				user_id: userId,
				subject: options.subject,

				// TODO: first pull user from db to get name, etc
				content: Handlebars.compile(options.template || '')({ project: project, cycle: project.cycle })
			}, true)

			.then(function(notification){
				return users.get(conn, notification.user_id, true)

				// email recovery token to user
				.then(function(user){
					return resources.mail({
						to: user.email,
						subject: notification.subject,
						html: notification.content
					});
				});
			});
		}));
	};
};

@@ -431,19 +431,22 @@
 Promise.
-all
+settle
 (Object.
@@ -1027,45 +1027,8 @@
 rue)
-%0A%0A%09%09%09%09// email recovery token to user
 %0A%09%09%09
@@ -1054,20 +1054,50 @@
 r)%7B%0A
+%0A
 %09%09%09%09%09
-return 
+// send mail in the background%0A%09%09%09%09%09
 reso
@@ -1210,16 +1210,17 @@
 %09%09%09%09%7D);%0A
+%0A
 %09%09%09%09%7D);%0A

Send triggered emails in the background