# All files / dist/indexer-management/resolvers indexing-rules.js

10.2% Statements 5/49
11.11% Branches 3/27
0% Functions 0/25
12.82% Lines 5/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86

    3x

    3x
    3x
    3x

    3x

     "use strict";
    /* eslint-disable @typescript-eslint/ban-types */
    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 });
    const models_1 = require("../models");
    const resetGlobalRule = (deployment, defaults, models, transaction) => __awaiter(void 0, void 0, void 0, function* () {
        yield models.IndexingRule.upsert(Object.assign(Object.assign({}, defaults), { allocationAmount: defaults.allocationAmount.toString(), deployment }), { transaction });
    });
    exports.default = {
        indexingRule: ({ deployment, merged }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
            const rule = yield models.IndexingRule.findOne({
                where: { deployment },
            });
            if (rule && merged) {
                return rule.mergeToGraphQL(yield models.IndexingRule.findOne({
                    where: { deployment: models_1.INDEXING_RULE_GLOBAL },
                }));
            }
            else {
                return (rule === null || rule === void 0 ? void 0 : rule.toGraphQL()) || null;
            }
        }),
        indexingRules: ({ merged }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
            const rules = yield models.IndexingRule.findAll({
                order: [['deployment', 'DESC']],
            });
            if (merged) {
                const global = yield models.IndexingRule.findOne({
                    where: { deployment: models_1.INDEXING_RULE_GLOBAL },
                });
                return rules.map((rule) => rule.mergeToGraphQL(global));
            }
            else {
                return rules.map((rule) => rule.toGraphQL());
            }
        }),
        setIndexingRule: ({ rule }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
            yield models.IndexingRule.upsert(rule);
            // Since upsert succeeded, we _must_ have a rule
            const updatedRule = yield models.IndexingRule.findOne({
                where: { deployment: rule.deployment },
            });
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            return updatedRule.toGraphQL();
        }),
        deleteIndexingRule: ({ deployment }, { models, defaults }) => __awaiter(void 0, void 0, void 0, function* () {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            return yield models.IndexingRule.sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
                const numDeleted = yield models.IndexingRule.destroy({
                    where: {
                        deployment,
                    },
                    transaction,
                });
                // Reset the global rule
                if (deployment === 'global') {
                    yield resetGlobalRule(deployment, defaults.globalIndexingRule, models, transaction);
                }
                return numDeleted > 0;
            }));
        }),
        deleteIndexingRules: ({ deployments }, { models, defaults }) => __awaiter(void 0, void 0, void 0, function* () {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            return yield models.IndexingRule.sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
                const numDeleted = yield models.IndexingRule.destroy({
                    where: {
                        deployment: deployments,
                    },
                    transaction,
                });
                if (deployments.includes('global')) {
                    yield resetGlobalRule('global', defaults.globalIndexingRule, models, transaction);
                }
                return numDeleted > 0;
            }));
        }),
    };
    //# sourceMappingURL=indexing-rules.js.map