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(rethinkdb): SlackAuth #10154

Merged
merged 21 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"config": {
"contextType": "../graphql#GQLContext",
"mappers": {
"SetSlackNotificationPayload": "./types/SetSlackNotificationPayload#SetSlackNotificationPayloadSource",
"SetDefaultSlackChannelSuccess": "./types/SetDefaultSlackChannelSuccess#SetDefaultSlackChannelSuccessSource",
"AddSlackAuthPayload": "./types/AddSlackAuthPayload#AddSlackAuthPayloadSource",
"RemoveAgendaItemPayload": "./types/RemoveAgendaItemPayload#RemoveAgendaItemPayloadSource",
"AddAgendaItemPayload": "./types/AddAgendaItemPayload#AddAgendaItemPayloadSource",
"UpdateAgendaItemPayload": "./types/UpdateAgendaItemPayload#UpdateAgendaItemPayloadSource",
Expand Down
5 changes: 5 additions & 0 deletions packages/server/dataloader/foreignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
selectAgendaItems,
selectOrganizations,
selectRetroReflections,
selectSlackAuths,
selectSuggestedAction,
selectTeams,
selectTemplateDimension,
Expand Down Expand Up @@ -191,3 +192,7 @@ export const agendaItemsByMeetingId = foreignKeyLoaderMaker(
return selectAgendaItems().where('meetingId', 'in', meetingIds).orderBy('sortOrder').execute()
}
)

export const slackAuthByUserId = foreignKeyLoaderMaker('slackAuths', 'userId', async (userIds) => {
return selectSlackAuths().where('userId', 'in', userIds).execute()
})
5 changes: 5 additions & 0 deletions packages/server/dataloader/primaryKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
selectMeetingSettings,
selectOrganizations,
selectRetroReflections,
selectSlackAuths,
selectSuggestedAction,
selectTeamPromptResponses,
selectTeams,
Expand Down Expand Up @@ -95,3 +96,7 @@ export const meetingSettings = primaryKeyLoaderMaker((ids: readonly string[]) =>
export const agendaItems = primaryKeyLoaderMaker((ids: readonly string[]) => {
return selectAgendaItems().where('id', 'in', ids).execute()
})

export const slackAuths = primaryKeyLoaderMaker((ids: readonly string[]) => {
return selectSlackAuths().where('id', 'in', ids).execute()
})
9 changes: 0 additions & 9 deletions packages/server/dataloader/rethinkForeignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ export const meetingMembersByUserId = new RethinkForeignKeyLoaderMaker(
}
)

export const slackAuthByUserId = new RethinkForeignKeyLoaderMaker(
'slackAuths',
'userId',
async (userIds) => {
const r = await getRethink()
return r.table('SlackAuth').getAll(r.args(userIds), {index: 'userId'}).run()
}
)

export const slackNotificationsByTeamId = new RethinkForeignKeyLoaderMaker(
'slackNotifications',
'teamId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const meetingMembers = new RethinkPrimaryKeyLoaderMaker('MeetingMember')
export const newMeetings = new RethinkPrimaryKeyLoaderMaker('NewMeeting')
export const newFeatures = new RethinkPrimaryKeyLoaderMaker('NewFeature')
export const notifications = new RethinkPrimaryKeyLoaderMaker('Notification')
export const slackAuths = new RethinkPrimaryKeyLoaderMaker('SlackAuth')
export const slackNotifications = new RethinkPrimaryKeyLoaderMaker('SlackNotification')
export const tasks = new RethinkPrimaryKeyLoaderMaker('Task')
export const teamInvitations = new RethinkPrimaryKeyLoaderMaker('TeamInvitation')
135 changes: 0 additions & 135 deletions packages/server/graphql/mutations/addSlackAuth.ts

This file was deleted.

34 changes: 17 additions & 17 deletions packages/server/graphql/mutations/helpers/activatePrevSlackAuth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import ms from 'ms'
import getRethink from '../../../database/rethinkDriver'
import {DataLoaderInstance} from '../../../dataloader/RootDataLoader'
import getKysely from '../../../postgres/getKysely'
import {Logger} from '../../../utils/Logger'
import SlackServerManager from '../../../utils/SlackServerManager'
import {upsertNotifications} from '../addSlackAuth'

const activatePrevSlackAuth = async (userId: string, teamId: string) => {
const r = await getRethink()
const now = new Date()
const previousAuth = await r
.table('SlackAuth')
.getAll(userId, {index: 'userId'})
.filter({teamId})
.nth(0)
.default(null)
.run()
import {upsertNotifications} from '../../public/mutations/addSlackAuth'

const activatePrevSlackAuth = async (
userId: string,
teamId: string,
dataLoader: DataLoaderInstance
) => {
const previousAuths = await dataLoader.get('slackAuthByUserId').load(userId)
const previousAuth = previousAuths.find((auth) => auth.teamId === teamId)
if (!previousAuth) return
const LAST_YEAR = new Date(Date.now() - ms('1y'))
const {
Expand All @@ -34,10 +31,13 @@ const activatePrevSlackAuth = async (userId: string, teamId: string) => {
return
}

await r({
auth: r.table('SlackAuth').get(authId).update({isActive: true, updatedAt: now}),
notifications: upsertNotifications(userId, teamId, defaultTeamChannelId, slackUserId)
}).run()
await getKysely()
.updateTable('SlackAuth')
.set({isActive: true})
.where('id', '=', authId)
.execute()
dataLoader.clearAll('slackAuths')
await upsertNotifications(userId, teamId, defaultTeamChannelId, slackUserId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,22 +592,15 @@ export const SlackNotifier = {
stageIndex: number,
channelId: string
) {
const r = await getRethink()
const [team, user, meeting, reflectionGroup, reflections, slackAuth] = await Promise.all([
const [team, user, meeting, reflectionGroup, reflections, userSlackAuths] = await Promise.all([
dataLoader.get('teams').loadNonNull(teamId),
dataLoader.get('users').loadNonNull(userId),
dataLoader.get('newMeetings').load(meetingId),
dataLoader.get('retroReflectionGroups').loadNonNull(reflectionGroupId),
dataLoader.get('retroReflectionsByGroupId').load(reflectionGroupId),
r
.table('SlackAuth')
.getAll(userId, {index: 'userId'})
.filter({teamId})
.nth(0)
.default(null)
.run()
dataLoader.get('slackAuthByUserId').load(userId)
])

const slackAuth = userSlackAuths.find((auth) => auth.teamId === teamId)
if (!slackAuth) {
throw new Error('Slack auth not found')
}
Expand Down
31 changes: 11 additions & 20 deletions packages/server/graphql/mutations/helpers/removeSlackAuths.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
import getRethink from '../../../database/rethinkDriver'
import getKysely from '../../../postgres/getKysely'

const removeSlackAuths = async (
userId: string,
teamIds: string | string[],
removeToken?: boolean
) => {
const r = await getRethink()
const now = new Date()
const teamIdsArr = Array.isArray(teamIds) ? teamIds : [teamIds]
const existingAuths = await r
.table('SlackAuth')
.getAll(r.args(teamIdsArr), {index: 'teamId'})
.filter({userId})
.run()

if (!existingAuths.length) {
const error = new Error('Auth not found')
return {authIds: null, error}
if (teamIdsArr.length === 0) {
return {authIds: null, error: 'No teams provided'}
}
const inactiveAuths = await getKysely()
.updateTable('SlackAuth')
.set({botAccessToken: removeToken ? null : undefined, isActive: false})
.where('teamId', 'in', teamIdsArr)
.where('userId', '=', userId)
.returning('id')
.execute()

const authIds = existingAuths.map(({id}) => id)
await r({
auth: r
.table('SlackAuth')
.getAll(r.args(authIds))
.update({
botAccessToken: removeToken ? null : undefined,
isActive: false,
updatedAt: now
}),
notifications: r
.table('SlackNotification')
.getAll(r.args(teamIdsArr), {index: 'teamId'})
.filter({userId})
.delete()
}).run()

const authIds = inactiveAuths.map(({id}) => id)
return {authIds, error: null}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/graphql/mutations/removeSlackAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default {
removeSlackAuths(viewerId, teamId, true),
dataLoader.get('users').loadNonNull(viewerId)
])
if (res.error) {
return standardError(res.error, {userId: viewerId})
if (!res.authIds) {
return {error: {message: res.error}}
}
const authId = res.authIds[0]

Expand Down
Loading
Loading