'use strict';

// external deps 
var ObjectId = require('mongodb').ObjectId;

// internal deps
require('mongodb-toolkit');
var BaseManager = require('module-toolkit').BaseManager;
var BateeqModels = require('bateeq-models');
var map = BateeqModels.map;

var Storage = BateeqModels.master.Storage;


module.exports = class StorageManager extends BaseManager {
    constructor(db, user) {
        super(db, user);
        this.collection = this.db.use(map.master.Storage);
    }

    _getQuery(paging) {
        var basicFilter = {
            _deleted: false
        }, keywordFilter = {};

        var query = {};

        if (paging.keyword) {
            var regex = new RegExp(paging.keyword, "i");
            var filterCode = {
                'code': {
                    '$regex': regex
                }
            };
            var filterName = {
                'name': {
                    '$regex': regex
                }
            };

            keywordFilter = {
                '$or': [filterCode, filterName]
            };
        }
        query = { '$and': [basicFilter, paging.filter, keywordFilter] };
        return query;
    }

    _validate(storage) {
        var errors = {};
        return new Promise((resolve, reject) => {
            var valid = new Storage(storage);
            // 1. begin: Declare promises.
            var getStorage = this.collection.singleOrDefault({
                "$and": [{
                    _id: {
                        '$ne': new ObjectId(valid._id)
                    }
                }, {
                    code: valid.code
                }]
            });
            // 1. end: Declare promises.

            // 2. begin: Validation.
            Promise.all([getStorage])
                .then(results => {
                    var _storage = results[0];

                    if (!valid.code || valid.code == '')
                        errors["code"] = "code is required";
                    else if (_storage) {
                        errors["code"] = "code already exists";
                    }

                    if (!valid.name || valid.name == '')
                        errors["name"] = "name is required";

                    // 2c. begin: check if data has any error, reject if it has.
                    for (var prop in errors) {
                        var ValidationError = require('module-toolkit').ValidationError;
                        reject(new ValidationError('data does not pass validation', errors));
                    }

                    valid.stamp(this.user.username, 'manager');
                    resolve(valid);
                })
                .catch(e => {
                    reject(e);
                })
        });
    }
};
@@ -1149,24 +1149,449 @@
 ery;%0A    %7D%0A%0A
+    getByCode(_code) %7B%0A        return new Promise((resolve, reject) =%3E %7B%0A            var query = %7B%0A                _deleted: false,%0A                code: _code%0A            %7D;%0A            this.collection.where(query).execute()%0A                .then((results) =%3E %7B%0A                    resolve(results.data);%0A                %7D)%0A                .catch(e =%3E %7B%0A                    reject(e);%0A                %7D);%0A        %7D);%0A    %7D%0A%0A
     _validat

add getByCode in storage-manager