'use strict';

var q = require("q");
var dateUtil = require('../../utils/UtilDate');
var logger = require('../../utils/Logger').loggers.get('db');

module.exports.preProcess = function (field, document) {
    var deferred = q.defer();

    if (document[field.name]) {
        var saveValue = dateUtil.convertToDate(document[field.name]);
        if (saveValue) {
            document[field.name] = saveValue;
            deferred.resolve(document);
        } else {
            var errMsg = field.componentName + " field named " + field.name +
                " has invalid value \"" + saveValue + "\"";
            logger.error("Preprocessing Failure : " + errMsg);
            deferred.reject(errMsg);
        }
    } else {
        deferred.resolve(document);
    }
    return deferred.promise;
};
