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

hicommonwealth / commonwealth / 12265983248

10 Dec 2024 10:40PM UTC coverage: 47.156%. Remained the same
12265983248

Pull #10055

github

web-flow
Merge 19e45fac1 into f0980d94f
Pull Request #10055: sticky editor implementation - ready for review

1189 of 2831 branches covered (42.0%)

Branch coverage included in aggregate %.

2451 of 4888 relevant lines covered (50.14%)

31.48 hits per line

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

0.0
/libs/model/src/comment/SearchComments.query.ts
1
import { type Query } from '@hicommonwealth/core';
2
import * as schemas from '@hicommonwealth/schemas';
3
import { QueryTypes } from 'sequelize';
4
import { models } from '../database';
5

6
export function SearchComments(): Query<typeof schemas.SearchComments> {
7
  return {
×
8
    ...schemas.SearchComments,
9
    auth: [],
10
    body: async ({ payload }) => {
11
      const { community_id, search, limit, page, orderBy, orderDirection } =
12
        payload;
×
13
      // sort by rank by default
14
      let sortOptions: schemas.PaginationSqlOptions = {
×
15
        limit: Math.min(limit, 100) || 10,
×
16
        page: page || 1,
×
17
        orderDirection,
18
      };
19
      switch (orderBy) {
×
20
        case 'created_at':
21
          sortOptions = {
×
22
            ...sortOptions,
23
            orderBy: `"Comments".${orderBy}`,
24
          };
25
          break;
×
26
        default:
27
          sortOptions = {
×
28
            ...sortOptions,
29
            orderBy: `rank`,
30
            orderBySecondary: `"Comments".created_at`,
31
            orderDirectionSecondary: 'DESC',
32
          };
33
      }
34

35
      const { sql: paginationSort, bind: paginationBind } =
36
        schemas.buildPaginationSql(sortOptions);
×
37

38
      const bind: {
39
        searchTerm?: string;
40
        community?: string;
41
        limit?: number;
42
      } = {
×
43
        searchTerm: search,
44
        ...paginationBind,
45
      };
46
      if (community_id) {
×
47
        bind.community = community_id;
×
48
      }
49

50
      const communityWhere = bind.community
×
51
        ? '"Threads".community_id = $community AND'
52
        : '';
53

54
      const sqlBaseQuery = `
×
55
    SELECT
56
      "Comments".id,
57
      "Threads".title,
58
      "Comments".body,
59
      "Comments".thread_id as proposalId,
60
      'comment' as type,
61
      "Addresses".id as address_id,
62
      "Addresses".address,
63
      "Addresses".community_id as address_community_id,
64
      "Comments".created_at,
65
      "Threads".community_id as community_id,
66
      ts_rank_cd("Comments".search, query) as rank
67
    FROM "Comments"
68
    JOIN "Threads" ON "Comments".thread_id = "Threads".id
69
    JOIN "Addresses" ON "Comments".address_id = "Addresses".id,
70
    websearch_to_tsquery('english', $searchTerm) as query
71
    WHERE
72
      ${communityWhere}
73
      "Comments".deleted_at IS NULL AND
74
      query @@ "Comments".search
75
    ${paginationSort}
76
  `;
77

78
      const sqlCountQuery = `
×
79
    SELECT
80
      COUNT (*) as count
81
    FROM "Comments"
82
    JOIN "Threads" ON "Comments".thread_id = "Threads".id
83
    JOIN "Addresses" ON "Comments".address_id = "Addresses".id,
84
    websearch_to_tsquery('english', $searchTerm) as query
85
    WHERE
86
      ${communityWhere}
87
      "Comments".deleted_at IS NULL AND
88
      query @@ "Comments".search
89
  `;
90

91
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
      const [results, [{ count }]]: [any[], any[]] = await Promise.all([
×
93
        models.sequelize.query(sqlBaseQuery, {
94
          bind,
95
          type: QueryTypes.SELECT,
96
        }),
97
        models.sequelize.query(sqlCountQuery, {
98
          bind,
99
          type: QueryTypes.SELECT,
100
        }),
101
      ]);
102

103
      const totalResults = parseInt(count, 10);
×
104

105
      return schemas.buildPaginatedResponse(results, totalResults, bind);
×
106
    },
107
  };
108
}
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