Skip to content

Commit

Permalink
Add bulkOffchain data and stake and tags to GetCommunity() query.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnaviask committed Aug 7, 2024
1 parent 7935af2 commit d02b724
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
78 changes: 70 additions & 8 deletions libs/model/src/community/GetCommunity.query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { type Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { Includeable } from 'sequelize';
import { Includeable, literal, Op } from 'sequelize';
import { models } from '../database';
import { CommunityAttributes } from '../models';

export function GetCommunity(): Query<typeof schemas.GetCommunity> {
return {
Expand All @@ -10,20 +11,81 @@ export function GetCommunity(): Query<typeof schemas.GetCommunity> {
secure: false,
body: async ({ payload }) => {
const where = { id: payload.id };
const include: Includeable[] = [];
const include: Includeable[] = [
{
model: models.Address,
attributes: ['address', 'role'],
where: {
// @ts-expect-error StrictNullChecks
community_id: id,
[Op.or]: [{ role: 'admin' }, { role: 'moderator' }],
},
as: 'adminsAndMods',
},
{
model: models.CommunityBanner,
},
{
model: models.CommunityStake,
},
{
model: models.CommunityTags,
include: [
{
model: models.Tags,
},
],
},
];
if (payload.include_node_info) {
include.push({
model: models.ChainNode,
required: true,
});
}

return (
await models.Community.findOne({
where,
include,
})
)?.toJSON();
const result = await models.Community.findOne({
where,
include,
attributes: {
include: [
[
literal(`
SELECT COUNT(*) FROM "Threads"
WHERE "Threads"."community_id" = "Communities"."id"
AND "Threads"."stage" = 'voting')
`),
'numVotingThreads',
],
[
literal(`
SELECT COUNT(*) FROM "Threads"
WHERE "Threads"."community_id" = "Communities"."id"
AND "Threads"."marked_as_spam_at" IS NULL)
`),
'numTotalThreads',
],
[
literal(`
SELECT banner_text FROM "CommunityBanners"
WHERE "CommunityBanners"."community_id" = "Communities"."id")
`),
'communityBanner',
],
],
},
});
return result?.toJSON() as
| (CommunityAttributes & {
numVotingThreads: number;
numTotalThreads: number;
adminsAndMods: Array<{
address: string;
role: 'admin' | 'moderator';
}>;
communityBanner: string | undefined;
})
| undefined;
},
};
}
12 changes: 11 additions & 1 deletion libs/schemas/src/queries/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ export const GetCommunity = {
id: z.string(),
include_node_info: z.boolean().optional(),
}),
output: Community.optional(),
output: Community.extend({
numVotingThreads: PG_INT,
numTotalThreads: PG_INT,
adminsAndMods: z.array(
z.object({
address: z.string(),
role: z.enum(['admin', 'moderator']),
}),
),
communityBanner: z.string().optional(),
}).optional(),
};

export const GetCommunityStake = {
Expand Down

0 comments on commit d02b724

Please sign in to comment.