Skip to content

Commit

Permalink
Merge branch 'v3' of https://github.com/uzh-bf/klicker-uzh into DemoQ…
Browse files Browse the repository at this point in the history
…uestions
  • Loading branch information
sjschlapbach committed Sep 27, 2023
2 parents 939e4a8 + c797161 commit d8bc439
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 294 deletions.
1 change: 1 addition & 0 deletions apps/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@klicker-uzh/shared-components": "workspace:*",
"@next-auth/prisma-adapter": "1.0.7",
"@uzh-bf/design-system": "2.1.7",
"axios": "1.4.0",
"bcryptjs": "2.4.3",
"js-cookie": "3.0.5",
"jsonwebtoken": "9.0.1",
Expand Down
15 changes: 15 additions & 0 deletions apps/auth/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from 'axios'

export async function sendTeamsNotifications(scope: string, text: string) {
if (process.env.TEAMS_WEBHOOK_URL) {
return axios.post(process.env.TEAMS_WEBHOOK_URL, {
'@context': 'https://schema.org/extensions',
'@type': 'MessageCard',
themeColor: '0076D7',
title: scope,
text: `[${process.env.NODE_ENV}:${scope}] ${text}`,
})
}

return null
}
10 changes: 9 additions & 1 deletion apps/auth/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sendTeamsNotifications } from '@/lib/util'
import { UserLoginScope, UserRole } from '@klicker-uzh/prisma'
import { PrismaAdapter } from '@next-auth/prisma-adapter'
import bcrypt from 'bcryptjs'
Expand Down Expand Up @@ -194,7 +195,7 @@ export const authOptions: NextAuthOptions = {
})

if (userAccount) {
await prisma.user.update({
const user = await prisma.user.update({
where: { id: userAccount.userId },
data: {
email: profile.email,
Expand All @@ -206,6 +207,13 @@ export const authOptions: NextAuthOptions = {
) ?? false,
},
})

if (user.firstLogin) {
await sendTeamsNotifications(
'eduId/signUp',
`User ${user.shortname} with email ${user.email} logged in for the first time.`
)
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/graphql/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { GraphQLError } from 'graphql'
Expand Down Expand Up @@ -38,4 +39,18 @@ export function formatDate(dateTime: Date) {
}
}

export async function sendTeamsNotifications(scope: string, text: string) {
if (process.env.TEAMS_WEBHOOK_URL) {
return axios.post(process.env.TEAMS_WEBHOOK_URL, {
'@context': 'https://schema.org/extensions',
'@type': 'MessageCard',
themeColor: '0076D7',
title: scope,
text: `[${process.env.NODE_ENV}:${scope}] ${text}`,
})
}

return null
}

export { levelFromXp } from '@klicker-uzh/prisma/dist/util'
18 changes: 17 additions & 1 deletion packages/graphql/src/services/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import JWT from 'jsonwebtoken'
import isEmail from 'validator/lib/isEmail'
import normalizeEmail from 'validator/lib/normalizeEmail'
import { Context, ContextWithUser } from '../lib/context'
import { sendTeamsNotifications } from '../lib/util'
import { prepareInitialInstanceResults, processQuestionData } from './sessions'

const COOKIE_SETTINGS: CookieOptions = {
Expand Down Expand Up @@ -37,7 +38,13 @@ export async function loginUserToken(
where: { email: normalizedEmail },
})

if (!user) return null
if (!user) {
await sendTeamsNotifications(
'graphql/loginUserToken',
`LOGIN FAILED: User with email ${normalizedEmail} not found.`
)
return null
}

const isLoginValid =
token === user.loginToken &&
Expand Down Expand Up @@ -313,12 +320,21 @@ export async function createParticipantAccount(

ctx.res.cookie('NEXT_LOCALE', participant.locale, COOKIE_SETTINGS)

await sendTeamsNotifications(
'graphql/createParticipantAccount',
`New participant account created: ${participant.email}`
)

return {
participant,
participantToken: jwt,
}
} catch (e) {
console.error(e)
await sendTeamsNotifications(
'graphql/createParticipantAccount',
`Failed to create participant account: ${email}`
)
return null
}
}
Expand Down
Loading

0 comments on commit d8bc439

Please sign in to comment.