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

hicommonwealth / commonwealth / 13497108514

24 Feb 2025 11:36AM UTC coverage: 46.449% (+0.08%) from 46.365%
13497108514

Pull #11078

github

web-flow
Merge 56f84aad3 into beadf67b7
Pull Request #11078: Improvements to Community Homepages

1317 of 3113 branches covered (42.31%)

Branch coverage included in aggregate %.

2490 of 5083 relevant lines covered (48.99%)

38.03 hits per line

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

82.35
/libs/model/src/community/GetTopics.query.ts
1
import { Query } from '@hicommonwealth/core';
2
import * as schemas from '@hicommonwealth/schemas';
3
import { QueryTypes } from 'sequelize';
4
import { z } from 'zod';
5
import { models } from '../database';
6
import { buildChainNodeUrl } from '../utils';
7

8
const includeContestManagersQuery = `
29✔
9
    SELECT td.*,
10
           coalesce((SELECT jsonb_agg(jsonb_set(to_jsonb(cm), -- Convert the contest manager (cm) row to JSONB
11
                                                '{content}', -- Set the 'content' key in the resulting JSONB
12
                                                coalesce(
13
                                                    -- Aggregates the filtered actions into content
14
                                                        (SELECT jsonb_agg(ca)
15
                                                         FROM "ContestActions" ca
16
                                                         WHERE ca.contest_address = cm.contest_address
17
                                                           AND ca.action = 'added'
18
                                                           AND ca.created_at > co.start_time
19
                                                           AND ca.created_at < co.end_time),
20
                                                    -- Use an empty array as fallback if no actions are found
21
                                                        '[]'::jsonb)
22
                                      ))
23
                     FROM "Topics" t
24
                              LEFT JOIN "ContestManagers" cm ON cm.topic_id = t.id
25
                              JOIN (
26
                         -- Subquery to get the max contest_id, start_time, and end_time for each contest address
27
                         SELECT contest_address,
28
                                max(contest_id) AS max_contest_id,
29
                                max(start_time) AS start_time,
30
                                max(end_time)   AS end_time
31
                         FROM "Contests"
32
                         GROUP BY contest_address) co ON cm.contest_address = co.contest_address
33
                     WHERE t.id = td.id
34
                       AND cm.community_id = :community_id
35
                       AND COALESCE(cm.cancelled, FALSE) = FALSE -- Exclude cancelled managers
36
                       AND (cm.interval = 0
37
                                AND now() < co.end_time -- Check if the interval is 0 and the contest is ongoing
38
                         OR cm.interval > 0 -- Or if there is a valid interval
39
                         )), '[]'::jsonb) AS active_contest_managers
40
    FROM topic_data td
41
`;
42

43
export function GetTopics(): Query<typeof schemas.GetTopics> {
44
  return {
2✔
45
    ...schemas.GetTopics,
46
    auth: [],
47
    secure: false,
48
    body: async ({ payload }) => {
49
      const { community_id, with_contest_managers, with_archived_topics } =
50
        payload;
2✔
51

52
      const contest_managers = with_contest_managers
2!
53
        ? includeContestManagersQuery
54
        : `SELECT *, '[]'::json as active_contest_managers FROM topic_data`;
55

56
      const archivedTopicsQuery = with_archived_topics
2✔
57
        ? ''
58
        : 'AND archived_at IS NULL';
59

60
      const sql = `
2✔
61
          WITH topic_data AS (SELECT t.id,
62
                                     t.name,
63
                                     t.community_id,
64
                                     t.description,
65
                                     t.telegram,
66
                                     t.featured_in_sidebar,
67
                                     t.featured_in_new_post,
68
                                     t.default_offchain_template,
69
                                     t."order",
70
                                     t.channel_id,
71
                                     t.group_ids,
72
                                     t.weighted_voting,
73
                                     t.token_symbol,
74
                                     t.vote_weight_multiplier,
75
                                     t.token_address,
76
                                     cn.url as chain_node_url,
77
                                     cn.eth_chain_id as eth_chain_id,
78
                                     t.created_at::text           AS created_at,
79
                                     t.updated_at::text           AS updated_at,
80
                                     t.deleted_at::text           AS deleted_at,
81
                                     t.archived_at::text          AS archived_at,
82
                                     (SELECT count(*)::int
83
                                      FROM "Threads"
84
                                      WHERE community_id = :community_id
85
                                        AND topic_id = t.id
86
                                        AND deleted_at IS NULL) AS total_threads
87
                              FROM "Topics" t
88
                                LEFT JOIN "ChainNodes" cn
89
                                ON t.chain_node_id = cn.id
90
                              WHERE t.community_id = :community_id
91
                                AND t.deleted_at IS NULL ${archivedTopicsQuery})
92
              ${contest_managers}
93
      `;
94

95
      const results = await models.sequelize.query<
2✔
96
        z.infer<typeof schemas.TopicView>
97
      >(sql, {
98
        replacements: { community_id },
99
        type: QueryTypes.SELECT,
100
        raw: true,
101
      });
102

103
      results.forEach((r) => {
2✔
104
        if (r.chain_node_url) {
7!
105
          r.chain_node_url = buildChainNodeUrl(r.chain_node_url, 'public');
×
106
        }
107
      });
108

109
      return results;
2✔
110
    },
111
  };
112
}
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

© 2025 Coveralls, Inc