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

Membership #1153

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 16 additions & 9 deletions apps/web/src/app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import ProfilePoster from "@/components/views/ProfileView"
import { getServerSession } from "next-auth"
import { redirect } from "next/navigation"
"use client"

const ProfilePage = async () => {
const session = await getServerSession()
import ProfilePoster from "@/components/views/ProfileView"
import { trpc } from "@/utils/trpc/client"
import { Button } from "@dotkomonline/ui"

if (session === null) {
redirect("/")
}
const ProfilePage = () => {
const { data: user } = trpc.user.getMe.useQuery()
const {
mutate: refreshMembership,
data: membership,
} = trpc.user.refreshMembership.useMutation();

return (
<>
<ProfilePoster user={session.user} />
{user && <ProfilePoster user={user} />}
<Button className="mt-8" onClick={() => refreshMembership()}>
Oppdater medlemsskap
</Button>
<pre>{JSON.stringify(membership, null, 2)}</pre>
<div className="h-screen" />
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import type { FC } from "react"
import StudentProgress from "../StudentProgress/StudentProgress"
import type { Membership, User } from "@dotkomonline/types"
import { membershipGrade } from "@dotkomonline/types"

function memberDescription(membership: Membership) {
switch (membership.type) {
case "BACHELOR":
return "Bachelor"
case "MASTER":
return "Master"
case "SOCIAL":
return "Sosialt medlem"
case "KNIGHT":
return "Ridder"
}
}

type StudyProgressionBoxProps = {
className?: string
user: User
}

const StudyProgressionBox: FC<StudyProgressionBoxProps> = ({ className }) => {
// TODO: Implement dynamic way of getting grade
const startYear = 2022
const grade = 3
const StudyProgressionBox: FC<StudyProgressionBoxProps> = ({ className, user }) => {
if (!user.membership) {
return null
}

const grade = membershipGrade(user.membership);

return (
<div className={`flex flex-col items-center justify-center gap-3 ${className ?? ""}`}>
<p>{grade}. klasse</p>
<p>Studiesett: {startYear}</p>
{ grade && <p className="text-3xl">{grade}. klasse</p> }
<p className="text-3xl">{memberDescription(user.membership)}</p>
<div className="transform scale-90">
<StudentProgress year={grade} />
{ grade && <StudentProgress year={grade} /> }
</div>
</div>
)
Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/components/organisms/Navbar/ProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ const InnerProfileMenu = () => {
<Button
variant="subtle"
className={cn(navigationMenuTriggerStyle(), "hover:translate-y-0 active:translate-y-0")}
onClick={async () => signIn("auth0")}
onClick={async () => signIn("auth0", undefined, {
prompt: "login",
})}
>
Log in
</Button>
<Button
variant="gradient"
className={cn(navigationMenuTriggerStyle(), "ml-3 hover:translate-y-0 active:translate-y-0")}
onClick={async () => signIn("auth0")}
onClick={async () => signIn("auth0", undefined, {
prompt: "signup",
})}
>
Sign up
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BioDisplay from "@/components/molecules/ProfileMolecules/BioDisplay"
import PersonalInfo from "@/components/molecules/ProfileMolecules/PersonalInfo"
import StudyProgressionBox from "@/components/molecules/ProfileMolecules/StudyProgressionBox"
import type { User } from "next-auth"
import type { User } from "@dotkomonline/types"
import type { FC } from "react"

type ProfileInfoBoxProps = {
Expand All @@ -27,7 +27,7 @@ const ProfileInfoBox: FC<ProfileInfoBoxProps> = ({ user }) => {
</div>
)}
<div className={`min-w-[410px] ${lineStyle}`}>
<StudyProgressionBox />
<StudyProgressionBox user={user} />
</div>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/views/ProfileView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ProfileInfoBox from "@/components/organisms/ProfileComponents/ProfileInfoBox"
import type { NextPage } from "next"
import type { User } from "next-auth"
import type { User } from "@dotkomonline/types"

const ProfilePoster: NextPage<{ user: User }> = ({ user }) => {
return (
Expand Down
14 changes: 14 additions & 0 deletions infra/auth0/js/actions/updateMembership.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
// Only try to link accounts when logging in via Feide
if (event.connection.id !== event.secrets.FEIDE_CONNECTION_ID) {
return;
}


Comment on lines +11 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra newlines

};
25 changes: 25 additions & 0 deletions infra/auth0/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ resource "auth0_client_grant" "monoweb_backend_mgmt_grant" {
scopes = [
"read:users",
"update:users",
"read:user_idp_tokens"
]
}

Expand Down Expand Up @@ -859,6 +860,30 @@ resource "auth0_action" "feide_account_linking" {
}
}

resource "auth0_action" "update_membership" {
count = terraform.workspace == "prd" ? 0 : 1

name = "Membership Update"
runtime = "node18"
code = file("js/actions/updateMembership.js")
deploy = true

supported_triggers {
id = "post-login"
version = "v3"
}

secrets {
name = "FEIDE_CONNECTION_ID"
value = auth0_connection.feide[0].id
}

secrets {
name = "RPC_HOST"
value = "rpc.staging.online.ntnu.no"
}
}

resource "auth0_trigger_actions" "login_flow" {
count = terraform.workspace == "prd" ? 0 : 1

Expand Down
27 changes: 17 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "monorepo",
"version": "1.0.0",
"description": "Monoweb is the next-generation web application for Online. This is the monorepo source.",
"keywords": ["online", "ntnu", "student-association"],
"keywords": [
"online",
"ntnu",
"student-association"
],
"homepage": "https://online.ntnu.no",
"author": "Dotkom <[email protected]> (https://online.ntnu.no)",
"bugs": {
Expand All @@ -24,24 +28,27 @@
"lint-check": "turbo run lint-check",
"test": "turbo run test",
"test:it": "turbo run test:it",
"dev": "pnpm -rc -F @dotkomonline/web -F @dotkomonline/dashboard -F @dotkomonline/rpc -F @dotkomonline/invoicification -F @dotkomonline/rif exec doppler run --preserve-env pnpm run dev",
"migrate": "pnpm -rc -F @dotkomonline/migrator exec doppler run --preserve-env pnpm run migrate",
"migrate-down": "pnpm -rc -F @dotkomonline/migrator exec doppler run --preserve-env pnpm run migrate -- down",
"migrate-down-all": "pnpm -rc -F @dotkomonline/migrator exec doppler run --preserve-env pnpm run migrate",
"migrate-with-fixtures": "pnpm -rc -F @dotkomonline/migrator exec doppler run --preserve-env pnpm run migrate latest --with-fixtures",
"migrate-with-sample-data": "pnpm -rc -F @dotkomonline/migrator exec doppler run --preserve-env pnpm run migrate -- latest --sample-data",
"dev": "pnpm -rc -F @dotkomonline/web -F @dotkomonline/dashboard -F @dotkomonline/rpc -F @dotkomonline/invoicification -F @dotkomonline/rif exec doppler run --preserve-env -- pnpm run dev",
"migrate": "pnpm -rc -F @dotkomonline/migrator exec doppler run -- pnpm run migrate",
"migrate-down": "pnpm -rc -F @dotkomonline/migrator exec doppler run -- pnpm run migrate -- down",
"migrate-down-all": "pnpm -rc -F @dotkomonline/migrator exec doppler run -- pnpm run migrate",
"migrate-with-fixtures": "pnpm -rc -F @dotkomonline/migrator exec doppler run -- pnpm run migrate latest --with-fixtures",
"migrate-with-sample-data": "pnpm -rc -F @dotkomonline/migrator exec doppler run -- pnpm run migrate -- latest --sample-data",
Comment on lines -27 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rebase

"storybook": "turbo run storybook --filter=storybook",
"type-check": "turbo run type-check",
"clean": "turbo run clean",
"docker:login": "aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 891459268445.dkr.ecr.eu-north-1.amazonaws.com",
"docker:login:public": "aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws",
"shell": "pnpm --filter=@dotkomonline/shell start"
},
"workspaces": ["packages/*", "apps/*"],
"packageManager": "[email protected]",
"workspaces": [
"packages/*",
"apps/*"
],
"packageManager": "[email protected]",
"engines": {
"node": ">=20.12.2",
"pnpm": ">=9.15.5"
"pnpm": ">=9.0.0"
},
"devDependencies": {
"turbo": "^2.3.4",
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@aws-sdk/s3-presigned-post": "^3.665.0",
"@dotkomonline/db": "workspace:*",
"@dotkomonline/logger": "workspace:*",
"@dotkomonline/utils": "workspace:*",
"auth0": "^4.18.0",
"date-fns": "^4.1.0",
"kysely": "^0.27.0",
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/modules/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import { type EventCompanyRepository, EventCompanyRepositoryImpl } from "./event
import { type EventCompanyService, EventCompanyServiceImpl } from "./event/event-company-service"
import { type EventRepository, EventRepositoryImpl } from "./event/event-repository"
import { type EventService, EventServiceImpl } from "./event/event-service"
import { type FeideGroupsRepository, FeideGroupsRepositoryImpl } from "./external/feide-groups-repository"
import {
type NTNUStudyplanRepository,
NTNUStudyplanRepositoryImpl,
} from "./external/ntnu-studyplan-repository/ntnu-studyplan-repository"
import { type S3Repository, S3RepositoryImpl } from "./external/s3-repository"
import { type InterestGroupRepository, InterestGroupRepositoryImpl } from "./interest-group/interest-group-repository"
import { type InterestGroupService, InterestGroupServiceImpl } from "./interest-group/interest-group-service"
Expand Down Expand Up @@ -132,10 +137,15 @@ export const createServiceLayer = async ({
const articleTagRepository: ArticleTagRepository = new ArticleTagRepositoryImpl(db)
const articleTagLinkRepository: ArticleTagLinkRepository = new ArticleTagLinkRepositoryImpl(db)

const feideGroupsRepository: FeideGroupsRepository = new FeideGroupsRepositoryImpl()
const ntnuStudyplanRepository: NTNUStudyplanRepository = new NTNUStudyplanRepositoryImpl()

const userService: UserService = new UserServiceImpl(
userRepository,
privacyPermissionsRepository,
notificationPermissionsRepository
notificationPermissionsRepository,
feideGroupsRepository,
ntnuStudyplanRepository
)

const eventCommitteeService: EventCommitteeService = new EventCommitteeServiceImpl(committeeOrganizerRepository)
Expand Down
76 changes: 76 additions & 0 deletions packages/core/src/modules/external/feide-groups-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { z } from "zod"

type GroupType = "subject" | "studyProgramme" | "studySpecialisation"

export type FeideGroup = {
name: string
code: string
finished?: Date
}

export type StudentInformation = {
courses: FeideGroup[]
studyProgrammes: FeideGroup[]
studySpecializations: FeideGroup[]
}

export interface FeideGroupsRepository {
getStudentInformation(accessToken: string): Promise<StudentInformation>
}

const FeideResponseGroupSchema = z.object({
id: z.string(),
type: z.string(),
displayName: z.string(),
parent: z.string().optional(),
membership: z
.object({
basic: z.string(),
displayName: z.string().optional(),
notAfter: z.string().optional(),
notBefore: z.string().optional(),
})
.optional(),
})

export class FeideGroupsRepositoryImpl implements FeideGroupsRepository {
private readonly baseUrl = "https://groups-api.dataporten.no/groups"

private responseGroupToFeideGroup(group: z.infer<typeof FeideResponseGroupSchema>): FeideGroup {
return {
name: group.displayName,
code: group.id.split(":")[5],
finished: group.membership?.notAfter ? new Date(group.membership.notAfter) : undefined,
}
}

async getStudentInformation(accessToken: string): Promise<StudentInformation> {
const response = await fetch(`${this.baseUrl}/me/groups`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})

if (!response.ok) {
throw new Error(`Failed to fetch student information: ${await response.text()}`)
}

const responseGroups = z.array(FeideResponseGroupSchema).parse(await response.json())

const courses = responseGroups.filter((group) => group.type === "fc:fs:emne").map(this.responseGroupToFeideGroup)

const studySpecializations = responseGroups
.filter((group) => group.type === "fc:fs:str")
.map(this.responseGroupToFeideGroup)

const studyProgrammes = responseGroups
.filter((group) => group.type === "fc:fs:prg")
.map(this.responseGroupToFeideGroup)

return {
courses,
studyProgrammes,
studySpecializations,
}
}
}
Loading