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

Api overhaul #87

Merged
merged 71 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
1948d7a
Replace "Profile" with first section of user name
yrjarv Sep 20, 2024
9cfb39e
Allow width of LoginButton to be automatic
yrjarv Sep 20, 2024
3ac806f
Enable shortening of first name in profile button
yrjarv Sep 23, 2024
9056baf
Added force-graph logic
EricSvebakk Sep 24, 2024
4e18084
Reduce max first name length in profile button to 15
yrjarv Sep 24, 2024
216dbd2
Reload page when user info is updated
yrjarv Sep 24, 2024
0b9a317
Replace hyphens in first name with non-breaking hyphen
yrjarv Sep 24, 2024
cee4b41
Merge pull request #44 from yrjarv/main
EricSvebakk Sep 26, 2024
99e5190
Manually replaces "About CYB" with "About" on mobile
yrjarv Sep 27, 2024
2c516d4
Audited mui version
EricSvebakk Oct 1, 2024
15dc90b
Update development.yml
EricSvebakk Oct 1, 2024
0972970
Merge branch 'feature-recruitment-graph' into development
EricSvebakk Oct 1, 2024
eb8b2f2
Merge branch 'development'
EricSvebakk Oct 1, 2024
4f7d813
Merge pull request #58 from yrjarv/shorter-about-mobile
EricSvebakk Oct 1, 2024
4dfd6f8
Merge branch 'development'
EricSvebakk Oct 1, 2024
bcbc8d1
Squashed commit adding pr/53
EricSvebakk Oct 10, 2024
420e092
Squashed commit adding pr/67:
EricSvebakk Oct 14, 2024
3a2cabc
Squashed commit adding pr/75
EricSvebakk Oct 17, 2024
ebe4c43
Squashed commit adding pr/64
EricSvebakk Oct 17, 2024
184fe7d
Squashed commit adding pr/82
EricSvebakk Oct 28, 2024
9440123
updated api to include api version in path
Sebbben Oct 29, 2024
fad4284
updated all links to use new api path
Sebbben Oct 29, 2024
c77a94a
beginning of api rewrite
Sebbben Oct 29, 2024
9e9ae13
logs
Sebbben Oct 29, 2024
fa72104
admin page converted to new api
Sebbben Oct 30, 2024
ab2adb6
middleware proof of concept
Sebbben Oct 31, 2024
123b657
further middleware implementation
Sebbben Nov 1, 2024
9745bd6
turned off middleware after realising it wont work for auth
Sebbben Nov 1, 2024
5a899b8
more REST api implementation
Sebbben Nov 1, 2024
2de5d3e
board page
Sebbben Nov 3, 2024
5612c7f
profile page
Sebbben Nov 3, 2024
d7a4473
volunteering page
Sebbben Nov 3, 2024
d8dbc35
fixed typo
Sebbben Nov 3, 2024
025025a
log and cafe pages
Sebbben Nov 4, 2024
535d39d
fixed stuff that broke with shift scheduler
Sebbben Nov 4, 2024
77b9ad2
patch request for profile page
Sebbben Nov 4, 2024
467ad3d
voucher and work logs posts
Sebbben Nov 4, 2024
2edd695
user to workgroup post
Sebbben Nov 5, 2024
9ca89f6
Add members
Sebbben Nov 5, 2024
ef572bb
Register user
Sebbben Nov 5, 2024
738ce30
cafe shift post
Sebbben Nov 5, 2024
5a0b333
admin setroles post
Sebbben Nov 5, 2024
5340df0
api rework complete, but potentialy contain bugs
Sebbben Nov 6, 2024
d96a721
bug fixes
Sebbben Nov 6, 2024
ba102fa
first auth attempt
Sebbben Nov 6, 2024
e80c943
second auth attempt and beginning of securing api
Sebbben Nov 7, 2024
c36efde
added x-auth-verified custom header to api responses that has been ve…
Sebbben Nov 7, 2024
b483e92
added custom header to api error responses
Sebbben Nov 7, 2024
5201382
role based and param based auth added to all api endpoints
Sebbben Nov 7, 2024
e40ccd6
workaround for chaining with async functions not working properly
Sebbben Nov 8, 2024
22a378d
fixed roles not showing on profile page
Sebbben Nov 8, 2024
35962f6
fixed some roles but that was introduced with slimming of session object
Sebbben Nov 8, 2024
eabcbd6
fix for half-chaining requirements
Sebbben Nov 10, 2024
f466be2
swithced back to chaining syntax, but made entire auth class non-async
Sebbben Nov 10, 2024
b59a8eb
replaced relative imports of authOptions for absolute import
Sebbben Nov 10, 2024
2384869
added ownership requirement to updating user userinfo
Sebbben Nov 10, 2024
edd075d
fixed edgecase in require ownership if session is null
Sebbben Nov 10, 2024
af8200f
admin buypass on ownership requirements
Sebbben Nov 10, 2024
83f5056
Auth util documentation updated
Sebbben Nov 10, 2024
81e8920
updated access to semester and semesterVolunteerInfo endpoints to not…
Sebbben Nov 11, 2024
f02aa36
fixed issues pointed out by pvk05's review
Sebbben Nov 11, 2024
22335f0
removed last middleware file
Sebbben Nov 12, 2024
6861a17
merged dev into featureBranch for fast forward merge when integrating…
Sebbben Nov 12, 2024
9546e15
fixed unmerged conflicts and removed middleware.js
Sebbben Nov 15, 2024
b71fa04
changed api auth to only require one of the given roles to get access…
Sebbben Nov 15, 2024
c53b2ed
fixed issue with switch case missing breaks so all requests returned …
Sebbben Nov 15, 2024
c1e1300
fixed problems with setting roles
Sebbben Nov 15, 2024
b6209d7
fixed issues with headers in customtabes on admin page
Sebbben Nov 15, 2024
dc4e9fd
changed default filter on logs table on admin page
Sebbben Nov 15, 2024
23799d5
removed intern role for accessing endoints
Sebbben Nov 15, 2024
2009232
fixed board page to not include orphans
Sebbben Nov 15, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
DATABASE_URL = 'mysql://${{ secrets.DATABASE_USER }}:${{ secrets.DATABASE_PASSWORD }}@localhost:3306/${{ secrets.DATABASE_NAME }}'

