-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9584 from hicommonwealth/rotorsoft/9103-get-topic…
…s-query Refactor getTopics
- Loading branch information
Showing
49 changed files
with
262 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Query } from '@hicommonwealth/core'; | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { QueryTypes } from 'sequelize'; | ||
import { z } from 'zod'; | ||
import { models } from '../database'; | ||
|
||
export function GetTopics(): Query<typeof schemas.GetTopics> { | ||
return { | ||
...schemas.GetTopics, | ||
auth: [], | ||
secure: false, | ||
body: async ({ payload }) => { | ||
const { community_id, with_contest_managers } = payload; | ||
|
||
const contest_managers = with_contest_managers | ||
? ` | ||
SELECT | ||
td.*, | ||
coalesce(( | ||
SELECT | ||
jsonb_agg(jsonb_set(to_jsonb (cm), -- Convert the contest manager (cm) row to JSONB | ||
'{content}', -- Set the 'content' key in the resulting JSONB | ||
coalesce(( | ||
SELECT | ||
jsonb_agg(ca) -- Aggregates the filtered actions into content | ||
FROM "ContestActions" ca | ||
WHERE | ||
ca.contest_address = cm.contest_address | ||
AND ca.action = 'added' | ||
AND ca.created_at > co.start_time | ||
AND ca.created_at < co.end_time), '[]'::jsonb) -- Use an empty array as fallback if no actions are found | ||
)) | ||
FROM "ContestTopics" ct | ||
LEFT JOIN "ContestManagers" cm ON cm.contest_address = ct.contest_address | ||
JOIN ( | ||
-- Subquery to get the max contest_id, start_time, and end_time for each contest address | ||
SELECT | ||
contest_address, | ||
max(contest_id) AS max_contest_id, | ||
max(start_time) AS start_time, | ||
max(end_time) AS end_time | ||
FROM | ||
"Contests" | ||
GROUP BY | ||
contest_address) co ON cm.contest_address = co.contest_address | ||
WHERE | ||
ct.topic_id = td.id | ||
AND cm.community_id = :community_id | ||
AND COALESCE(cm.cancelled, FALSE) = FALSE -- Exclude cancelled managers | ||
AND (cm.interval = 0 | ||
AND now() < co.end_time -- Check if the interval is 0 and the contest is ongoing | ||
OR cm.interval > 0 -- Or if there is a valid interval | ||
)), '[]'::jsonb) AS active_contest_managers | ||
FROM | ||
topic_data td | ||
` | ||
: `SELECT *, '[]'::json as active_contest_managers FROM topic_data`; | ||
|
||
const sql = ` | ||
WITH topic_data AS ( | ||
SELECT | ||
id, | ||
name, | ||
community_id, | ||
description, | ||
telegram, | ||
featured_in_sidebar, | ||
featured_in_new_post, | ||
default_offchain_template, | ||
"order", | ||
channel_id, | ||
group_ids, | ||
weighted_voting, | ||
token_symbol, | ||
vote_weight_multiplier, | ||
created_at::text AS created_at, | ||
updated_at::text AS updated_at, | ||
deleted_at::text AS deleted_at, | ||
( | ||
SELECT | ||
count(*)::int | ||
FROM | ||
"Threads" | ||
WHERE | ||
community_id = :community_id | ||
AND topic_id = t.id | ||
AND deleted_at IS NULL) AS total_threads | ||
FROM | ||
"Topics" t | ||
WHERE | ||
t.community_id = :community_id | ||
AND t.deleted_at IS NULL | ||
) | ||
${contest_managers} | ||
`; | ||
|
||
return await models.sequelize.query<z.infer<typeof schemas.TopicView>>( | ||
sql, | ||
{ | ||
replacements: { community_id }, | ||
type: QueryTypes.SELECT, | ||
raw: true, | ||
}, | ||
); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,4 @@ | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { z } from 'zod'; | ||
|
||
const ActiveContestManagers = z.object({ | ||
content: z.array(schemas.ContestAction), | ||
contest_manager: schemas.ContestManager, | ||
}); | ||
|
||
type TopicAttributesBase = z.infer<typeof schemas.Topic>; | ||
|
||
type TopicAttributesExtended = TopicAttributesBase & { | ||
active_contest_managers: Array<z.infer<typeof ActiveContestManagers>>; | ||
total_threads: number; | ||
}; | ||
|
||
export type TopicAttributes = TopicAttributesExtended; | ||
|
||
class Topic { | ||
public readonly name: TopicAttributes['name']; | ||
public readonly id: TopicAttributes['id']; | ||
public readonly description: TopicAttributes['description']; | ||
public readonly telegram: TopicAttributes['telegram']; | ||
public readonly communityId: TopicAttributes['community_id']; | ||
public readonly channelId: TopicAttributes['channel_id']; | ||
public readonly featuredInSidebar: TopicAttributes['featured_in_sidebar']; | ||
public readonly featuredInNewPost: TopicAttributes['featured_in_new_post']; | ||
public order: TopicAttributes['order']; | ||
public readonly defaultOffchainTemplate: TopicAttributes['default_offchain_template']; | ||
public totalThreads: TopicAttributes['total_threads']; | ||
public readonly activeContestManagers: TopicAttributes['active_contest_managers']; | ||
public readonly groupIds: TopicAttributes['group_ids']; | ||
public readonly defaultOffchainTemplateBackup: TopicAttributes['default_offchain_template_backup']; | ||
public readonly weightedVoting: TopicAttributes['weighted_voting']; | ||
public readonly tokenAddress: TopicAttributes['token_address']; | ||
public readonly tokenSymbol: TopicAttributes['token_symbol']; | ||
public readonly voteWeightMultiplier: TopicAttributes['vote_weight_multiplier']; | ||
|
||
constructor({ | ||
name, | ||
id, | ||
description, | ||
telegram, | ||
community_id, | ||
featured_in_sidebar, | ||
featured_in_new_post, | ||
order, | ||
default_offchain_template, | ||
total_threads, | ||
channel_id, | ||
active_contest_managers, | ||
group_ids, | ||
default_offchain_template_backup, | ||
weighted_voting, | ||
token_address, | ||
token_symbol, | ||
vote_weight_multiplier, | ||
}: TopicAttributes) { | ||
this.name = name; | ||
this.id = id; | ||
this.description = description; | ||
this.telegram = telegram; | ||
this.communityId = community_id; | ||
this.featuredInSidebar = featured_in_sidebar; | ||
this.featuredInNewPost = featured_in_new_post; | ||
this.order = order; | ||
this.defaultOffchainTemplate = default_offchain_template; | ||
this.totalThreads = total_threads || 0; | ||
this.channelId = channel_id; | ||
this.activeContestManagers = active_contest_managers || []; | ||
this.groupIds = group_ids; | ||
this.defaultOffchainTemplateBackup = default_offchain_template_backup; | ||
this.weightedVoting = weighted_voting; | ||
this.tokenAddress = token_address; | ||
this.tokenSymbol = token_symbol; | ||
this.voteWeightMultiplier = vote_weight_multiplier; | ||
} | ||
} | ||
|
||
export default Topic; | ||
export type Topic = z.infer<typeof schemas.TopicView>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/commonwealth/client/scripts/state/api/threads/createThread.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/commonwealth/client/scripts/state/api/threads/helpers/cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/commonwealth/client/scripts/state/api/topics/createTopic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/commonwealth/client/scripts/state/api/topics/deleteTopic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
packages/commonwealth/client/scripts/state/api/topics/editTopic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.