-
Notifications
You must be signed in to change notification settings - Fork 69
/
session.ts
66 lines (58 loc) · 1.65 KB
/
session.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
import type { UserLesson } from '.prisma/client'
import { Context } from '../../@types/helpers'
import { userInfo } from './userInfoController'
import prisma from '../../prisma'
interface lessonMentorMapType {
[lessonId: number]: string
}
export const session = async (_parent: void, _args: void, context: Context) => {
const user = context?.req?.user
if (!user) return { lessonStatus: [] }
// FYI: The reason we are querying with parallelized promises:
// https://github.com/garageScript/c0d3-app/wiki/Sequelize-Query-Performance
const [session, starsGiven] = await Promise.all([
userInfo(_parent, { username: user.username }),
prisma.star.findMany({
where: {
studentId: user.id
},
include: {
lesson: {
select: {
id: true
}
},
mentor: {
select: {
username: true
}
}
}
})
])
const {
submissions,
lessonStatus: userLessons,
user: { discordUserId, discordAvatarUrl, discordUsername }
} = session
const userWithDiscordStatus = {
...user,
discordUserId,
discordAvatarUrl,
discordUsername,
isConnectedToDiscord: !!user.discordId // using this to avoid a second fetch to get Discord username
}
const lessonMentorMap = starsGiven.reduce((map, { lessonId, mentor }) => {
map[lessonId] = mentor.username
return map
}, {} as lessonMentorMapType)
const lessonStatus = userLessons.map((userLesson: UserLesson) => ({
...userLesson,
starGiven: lessonMentorMap[userLesson.lessonId!] || ''
}))
return {
user: userWithDiscordStatus,
lessonStatus,
submissions
}
}