'use strict';

/**
 * This microservice retrieves all the selfies from the database.
 * 
 * @author Sam Verschueren      <sam.verschueren@gmail.com>
 * @since  4 Aug. 2015
 */

// module dependencies
var db = require('dynongo'),
    moment = require('moment'),
    Q = require('q'),
    _ = require('lodash');

// connect with the database
db.connect();

var Selfie = db.table('Selfie');
 
var MIN_DATE = moment('2015-08-14').startOf('day'),
    ITEMS_PER_PAGE = 50;

/**
 * Main entrypoint of the service.
 * 
 * @param {object}  event       The data regarding the event.
 * @param {object}  context     The AWS Lambda execution context.
 */
exports.handler = function(event, context) {
    // Log the event for debugging purposes
    console.log(event);
    
    var since;
    
    Q.fcall(function() {
        if(event.since && event.since.length > 0) {
            // If the since query parameter is provided, set it
            since = moment(event.since);
        }
        
        // Retrieve all the selfies from the database
        return fetch(since);
    }).then(function(result) {
        // Send the result back to the client
        context.succeed(_.flatten(result));
    }).catch(function(err) {
        // Print the error if something went wrong
        console.error(err, err.stack);
        
        // Something went wrong
        context.fail(err);
    });
    
    function fetch(date) {
        return fetchHelper(date);
    };
    
    function fetchHelper(date, numberOfItems) {
        var start = (moment(date) || moment()).startOf('day');
        
        return Q.fcall(function() {
                var query;
            
                if(since) {
                    console.log({id: start.format('YYYY-MM-DD'), date: {$lt: since.format()}});
                    
                    query = Selfie.find({id: start.format('YYYY-MM-DD'), date: {$lt: since.format()}}, 'DateIndex');
                }
                else {
                    query = Selfie.find({id: start.format('YYYY-MM-DD')});
                }
            
                return query.select('name email date description image').sort(-1).limit(numberOfItems || ITEMS_PER_PAGE).exec()
            })
            .then(function(result) {
                console.log(result);
                
                if(result.length < ITEMS_PER_PAGE && start.diff(MIN_DATE, 'days') > 0) {
                    start.subtract(1, 'day');
                    
                    return fetchHelper(start, ITEMS_PER_PAGE-result.length)
                        .then(function(data) {
                            return result.concat(data);
                        });
                }
                
                return result;
            });
    }
};
@@ -1680,125 +1680,8 @@
 ) %7B%0A
-                    console.log(%7Bid: start.format('YYYY-MM-DD'), date: %7B$lt: since.format()%7D%7D);%0A                    %0A
     
@@ -2140,28 +2140,204 @@
     
-console.log(result);
+if(result.length === 0) %7B%0A                    start.subtract(1, 'day');%0A                %7D%0A                else %7B%0A                    since = moment(result%5Bresult.length-1%5D.date);%0A                %7D
 %0A   
@@ -2443,75 +2443,8 @@
 ) %7B%0A
-                    start.subtract(1, 'day');%0A                    %0A
     

We should not always subtract 1 day