-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathsafeEndTeamPrompt.ts
137 lines (124 loc) · 4.59 KB
/
safeEndTeamPrompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {sql} from 'kysely'
import {SubscriptionChannel} from 'parabol-client/types/constEnums'
import {checkTeamsLimit} from '../../../billing/helpers/teamLimitsCheck'
import getRethink, {ParabolR} from '../../../database/rethinkDriver'
import TimelineEventTeamPromptComplete from '../../../database/types/TimelineEventTeamPromptComplete'
import getKysely from '../../../postgres/getKysely'
import {getTeamPromptResponsesByMeetingId} from '../../../postgres/queries/getTeamPromptResponsesByMeetingIds'
import {TeamPromptMeeting} from '../../../postgres/types/Meeting'
import {Logger} from '../../../utils/Logger'
import {analytics} from '../../../utils/analytics/analytics'
import publish, {SubOptions} from '../../../utils/publish'
import standardError from '../../../utils/standardError'
import {InternalContext} from '../../graphql'
import sendNewMeetingSummary from './endMeeting/sendNewMeetingSummary'
import gatherInsights from './gatherInsights'
import generateStandupMeetingSummary from './generateStandupMeetingSummary'
import {IntegrationNotifier} from './notifications/IntegrationNotifier'
import updateQualAIMeetingsCount from './updateQualAIMeetingsCount'
import updateTeamInsights from './updateTeamInsights'
const summarizeTeamPrompt = async (meeting: TeamPromptMeeting, context: InternalContext) => {
const {dataLoader} = context
const pg = getKysely()
const r = await getRethink()
const summary = await generateStandupMeetingSummary(meeting, dataLoader)
await pg.updateTable('NewMeeting').set({summary}).where('id', '=', meeting.id).execute()
await r
.table('NewMeeting')
.get(meeting.id)
.update({
summary
})
.run()
dataLoader.clearAll('newMeetings')
// wait for whole meeting summary to be generated before sending summary email and updating qualAIMeetingCount
sendNewMeetingSummary(meeting, context).catch(Logger.log)
updateQualAIMeetingsCount(meeting.id, meeting.teamId, dataLoader)
// wait for meeting stats to be generated before sending Slack notification
IntegrationNotifier.endMeeting(dataLoader, meeting.id, meeting.teamId)
const data = {meetingId: meeting.id}
const operationId = dataLoader.share()
const subOptions = {operationId}
publish(SubscriptionChannel.MEETING, meeting.id, 'EndTeamPromptSuccess', data, subOptions)
}
const safeEndTeamPrompt = async ({
meeting,
now,
viewerId,
r,
context,
subOptions
}: {
meeting: TeamPromptMeeting
now: Date
viewerId?: string
r: ParabolR
context: InternalContext
subOptions: SubOptions
}) => {
const pg = getKysely()
const {dataLoader} = context
const {endedAt, id: meetingId, teamId} = meeting
if (endedAt) return standardError(new Error('Meeting already ended'), {userId: viewerId})
// RESOLUTION
const insights = await gatherInsights(meeting, dataLoader)
await pg
.updateTable('NewMeeting')
.set({
endedAt: sql`CURRENT_TIMESTAMP`,
usedReactjis: JSON.stringify(insights.usedReactjis),
engagement: insights.engagement
})
.where('id', '=', meetingId)
.execute()
const completedTeamPrompt = await r
.table('NewMeeting')
.get(meetingId)
.update(
{
endedAt: now,
...insights
},
{returnChanges: true}
)('changes')(0)('new_val')
.default(null)
.run()
if (!completedTeamPrompt) {
return standardError(new Error('Completed team prompt meeting does not exist'), {
userId: viewerId
})
}
if (completedTeamPrompt.meetingType !== 'teamPrompt') {
return standardError(new Error('Meeting is not a team prompt'), {userId: viewerId})
}
const [meetingMembers, team, teamMembers, responses] = await Promise.all([
dataLoader.get('meetingMembersByMeetingId').load(meetingId),
dataLoader.get('teams').loadNonNull(teamId),
dataLoader.get('teamMembersByTeamId').load(teamId),
getTeamPromptResponsesByMeetingId(meetingId),
updateTeamInsights(teamId, dataLoader)
])
const events = teamMembers.map(
(teamMember) =>
new TimelineEventTeamPromptComplete({
userId: teamMember.userId,
teamId,
orgId: team.orgId,
meetingId
})
)
const timelineEventId = events[0]!.id
await pg.insertInto('TimelineEvent').values(events).execute()
summarizeTeamPrompt(meeting, context)
analytics.teamPromptEnd(completedTeamPrompt, meetingMembers, responses, dataLoader)
checkTeamsLimit(team.orgId, dataLoader)
dataLoader.get('newMeetings').clear(meetingId)
const data = {
meetingId,
teamId,
timelineEventId
}
publish(SubscriptionChannel.TEAM, teamId, 'EndTeamPromptSuccess', data, subOptions)
return data
}
export default safeEndTeamPrompt