NEXTAUTH_URL = '${{ vars.NEXTAUTH_URL }}'
NEXTAUTH_CALLBACK = '${{ secrets.NEXTAUTH_URL }}/pages/main/home'
NEXTAUTH_CALLBACK = '${{ secrets.NEXTAUTH_URL }}/'
NEXTAUTH_SECRET = '${{ secrets.NEXTAUTH_SECRET }}'

NEXTAUTH_GITHUB_CLIENT_ID = '${{ vars.NEXTAUTH_GITHUB_CLIENT_ID }}'
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
DATABASE_URL = 'mysql://${{ secrets.DATABASE_USER }}:${{ secrets.DATABASE_PASSWORD }}@localhost:3306/${{ secrets.DATABASE_NAME }}'

NEXTAUTH_URL = '${{ vars.NEXTAUTH_URL }}'
NEXTAUTH_CALLBACK = '${{ secrets.NEXTAUTH_URL }}/pages/main/home'
NEXTAUTH_CALLBACK = '${{ secrets.NEXTAUTH_URL }}/'
NEXTAUTH_SECRET = '${{ secrets.NEXTAUTH_SECRET }}'

NEXTAUTH_GITHUB_CLIENT_ID = '${{ vars.NEXTAUTH_GITHUB_CLIENT_ID }}'
Expand All @@ -42,9 +42,7 @@ jobs:
NEXT_PUBLIC_SANITY_DATASET = '${{ vars.NEXT_PUBLIC_SANITY_DATASET }}'
NEXT_PUBLIC_SANITY_PROJECT_ID = '${{ vars.NEXT_PUBLIC_SANITY_PROJECT_ID }}'
EOT
npm install -f
npm audit fix
npm run prismapull
npm install
npx prisma generate
npm run build
npx pm2 restart ISV2_dev
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ DATABASE_SCHEMA = 'ISV2_development'
DATABASE_URL = "mysql://${DATABASE_USER}:${DATABASE_PASS}@localhost:3307/${DATABASE_SCHEMA}"

