<commit_before>/* eslint no-underscore-dangle:0, no-unused-vars:0 */

"use strict";
import CategoryDb from "./CategoryDb.js";
import { CategoryDocument } from "../actions/CategoryDocuments.js";
import StringUtil from "../../../../../common/src/util/StringUtil.js";
import TwitterDb from "../../twitter/TwitterDb.js";

export default class CategoriesApplicationQueries {

    static fetchAllCategories() {
        return new Promise((resolve, reject) => {
            CategoryDb.fetchAllCategoryDocuments().then((categoryDocs) => {
                let categories = categoryDocs.sort((first, second)=> {
                    return first.createdTime - second.createdTime;
                }).map((category) => {
                    return { "_id": category._id, "name": category.name };
                });
                resolve(categories);
            }).catch((error) => {
                reject(error);
            });
        });
    }

    static fetchSourceUrlsObj(categoryId) {
        return new Promise((resolve, reject) => {
            if(StringUtil.isEmptyString(categoryId)) {
                reject("category id can not be empty");
            }
            CategoryDb.fetchSourceConfigurationsByCategoryId(categoryId).then(rssConfigurations => {
                let sourceUrls = {};
                rssConfigurations.forEach((rssConfiguration) => {
                    if (!sourceUrls[rssConfiguration.sourceType]) {
                        sourceUrls[rssConfiguration.sourceType] = [];
                    }
                    sourceUrls[rssConfiguration.sourceType].push({ "_id": rssConfiguration._id, "url": rssConfiguration.url, "status": rssConfiguration.status });
                });
                this._sortSourceUrls(sourceUrls.rss);
                this._sortSourceUrls(sourceUrls.facebook);
                this._sortSourceUrls(sourceUrls.twitter);
                resolve(sourceUrls);
            }).catch(error => {
                reject(error);
            });
        });
    }

    static _sortSourceUrls(sources) {
        if(!sources) {
            return;
        }
        sources.sort((source1, source2) => {
            let lowerUrl1 = source1.url.toLowerCase();
            let lowerUrl2 = source2.url.toLowerCase();
            if(lowerUrl1 === lowerUrl2) {
                return 0;
            }
            if(lowerUrl1 < lowerUrl2) {
                return -1; //eslint-disable-line
            }
            if(lowerUrl1 > lowerUrl2) {
                return 1;
            }
        });
    }

    static addRssFeeds(sourceId, feeds) {
        const feedDocuments = CategoryDocument.getNewFeedDocuments(sourceId, feeds);
        return CategoryDb.createFeeds(feedDocuments);
    }
}<commit_msg>205 [Vik] source urls sorting in ui<commit_after>/* eslint no-underscore-dangle:0, no-unused-vars:0 */

"use strict";
import CategoryDb from "./CategoryDb.js";
import { CategoryDocument } from "../actions/CategoryDocuments.js";
import StringUtil from "../../../../../common/src/util/StringUtil.js";
import TwitterDb from "../../twitter/TwitterDb.js";

export default class CategoriesApplicationQueries {

    static fetchAllCategories() {
        return new Promise((resolve, reject) => {
            CategoryDb.fetchAllCategoryDocuments().then((categoryDocs) => {
                let categories = categoryDocs.sort((first, second)=> {
                    return first.createdTime - second.createdTime;
                }).map((category) => {
                    return { "_id": category._id, "name": category.name };
                });
                resolve(categories);
            }).catch((error) => {
                reject(error);
            });
        });
    }

    static fetchSourceUrlsObj(categoryId) {
        return new Promise((resolve, reject) => {
            if(StringUtil.isEmptyString(categoryId)) {
                reject("category id can not be empty");
            }
            CategoryDb.fetchSourceConfigurationsByCategoryId(categoryId).then(rssConfigurations => {
                let sourceUrls = {};
                rssConfigurations.forEach((rssConfiguration) => {
                    if (!sourceUrls[rssConfiguration.sourceType]) {
                        sourceUrls[rssConfiguration.sourceType] = [];
                    }
                    sourceUrls[rssConfiguration.sourceType].push({ "_id": rssConfiguration._id, "url": rssConfiguration.url, "status": rssConfiguration.status });
                });
                this._sortSourceUrls(sourceUrls.rss);
                this._sortSourceUrls(sourceUrls.facebook);
                this._sortSourceUrls(sourceUrls.twitter);
                resolve(sourceUrls);
            }).catch(error => {
                reject(error);
            });
        });
    }

    static _sortSourceUrls(sources) {
        if(!sources) {
            return;
        }
        sources.sort((source1, source2) => {
            let lowerUrl1 = source1.url.toLowerCase();
            let lowerUrl2 = source2.url.toLowerCase();
            if(lowerUrl1 === lowerUrl2) {
                return 0;
            }
            if(lowerUrl1 < lowerUrl2) {
                return -1; //eslint-disable-line
            }
            if(lowerUrl1 > lowerUrl2) {
                return 1;
            }
        });
    }

    static _sortSourceUrls(sources) {
        if(!sources) {
            return;
        }
        sources.sort((source1, source2) => {
            let lowerUrl1 = source1.url.toLowerCase();
            let lowerUrl2 = source2.url.toLowerCase();
            if(lowerUrl1 === lowerUrl2) {
                return 0;
            }
            if(lowerUrl1 < lowerUrl2) {
                return -1; //eslint-disable-line
            }
            if(lowerUrl1 > lowerUrl2) {
                return 1;
            }
        });
    }

    static addRssFeeds(sourceId, feeds) {
        const feedDocuments = CategoryDocument.getNewFeedDocuments(sourceId, feeds);
        return CategoryDb.createFeeds(feedDocuments);
    }
}
{'fraction_non_alphanumeric': 0.12160371106693174, 'fraction_numerical': 0.008614976805831677, 'mean_word_length': 1.7230491655390168, 'pattern_counts': {'":': 10, '<': 6, '<?xml version=': 0, '>': 27, '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': '2492030', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1528, 'n_words': 263}