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

hicommonwealth / commonwealth / 14273131683

04 Apr 2025 07:55PM UTC coverage: 46.389% (+0.02%) from 46.37%
14273131683

push

github

web-flow
Merge pull request #11764 from hicommonwealth/ka.randomResourceIdsFix

Fixed randomResourceId performance

1585 of 3785 branches covered (41.88%)

Branch coverage included in aggregate %.

0 of 5 new or added lines in 1 file covered. (0.0%)

2937 of 5963 relevant lines covered (49.25%)

40.34 hits per line

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

0.0
/libs/model/src/aggregates/user/GetRandomResourceIds.query.ts
1
import { type 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

7
export function GetRandomResourceIds(): Query<
8
  typeof schemas.GetRandomResourceIds
9
> {
10
  return {
×
11
    ...schemas.GetRandomResourceIds,
12
    auth: [],
13
    secure: true,
14
    body: async ({ payload, actor }) => {
NEW
15
      const { exclude_joined_communities } = payload;
×
16

17
      const sql = `
×
18
        ${
19
          exclude_joined_communities
×
20
            ? 'WITH UserJoinedCommunities AS (SELECT community_id as id FROM "Addresses" where user_id = :user_id)'
21
            : ''
22
        }
23
        SELECT 
24
          C.id as community_id, 
25
          T.id as thread_id, 
26
          COM.id as comment_id
27
        FROM "Communities" C
28
          LEFT JOIN "Threads" T ON T.community_id = C.id
29
          LEFT JOIN "Comments" COM ON T.id = COM.thread_id
30
        WHERE 
31
          C.active IS true 
32
          AND C.lifetime_thread_count > 0
33
          AND T.id IS NOT NULL
34
          AND COM.id IS NOT NULL
35
          AND T.deleted_at IS NULL
36
          AND COM.deleted_at IS NULL
37
          AND T.marked_as_spam_at IS NULL
38
          AND COM.marked_as_spam_at IS NULL
39
          ${exclude_joined_communities ? 'AND C.id NOT IN (SELECT id FROM UserJoinedCommunities)' : ''}
×
40
        LIMIT 200;
41
      `;
42

NEW
43
      const resourceIds = await models.sequelize.query<
×
44
        z.infer<typeof schemas.RandomResourceIdsView>
45
      >(sql, {
46
        replacements: {
47
          user_id: actor.user.id,
48
        },
49
        type: QueryTypes.SELECT,
50
      });
51

NEW
52
      const randomResourceIds = resourceIds
×
NEW
53
        .sort(() => Math.random() - 0.5)
×
54
        .slice(0, 100);
55

NEW
56
      return {
×
57
        results: randomResourceIds,
58
      };
59
    },
60
  };
61
}
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