NEXTAUTH_URL = 'http://localhost:3005'
NEXTAUTH_CALLBACK = '/pages/main/home'
NEXTAUTH_CALLBACK = '/'
NEXTAUTH_SECRET = 'QuTxFdD3yon9X+83rRaio/vSgkfhvnwUglcIrnUwTLA='

NODEMAILER_NOREPLY_USER = '[email protected]'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions app/(pages)/(main)/admin/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from "next/server";

export async function middleware(req) {
Copy link
Member

Choose a reason for hiding this comment

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

remove unused function

return NextResponse.next()
}
143 changes: 59 additions & 84 deletions app/pages/main/admin/page.js → app/(pages)/(main)/admin/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Typography,
} from "@mui/material";
import { useEffect, useState } from "react";
import prismaRequest from "@/app/middleware/prisma/prismaRequest";
import authWrapper from "@/app/middleware/authWrapper";
import CustomAutoComplete from "@/app/components/input/CustomAutocomplete";
import Link from "next/link";
Expand All @@ -29,8 +28,9 @@ const TABLE_HEADERS_LOGS = [
{ id: "description", name: "Description" },
];

function AdminPage(params) {

function AdminPage({ props }) {


const [roles, setRoles] = useState([]);
const [users, setUsers] = useState([]);
const [logs, setLogs] = useState([]);
Expand All @@ -44,69 +44,66 @@ function AdminPage(params) {
const [roleChangeResponse, setRoleChangeResponse] = useState("");

useEffect(() => {

prismaRequest({
model: "role",
method: "find",
callback: (data) => setRoles(data.data),
});
fetch("/api/v2/roles")
.then(res => res.json())
.then(roles => {
setRoles(roles.roles)
})

prismaRequest({
model: "user",
method: "find",
request: {
include: {
roles: {
include: {
role: true,
},
},
},
},
callback: (data) => {
const users = data.data.map((e) => {
return {
...e,
name: `${e.firstName} ${e.lastName ? e.lastName : ""}`,
};
})
setUsers(users)
},
});
fetch("/api/v2/users")
.then(res => res.json())
.then((data) => {
const users = data.users.map((e) => {
return {
...e,
name: `${e.firstName} ${e.lastName ? e.lastName : ""}`,
};
})
setUsers(users)
})
}, []);

useEffect(() => {
if (selectedUser && showLogSettings) {
prismaRequest({
model: "workLog",
method: "find",
request: {
where: { loggedFor: selectedUser.id },
include: { LoggedByUser: true },
},
callback: (data) => {
const newLogs = data.data.map((e) => {
const user = e.LoggedByUser;
const name = user ? `${user.firstName} ${user.lastName}` : null;
return {
...e,
loggedBy: name,
workedAt_num: parseISO(e.workedAt).getTime(),
workedAt: format(
parseISO(e.workedAt),
"dd MMM 'kl.'HH:mm"
).toLowerCase(),
};
});

setLogs(newLogs);
}
if (selectedUser && showLogSettings) {
fetch(`/api/v2/users/${selectedUser.id}/workLogs`)
.then(res => res.json())
.then(data => {
const newLogs = data.workLogs.map((e) => {
const user = e.LoggedByUser;
const name = user ? `${user.firstName} ${user.lastName}` : null;
return {
...e,
loggedBy: name,
workedAt_num: parseISO(e.workedAt).getTime(),
workedAt: format(
parseISO(e.workedAt),
"dd MMM 'kl.'HH:mm"
).toLowerCase(),
};
});

});
setLogs(newLogs);
})
}

}, [selectedUser, showLogSettings])

function handleChangeUser(user) {
setSelectedUser(user);

if (user !== null) {

fetch(`/api/v2/users/${user.id}/roles`)
.then(res => res.json())
.then(data => {
setAssignedRoles(data.userRoles)
setAvailableRoles(roles.filter(e => !data.userRoles.includes(e)))
if (roleChangeResponse !== "") setRoleChangeResponse("");
})

}
}

return (
<Box>
<PageHeader text="Admin panel" variant="h4" />
Expand All @@ -120,30 +117,7 @@ function AdminPage(params) {
<CustomAutoComplete
label="Select user"
value={selectedUser}
callback={(data) => {
setSelectedUser(data);

if (data !== null) {
const roleIds =
data.roles.length !== 0
? data.roles.map((e) => e.role.id)
: [];
const newAvailableRoles = roles.filter(
(e) => !roleIds.includes(e.id) && e.id !== "hihih"
);

const newAssignedRoles = data.roles
.filter((e) => e.role.id !== "hihih")
.map((e) => e.role);

console.log(newAvailableRoles, newAssignedRoles);

setAvailableRoles(newAvailableRoles);
setAssignedRoles(newAssignedRoles);

if (roleChangeResponse !== "") setRoleChangeResponse("");
}
}}
callback={handleChangeUser}
data={users}
dataLabel="name"
subDataLabel="email"
Expand Down Expand Up @@ -206,6 +180,7 @@ function AdminPage(params) {
);
}


function roleSettings(
selectedUser,
availableRoles,
Expand All @@ -217,7 +192,7 @@ function roleSettings(
) {

const handleSave = async () => {
const response = await fetch("/api/data/setRoles", {
const response = await fetch(`/api/v2/users/${selectedUser.id}/roles`, {
method: "post",
mode: "cors",
headers: {
Expand Down Expand Up @@ -270,7 +245,7 @@ function roleSettings(
"&:hover": { color: cybTheme.palette.primary.main },
}}
>
{e.name}
{e}
</Typography>
</Link>
);
Expand Down Expand Up @@ -304,7 +279,7 @@ function roleSettings(
"&:hover": { color: cybTheme.palette.primary.main },
}}
>
{e.name}
{e}
</Typography>
</Link>
);
Expand Down
29 changes: 29 additions & 0 deletions app/(pages)/(main)/board/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

"use client"

import { PageHeader } from "@/app/components/sanity/PageBuilder";
import { Box } from "@mui/material";
import { useEffect, useState } from "react";
import Forcegraph from "@/app/components/RecruitmentGraph"

export default function BoardPage() {

const [data, setData] = useState({ nodes: [], edges: [] });

useEffect(() => {
fetch(`/api/v2/recruitGraph`)
.then(res => res.json())
.then(data => {
setData({ nodes: data.nodes, edges: data.edges})
})

}, []);

return (
<Box>
<PageHeader text="Board tools" />

<Forcegraph data={data}/>
</Box>
);
}
11 changes: 7 additions & 4 deletions app/pages/main/layout.js → app/(pages)/(main)/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
ThemeProvider,
Paper,
} from "@mui/material";
import { SessionProvider } from "next-auth/react";
import { SessionProvider, useSession } from "next-auth/react";
import { usePathname } from "next/navigation";
import { NavBar } from "@/app/components/layout/AppBar";
import { cybTheme } from "@/app/components/themeCYB";
import LayoutFooter from "@/app/components/layout/Footer";

const NavItems = [
{ id: "home", path: "home", name: "Home", icon: <Home /> },
{ id: "home", path: "", name: "Home", icon: <Home /> },
{ id: "aboutCYB", path: "aboutCYB", name: "About CYB", icon: <Groups /> },
{
id: "volunteering",
Expand All @@ -28,7 +28,7 @@ const NavItems = [
},
];

export default function AppLayout({ children }) {
export default async function AppLayout({ children }) {
Copy link
Member

Choose a reason for hiding this comment

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

remove async from client component


const pathname = usePathname();

Expand Down Expand Up @@ -61,7 +61,10 @@ export default function AppLayout({ children }) {
}}
>
<Container sx={{ mb: 3 }}>
<SessionProvider>{children}</SessionProvider>
<SessionProvider
basePath="/api/v2/auth"
refetchOnWindowFocus={false}
>{children}</SessionProvider>
</Container>
</Container>
</Box>
Expand Down
6 changes: 6 additions & 0 deletions app/(pages)/(main)/middleware.js
Copy link
Member

Choose a reason for hiding this comment

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

remove unused middleware function

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NextResponse } from "next/server";

export function middleware(req) {
console.log("Main middleware")
return NextResponse.next()
}
File renamed without changes.
Loading