-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95ae62d
commit beaea16
Showing
16 changed files
with
429 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ | |
{ | ||
name: 'Fahrer', | ||
href: '/drivers' | ||
}, | ||
{ | ||
name: 'Verwaltung', | ||
href: '/administration' | ||
} | ||
]; | ||
</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Actions } from './$types'; | ||
import { db } from '$lib/database'; | ||
import { fail } from '@sveltejs/kit'; | ||
import type { PageServerLoad } from './$types.js'; | ||
|
||
export const load: PageServerLoad = async (event) => { | ||
const administrators = await db | ||
.selectFrom('auth_user') | ||
.where('company_id', '=', event.locals.user!.company!) | ||
.where('is_entrepreneur', '=', true) | ||
.selectAll() | ||
.execute(); | ||
return { | ||
administrators | ||
}; | ||
}; | ||
|
||
export const actions = { | ||
default: async (event) => { | ||
const email = (await event.request.formData()).get('email')!.toString(); | ||
const companyId = event.locals.user!.company!; | ||
if (!email) { | ||
return fail(400, { email, missing: true }); | ||
} | ||
try { | ||
const user = await db | ||
.selectFrom('auth_user') | ||
.where('email', '=', email) | ||
.selectAll() | ||
.executeTakeFirstOrThrow(); | ||
if (user.company_id != null && user.is_entrepreneur) { | ||
return { existed: true }; | ||
} | ||
await db | ||
.updateTable('auth_user') | ||
.set({ company_id: companyId, is_entrepreneur: true }) | ||
.where('email', '=', email) | ||
.executeTakeFirst(); | ||
} catch { | ||
return fail(400, { email, incorrect: true }); | ||
} | ||
return { updated: true }; | ||
} | ||
} satisfies Actions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<script lang="ts"> | ||
import { Button } from '$lib/components/ui/button'; | ||
import * as Card from '$lib/components/ui/card'; | ||
import { Input } from '$lib/components/ui/input'; | ||
import { Label } from '$lib/components/ui/label'; | ||
import * as Table from '$lib/components/ui/table/index.js'; | ||
import { invalidateAll } from '$app/navigation'; | ||
import type { ActionData, PageData } from './$types'; | ||
const { data, form } = $props<{ data: PageData; form: ActionData }>(); | ||
const removeUser = async (email: string) => { | ||
await fetch('/api/user', { | ||
method: 'PUT', | ||
body: JSON.stringify({ email }) | ||
}); | ||
invalidateAll(); | ||
}; | ||
</script> | ||
|
||
<div class="grid grid-rows-2 gap-4"> | ||
{@render assignment()} | ||
{@render drivers()} | ||
</div> | ||
|
||
{#snippet assignment()} | ||
<Card.Root class="h-full w-5/6 m-2"> | ||
<Card.Header> | ||
<Card.Title>Verwalter freischalten</Card.Title> | ||
</Card.Header> | ||
<Card.Content> | ||
<form method="POST"> | ||
<div class="grid w-full grid-rows-2 grid-cols-2 gap-4"> | ||
<Label> | ||
{#if form?.missing} | ||
<div class="text-[0.8rem] font-medium text-destructive mt-1"> | ||
Das Email Feld muss ausgefüllt werden. | ||
</div> | ||
{/if} | ||
{#if form?.incorrect} | ||
<div class="text-[0.8rem] font-medium text-destructive mt-1"> | ||
Es existiert kein Benutzer mit der angegebenen Emailadresse. | ||
</div> | ||
{/if} | ||
{#if form?.updated} | ||
<div class="text-[0.8rem] font-medium text-green-600 mt-1"> | ||
Freischaltung erfolgreich! | ||
</div> | ||
{/if} | ||
{#if form?.existed} | ||
<div class="text-[0.8rem] font-medium text-yellow-700 mt-1"> | ||
Nutzer bereits freigeschaltet | ||
</div> | ||
{/if} | ||
<Input class="mt-2" name="email" type="text" /> | ||
</Label> | ||
<div class="mt-6 row-start-2 col-span-2 text-right"> | ||
<Button type="submit">Freischalten</Button> | ||
</div> | ||
</div> | ||
</form> | ||
</Card.Content> | ||
</Card.Root> | ||
{/snippet} | ||
|
||
{#snippet drivers()} | ||
<Card.Root class="h-full w-5/6 m-2"> | ||
<Card.Header> | ||
<Card.Title>Inhaber</Card.Title> | ||
</Card.Header> | ||
<Card.Content> | ||
<Table.Root> | ||
<Table.Header> | ||
<Table.Row> | ||
<Table.Head>Email</Table.Head> | ||
<Table.Head></Table.Head> | ||
</Table.Row> | ||
</Table.Header> | ||
<Table.Body> | ||
{#each data.administrators as admin} | ||
<Table.Row> | ||
<Table.Cell>{admin.email}</Table.Cell> | ||
<Table.Cell class="text-right" | ||
><Button on:click={() => removeUser(admin.email)}>x</Button></Table.Cell | ||
> | ||
</Table.Row> | ||
{/each} | ||
</Table.Body> | ||
</Table.Root> | ||
</Card.Content> | ||
</Card.Root> | ||
{/snippet} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { error, json } from '@sveltejs/kit'; | ||
import { db } from '$lib/database'; | ||
|
||
export const PUT = async (event) => { | ||
const company = event.locals.user?.company; | ||
if (!company) { | ||
error(400, { | ||
message: 'not allowed without write access to company' | ||
}); | ||
} | ||
const request = event.request; | ||
const userMail = event.locals.user?.email; | ||
try { | ||
const { email } = | ||
await request.json(); | ||
if (email == userMail) { | ||
error(400, { | ||
message: 'not allowed for this user' | ||
}); | ||
} | ||
const user = await db | ||
.selectFrom('auth_user') | ||
.where('company_id', '=', company) | ||
.where('email', '=', email) | ||
.selectAll() | ||
.executeTakeFirst(); | ||
if (user == null) { | ||
error(404, { | ||
message: 'user not found' | ||
}); | ||
} | ||
if (user!.is_entrepreneur) { | ||
await db | ||
.updateTable('auth_user') | ||
.set({ | ||
is_entrepreneur: false | ||
}) | ||
.where('company_id', '=', company) | ||
.where('email', '=', email) | ||
.executeTakeFirst(); | ||
} else { | ||
await db | ||
.updateTable('auth_user') | ||
.set({ | ||
company_id: null | ||
}) | ||
.where('company_id', '=', company) | ||
.where('email', '=', email) | ||
.executeTakeFirst(); | ||
} | ||
} catch (e) { | ||
error(500, { | ||
message: 'An unknown error occurred' | ||
}); | ||
} | ||
return json({}); | ||
}; |
Oops, something went wrong.