Skip to content

Commit

Permalink
Merge branch 'PWATypes' of https://github.com/uzh-bf/klicker-uzh into…
Browse files Browse the repository at this point in the history
… PWATypes
  • Loading branch information
sjschlapbach committed May 30, 2024
2 parents 08c88d0 + 605e97b commit 1c386a0
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 39 deletions.
1 change: 1 addition & 0 deletions apps/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"scripts": {
"build": "next build",
"build:test": "cross-env NODE_ENV=test next build",
"check": "tsc",
"dev": "next dev --port 3010",
"dev:offline": "next dev --port 3010",
"dev:test": "cross-env NODE_ENV=test next dev --port 3010",
Expand Down
34 changes: 24 additions & 10 deletions apps/auth/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ import prisma from 'src/lib/prisma'

export const COOKIE_NAME = 'next-auth.session-token'

interface ExtendedProfile extends Profile {
export interface ExtendedProfile extends Profile {
swissEduPersonUniqueID: string
swissEduIDLinkedAffiliation?: string[]
}

export interface ExtendedUser {
id: string
email: string
role: UserRole
shortname: string
scope: string
catalystInstitutional: boolean
catalystIndividual: boolean
}

function reduceCatalyst(acc: boolean, affiliation: string) {
try {
if (
Expand Down Expand Up @@ -207,12 +217,13 @@ export const authOptions: NextAuthOptions = {

callbacks: {
async signIn({ user, account, profile, email }) {
if (profile?.sub && account?.provider) {
const profileData = profile as ExtendedProfile
if (profileData?.sub && account?.provider) {
const userAccount = await prisma.account.findUnique({
where: {
provider_providerAccountId: {
provider: account.provider,
providerAccountId: profile.sub,
providerAccountId: profileData.sub,
},
},
})
Expand All @@ -221,11 +232,11 @@ export const authOptions: NextAuthOptions = {
const user = await prisma.user.update({
where: { id: userAccount.userId },
data: {
email: profile.email,
email: profileData.email,
lastLoginAt: new Date(),
catalystInstitutional:
(profile.email?.endsWith('uzh.ch') ||
profile.swissEduIDLinkedAffiliation?.reduce<boolean>(
(profileData.email?.endsWith('uzh.ch') ||
profileData.swissEduIDLinkedAffiliation?.reduce<boolean>(
reduceCatalyst,
false
)) ??
Expand All @@ -246,17 +257,20 @@ export const authOptions: NextAuthOptions = {
},

async jwt({ token, user, account, profile, trigger }) {
const profileData = profile as ExtendedProfile
const userData = user as ExtendedUser

if (typeof user !== 'undefined') {
token.shortname = user.shortname
token.shortname = userData.shortname

if (typeof profile?.swissEduPersonUniqueID === 'string') {
if (typeof profileData?.swissEduPersonUniqueID === 'string') {
token.scope = UserLoginScope.ACCOUNT_OWNER
} else {
token.scope = (user as any).scope as UserLoginScope
}

token.catalystInstitutional = user.catalystInstitutional
token.catalystIndividual = user.catalystIndividual
token.catalystInstitutional = userData.catalystInstitutional
token.catalystIndividual = userData.catalystIndividual

token.role = UserRole.USER
}
Expand Down
4 changes: 2 additions & 2 deletions apps/auth/src/pages/api/discourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export default async function handler(
const nonce = ssoURL.searchParams.get('nonce')
const redirectURL = ssoURL.searchParams.get('return_sso_url')

const userEmail = session.email
const userEmail = session.email!
const userId = session.sub
const userShortname = session.shortname
const userShortname = session.shortname as string
const userRole = session.role

let payload = `nonce=${nonce}&email=${encodeURIComponent(
Expand Down
2 changes: 1 addition & 1 deletion apps/backend-docker/scripts/checkRedisConsistency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaClient } from '@klicker-uzh/prisma'
import Redis from 'ioredis'
import { Redis } from 'ioredis'

async function run() {
const prisma = new PrismaClient()
Expand Down
2 changes: 1 addition & 1 deletion apps/backend-docker/scripts/fixPointsInconsistency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// pn exec:prod scripts/fixPointsInconsistency.ts

import { PrismaClient } from '@klicker-uzh/prisma'
import Redis from 'ioredis'
import { Redis } from 'ioredis'
const prisma = new PrismaClient()

const redisExec = new Redis({
Expand Down
6 changes: 3 additions & 3 deletions apps/backend-docker/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { usePersistedOperations } from '@graphql-yoga/plugin-persisted-operation
import { enhanceContext, schema } from '@klicker-uzh/graphql'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import express, { Request } from 'express'
import express, { type Request } from 'express'
import { createYoga } from 'graphql-yoga'
import { createRequire } from 'node:module'
import passport from 'passport'
Expand Down Expand Up @@ -102,7 +102,7 @@ function prepareApp({ prisma, redisExec, pubSub, cache, emitter }: any) {

app.use(cookieParser())
app.use((req: any, res, next) =>
passport.authenticate('jwt', { session: false }, (err, user) => {
passport.authenticate('jwt', { session: false }, (_: any, user: any) => {
req.locals = { user }
next()
})(req, res, next)
Expand Down Expand Up @@ -170,7 +170,7 @@ function prepareApp({ prisma, redisExec, pubSub, cache, emitter }: any) {
res.send('OK')
})

app.use('/api/graphql', yogaApp)
app.use('/api/graphql', yogaApp as any)

return { app, yogaApp }
}
Expand Down
4 changes: 2 additions & 2 deletions apps/backend-docker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { PrismaClient } from '@klicker-uzh/prisma'
import * as Sentry from '@sentry/node'
import '@sentry/tracing'
import { createPubSub } from 'graphql-yoga'
import Redis from 'ioredis'
import { Redis } from 'ioredis'
import prepareApp from './app.js'

import { Cache, createInMemoryCache } from '@envelop/response-cache'
import { createInMemoryCache, type Cache } from '@envelop/response-cache'
import { createRedisCache } from '@envelop/response-cache-redis'
import { useServer } from 'graphql-ws/lib/use/ws'
import { EventEmitter } from 'node:events'
Expand Down
15 changes: 9 additions & 6 deletions apps/backend-docker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"include": [
"./**/*"
],
"exclude": [
"instrumented/"
],
"compilerOptions": {
"baseUrl": ".",
/* Base Options: */
Expand All @@ -18,12 +24,9 @@
"module": "NodeNext",
"outDir": "dist",
"sourceMap": true,
/* AND if you're building for a library: */
"declaration": true,
/* AND if you're building for a library in a monorepo: */
"composite": true,
"declarationMap": true,
/* If your code doesn't run in the DOM: */
"lib": ["es2022"]
"lib": [
"es2022"
]
}
}
6 changes: 3 additions & 3 deletions packages/graphql/src/services/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ export async function getCourseOverviewData(
)

const sortByScoreAndUsername = R.curry(R.sortWith)([
R.descend(R.prop<number>('score')),
R.ascend(R.prop<string>('username')),
R.descend(R.prop('score')),
R.ascend(R.prop('username')),
])

const sortedEntries: typeof allEntries.mapped = sortByScoreAndUsername(
Expand Down Expand Up @@ -643,7 +643,7 @@ export async function getCourseData(
(groupActivity) => {
return {
...groupActivity,
numOfQuestions: groupActivity.stacks[0].elements.length,
numOfQuestions: groupActivity.stacks[0]!.elements.length,
}
}
)
Expand Down
12 changes: 6 additions & 6 deletions packages/graphql/src/services/feedbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function respondToFeedback(
},
})

if (!feedback || feedback.session.ownerId !== ctx.user.sub) return null
if (!feedback || feedback.session!.ownerId !== ctx.user.sub) return null

const feedbackPublished = feedback.isPublished
const updatedFeedback = await ctx.prisma.feedback.update({
Expand Down Expand Up @@ -185,7 +185,7 @@ export async function publishFeedback(
},
})

if (!feedback || feedback.session.ownerId !== ctx.user.sub) return null
if (!feedback || feedback.session!.ownerId !== ctx.user.sub) return null

const updatedFeedback = await ctx.prisma.feedback.update({
where: {
Expand Down Expand Up @@ -225,7 +225,7 @@ export async function pinFeedback(
},
})

if (!feedback || feedback.session.ownerId !== ctx.user.sub) return null
if (!feedback || feedback.session!.ownerId !== ctx.user.sub) return null

const updatedFeedback = await ctx.prisma.feedback.update({
where: {
Expand Down Expand Up @@ -261,7 +261,7 @@ export async function resolveFeedback(
},
})

if (!feedback || feedback.session.ownerId !== ctx.user.sub) return null
if (!feedback || feedback.session!.ownerId !== ctx.user.sub) return null

const updatedFeedback = await ctx.prisma.feedback.update({
where: { id },
Expand Down Expand Up @@ -296,7 +296,7 @@ export async function deleteFeedback(
},
})

if (!feedback || feedback.session.ownerId !== ctx.user.sub) return null
if (!feedback || feedback.session!.ownerId !== ctx.user.sub) return null

const deletedFeedback = await ctx.prisma.feedback.delete({
where: { id },
Expand Down Expand Up @@ -330,7 +330,7 @@ export async function deleteFeedbackResponse(

if (
!feedbackResponse ||
feedbackResponse.feedback.session.ownerId !== ctx.user.sub
feedbackResponse.feedback.session!.ownerId !== ctx.user.sub
)
return null

Expand Down
6 changes: 4 additions & 2 deletions packages/graphql/src/services/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export async function manipulateGroupActivity(
options: {},
elements: {
create: stack.elements.map((elem) => {
const element = elementMap[elem.elementId]
const element = elementMap[elem.elementId]!
const processedElementData = processElementData(element)

return {
Expand Down Expand Up @@ -841,6 +841,8 @@ export async function submitGroupActivityDecisions(
response: response,
})

if (!updatedResults.results) return

// update the instance with the new results
await prisma.elementInstance.update({
where: { id: instanceId },
Expand Down Expand Up @@ -1177,7 +1179,7 @@ export async function finalizeGroupActivityGrading(
leaderboard: participant.leaderboards.length > 0,
}
if (instance.results!.passed) {
acc[participant.id].achievements.push(8)
acc[participant.id]!.achievements.push(8)
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/services/microLearning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export async function manipulateMicroLearning(
options: {},
elements: {
create: stack.elements.map((elem) => {
const element = elementMap[elem.elementId]
const element = elementMap[elem.elementId]!

const processedElementData = processElementData(element)

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/services/participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export async function getParticipations(
if (!participant) return []

return R.sort(
R.ascend(R.prop<string>('course.displayName')),
R.ascend(R.prop('course.displayName')),
participant.participations
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/types/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
Element,
ElementStackType,
ElementType,
Expand Down

0 comments on commit 1c386a0

Please sign in to comment.