<commit_before><commit_msg>adding color type manager<commit_after>"use strict";

var ObjectId = require("mongodb").ObjectId;

require("mongodb-toolkit");

var DLModels = require("dl-models");
var map = DLModels.map;
var ColorType = DLModels.master.ColorType;
var BaseManager = require("module-toolkit").BaseManager;
var i18n = require("dl-i18n");
var generateCode = require("../../utils/code-generator");

module.exports = class ColorTypeManager extends BaseManager {

    constructor(db, user) {
        super(db, user);
        this.collection = this.db.use(map.master.collection.ColorType);
    }

    _getQuery(paging) {
        var _default = {
                _deleted: false
            },
            pagingFilter = paging.filter || {},
            keywordFilter = {},
            query = {};

        if (paging.keyword) {
            var regex = new RegExp(paging.keyword, "i");
            var codeFilter = {
                "code": {
                    "$regex": regex
                }
            };
            var nameFilter = {
                "name": {
                    "$regex": regex
                }
            };
            keywordFilter["$or"] = [codeFilter, nameFilter];
        }
        query["$and"] = [_default, keywordFilter, pagingFilter];
        return query;
    }

    _validate(color) {
        var errors = {};
        var valid = color;
        // 1. begin: Declare promises.
        var getcolorPromise = this.collection.singleOrDefault({
            _id: {
                '$ne': new ObjectId(valid._id)
            },
            code: valid.code
        });

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

                if (!valid.code || valid.code == "")
                    errors["code"] = i18n.__("ColorType.code.isRequired:%s is required", i18n.__("ColorType.code._:Code"));//"code Jenis color tidak boleh kosong";
                else if (_color) {
                    errors["code"] = i18n.__("ColorType.code.isExists:%s is already exists", i18n.__("ColorType.code._:Code")); 
                }

                if (!valid.name || valid.name == "")
                    errors["name"] = i18n.__("ColorType.name.isRequired:%s is required", i18n.__("ColorType.name._:Name")); //"Nama Jenis color tidak boleh kosong";

                if (Object.getOwnPropertyNames(errors).length > 0) {
                    var ValidationError = require("module-toolkit").ValidationError;
                    return Promise.reject(new ValidationError("data does not pass validation", errors));
                }

                valid = new ColorType(valid);
                valid.stamp(this.user.username, "manager");
                return Promise.resolve(valid);
            });
    }

    
    _createIndexes() {
        var dateIndex = {
            name: `ix_${map.master.collection.ColorType}__updatedDate`,
            key: {
                _updatedDate: -1
            }
        };

        var codeIndex = {
            name: `ix_${map.master.collection.ColorType}_code`,
            key: {
                code: 1
            },
            unique: true
        };

        return this.collection.createIndexes([dateIndex, codeIndex]);
    }

}

{'fraction_non_alphanumeric': 0.13121212121212122, 'fraction_numerical': 0.006666666666666667, 'mean_word_length': 1.6492776886035314, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4217231', 'n_tokens_mistral': 926, 'n_tokens_neox': 871, 'n_words': 178}