UNPKG

@brontosaurus/db

Version:
752 lines (751 loc) 32.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStandaloneAcitveAccountPagesByKeyword = exports.getAccountPagesByKeyword = exports.getActiveAccountPagesByKeyword = exports.getAccountsByTagsLean = exports.getAccountsByTags = exports.getActiveAccountsByTagsLean = exports.getActiveAccountsByTags = exports.getTotalActiveAccountPages = exports.getSelectedAccountPages = exports.getSelectedActiveAccountPages = exports.getTotalAccountPages = exports.getAccountByIdLean = exports.getAccountById = exports.getAllAccountsLean = exports.getAllAccounts = exports.getActiveAccountByUsernameAndNamespaceLean = exports.getActiveAccountByUsernameAndNamespace = exports.getAccountByUsernameAndNamespaceLean = exports.getAccountByUsernameAndNamespace = exports.getAccountsByGroupsLean = exports.getAccountsByGroups = exports.getActiveAccountsByGroupsLean = exports.getActiveAccountsByGroups = exports.getAccountsByDecoratorPages = exports.getAccountsByDecoratorAndPageLean = exports.getAccountsByDecoratorLean = exports.getAccountsByDecoratorAndPage = exports.getAccountsByDecorator = exports.getAccountsByNamespacePages = exports.getAccountsByNamespaceAndPageLean = exports.getAccountsByNamespaceLean = exports.getAccountsByNamespaceAndPage = exports.getAccountsByNamespace = exports.getAccountsByTagPages = exports.getAccountsByTagAndPageLean = exports.getAccountsByTagLean = exports.getAccountsByTagAndPage = exports.getAccountsByTag = exports.getAccountsByGroupPages = exports.getAccountsByGroupAndPageLean = exports.getAccountsByGroupLean = exports.getAccountsByGroupAndPage = exports.getAccountsByGroup = exports.getAccountsByOrganizationPages = exports.getAccountsByOrganizationAndPageLean = exports.getAccountsByOrganizationLean = exports.getAccountsByOrganizationAndPage = exports.getAccountsByOrganization = exports.createUnsavedAccount = exports.createOnLimboUnsavedAccount = void 0; exports.getAccountsByIdsLean = exports.getAccountsByIds = exports.getAccountsByUsernamesLean = exports.getAccountsByUsernames = exports.getAccountsByQueryLean = exports.getAccountsByQuery = exports.getActiveAccountsByGroupLean = exports.getActiveAccountsByGroup = exports.getActiveAccountCountByGroup = exports.getActiveAccountByGroupAndOrganizationLean = exports.getActiveAccountByGroupAndOrganization = exports.getAccountCountByOrganization = exports.resetAccountPassword = exports.isAccountDuplicatedByUsernameAndNamespace = exports.getAllAccountsByPageLean = exports.getAllAccountsByPage = exports.getAllActiveAccountsByPageLean = exports.getAllActiveAccountsByPage = exports.getAccountsByPageLean = exports.getAccountsByPage = exports.getActiveAccountsByPageLean = exports.getActiveAccountsByPage = exports.getSelectedAccountsByPageLean = exports.getSelectedAccountsByPage = exports.getSelectedActiveAccountsByPageLean = exports.getSelectedActiveAccountsByPage = exports.getStandaloneActiveAccountsByPageLean = exports.getStandaloneActiveAccountsByPage = void 0; const random_1 = require("@sudoo/bark/random"); const common_1 = require("../data/common"); const account_1 = require("../model/account"); const auth_1 = require("../util/auth"); const object_id_1 = require("../util/object-id"); const token_1 = require("../util/token"); const createOnLimboUnsavedAccount = (username, password, namespace, displayName, email, phone, organization, groups = [], infos = {}, beacons = {}, tags = []) => { const infoList = token_1.parseInfo(infos); const beaconList = token_1.parseInfo(beacons); const salt = random_1._Random.unique(); const mint = random_1._Random.unique(); const anchor = common_1.fitAnchor(username); const config = { anchor, username, namespace, displayName, password: auth_1.garblePassword(password, salt), previousPasswords: [], email, phone, infos: infoList, beacons: beaconList, mint, salt, organization, groups, tags, }; return new account_1.AccountModel(Object.assign({ limbo: true }, config)); }; exports.createOnLimboUnsavedAccount = createOnLimboUnsavedAccount; const createUnsavedAccount = (username, password, namespace, displayName, email, phone, organization, groups = [], infos = {}, beacons = {}, tags = []) => { const infoList = token_1.parseInfo(infos); const beaconList = token_1.parseInfo(beacons); const salt = random_1._Random.unique(); const mint = random_1._Random.unique(); const anchor = common_1.fitAnchor(username); const config = { anchor, username, namespace, displayName, password: auth_1.garblePassword(password, salt), previousPasswords: [], email, phone, infos: infoList, beacons: beaconList, mint, salt, organization, groups, tags, }; return new account_1.AccountModel(config); }; exports.createUnsavedAccount = createUnsavedAccount; const getAccountsByOrganization = (organization) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ organization: organization, }).sort({ _id: -1 }); }); exports.getAccountsByOrganization = getAccountsByOrganization; const getAccountsByOrganizationAndPage = (organization, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ organization: organization, }).skip(page * limit).limit(limit).sort({ _id: -1 }); }); exports.getAccountsByOrganizationAndPage = getAccountsByOrganizationAndPage; const getAccountsByOrganizationLean = (organization) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ organization: organization, }).sort({ _id: -1 }).lean(); }); exports.getAccountsByOrganizationLean = getAccountsByOrganizationLean; const getAccountsByOrganizationAndPageLean = (organization, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ organization: organization, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); }); exports.getAccountsByOrganizationAndPageLean = getAccountsByOrganizationAndPageLean; const getAccountsByOrganizationPages = (organization, limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } const count = yield account_1.AccountModel.countDocuments({ organization: organization, }); return Math.ceil(count / limit); }); exports.getAccountsByOrganizationPages = getAccountsByOrganizationPages; const getAccountsByGroup = (group) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, }).sort({ _id: -1 }); }); exports.getAccountsByGroup = getAccountsByGroup; const getAccountsByGroupAndPage = (group, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ groups: group, }).skip(page * limit).limit(limit).sort({ _id: -1 }); }); exports.getAccountsByGroupAndPage = getAccountsByGroupAndPage; const getAccountsByGroupLean = (group) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, }).sort({ _id: -1 }).lean(); }); exports.getAccountsByGroupLean = getAccountsByGroupLean; const getAccountsByGroupAndPageLean = (group, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ groups: group, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); }); exports.getAccountsByGroupAndPageLean = getAccountsByGroupAndPageLean; const getAccountsByGroupPages = (group, limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } const count = yield account_1.AccountModel.countDocuments({ groups: group, }); return Math.ceil(count / limit); }); exports.getAccountsByGroupPages = getAccountsByGroupPages; const getAccountsByTag = (tag) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: tag, }).sort({ _id: -1 }); }); exports.getAccountsByTag = getAccountsByTag; const getAccountsByTagAndPage = (tag, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ tags: tag, }).skip(page * limit).limit(limit).sort({ _id: -1 }); }); exports.getAccountsByTagAndPage = getAccountsByTagAndPage; const getAccountsByTagLean = (tag) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: tag, }).sort({ _id: -1 }).lean(); }); exports.getAccountsByTagLean = getAccountsByTagLean; const getAccountsByTagAndPageLean = (tag, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ tags: tag, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); }); exports.getAccountsByTagAndPageLean = getAccountsByTagAndPageLean; const getAccountsByTagPages = (tag, limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } const count = yield account_1.AccountModel.countDocuments({ tags: tag, }); return Math.ceil(count / limit); }); exports.getAccountsByTagPages = getAccountsByTagPages; const getAccountsByNamespace = (namespace) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ namespace: namespace, }).sort({ _id: -1 }); }); exports.getAccountsByNamespace = getAccountsByNamespace; const getAccountsByNamespaceAndPage = (namespace, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ namespace: namespace, }).skip(page * limit).limit(limit).sort({ _id: -1 }); }); exports.getAccountsByNamespaceAndPage = getAccountsByNamespaceAndPage; const getAccountsByNamespaceLean = (namespace) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ namespace: namespace, }).sort({ _id: -1 }).lean(); }); exports.getAccountsByNamespaceLean = getAccountsByNamespaceLean; const getAccountsByNamespaceAndPageLean = (namespace, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ namespace: namespace, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); }); exports.getAccountsByNamespaceAndPageLean = getAccountsByNamespaceAndPageLean; const getAccountsByNamespacePages = (namespace, limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } const count = yield account_1.AccountModel.countDocuments({ namespace: namespace, }); return Math.ceil(count / limit); }); exports.getAccountsByNamespacePages = getAccountsByNamespacePages; const getAccountsByDecorator = (decorator) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ decorators: decorator, }).sort({ _id: -1 }); }); exports.getAccountsByDecorator = getAccountsByDecorator; const getAccountsByDecoratorAndPage = (decorator, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ decorators: decorator, }).skip(page * limit).limit(limit).sort({ _id: -1 }); }); exports.getAccountsByDecoratorAndPage = getAccountsByDecoratorAndPage; const getAccountsByDecoratorLean = (decorator) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ decorators: decorator, }).sort({ _id: -1 }).lean(); }); exports.getAccountsByDecoratorLean = getAccountsByDecoratorLean; const getAccountsByDecoratorAndPageLean = (decorator, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0 || limit < 1) { return []; } return yield account_1.AccountModel.find({ decorators: decorator, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); }); exports.getAccountsByDecoratorAndPageLean = getAccountsByDecoratorAndPageLean; const getAccountsByDecoratorPages = (decorator, limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } const count = yield account_1.AccountModel.countDocuments({ decorators: decorator, }); return Math.ceil(count / limit); }); exports.getAccountsByDecoratorPages = getAccountsByDecoratorPages; const getActiveAccountsByGroups = (groups) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: { $in: object_id_1.parseObjectIDList(groups), }, active: true, }); }); exports.getActiveAccountsByGroups = getActiveAccountsByGroups; const getActiveAccountsByGroupsLean = (groups) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: { $in: object_id_1.parseObjectIDList(groups), }, active: true, }).lean(); }); exports.getActiveAccountsByGroupsLean = getActiveAccountsByGroupsLean; const getAccountsByGroups = (groups) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: { $in: object_id_1.parseObjectIDList(groups), }, }); }); exports.getAccountsByGroups = getAccountsByGroups; const getAccountsByGroupsLean = (groups) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: { $in: object_id_1.parseObjectIDList(groups), }, }).lean(); }); exports.getAccountsByGroupsLean = getAccountsByGroupsLean; const getAccountByUsernameAndNamespace = (username, namespace) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(username); return yield account_1.AccountModel.findOne({ anchor, namespace, }); }); exports.getAccountByUsernameAndNamespace = getAccountByUsernameAndNamespace; const getAccountByUsernameAndNamespaceLean = (username, namespace) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(username); return yield account_1.AccountModel.findOne({ anchor, namespace, }).lean(); }); exports.getAccountByUsernameAndNamespaceLean = getAccountByUsernameAndNamespaceLean; const getActiveAccountByUsernameAndNamespace = (username, namespace) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(username); return yield account_1.AccountModel.findOne({ active: true, anchor, namespace, }); }); exports.getActiveAccountByUsernameAndNamespace = getActiveAccountByUsernameAndNamespace; const getActiveAccountByUsernameAndNamespaceLean = (username, namespace) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(username); return yield account_1.AccountModel.findOne({ active: true, anchor, namespace, }).lean(); }); exports.getActiveAccountByUsernameAndNamespaceLean = getActiveAccountByUsernameAndNamespaceLean; const getAllAccounts = () => __awaiter(void 0, void 0, void 0, function* () { return account_1.AccountModel.find({}); }); exports.getAllAccounts = getAllAccounts; const getAllAccountsLean = () => __awaiter(void 0, void 0, void 0, function* () { return account_1.AccountModel.find({}).lean(); }); exports.getAllAccountsLean = getAllAccountsLean; const getAccountById = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.findOne({ _id: id, }); }); exports.getAccountById = getAccountById; const getAccountByIdLean = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.findOne({ _id: id, }).lean(); }); exports.getAccountByIdLean = getAccountByIdLean; const getTotalAccountPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } return (yield account_1.AccountModel.estimatedDocumentCount({})) / limit; }); exports.getTotalAccountPages = getTotalAccountPages; const getSelectedActiveAccountPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } if (keyword) { return yield exports.getActiveAccountPagesByKeyword(limit, keyword); } return yield exports.getTotalActiveAccountPages(limit); }); exports.getSelectedActiveAccountPages = getSelectedActiveAccountPages; const getSelectedAccountPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } if (keyword) { return yield exports.getAccountPagesByKeyword(limit, keyword); } return yield exports.getTotalAccountPages(limit); }); exports.getSelectedAccountPages = getSelectedAccountPages; const getTotalActiveAccountPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { if (limit <= 0) { return Infinity; } return (yield account_1.AccountModel.countDocuments({ active: true, })) / limit; }); exports.getTotalActiveAccountPages = getTotalActiveAccountPages; const getActiveAccountsByTags = (tags) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: { $in: object_id_1.parseObjectIDList(tags), }, active: true, }); }); exports.getActiveAccountsByTags = getActiveAccountsByTags; const getActiveAccountsByTagsLean = (tags) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: { $in: object_id_1.parseObjectIDList(tags), }, active: true, }).lean(); }); exports.getActiveAccountsByTagsLean = getActiveAccountsByTagsLean; const getAccountsByTags = (tags) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: { $in: object_id_1.parseObjectIDList(tags), }, }); }); exports.getAccountsByTags = getAccountsByTags; const getAccountsByTagsLean = (tags) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ tags: { $in: object_id_1.parseObjectIDList(tags), }, }).lean(); }); exports.getAccountsByTagsLean = getAccountsByTagsLean; const getActiveAccountPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield account_1.AccountModel.countDocuments({ anchor: { $regex: regexp, }, active: true, })) / limit; }); exports.getActiveAccountPagesByKeyword = getActiveAccountPagesByKeyword; const getAccountPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield account_1.AccountModel.countDocuments({ anchor: { $regex: regexp, }, })) / limit; }); exports.getAccountPagesByKeyword = getAccountPagesByKeyword; const getStandaloneAcitveAccountPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield account_1.AccountModel.countDocuments({ anchor: { $regex: regexp, }, organization: { $exists: false, }, active: true, })) / limit; }); exports.getStandaloneAcitveAccountPagesByKeyword = getStandaloneAcitveAccountPagesByKeyword; const getStandaloneActiveAccountsByPage = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, organization: { $exists: false, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return accounts; }); exports.getStandaloneActiveAccountsByPage = getStandaloneActiveAccountsByPage; const getStandaloneActiveAccountsByPageLean = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, organization: { $exists: false, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return accounts; }); exports.getStandaloneActiveAccountsByPageLean = getStandaloneActiveAccountsByPageLean; const getSelectedActiveAccountsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveAccountsByPage(keyword, limit, page); } return yield exports.getAllActiveAccountsByPage(limit, page); }); exports.getSelectedActiveAccountsByPage = getSelectedActiveAccountsByPage; const getSelectedActiveAccountsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveAccountsByPageLean(keyword, limit, page); } return yield exports.getAllActiveAccountsByPageLean(limit, page); }); exports.getSelectedActiveAccountsByPageLean = getSelectedActiveAccountsByPageLean; const getSelectedAccountsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getAccountsByPage(keyword, limit, page); } return yield exports.getAllAccountsByPage(limit, page); }); exports.getSelectedAccountsByPage = getSelectedAccountsByPage; const getSelectedAccountsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getAccountsByPageLean(keyword, limit, page); } return yield exports.getAllAccountsByPageLean(limit, page); }); exports.getSelectedAccountsByPageLean = getSelectedAccountsByPageLean; const getActiveAccountsByPage = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return accounts; }); exports.getActiveAccountsByPage = getActiveAccountsByPage; const getActiveAccountsByPageLean = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return accounts; }); exports.getActiveAccountsByPageLean = getActiveAccountsByPageLean; const getAccountsByPage = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return accounts; }); exports.getAccountsByPage = getAccountsByPage; const getAccountsByPageLean = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); const accounts = yield account_1.AccountModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return accounts; }); exports.getAccountsByPageLean = getAccountsByPageLean; const getAllActiveAccountsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const accounts = yield account_1.AccountModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return accounts; }); exports.getAllActiveAccountsByPage = getAllActiveAccountsByPage; const getAllActiveAccountsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const accounts = yield account_1.AccountModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return accounts; }); exports.getAllActiveAccountsByPageLean = getAllActiveAccountsByPageLean; const getAllAccountsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const accounts = yield account_1.AccountModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }); return accounts; }); exports.getAllAccountsByPage = getAllAccountsByPage; const getAllAccountsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const accounts = yield account_1.AccountModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return accounts; }); exports.getAllAccountsByPageLean = getAllAccountsByPageLean; const isAccountDuplicatedByUsernameAndNamespace = (username, namespace) => __awaiter(void 0, void 0, void 0, function* () { const account = yield exports.getAccountByUsernameAndNamespace(username, namespace); return Boolean(account); }); exports.isAccountDuplicatedByUsernameAndNamespace = isAccountDuplicatedByUsernameAndNamespace; const resetAccountPassword = (username, namespace, newPassword) => __awaiter(void 0, void 0, void 0, function* () { const account = yield exports.getAccountByUsernameAndNamespace(username, namespace); if (!account) { return null; } account.setPassword(newPassword, 'reset'); yield account.save(); return account; }); exports.resetAccountPassword = resetAccountPassword; const getAccountCountByOrganization = (organizationId) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.countDocuments({ organization: organizationId, active: true, }); }); exports.getAccountCountByOrganization = getAccountCountByOrganization; const getActiveAccountByGroupAndOrganization = (group, organization) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, organization, active: true, }); }); exports.getActiveAccountByGroupAndOrganization = getActiveAccountByGroupAndOrganization; const getActiveAccountByGroupAndOrganizationLean = (group, organization) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, organization, active: true, }).lean(); }); exports.getActiveAccountByGroupAndOrganizationLean = getActiveAccountByGroupAndOrganizationLean; const getActiveAccountCountByGroup = (group) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.countDocuments({ groups: group, active: true, }); }); exports.getActiveAccountCountByGroup = getActiveAccountCountByGroup; const getActiveAccountsByGroup = (group) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, active: true, }); }); exports.getActiveAccountsByGroup = getActiveAccountsByGroup; const getActiveAccountsByGroupLean = (group) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ groups: group, active: true, }).lean(); }); exports.getActiveAccountsByGroupLean = getActiveAccountsByGroupLean; const getAccountsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find(query); }); exports.getAccountsByQuery = getAccountsByQuery; const getAccountsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find(query).lean(); }); exports.getAccountsByQueryLean = getAccountsByQueryLean; const getAccountsByUsernames = (usernames) => __awaiter(void 0, void 0, void 0, function* () { const anchors = usernames.map((username) => common_1.fitAnchor(username)); return yield account_1.AccountModel.find({ anchor: { $in: anchors, }, }); }); exports.getAccountsByUsernames = getAccountsByUsernames; const getAccountsByUsernamesLean = (usernames) => __awaiter(void 0, void 0, void 0, function* () { const anchors = usernames.map((username) => common_1.fitAnchor(username)); return yield account_1.AccountModel.find({ anchor: { $in: anchors, }, }).lean(); }); exports.getAccountsByUsernamesLean = getAccountsByUsernamesLean; const getAccountsByIds = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ _id: { $in: ids, }, }); }); exports.getAccountsByIds = getAccountsByIds; const getAccountsByIdsLean = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield account_1.AccountModel.find({ _id: { $in: ids, }, }).lean(); }); exports.getAccountsByIdsLean = getAccountsByIdsLean;