<commit_before>'use strict'

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

require("mongodb-toolkit");

var DLModels = require('dl-models');
var map = DLModels.map;
var Currency = DLModels.master.Currency;
var BaseManager = require('../base-manager');
var i18n = require('dl-i18n');

module.exports = class CurrencyManager extends BaseManager {

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

    _getQuery(paging) {
        var deleted = {
            _deleted: false
        };
        var query = paging.keyword ? {
            '$and': [deleted]
        } : deleted;

        if (paging.keyword) {
            var regex = new RegExp(paging.keyword, "i");
            var filterCode = {
                'code': {
                    '$regex': regex
                }
            };
            var filterSymbol = {
                'symbol': {
                    '$regex': regex
                }
            };
            var filterDescription = {
                'description': {
                    '$regex': regex
                }
            };
            var $or = {
                '$or': [filterCode, filterSymbol, filterDescription]
            };

            query['$and'].push($or);
        }
        return query;
    }

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

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

                    if (!valid.code || valid.code == '')
                        errors["code"] = i18n.__("Currency.code.isRequired:%s is required", i18n.__("Currency.code._:Code")); //"Kode mata uang Tidak Boleh Kosong";
                    else if (_currency) {
                        errors["code"] = i18n.__("Currency.code.isExists:%s is already exists", i18n.__("Currency.code._:Code")); //"Kode mata uang sudah terdaftar";
                    }

                    if (!valid.symbol || valid.symbol == '')
                        errors["symbol"] = i18n.__("Currency.symbol.isRequired:%s is required", i18n.__("Currency.symbol._:Symbol")); //"Simbol mata uang Tidak Boleh Kosong";

                    if (!valid.rate || valid.rate == 0)
                        errors["rate"] = i18n.__("Currency.rate.isRequired:%s is required", i18n.__("Currency.rate._:Rate")); //"Rate mata uang Tidak Boleh Kosong";

                    if (Object.getOwnPropertyNames(errors).length > 0) {
                        var ValidationError = require('../../validation-error');
                        reject(new ValidationError('data does not pass validation', errors));
                    }

                    valid = new Currency(currency);
                    valid.stamp(this.user.username, 'manager');
                    resolve(valid);
                })
                .catch(e => {
                    reject(e);
                })
        });
    }

    _createIndexes() {
        var dateIndex = {
            name: `ix_${map.master.collection.Currency}__createDate`,
            key: {
                _createdDate: -1
            }
        }

        var codeIndex = {
            name: `ix_${map.master.collection.Currency}_code`,
            key: {
                code: 1
            },
            unique: true<commit_msg>make _createIndexes in module currency-manager<commit_after>'use strict'

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

require("mongodb-toolkit");

var DLModels = require('dl-models');
var map = DLModels.map;
var Currency = DLModels.master.Currency;
var BaseManager = require('../base-manager');
var i18n = require('dl-i18n');

module.exports = class CurrencyManager extends BaseManager {

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

    _getQuery(paging) {
        var deleted = {
            _deleted: false
        };
        var query = paging.keyword ? {
            '$and': [deleted]
        } : deleted;

        if (paging.keyword) {
            var regex = new RegExp(paging.keyword, "i");
            var filterCode = {
                'code': {
                    '$regex': regex
                }
            };
            var filterSymbol = {
                'symbol': {
                    '$regex': regex
                }
            };
            var filterDescription = {
                'description': {
                    '$regex': regex
                }
            };
            var $or = {
                '$or': [filterCode, filterSymbol, filterDescription]
            };

            query['$and'].push($or);
        }
        return query;
    }

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

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

                    if (!valid.code || valid.code == '')
                        errors["code"] = i18n.__("Currency.code.isRequired:%s is required", i18n.__("Currency.code._:Code")); //"Kode mata uang Tidak Boleh Kosong";
                    else if (_currency) {
                        errors["code"] = i18n.__("Currency.code.isExists:%s is already exists", i18n.__("Currency.code._:Code")); //"Kode mata uang sudah terdaftar";
                    }

                    if (!valid.symbol || valid.symbol == '')
                        errors["symbol"] = i18n.__("Currency.symbol.isRequired:%s is required", i18n.__("Currency.symbol._:Symbol")); //"Simbol mata uang Tidak Boleh Kosong";

                    if (!valid.rate || valid.rate == 0)
                        errors["rate"] = i18n.__("Currency.rate.isRequired:%s is required", i18n.__("Currency.rate._:Rate")); //"Rate mata uang Tidak Boleh Kosong";

                    if (Object.getOwnPropertyNames(errors).length > 0) {
                        var ValidationError = require('../../validation-error');
                        reject(new ValidationError('data does not pass validation', errors));
                    }

                    valid = new Currency(currency);
                    valid.stamp(this.user.username, 'manager');
                    resolve(valid);
                })
                .catch(e => {
                    reject(e);
                })
        });
    }

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

        var codeIndex = {
            name: `ix_${map.master.collection.Currency}_code`,
            key: {
                code: 1
            },
            unique: true
{'fraction_non_alphanumeric': 0.12919727177334733, 'fraction_numerical': 0.00708289611752361, 'mean_word_length': 1.321863580998782, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, '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': '4809966', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1943, 'n_words': 405}