• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 22329524614

23 Feb 2026 11:33PM UTC coverage: 38.204% (+0.03%) from 38.173%
22329524614

Pull #13194

github

web-flow
Merge 19ef2c5a5 into 6658c85df
Pull Request #13194: AI Completion Fixes

2110 of 6026 branches covered (35.01%)

Branch coverage included in aggregate %.

9 of 80 new or added lines in 3 files covered. (11.25%)

30 existing lines in 14 files now uncovered.

3752 of 9318 relevant lines covered (40.27%)

52.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libs/model/src/aggregates/discordBot/setDiscordBotConfig.command.ts
1
import {
2
  InvalidInput,
3
  InvalidState,
4
  logger,
5
  type Command,
6
} from '@hicommonwealth/core';
7
import * as schemas from '@hicommonwealth/schemas';
8
import { DISCORD_BOT_ADDRESS, DISCORD_BOT_EMAIL } from '@hicommonwealth/shared';
9
import { models } from '../../database';
10
import { authRoles } from '../../middleware';
11

UNCOV
12
const log = logger(import.meta);
×
13

UNCOV
14
const Errors = {
×
15
  ConfigNotFound: 'Config not found',
16
  TokenExpirationNotSet: 'Token expiration not set',
17
  TokenExpired: 'Verification token expired',
18
  CommunityNotFound: 'Community not found',
19
  AlreadyConnected: 'Discord server is already connected to another community',
20
};
21

22
export function SetDiscordBotConfig(): Command<
23
  typeof schemas.SetDiscordBotConfig
24
> {
25
  return {
×
26
    ...schemas.SetDiscordBotConfig,
27
    auth: [authRoles('admin')],
28
    body: async ({ payload }) => {
29
      const { community_id, guild_id, verification_token } = payload;
×
30

31
      const configEntry = await models.DiscordBotConfig.findOne({
×
32
        where: {
33
          community_id,
34
          verification_token,
35
        },
36
      });
37

38
      if (!configEntry) throw new InvalidInput(Errors.ConfigNotFound);
×
39
      if (!configEntry.token_expiration)
×
40
        throw new InvalidState(Errors.TokenExpirationNotSet);
×
41
      if (configEntry.token_expiration < new Date())
×
42
        throw new InvalidInput(Errors.TokenExpired);
×
43

44
      const existingCommunityWithGuildConnected =
45
        await models.DiscordBotConfig.findAll({ where: { guild_id } });
×
46

47
      const communityInstance = await models.Community.findOne({
×
48
        where: { id: community_id },
49
      });
50
      if (!communityInstance) throw new InvalidState(Errors.CommunityNotFound);
×
51

52
      if (
×
53
        existingCommunityWithGuildConnected &&
×
54
        existingCommunityWithGuildConnected.length > 0
55
      ) {
56
        await models.sequelize.transaction(async (transaction) => {
×
57
          // Handle discord already linked to another CW community
58
          communityInstance.discord_config_id = null;
×
59
          await communityInstance.save({ transaction });
×
60

61
          await models.DiscordBotConfig.destroy({
×
62
            where: {
63
              community_id,
64
            },
65
            transaction,
66
          });
67
        });
68

69
        log.info(
×
70
          'Attempted to add a guild that was already connected to another CW community.',
71
        );
72
        throw new InvalidInput(Errors.AlreadyConnected);
×
73
      }
74

75
      await models.sequelize.transaction(async (transaction) => {
×
76
        const user = await models.User.findOne({
×
77
          where: { email: DISCORD_BOT_EMAIL },
78
        });
79
        let created = false;
×
80
        let address = await models.Address.findOne({
×
81
          where: {
82
            user_id: user?.id,
83
            address: DISCORD_BOT_ADDRESS,
84
            community_id,
85
          },
86
          transaction,
87
        });
88
        if (!address) {
×
89
          created = true;
×
90
          address = await models.Address.create(
×
91
            {
92
              user_id: user?.id,
93
              address: DISCORD_BOT_ADDRESS,
94
              community_id,
95
              role: 'admin',
96
              verification_token: '123456',
97
              verification_token_expires: new Date(2030, 1, 1),
98
              verified: new Date(),
99
              last_active: new Date(),
100
              ghost_address: false,
101
              is_banned: false,
102
            },
103
            { transaction },
104
          );
105
        }
106

107
        if (!created && address.role !== 'admin') {
×
108
          address.role = 'admin';
×
109
          await address.save({ transaction });
×
110
        }
111

112
        communityInstance.discord_config_id = configEntry.id;
×
113
        await communityInstance.save({ transaction });
×
114

115
        await configEntry.update(
×
116
          {
117
            community_id,
118
            guild_id,
119
            verification_token: null,
120
            token_expiration: null,
121
            verified: true,
122
          },
123
          {
124
            where: {
125
              guild_id,
126
            },
127
            transaction,
128
          },
129
        );
130
      });
131

132
      return {
×
133
        message: 'Created a new discord bot config',
134
        discordConfigId: communityInstance.discord_config_id,
135
      };
136
    },
137
  };
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc