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

hicommonwealth / commonwealth / 14975069059

12 May 2025 02:35PM UTC coverage: 42.469% (-0.2%) from 42.635%
14975069059

push

github

web-flow
Merge pull request #12091 from hicommonwealth/rotorsoft/12080-refactor-topics-controller

Refactors topics controller

1683 of 4372 branches covered (38.49%)

Branch coverage included in aggregate %.

0 of 30 new or added lines in 2 files covered. (0.0%)

3054 of 6782 relevant lines covered (45.03%)

37.11 hits per line

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

0.0
/libs/model/src/aggregates/community/UpdateTopicChannel.command.ts
1
import { type Command } from '@hicommonwealth/core';
2
import * as schemas from '@hicommonwealth/schemas';
3
import { Op } from 'sequelize';
4
import { models } from '../../database';
5
import { authTopic, mustExist } from '../../middleware';
6

7
export function UpdateTopicChannel(): Command<
8
  typeof schemas.UpdateTopicChannel
9
> {
NEW
10
  return {
×
11
    ...schemas.UpdateTopicChannel,
12
    auth: [authTopic({ roles: ['admin', 'moderator'] })],
13
    body: async ({ payload }) => {
NEW
14
      const { topic_id, channel_id } = payload;
×
15

NEW
16
      const topic = await models.Topic.findByPk(topic_id!);
×
NEW
17
      mustExist('Topic', topic);
×
18

NEW
19
      await models.sequelize.transaction(async (transaction) => {
×
20
        // find previous topic associated with channel
NEW
21
        const old_topic = await models.Topic.findOne({
×
22
          where: { channel_id: channel_id || topic.channel_id },
×
23
          transaction,
24
        });
25

26
        // either we are removing a connect (channel_id is null) or we are connecting to a new channel
NEW
27
        if (old_topic && (old_topic.id !== topic.id || !channel_id)) {
×
28
          // previous threads on topic from discord bot
NEW
29
          const thread_ids = await models.Thread.findAll({
×
30
            where: {
31
              topic_id: old_topic.id,
32
              discord_meta: { [Op.ne]: null },
33
            },
34
            attributes: ['id'],
35
            transaction,
36
          });
NEW
37
          await models.Thread.update(
×
38
            { topic_id: topic.id },
39
            {
NEW
40
              where: { id: { [Op.in]: thread_ids.map((t) => t.id!) } },
×
41
              transaction,
42
            },
43
          );
44
          // remove channel_id from old topic
NEW
45
          old_topic.channel_id = null;
×
NEW
46
          await old_topic.save({ transaction });
×
47
        } else {
48
          // no previous topic associated with channel... set all threads with channel id to new topic
NEW
49
          const thread_ids = await models.Thread.findAll({
×
50
            where: {
51
              community_id: topic.community_id,
52
              discord_meta: { channel_id },
53
            },
54
            attributes: ['id'],
55
            transaction,
56
          });
NEW
57
          await models.Thread.update(
×
58
            { topic_id: topic.id },
59
            {
NEW
60
              where: { id: { [Op.in]: thread_ids.map((t) => t.id!) } },
×
61
              transaction,
62
            },
63
          );
64
        }
65

NEW
66
        topic.channel_id = channel_id;
×
NEW
67
        await topic.save({ transaction });
×
68
      });
69

NEW
70
      return topic.toJSON();
×
71
    },
72
  };
73
}
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