Exemplo n.º 1
0
relationship.prototype.updateItem = function(item, data) {

	if (!(this.path in data)) {
		return;
	}

	if (item.populated(this.path)) {
		throw new Error('fieldTypes.relationship.updateItem() Error - You cannot update populated relationships.');
	}

	if (this.many) {

		var arr = item.get(this.path),
			_old = arr.map(function(i) { return String(i); }),
			_new = data[this.path];

		if (!utils.isArray(_new)) {
			_new = String(_new || '').split(',');
		}

		_new = _.compact(_new);

		// remove ids
		_.difference(_old, _new).forEach(function(val) {
			arr.pull(val);
		});
		// add new ids
		_.difference(_new, _old).forEach(function(val) {
			arr.push(val);
		});

	} else {
		if (data[this.path]) {
			if (data[this.path] !== item.get(this.path)) {
				item.set(this.path, data[this.path]);
			}
		} else if (item.get(this.path)) {
			item.set(this.path, null);
		}
	}

};
Exemplo n.º 2
0
List.prototype.getUniqueValue = function(path, generator, limit, callback) {

	var model = this.model,
		count = 0,
		value;

	if (utils.isFunction(limit)) {
		callback = limit;
		limit = 10;
	}

	if (utils.isArray(generator)) {

		var fn = generator[0],
			args = generator.slice(1);

		generator = function() {
			return fn.apply(this, args);
		};

	}

	var check = function() {

		if (count++ > 10) {
			return callback(undefined, undefined);
		}

		value = generator();

		model.count().where(path, value).exec(function(err, matches) {
			if (err) return callback(err);
			if (matches) return check();
			callback(undefined, value);
		});

	};

	check();

};
Exemplo n.º 3
0
	this.render(locals, function(err, email) {

		callback = ('function' === typeof callback) ? callback : function() {};

		if (err) {
			return callback(err);
		}

		if (!utils.isObject(options)) {
			return callback({
				from: 'Email.send',
				key: 'invalid options',
				message: 'options object is required'
			});
		}

		if ('string' === typeof options.from) {
			options.fromName = options.from;
			options.fromEmail = options.from;
		} else if (utils.isObject(options.from)) {
			options.fromName = utils.isObject(options.from.name) ? options.from.name.full : options.from.name;
			options.fromEmail = options.from.email;
		}

		if (!options.fromName || !options.fromEmail) {
			return callback({
				from: 'Email.send',
				key: 'invalid options',
				message: 'options.fromName and options.fromEmail are required'
			});
		}

		if (!options.mandrill) {
			if (!landmark.get('mandrill api key'))
				return callback({
					from: 'Email.send',
					key: 'missing api key',
					message: 'You must either provide a Mandrill API Instance or set the mandrill api key before sending email.'
				});
			options.mandrill = new mandrillapi.Mandrill(landmark.get('mandrill api key'));
		}

		options.tags = utils.isArray(options.tags) ? options.tags : [];
		options.tags.push('sent:' + moment().format('YYYY-MM-DD'));
		options.tags.push(this.templateName);

		if (landmark.get('env') === 'development') {
			options.tags.push('development');
		}

		var recipients = [],
			mergeVars = [];

		options.to = Array.isArray(options.to) ? options.to : [options.to];

		for (var i = 0; i < options.to.length; i++) {

			if ('string' === typeof options.to[i]) {
				options.to[i] = { email: options.to[i] };
			} else if ('object' === typeof options.to[i]) {
				if (!options.to[i].email) {
					return callback({
						from: 'Email.send',
						key: 'invalid recipient',
						message: 'Recipient ' + (i + 1) + ' does not have a valid email address.'
					});
				}
			} else {
				return callback({
					from: 'Email.send',
					key: 'invalid recipient',
					message: 'Recipient ' + (i + 1) + ' is not a string or an object.'
				});
			}

			var recipient = { email: options.to[i].email },
				vars = [{ name: 'email', content: recipient.email }];

			if ('string' === typeof options.to[i].name) {
				recipient.name = options.to[i].name;
				vars.push({ name: 'name', content: options.to[i].name });
			} else if ('object' === typeof options.to[i].name) {
				recipient.name = options.to[i].name.full || '';
				vars.push({ name: 'name', content: options.to[i].name.full || '' });
				vars.push({ name: 'first_name', content: options.to[i].name.first || '' });
				vars.push({ name: 'last_name', content: options.to[i].name.last || '' });
			}

			recipients.push(recipient);

			mergeVars.push({
				rcpt: recipient.email,
				vars: vars
			});
		}

		var onSuccess = function(info) {
			callback(null, info);
		};

		var onFail = function(info) {
			callback({
				from: 'Email.send',
				key: 'send error',
				message: 'Mandrill encountered an error and did not send the emails.',
				info: info
			});
		};

		var message = {
			html: email.html,
			subject: email.subject,
			from_name: options.fromName,
			from_email: options.fromEmail,
			tags: options.tags,
			attachments: options.attachments,
			to: recipients,
			merge_vars: mergeVars,
			async: true
		};

		_.defaults(message, options.mandrillOptions);
		_.defaults(message, Email.defaults.mandrill);

		return process.nextTick(function() {
			options.mandrill.messages.send({ message: message }, onSuccess, onFail);
		});

	}.bind(this));