From 724a340e5872fc13963cad278bb13017f0ec1270 Mon Sep 17 00:00:00 2001 From: Matt Krick Date: Fri, 6 Sep 2024 16:00:12 -0700 Subject: [PATCH] fix: insert discussion before comment (#10194) Signed-off-by: Matt Krick --- .../addAgendaItemToActiveActionMeeting.ts | 25 +++++---- .../helpers/createNewMeetingPhases.ts | 52 ++++++++++++------- .../mutations/helpers/handleCompletedStage.ts | 8 +-- .../server/graphql/mutations/joinMeeting.ts | 13 ++--- .../graphql/mutations/updatePokerScope.ts | 9 ++-- .../postgres/queries/insertDiscussions.ts | 15 ------ .../queries/src/insertDiscussionsQuery.sql | 6 --- 7 files changed, 63 insertions(+), 65 deletions(-) delete mode 100644 packages/server/postgres/queries/insertDiscussions.ts delete mode 100644 packages/server/postgres/queries/src/insertDiscussionsQuery.sql diff --git a/packages/server/graphql/mutations/helpers/addAgendaItemToActiveActionMeeting.ts b/packages/server/graphql/mutations/helpers/addAgendaItemToActiveActionMeeting.ts index 4ceb9ab23c9..735ba4be831 100644 --- a/packages/server/graphql/mutations/helpers/addAgendaItemToActiveActionMeeting.ts +++ b/packages/server/graphql/mutations/helpers/addAgendaItemToActiveActionMeeting.ts @@ -2,7 +2,6 @@ import getRethink from '../../../database/rethinkDriver' import AgendaItemsStage from '../../../database/types/AgendaItemsStage' import MeetingAction from '../../../database/types/MeetingAction' import getKysely from '../../../postgres/getKysely' -import insertDiscussions from '../../../postgres/queries/insertDiscussions' import getPhase from '../../../utils/getPhase' import {DataLoaderWorker} from '../../graphql' @@ -47,16 +46,20 @@ const addAgendaItemToActiveActionMeeting = async ( updatedAt: now }) .run(), - getKysely().updateTable('AgendaItem').set({meetingId}).where('id', '=', agendaItemId).execute(), - insertDiscussions([ - { - id: discussionId, - teamId, - meetingId, - discussionTopicType: 'agendaItem' as const, - discussionTopicId: agendaItemId - } - ]) + getKysely() + .with('InsertDiscussion', (qb) => + qb.insertInto('Discussion').values({ + id: discussionId, + teamId, + meetingId, + discussionTopicType: 'agendaItem', + discussionTopicId: agendaItemId + }) + ) + .updateTable('AgendaItem') + .set({meetingId}) + .where('id', '=', agendaItemId) + .execute() ]) return meetingId diff --git a/packages/server/graphql/mutations/helpers/createNewMeetingPhases.ts b/packages/server/graphql/mutations/helpers/createNewMeetingPhases.ts index 56d4115ef63..098392fc6a6 100644 --- a/packages/server/graphql/mutations/helpers/createNewMeetingPhases.ts +++ b/packages/server/graphql/mutations/helpers/createNewMeetingPhases.ts @@ -23,7 +23,7 @@ import TeamHealthPhase from '../../../database/types/TeamHealthPhase' import TeamHealthStage from '../../../database/types/TeamHealthStage' import UpdatesPhase from '../../../database/types/UpdatesPhase' import UpdatesStage from '../../../database/types/UpdatesStage' -import insertDiscussions from '../../../postgres/queries/insertDiscussions' +import getKysely from '../../../postgres/getKysely' import {MeetingTypeEnum} from '../../../postgres/types/Meeting' import isPhaseAvailable from '../../../utils/isPhaseAvailable' import {DataLoaderWorker} from '../../graphql' @@ -77,6 +77,7 @@ const createNewMeetingPhases = async ( meetingType: MeetingTypeEnum, dataLoader: DataLoaderWorker ) => { + const pg = getKysely() const [meetingSettings, stageDurations, team] = await Promise.all([ dataLoader.get('meetingSettingsByType').load({teamId, meetingType}), getPastStageDurations(teamId), @@ -108,17 +109,22 @@ const createNewMeetingPhases = async ( case DISCUSS: const discussPhase = new DiscussPhase(durations) const discussStages = discussPhase.stages.filter((stage) => stage.reflectionGroupId) - asyncSideEffects.push( - insertDiscussions( - discussStages.map((stage) => ({ - id: stage.discussionId, - teamId, - meetingId, - discussionTopicId: stage.reflectionGroupId, - discussionTopicType: 'reflectionGroup' as const - })) + if (discussStages.length > 0) { + asyncSideEffects.push( + pg + .insertInto('Discussion') + .values( + discussStages.map((stage) => ({ + id: stage.discussionId, + teamId, + meetingId, + discussionTopicId: stage.reflectionGroupId, + discussionTopicType: 'reflectionGroup' + })) + ) + .execute() ) - ) + } return discussPhase case UPDATES: return new UpdatesPhase({durations, stages: [new UpdatesStage(facilitatorTeamMemberId)]}) @@ -127,14 +133,22 @@ const createNewMeetingPhases = async ( const agendaItemIds = agendaItems.map(({id}) => id) const agendaItemPhase = new AgendaItemsPhase(agendaItemIds, durations) const {stages} = agendaItemPhase - const discussions = stages.map((stage) => ({ - id: stage.discussionId, - teamId, - meetingId, - discussionTopicId: stage.agendaItemId, - discussionTopicType: 'agendaItem' as const - })) - asyncSideEffects.push(insertDiscussions(discussions)) + if (stages.length > 0) { + asyncSideEffects.push( + pg + .insertInto('Discussion') + .values( + stages.map((stage) => ({ + id: stage.discussionId, + teamId, + meetingId, + discussionTopicId: stage.agendaItemId, + discussionTopicType: 'agendaItem' + })) + ) + .execute() + ) + } return agendaItemPhase case 'ESTIMATE': return new EstimatePhase() diff --git a/packages/server/graphql/mutations/helpers/handleCompletedStage.ts b/packages/server/graphql/mutations/helpers/handleCompletedStage.ts index 7bd65438355..a6aa7267f88 100644 --- a/packages/server/graphql/mutations/helpers/handleCompletedStage.ts +++ b/packages/server/graphql/mutations/helpers/handleCompletedStage.ts @@ -6,7 +6,6 @@ import DiscussStage from '../../../database/types/DiscussStage' import GenericMeetingStage from '../../../database/types/GenericMeetingStage' import MeetingRetrospective from '../../../database/types/MeetingRetrospective' import getKysely from '../../../postgres/getKysely' -import insertDiscussions from '../../../postgres/queries/insertDiscussions' import {AnyMeeting} from '../../../postgres/types/Meeting' import {DataLoaderWorker} from '../../graphql' import addAIGeneratedContentToThreads from './addAIGeneratedContentToThreads' @@ -28,12 +27,11 @@ const handleCompletedRetrospectiveStage = async ( meeting: MeetingRetrospective, dataLoader: DataLoaderWorker ) => { + const pg = getKysely() if (stage.phaseType === REFLECT || stage.phaseType === GROUP) { const data: Record = await removeEmptyReflections(meeting, dataLoader) if (stage.phaseType === REFLECT) { - const pg = getKysely() - const [reflectionGroups, unsortedReflections] = await Promise.all([ dataLoader.get('retroReflectionGroupsByMeetingId').load(meeting.id), dataLoader.get('retroReflectionsByMeetingId').load(meeting.id) @@ -93,7 +91,9 @@ const handleCompletedRetrospectiveStage = async ( discussionTopicId: stage.reflectionGroupId })) // discussions must exist before we can add comments to them! - await insertDiscussions(discussions) + if (discussions.length > 0) { + await pg.insertInto('Discussion').values(discussions).execute() + } await Promise.all([ addAIGeneratedContentToThreads(discussPhaseStages, meetingId, dataLoader), publishToEmbedder({jobType: 'relatedDiscussions:start', data: {meetingId}, priority: 0}) diff --git a/packages/server/graphql/mutations/joinMeeting.ts b/packages/server/graphql/mutations/joinMeeting.ts index 7be33d39e2f..c95582aeea1 100644 --- a/packages/server/graphql/mutations/joinMeeting.ts +++ b/packages/server/graphql/mutations/joinMeeting.ts @@ -13,7 +13,7 @@ import RetroMeetingMember from '../../database/types/RetroMeetingMember' import TeamPromptMeetingMember from '../../database/types/TeamPromptMeetingMember' import TeamPromptResponseStage from '../../database/types/TeamPromptResponseStage' import UpdatesStage from '../../database/types/UpdatesStage' -import insertDiscussions from '../../postgres/queries/insertDiscussions' +import getKysely from '../../postgres/getKysely' import {TeamMember} from '../../postgres/types' import {analytics} from '../../utils/analytics/analytics' import {getUserId, isTeamMember} from '../../utils/authorization' @@ -145,15 +145,16 @@ const joinMeeting = { // only add a new stage for the new users (ie. invited to the team after the meeting was started) if (teamMemberResponseStage) return const responsesStage = new TeamPromptResponseStage({teamMemberId}) - await insertDiscussions([ - { + await getKysely() + .insertInto('Discussion') + .values({ id: responsesStage.discussionId, teamId, meetingId, discussionTopicId: teamMemberId, - discussionTopicType: 'teamPromptResponse' as const - } - ]) + discussionTopicType: 'teamPromptResponse' + }) + .execute() return addStageToPhase(responsesStage, 'RESPONSES') } diff --git a/packages/server/graphql/mutations/updatePokerScope.ts b/packages/server/graphql/mutations/updatePokerScope.ts index 0300d2f9832..823a6381383 100644 --- a/packages/server/graphql/mutations/updatePokerScope.ts +++ b/packages/server/graphql/mutations/updatePokerScope.ts @@ -1,12 +1,13 @@ import {GraphQLID, GraphQLList, GraphQLNonNull} from 'graphql' +import {Insertable} from 'kysely' import {SubscriptionChannel, Threshold} from 'parabol-client/types/constEnums' -import {Writeable} from '../../../client/types/generics' import {ESTIMATE_TASK_SORT_ORDER} from '../../../client/utils/constants' import getRethink from '../../database/rethinkDriver' import EstimateStage from '../../database/types/EstimateStage' import MeetingPoker from '../../database/types/MeetingPoker' import {TaskServiceEnum} from '../../database/types/Task' -import insertDiscussions, {InputDiscussions} from '../../postgres/queries/insertDiscussions' +import getKysely from '../../postgres/getKysely' +import {Discussion} from '../../postgres/pg' import RedisLockQueue from '../../utils/RedisLockQueue' import {getUserId, isTeamMember} from '../../utils/authorization' import getPhase from '../../utils/getPhase' @@ -112,7 +113,7 @@ const updatePokerScope = { // add stages const templateRef = await dataLoader.get('templateRefs').loadNonNull(templateRefId) const {dimensions} = templateRef - const newDiscussions = [] as Writeable + const newDiscussions = [] as Insertable[] const additiveUpdates = updates.filter((update) => { const {action, serviceTaskId} = update return action === 'ADD' && !stages.find((stage) => stage.serviceTaskId === serviceTaskId) @@ -168,7 +169,7 @@ const updatePokerScope = { }) .run() if (newDiscussions.length > 0) { - await insertDiscussions(newDiscussions) + await getKysely().insertInto('Discussion').values(newDiscussions).execute() } const data = {meetingId, newStageIds} publish(SubscriptionChannel.MEETING, meetingId, 'UpdatePokerScopeSuccess', data, subOptions) diff --git a/packages/server/postgres/queries/insertDiscussions.ts b/packages/server/postgres/queries/insertDiscussions.ts deleted file mode 100644 index 6e473a40bb8..00000000000 --- a/packages/server/postgres/queries/insertDiscussions.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {DeepNonNullable} from '../../../client/types/generics' -import getPg from '../getPg' -import { - IInsertDiscussionsQueryParams, - insertDiscussionsQuery -} from './generated/insertDiscussionsQuery' - -export type InputDiscussions = DeepNonNullable - -const insertDiscussions = async (discussions: InputDiscussions) => { - if (discussions.length === 0) return - insertDiscussionsQuery.run({discussions} as any, getPg()) -} - -export default insertDiscussions diff --git a/packages/server/postgres/queries/src/insertDiscussionsQuery.sql b/packages/server/postgres/queries/src/insertDiscussionsQuery.sql deleted file mode 100644 index 787f675b0ed..00000000000 --- a/packages/server/postgres/queries/src/insertDiscussionsQuery.sql +++ /dev/null @@ -1,6 +0,0 @@ -/* - @name insertDiscussionsQuery - @param discussions -> ((id, teamId, meetingId, discussionTopicId, discussionTopicType)...) -*/ -INSERT INTO "Discussion" ("id", "teamId", "meetingId", "discussionTopicId", "discussionTopicType") -VALUES :discussions;