Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: read ReflectionGroups from PG #9801

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"RequestToJoinDomainSuccess": "./types/RequestToJoinDomainSuccess#RequestToJoinDomainSuccessSource",
"ResetReflectionGroupsSuccess": "./types/ResetReflectionGroupsSuccess#ResetReflectionGroupsSuccessSource",
"RetroReflection": "../../database/types/RetroReflection#default as RetroReflectionDB",
"RetroReflectionGroup": "../../database/types/RetroReflectionGroup#default as RetroReflectionGroupDB",
"RetroReflectionGroup": "./types/RetroReflectionGroup#RetroReflectionGroupSource",
"RetrospectiveMeeting": "../../database/types/MeetingRetrospective#default",
"RetrospectiveMeetingMember": "../../database/types/RetroMeetingMember#default",
"RetrospectiveMeetingSettings": "../../database/types/MeetingSettingsRetrospective#default",
Expand Down
12 changes: 10 additions & 2 deletions packages/client/modules/demo/ClientGraphQLServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import NewMeetingStage from '../../../server/database/types/GenericMeetingStage'
import GoogleAnalyzedEntity from '../../../server/database/types/GoogleAnalyzedEntity'
import ReflectPhase from '../../../server/database/types/ReflectPhase'
import Reflection from '../../../server/database/types/Reflection'
import ReflectionGroup from '../../../server/database/types/ReflectionGroup'
import ITask from '../../../server/database/types/Task'
import {
ExternalLinks,
Expand Down Expand Up @@ -69,8 +68,17 @@ export type DemoReflection = Omit<Reflection, 'reactjis' | 'createdAt' | 'update
updatedAt: string | Date
}

