Permalink
Browse files

Implement deleteAllByUser on BadgeInstance model

  • Loading branch information...
1 parent 81eea59 commit f4aa11f6d0de5461ef92147e701d0e6a5f4526f7 @brianloveswords brianloveswords committed Nov 11, 2012
Showing with 48 additions and 0 deletions.
  1. +21 −0 models/badge-instance.js
  2. +27 −0 tests/badge-instance-model.test.js
View
@@ -1,3 +1,4 @@
+var async = require('async');
var db = require('./');
var env = require('../lib/environment');
var mongoose = require('mongoose');
@@ -153,4 +154,24 @@ BadgeInstance.markAllAsSeen = function markAllAsSeen(email, callback) {
};
BadgeInstance.markAllAsRead = BadgeInstance.markAllAsSeen;
+/**
+ * Remove all badge instances assigned to a user
+ *
+ * @param {String} email
+ */
+
+BadgeInstance.deleteAllByUser = function deleteAllByUser(email, callback) {
+ function remover(i, callback) { return i.remove(callback) }
+ var query = { user: email };
+ BadgeInstance.find(query, function (err, instances) {
+ if (err) return callback(err);
+ async.map(instances, remover, function (err) {
+ if (err) return callback(err);
+ return callback(null, instances);
+ });
+ });
+};
+
+
+
module.exports = BadgeInstance;
@@ -32,6 +32,20 @@ test.applyFixtures({
assertion: '{ "assertion" : "yep" }',
seen: false
}),
+ 'delete-instance1': new BadgeInstance({
+ user: 'brian-delete@example.org',
+ hash: 'hash',
+ badge: 'link-hyper-advanced',
+ assertion: '{ "assertion" : "yep" }',
+ seen: false
+ }),
+ 'delete-instance2': new BadgeInstance({
+ user: 'brian-delete@example.org',
+ hash: 'otherhash',
+ badge: 'link-turbo-advanced',
+ assertion: '{ "assertion" : "yep" }',
+ seen: false
+ }),
}, function (fixtures) {
test('BadgeInstance#save: test defaults', function (t) {
var currentish = Date.now() - 1;
@@ -80,6 +94,19 @@ test.applyFixtures({
});
+ test('BadgeInstance.deleteAllByUser', function (t) {
+ var instance1 = fixtures['delete-instance1'];
+ var instance2 = fixtures['delete-instance2'];
+ var email = instance1.user;
+ BadgeInstance.deleteAllByUser(email, function (err) {
+ BadgeInstance.find({ user: email }, function (err, instances) {
+ t.same(instances.length, 0, 'should not have any instances');
+ t.end();
+ });
+ });
+ });
+
+
// necessary to stop the test runner
test('shutting down #', function (t) {
db.close(); t.end();

0 comments on commit f4aa11f

Please sign in to comment.