# All files / indexer-management/resolvers indexing-rules.ts

8.33% Statements 3/36
0% Branches 0/16
0% Functions 0/18
9.38% Lines 3/32

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
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125

    1x

    1x

    1x

     /* eslint-disable @typescript-eslint/ban-types */

    import {
      IndexerManagementModels,
      IndexingRuleCreationAttributes,
      INDEXING_RULE_GLOBAL,
    } from '../models'
    import { IndexerManagementDefaults, IndexerManagementResolverContext } from '../client'
    import { Transaction } from 'sequelize/types'

    const resetGlobalRule = async (
      deployment: string,
      defaults: IndexerManagementDefaults['globalIndexingRule'],
      models: IndexerManagementModels,
      transaction: Transaction,
    ) => {
      await models.IndexingRule.upsert(
        {
          ...defaults,
          allocationAmount: defaults.allocationAmount.toString(),
          deployment,
        },
        { transaction },
      )
    }

    export default {
      indexingRule: async (
        { deployment, merged }: { deployment: string; merged: boolean },
        { models }: IndexerManagementResolverContext,
      ): Promise<object | null> => {
        const rule = await models.IndexingRule.findOne({
          where: { deployment },
        })
        if (rule && merged) {
          return rule.mergeToGraphQL(
            await models.IndexingRule.findOne({
              where: { deployment: INDEXING_RULE_GLOBAL },
            }),
          )
        } else {
          return rule?.toGraphQL() || null
        }
      },

      indexingRules: async (
        { merged }: { merged: boolean },
        { models }: IndexerManagementResolverContext,
      ): Promise<object[]> => {
        const rules = await models.IndexingRule.findAll({
          order: [['deployment', 'DESC']],
        })
        if (merged) {
          const global = await models.IndexingRule.findOne({
            where: { deployment: INDEXING_RULE_GLOBAL },
          })
          return rules.map((rule) => rule.mergeToGraphQL(global))
        } else {
          return rules.map((rule) => rule.toGraphQL())
        }
      },

      setIndexingRule: async (
        { rule }: { rule: IndexingRuleCreationAttributes },
        { models }: IndexerManagementResolverContext,
      ): Promise<object> => {
        await models.IndexingRule.upsert(rule)

        // Since upsert succeeded, we _must_ have a rule
        const updatedRule = await models.IndexingRule.findOne({
          where: { deployment: rule.deployment },
        })
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return updatedRule!.toGraphQL()
      },

      deleteIndexingRule: async (
        { deployment }: { deployment: string },
        { models, defaults }: IndexerManagementResolverContext,
      ): Promise<boolean> => {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return await models.IndexingRule.sequelize!.transaction(async (transaction) => {
          const numDeleted = await models.IndexingRule.destroy({
            where: {
              deployment,
            },
            transaction,
          })

          // Reset the global rule
          if (deployment === 'global') {
            await resetGlobalRule(
              deployment,
              defaults.globalIndexingRule,
              models,
              transaction,
            )
          }

          return numDeleted > 0
        })
      },

      deleteIndexingRules: async (
        { deployments }: { deployments: string[] },
        { models, defaults }: IndexerManagementResolverContext,
      ): Promise<boolean> => {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return await models.IndexingRule.sequelize!.transaction(async (transaction) => {
          const numDeleted = await models.IndexingRule.destroy({
            where: {
              deployment: deployments,
            },
            transaction,
          })

          if (deployments.includes('global')) {
            await resetGlobalRule('global', defaults.globalIndexingRule, models, transaction)
          }

          return numDeleted > 0
        })
      },
    }