export type DemoReflectionGroup = Omit<ReflectionGroup, 'team' | 'createdAt' | 'updatedAt'> & {
export type DemoReflectionGroup = {
__typename: string
id: string
isActive: boolean
meetingId: string
promptId: string
sortOrder: number
smartTitle: string | null
summary: string | null
title: string | null
discussionPromptQuestion: string | null
commentors: any
createdAt: string | Date
meeting: any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const makeSimilarDiscussionLink = async (
const {meetingId, discussionTopicId: reflectionGroupId} = discussion
const [meeting, reflectionGroup] = await Promise.all([
dataLoader.get('newMeetings').load(meetingId),
dataLoader.get('retroReflectionGroups').load(reflectionGroupId)
dataLoader.get('retroReflectionGroups').loadNonNull(reflectionGroupId)
])

if (!meeting || !isRetroMeeting(meeting)) throw new Error('invalid meeting type')
Expand Down
5 changes: 0 additions & 5 deletions packages/server/database/rethinkDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import OrganizationUser from './types/OrganizationUser'
import PasswordResetRequest from './types/PasswordResetRequest'
import PushInvitation from './types/PushInvitation'
import Reflection from './types/Reflection'
import ReflectionGroup from './types/ReflectionGroup'
import RetrospectivePrompt from './types/RetrospectivePrompt'
import SAML from './types/SAML'
import SuggestedActionCreateNewTeam from './types/SuggestedActionCreateNewTeam'
Expand Down Expand Up @@ -143,10 +142,6 @@ export type RethinkSchema = {
type: MeetingTemplate
index: 'teamId' | 'orgId'
}
RetroReflectionGroup: {
type: ReflectionGroup
index: 'meetingId'
}
RetroReflection: {
type: Reflection
index: 'meetingId' | 'reflectionGroupId'
Expand Down
14 changes: 14 additions & 0 deletions packages/server/dataloader/foreignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ export const embeddingsMetadataByRefId = foreignKeyLoaderMaker(
return pg.selectFrom('EmbeddingsMetadata').selectAll().where('refId', 'in', refId).execute()
}
)

export const retroReflectionGroupsByMeetingId = foreignKeyLoaderMaker(
'retroReflectionGroups',
'meetingId',
async (meetingIds) => {
const pg = getKysely()
return pg
.selectFrom('RetroReflectionGroup')
.selectAll()
.where('meetingId', 'in', meetingIds)
.where('isActive', '=', true)
.execute()
}
)
4 changes: 4 additions & 0 deletions packages/server/dataloader/primaryKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ export const domainJoinRequests = primaryKeyLoaderMaker(getDomainJoinRequestsByI
export const embeddingsMetadata = primaryKeyLoaderMaker((ids: readonly number[]) => {
return getKysely().selectFrom('EmbeddingsMetadata').selectAll().where('id', 'in', ids).execute()
})

export const retroReflectionGroups = primaryKeyLoaderMaker((ids: readonly string[]) => {
return getKysely().selectFrom('RetroReflectionGroup').selectAll().where('id', 'in', ids).execute()
})
13 changes: 0 additions & 13 deletions packages/server/dataloader/rethinkForeignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,6 @@ export const organizationUsersByUserId = new RethinkForeignKeyLoaderMaker(
}
)

export const retroReflectionGroupsByMeetingId = new RethinkForeignKeyLoaderMaker(
'retroReflectionGroups',
'meetingId',
async (meetingIds) => {
const r = await getRethink()
return r
.table('RetroReflectionGroup')
.getAll(r.args(meetingIds), {index: 'meetingId'})
.filter({isActive: true})
.run()
}
)

export const scalesByTeamId = new RethinkForeignKeyLoaderMaker(
'templateScales',
'teamId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const notifications = new RethinkPrimaryKeyLoaderMaker('Notification')
export const organizations = new RethinkPrimaryKeyLoaderMaker('Organization')
export const organizationUsers = new RethinkPrimaryKeyLoaderMaker('OrganizationUser')
export const templateScales = new RethinkPrimaryKeyLoaderMaker('TemplateScale')
export const retroReflectionGroups = new RethinkPrimaryKeyLoaderMaker('RetroReflectionGroup')
export const retroReflections = new RethinkPrimaryKeyLoaderMaker('RetroReflection')
export const slackAuths = new RethinkPrimaryKeyLoaderMaker('SlackAuth')
export const slackNotifications = new RethinkPrimaryKeyLoaderMaker('SlackNotification')
Expand Down
1 change: 0 additions & 1 deletion packages/server/graphql/mutations/createReflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export default {

await Promise.all([
pg.insertInto('RetroReflectionGroup').values(reflectionGroup).execute(),
r.table('RetroReflectionGroup').insert(reflectionGroup).run(),
r.table('RetroReflection').insert(reflection).run()
])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getRethink from '../../../database/rethinkDriver'
import getKysely from '../../../postgres/getKysely'
import OpenAIServerManager from '../../../utils/OpenAIServerManager'
import sendToSentry from '../../../utils/sendToSentry'
Expand Down Expand Up @@ -26,7 +25,6 @@ const generateDiscussionPrompt = async (
dataLoader.get('retroReflectionsByMeetingId').load(meetingId),
dataLoader.get('retroReflectionGroupsByMeetingId').load(meetingId)
])
const r = await getRethink()
const pg = getKysely()
const manager = new OpenAIServerManager()
if (!reflectionGroups.length) {
Expand All @@ -46,14 +44,11 @@ const generateDiscussionPrompt = async (
)
if (!fullQuestion) return
const discussionPromptQuestion = fullQuestion?.slice(0, 2000)
return Promise.all([
pg
.updateTable('RetroReflectionGroup')
.set({discussionPromptQuestion})
.where('id', '=', group.id)
.execute(),
r.table('RetroReflectionGroup').get(group.id).update({discussionPromptQuestion}).run()
])
return pg
.updateTable('RetroReflectionGroup')
.set({discussionPromptQuestion})
.where('id', '=', group.id)
.execute()
})
)
}
Expand Down
21 changes: 5 additions & 16 deletions packages/server/graphql/mutations/helpers/handleCompletedStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const handleCompletedRetrospectiveStage = async (
if (stage.phaseType === REFLECT) {
const r = await getRethink()
const pg = getKysely()
const now = new Date()

const [reflectionGroups, reflections] = await Promise.all([
dataLoader.get('retroReflectionGroupsByMeetingId').load(meeting.id),
Expand All @@ -60,21 +59,11 @@ const handleCompletedRetrospectiveStage = async (
await Promise.all(
sortedReflectionGroups.map((group, index) => {
group.sortOrder = index
return Promise.all([
pg
.updateTable('RetroReflectionGroup')
.set({sortOrder: index})
.where('id', '=', group.id)
.execute(),
r
.table('RetroReflectionGroup')
.get(group.id)
.update({
sortOrder: index,
updatedAt: now
} as any)
.run()
])
return pg
.updateTable('RetroReflectionGroup')
.set({sortOrder: index})
.where('id', '=', group.id)
.execute()
})
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ export const SlackNotifier = {
dataLoader.get('teams').loadNonNull(teamId),
dataLoader.get('users').loadNonNull(userId),
dataLoader.get('newMeetings').load(meetingId),
dataLoader.get('retroReflectionGroups').load(reflectionGroupId),
dataLoader.get('retroReflectionGroups').loadNonNull(reflectionGroupId),
r.table('RetroReflection').getAll(reflectionGroupId, {index: 'reflectionGroupId'}).run(),
r
.table('SlackAuth')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const removeEmptyReflectionGroup = async (
) => {
const r = await getRethink()
const pg = getKysely()
const now = new Date()
if (!reflectionGroupId) return false
const reflectionCount = await r
.table('RetroReflection')
Expand All @@ -17,18 +16,11 @@ const removeEmptyReflectionGroup = async (
.run()
if (reflectionCount > 0) return

return Promise.all([
pg
.updateTable('RetroReflectionGroup')
.set({isActive: false})
.where('id', '=', oldReflectionGroupId)
.execute(),
r
.table('RetroReflectionGroup')
.get(oldReflectionGroupId)
.update({isActive: false, updatedAt: now})
.run()
])
return pg
.updateTable('RetroReflectionGroup')
.set({isActive: false})
.where('id', '=', oldReflectionGroupId)
.execute()
}

export default removeEmptyReflectionGroup
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ const removeEmptyReflections = async (meeting: Meeting) => {
.updateTable('RetroReflectionGroup')
.set({isActive: false})
.where('id', 'in', emptyReflectionGroupIds)
.execute(),
r
.table('RetroReflectionGroup')
.getAll(r.args(emptyReflectionGroupIds), {index: 'id'})
.update({
isActive: false
})
.run()
.execute()
])
}
return {emptyReflectionGroupIds}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ const safeEndRetrospective = async ({
.where('meetingId', '=', meetingId)
.where('isActive', '=', false)
.execute(),
r
.table('RetroReflectionGroup')
.getAll(meetingId, {index: 'meetingId'})
.filter({isActive: false})
.delete()
.run(),
updateTeamInsights(teamId, dataLoader)
])
// wait for removeEmptyTasks before summarizeRetroMeeting
Expand Down
44 changes: 13 additions & 31 deletions packages/server/graphql/mutations/helpers/safelyCastVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {RValue} from '../../../database/stricterR'
import AuthToken from '../../../database/types/AuthToken'
import getKysely from '../../../postgres/getKysely'
import {getUserId} from '../../../utils/authorization'
import sendToSentry from '../../../utils/sendToSentry'
import standardError from '../../../utils/standardError'

const safelyCastVote = async (
Expand Down Expand Up @@ -40,36 +39,19 @@ const safelyCastVote = async (
return standardError(new Error('No votes remaining'), {userId: viewerId})
}

const [isVoteAddedToGroup, voteAddedResult] = await Promise.all([
r
.table('RetroReflectionGroup')
.get(reflectionGroupId)
.update((group: RValue) => {
return r.branch(
group('voterIds').count(userId).lt(maxVotesPerGroup),
{
updatedAt: now,
voterIds: group('voterIds').append(userId)
},
{}
)
})('replaced')
.eq(1)
.run(),
pg
.updateTable('RetroReflectionGroup')
.set({voterIds: sql`ARRAY_APPEND("voterIds",${userId})`})
.where('id', '=', reflectionGroupId)
.where(
sql`COALESCE(array_length(array_positions("voterIds", ${userId}),1),0)`,
'<',
maxVotesPerGroup
)
.executeTakeFirst()
])
const isVoteAddedToGroupPG = voteAddedResult.numUpdatedRows === BigInt(1)
if (isVoteAddedToGroupPG !== isVoteAddedToGroup)
sendToSentry(new Error('MISMATCH VOTE CAST LOGIC'))
const voteAddedResult = await pg
.updateTable('RetroReflectionGroup')
.set({voterIds: sql`ARRAY_APPEND("voterIds",${userId})`})
.where('id', '=', reflectionGroupId)
.where(
sql`COALESCE(array_length(array_positions("voterIds", ${userId}),1),0)`,
'<',
maxVotesPerGroup
)
.executeTakeFirst()

const isVoteAddedToGroup = voteAddedResult.numUpdatedRows === BigInt(1)

if (!isVoteAddedToGroup) {
await r
.table('MeetingMember')
Expand Down
38 changes: 9 additions & 29 deletions packages/server/graphql/mutations/helpers/safelyWithdrawVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {RValue} from '../../../database/stricterR'
import AuthToken from '../../../database/types/AuthToken'
import getKysely from '../../../postgres/getKysely'
import {getUserId} from '../../../utils/authorization'
import sendToSentry from '../../../utils/sendToSentry'
import standardError from '../../../utils/standardError'

const safelyWithdrawVote = async (
Expand All @@ -19,37 +18,18 @@ const safelyWithdrawVote = async (
const pg = getKysely()
const now = new Date()
const viewerId = getUserId(authToken)
const [isVoteRemovedFromGroup, voteRemovedResult] = await Promise.all([
r
.table('RetroReflectionGroup')
.get(reflectionGroupId)
.update((group: RValue) => {
return r.branch(
group('voterIds').offsetsOf(userId).count().ge(1),
{
updatedAt: now,
voterIds: group('voterIds').deleteAt(group('voterIds').offsetsOf(userId).nth(0))
},
{}
)
})('replaced')
.eq(1)
.run(),
pg
.updateTable('RetroReflectionGroup')
.set({
voterIds: sql`array_cat(
const voteRemovedResult = await pg
.updateTable('RetroReflectionGroup')
.set({
voterIds: sql`array_cat(
"voterIds"[1:array_position("voterIds",${userId})-1],
"voterIds"[array_position("voterIds",${userId})+1:]
)`
})
.where('id', '=', reflectionGroupId)
.where(sql`${userId}`, '=', sql`ANY("voterIds")`)
.executeTakeFirst()
])
const isVoteRemovedFromGroupPG = voteRemovedResult.numUpdatedRows === BigInt(1)
if (isVoteRemovedFromGroup !== isVoteRemovedFromGroupPG)
sendToSentry(new Error('MISMATCH VOTE REMOVED LOGIC'))
})
.where('id', '=', reflectionGroupId)
.where(sql`${userId}`, '=', sql`ANY("voterIds")`)
.executeTakeFirst()
const isVoteRemovedFromGroup = voteRemovedResult.numUpdatedRows === BigInt(1)
if (!isVoteRemovedFromGroup) {
return standardError(new Error('Already removed vote'), {userId: viewerId})
}
Expand Down
Loading
Loading