diff --git a/modules/odr_frontend/src/routes/admin/+page.svelte b/modules/odr_frontend/src/routes/admin/+page.svelte index 9d6d4bf..ae2ad9f 100644 --- a/modules/odr_frontend/src/routes/admin/+page.svelte +++ b/modules/odr_frontend/src/routes/admin/+page.svelte @@ -3,7 +3,7 @@ - OMI Data Pipeline| Admin + Admin | OMI Data Pipeline diff --git a/modules/odr_frontend/src/routes/admin/teams/+page.svelte b/modules/odr_frontend/src/routes/admin/teams/+page.svelte index 49c0923..1da58ad 100644 --- a/modules/odr_frontend/src/routes/admin/teams/+page.svelte +++ b/modules/odr_frontend/src/routes/admin/teams/+page.svelte @@ -4,9 +4,11 @@ import UserRow from '../users/UserRow.svelte'; export let data; - const teams = data.teams; - const users = data.users; - const teams_users = data.teams_users; + + $: teams = data.teams; + $: users = data.users; + $: teams_users = data.teams_users; + let newTeamName = ''; let userSearch = ''; let selected_team: null | number = null; @@ -19,8 +21,18 @@ }, body: JSON.stringify({ newTeamName }) }); + const res = await req.json(); - if (!res.success) { + + if (res.success) { + const newTeam = { + ...res.team, + created_at: new Date(res.team.created_at), + updated_at: new Date(res.team.created_at), + }; + teams = [...teams, newTeam]; + newTeamName = ''; + } else { console.error(res.error); } } @@ -33,9 +45,12 @@ body: JSON.stringify({ userId, teamId }) }); const res = await req.json(); - if (!res.success) { - console.error(res.error); - } + if (res.success) { + teams_users = [...teams_users, res.team_user]; + userSearch = ''; + } else { + console.error(res.error); + } } let modal: ModalSettings = { @@ -43,9 +58,9 @@ title: 'Please Confirm', body: `Are you sure you wish to create a team named ${newTeamName}?`, // TRUE if confirm pressed, FALSE if cancel pressed - response: (r: boolean) => { + response: async (r: boolean) => { if (r) { - createTeam(); + await createTeam(); } } }; @@ -70,12 +85,12 @@ title: 'Please Confirm', body: `Are you sure you wish to add ${event.detail.label} to the ${teams.find((t) => t.id === selected_team)?.name} team?`, // TRUE if confirm pressed, FALSE if cancel pressed - response: (r: boolean) => { - if (r) { - console.log(`Adding user ${event.detail.label} to team ${selected_team}`); - addUserToTeam(parseInt(event.detail.value), selected_team as number); - } - } + response: async (r: boolean) => { + if (r) { + console.log(`Adding user ${event.detail.label} to team ${selected_team}`); + await addUserToTeam(parseInt(event.detail.value), selected_team as number); + } + } }; modalStore.trigger(add_user_to_team_modal); } diff --git a/modules/odr_frontend/src/routes/admin/teams/api/+server.ts b/modules/odr_frontend/src/routes/admin/teams/api/+server.ts index e603c8d..5d506a9 100644 --- a/modules/odr_frontend/src/routes/admin/teams/api/+server.ts +++ b/modules/odr_frontend/src/routes/admin/teams/api/+server.ts @@ -1,16 +1,33 @@ import { pgClient } from '$lib/server/pg'; import type { RequestHandler } from '@sveltejs/kit'; + export const POST: RequestHandler = async ({ request }) => { - const body = await request.json(); - if (!body.newTeamName) { - return new Response(JSON.stringify({ success: false, error: 'No Team name provided' })); - } - try { - const result = await pgClient.query('INSERT INTO teams (name) VALUES ($1)', [body.newTeamName]); - console.info(`Added team ${body.newTeamName}`); - return new Response(JSON.stringify({ success: true, result: result })); - } catch (e) { - console.error(`Failed to add team ${body.newTeamName}`); - return new Response(JSON.stringify({ success: false, error: e })); - } + const body = await request.json(); + if (!body.newTeamName) { + return new Response(JSON.stringify({ success: false, error: 'No Team name provided' }), { + status: 400, + headers: { 'Content-Type': 'application/json' } + }); + } + + try { + const result = await pgClient.query( + 'INSERT INTO teams (name) VALUES ($1) RETURNING id, name, created_at, updated_at', + [body.newTeamName] + ); + + const newTeam = result.rows[0]; + console.info(`Added team ${body.newTeamName} with id ${newTeam.id}`); + + return new Response(JSON.stringify({ success: true, team: newTeam }), { + status: 201, + headers: { 'Content-Type': 'application/json' } + }); + } catch (e) { + console.error(`Failed to add team ${body.newTeamName}:`, e); + return new Response(JSON.stringify({ success: false, error: e.message }), { + status: 500, + headers: { 'Content-Type': 'application/json' } + }); + } }; diff --git a/modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts b/modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts index 6c944a1..8ca8f42 100644 --- a/modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts +++ b/modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts @@ -1,28 +1,44 @@ import { PG_API, pgClient } from '$lib/server/pg'; import type { RequestHandler } from '@sveltejs/kit'; + export const POST: RequestHandler = async ({ request }) => { - const body = await request.json(); - if (!body.userId) { - return new Response(JSON.stringify({ success: false, error: 'No user ID provided' })); - } - if (!body.teamId) { - return new Response(JSON.stringify({ success: false, error: 'No team ID provided' })); - } + const body = await request.json(); + if (!body.userId) { + return new Response(JSON.stringify({ success: false, error: 'No user ID provided' }), { + status: 400, + headers: { 'Content-Type': 'application/json' } + }); + } + if (!body.teamId) { + return new Response(JSON.stringify({ success: false, error: 'No team ID provided' }), { + status: 400, + headers: { 'Content-Type': 'application/json' } + }); + } - const team_users = await PG_API.teams.getUsers(body.teamId); - if (team_users.some((user) => user.user_id == body.userId)) { - return new Response(JSON.stringify({ success: false, error: 'User already in team' })); - } + const team_users = await PG_API.teams.getUsers(body.teamId); + if (team_users.some((user) => user.user_id == body.userId)) { + return new Response(JSON.stringify({ success: false, error: 'User already in team' }), { + status: 400, + headers: { 'Content-Type': 'application/json' } + }); + } - try { - const result = await pgClient.query( - 'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3)', - [body.teamId, body.userId, 'member'] - ); - console.info(`Added user ${body.userId} to team ${body.teamId}`); - return new Response(JSON.stringify({ success: true, result: result })); - } catch (e) { - console.error(`Failed to add user ${body.userId} to team ${body.teamId}`); - return new Response(JSON.stringify({ success: false, error: e })); - } + try { + const result = await pgClient.query( + 'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3) RETURNING *', + [body.teamId, body.userId, 'member'] + ); + console.info(`Added user ${body.userId} to team ${body.teamId}`); + return new Response(JSON.stringify({ success: true, team_user: result.rows[0] }), { + status: 201, + headers: { 'Content-Type': 'application/json' } + }); + } catch (e) { + console.error(`Failed to add user ${body.userId} to team ${body.teamId}:`, e); + return new Response(JSON.stringify({ success: false, error: e.message }), { + status: 500, + headers: { 'Content-Type': 'application/json' } + }); + } };