Commit 195ae1aa authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement Subscriptions class.

Performs queries to:

- retrieve all targets a user has subscribed to.
- retrieve all users who have subscribed to a target.
parent 6c3333d7
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -162,3 +162,21 @@ INSERT INTO updates SET `user_id` = 1, `comment_id` = 1, `action` = 'update', `c
INSERT INTO updates SET `user_id` = 1, `comment_id` = 6,  `action` = 'delete', `created_at` = '2011-10-05 00:00:00';
INSERT INTO updates SET `user_id` = 5, `comment_id` = 9,  `action` = 'delete', `created_at` = '2012-01-08 00:00:00';
INSERT INTO updates SET `user_id` = 6, `comment_id` = 25, `action` = 'delete', `created_at` = '2012-05-20 00:00:00';


-- subscriptions

-- subscribe renku to Ext & Ext.Img
INSERT INTO subscriptions SET `user_id` = 1, `target_id` = 1, `created_at` = '2011-01-01 00:00:00';
INSERT INTO subscriptions SET `user_id` = 1, `target_id` = 8, `created_at` = '2011-01-01 00:00:00';

-- subscribe john to Ext & String#replace
INSERT INTO subscriptions SET `user_id` = 2, `target_id` = 1, `created_at` = '2011-01-01 00:00:00';
INSERT INTO subscriptions SET `user_id` = 2, `target_id` = 6, `created_at` = '2011-01-01 00:00:00';

-- subscribe mary to Ext
INSERT INTO subscriptions SET `user_id` = 3, `target_id` = 1, `created_at` = '2011-01-01 00:00:00';

-- subscribe jack to touch-2 Ext
INSERT INTO subscriptions SET `user_id` = 4, `target_id` = 14, `created_at` = '2011-01-01 00:00:00';
+53 −0
Original line number Diff line number Diff line
/**
 * Represents a Subscriptions table.
 *
 * @constructor
 * Initializes Subscriptions with a database connection and a target domain.
 *
 * @param {DbFacade} db Instance of DbFacade.
 * @param {String} domain The comments domain within which to work.
 */
function Subscriptions(db, domain) {
    this.db = db;
    this.domain = domain;
}

Subscriptions.prototype = {
    /**
     * Finds all targets a user has subscribed to.
     *
     * @param {Number} user_id ID of the user.
     * @param {Function} callback Called with the result.
     * @param {Error} callback.err The error object.
     * @param {Object[]} callback.targets An array of targets.
     */
    findTargetsByUser: function(user_id, callback) {
        var sql = [
            'SELECT targets.*',
            'FROM subscriptions JOIN targets ON subscriptions.target_id = targets.id',
            'WHERE domain = ? AND user_id = ?'
        ];

        this.db.query(sql, [this.domain, user_id], callback);
    },

    /**
     * Finds all users who have subscribed to a particular target.
     *
     * @param {Number} target_id ID of the target.
     * @param {Function} callback Called with the result.
     * @param {Error} callback.err The error object.
     * @param {Object[]} callback.users An array of users.
     */
    findUsersByTarget: function(target_id, callback) {
        var sql = [
            'SELECT users.*',
            'FROM subscriptions JOIN users ON subscriptions.user_id = users.id',
            'WHERE target_id = ?'
        ];

        this.db.query(sql, [target_id], callback);
    }
};

module.exports = Subscriptions;
+67 −0
Original line number Diff line number Diff line
describe("Subscriptions", function() {
    var Subscriptions = require("./subscriptions");
    var DbFacade = require('./db_facade');
    var connection;
    var subscriptions;

    beforeEach(function() {
        connection = new DbFacade({
            host: 'localhost',
            user: '',
            password: '',
            database: 'comments_test'
        });

        subscriptions = new Subscriptions(connection, "ext-js-4");
    });

    afterEach(function() {
        connection.end();
    });

    it("#findTargetsByUser returns empty array when user has no subscriptions in current domain", function(done) {
        subscriptions.findTargetsByUser(4, function(err, subs) {
            expect(subs.length).toEqual(0);
            done();
        });
    });

    it("#findTargetsByUser returns all subscription targets of a user", function(done) {
        subscriptions.findTargetsByUser(1, function(err, subs) {
            expect(subs.length).toEqual(2);
            done();
        });
    });

    it("#findTargetsByUser returns subscription target type, cls & member fields", function(done) {
        subscriptions.findTargetsByUser(3, function(err, subs) {
            expect(subs[0].type).toEqual("class");
            expect(subs[0].cls).toEqual("Ext");
            expect(subs[0].member).toEqual("");
            done();
        });
    });

    it("#findUsersByTarget returns empty array when no subscriptions to a target", function(done) {
        subscriptions.findUsersByTarget(2, function(err, subs) {
            expect(subs.length).toEqual(0);
            done();
        });
    });

    it("#findUsersByTarget returns all users who have subscribed to a particular target", function(done) {
        subscriptions.findUsersByTarget(1, function(err, subs) {
            expect(subs.length).toEqual(3);
            done();
        });
    });

    it("#findUsersByTarget returns username and email fields", function(done) {
        subscriptions.findUsersByTarget(8, function(err, subs) {
            expect(subs[0].username).toEqual("renku");
            expect(subs[0].email).toEqual("renku@example.com");
            done();
        });
    });

});