From ed799f154492dfee13dce970cb3941d201fbdbe9 Mon Sep 17 00:00:00 2001 From: kassad Date: Tue, 19 Nov 2024 07:30:24 -0800 Subject: [PATCH] Fixed profile count --- libs/model/src/community/JoinCommunity.command.ts | 9 ++++----- .../20241119145300-recompute-profile-counts.js | 13 +++++++++++++ .../server/util/verifySessionSignature.ts | 9 +++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 packages/commonwealth/server/migrations/20241119145300-recompute-profile-counts.js diff --git a/libs/model/src/community/JoinCommunity.command.ts b/libs/model/src/community/JoinCommunity.command.ts index 3c387b15898..d5baa55c1af 100644 --- a/libs/model/src/community/JoinCommunity.command.ts +++ b/libs/model/src/community/JoinCommunity.command.ts @@ -3,7 +3,6 @@ import * as schemas from '@hicommonwealth/schemas'; import { ChainBase, addressSwapper } from '@hicommonwealth/shared'; import { models } from '../database'; import { mustExist } from '../middleware/guards'; -import { incrementProfileCount } from '../utils'; import { findCompatibleAddress } from '../utils/findBaseAddress'; export const JoinCommunityErrors = { @@ -102,11 +101,11 @@ export function JoinCommunity(): Command { { transaction }, ); - await incrementProfileCount( - community.id, - actor.user.id!, + await models.Community.increment('profile_count', { + by: 1, + where: { id: community_id }, transaction, - ); + }); return created.id!; }, diff --git a/packages/commonwealth/server/migrations/20241119145300-recompute-profile-counts.js b/packages/commonwealth/server/migrations/20241119145300-recompute-profile-counts.js new file mode 100644 index 00000000000..ca4d36ef3fc --- /dev/null +++ b/packages/commonwealth/server/migrations/20241119145300-recompute-profile-counts.js @@ -0,0 +1,13 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.query(` + UPDATE "Communities" C + SET profile_count = (SELECT COUNT(DISTINCT (user_id)) FROM "Addresses" WHERE community_id = C.id); + `); + }, + + async down(queryInterface, Sequelize) {}, +}; diff --git a/packages/commonwealth/server/util/verifySessionSignature.ts b/packages/commonwealth/server/util/verifySessionSignature.ts index dcc44cb2604..746aa72d65c 100644 --- a/packages/commonwealth/server/util/verifySessionSignature.ts +++ b/packages/commonwealth/server/util/verifySessionSignature.ts @@ -91,8 +91,17 @@ const verifySessionSignature = async ( }); if (!user || !user.id) throw new Error('Failed to create user'); addressModel.user_id = user!.id; + + await addressModel.save(); + return; } } + + // user already exists but new community joined + await incrementProfileCount( + addressModel.community_id!, + addressModel.user_id!, + ); } else { // mark the address as verified addressModel.verification_token_expires = null;