Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenheger committed Sep 11, 2024
1 parent 95ae62d commit beaea16
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 125 deletions.
21 changes: 18 additions & 3 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@ export default defineConfig({
dependencies: ['setup db']
},
{
name: 'user test',
testMatch: /(.+\.)?(test|spec)\.[jt]s/,
dependencies: ['setup db', 'login']
name: 'entrepreneurAssignsRoles',
testMatch: /entrepreneurAssignsRoles\.ts/,
dependencies: ['login']
},
{
name: 'availability',
testMatch: /availability\.ts/,
dependencies: ['login']
},
{
name: 'move tour',
testMatch: /moveTour\.ts/,
dependencies: ['availability']
},
{
name: 'resdispose tour',
testMatch: /redisposeTour\.ts/,
dependencies: ['availability']
}
]
});
4 changes: 4 additions & 0 deletions src/routes/(user)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
{
name: 'Fahrer',
href: '/drivers'
},
{
name: 'Verwaltung',
href: '/administration'
}
];
</script>
Expand Down
44 changes: 44 additions & 0 deletions src/routes/(user)/administration/+page.server.ts
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;
93 changes: 93 additions & 0 deletions src/routes/(user)/administration/+page.svelte
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>
Email
{#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}
23 changes: 18 additions & 5 deletions src/routes/(user)/drivers/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
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 drivers = await db
.selectFrom('auth_user')
.where('company_id', '=', event.locals.user!.company!)
.where('is_entrepreneur', '=', false)
.selectAll()
.execute();
return {
drivers
};
};

export const actions = {
default: async ({ request }) => {
const email = (await request.formData()).get('email')!.toString();
const companyId = 0;
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 });
}
Expand All @@ -15,12 +28,12 @@ export const actions = {
.where('email', '=', email)
.selectAll()
.executeTakeFirstOrThrow();
if (user.is_driver) {
if (user.company_id != null) {
return { existed: true };
}
await db
.updateTable('auth_user')
.set({ company_id: companyId, is_driver: true })
.set({ company_id: companyId })
.where('email', '=', email)
.executeTakeFirst();
} catch {
Expand Down
35 changes: 23 additions & 12 deletions src/routes/(user)/drivers/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
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 type { ActionData } from './$types';
import { invalidateAll } from '$app/navigation';
import type { ActionData, PageData } from './$types';
export let form: ActionData;
const { data, form } = $props<{ data: PageData; form: ActionData }>();
const removeUser = async (email: string) => {
const response = await fetch('/api/user', {
method: 'PUT',
body: JSON.stringify({ email })
});
invalidateAll();
};
</script>

<div class="grid grid-rows-2 gap-4">
Expand Down Expand Up @@ -36,7 +45,7 @@
{/if}
{#if form?.updated}
<div class="text-[0.8rem] font-medium text-green-600 mt-1">
Freischalten erfolgreich!
Freischaltung erfolgreich!
</div>
{/if}
{#if form?.existed}
Expand All @@ -47,7 +56,7 @@
<Input class="mt-2" name="email" type="text" />
</Label>
<div class="mt-6 row-start-2 col-span-2 text-right">
<Button type="submit">Fahrer freischalten</Button>
<Button type="submit">Freischalten</Button>
</div>
</div>
</form>
Expand All @@ -64,17 +73,19 @@
<Table.Root>
<Table.Header>
<Table.Row>
<Table.Head>Name</Table.Head>
<Table.Head>Vorname</Table.Head>
<Table.Head>Fahrzeug</Table.Head>
<Table.Head>Email</Table.Head>
<Table.Head></Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>TODO</Table.Cell>
<Table.Cell>TODO</Table.Cell>
<Table.Cell>TODO</Table.Cell>
</Table.Row>
{#each data.drivers as driver}
<Table.Row>
<Table.Cell>{driver.email}</Table.Cell>
<Table.Cell class="text-right"
><Button on:click={() => removeUser(driver.email)}>x</Button></Table.Cell
>
</Table.Row>
{/each}
</Table.Body>
</Table.Root>
</Card.Content>
Expand Down
57 changes: 57 additions & 0 deletions src/routes/api/user/+server.ts
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({});
};
Loading

0 comments on commit beaea16

Please sign in to comment.