update cuz presentation
This commit is contained in:
parent
7a9e451739
commit
4e67cc4e19
29 changed files with 1065 additions and 88 deletions
63
components/alerts.vue
Normal file
63
components/alerts.vue
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { VAlert } from 'vuetify/components';
|
||||||
|
import { computed } from 'vue';
|
||||||
|
export type AlertData = Pick<InstanceType<typeof VAlert>["$props"],
|
||||||
|
| "color"
|
||||||
|
| "icon"
|
||||||
|
| "text"
|
||||||
|
| "title"
|
||||||
|
| "type"
|
||||||
|
| "closable"
|
||||||
|
| "closeIcon"
|
||||||
|
>;
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
alerts: Array<AlertData>;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
// eslint-disable-next-line func-call-spacing
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(e: "update:alerts", value: typeof props.alerts): void,
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const alerts = computed({
|
||||||
|
get() { return props.alerts ?? []; },
|
||||||
|
set(value: typeof props.alerts) { emit("update:alerts", value); },
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<VOverlay
|
||||||
|
model-value
|
||||||
|
:scrim="false"
|
||||||
|
no-click-animation
|
||||||
|
persistent
|
||||||
|
class="alerts"
|
||||||
|
>
|
||||||
|
<VAlert
|
||||||
|
v-for="i of alerts"
|
||||||
|
:key="alerts.indexOf(i)"
|
||||||
|
class="alert"
|
||||||
|
:color=" i.color"
|
||||||
|
model-value
|
||||||
|
:icon="i.icon ?? false"
|
||||||
|
:text="i.text"
|
||||||
|
:title="i.title"
|
||||||
|
:type="i.type ?? 'error'"
|
||||||
|
:closable="i.closable ?? true"
|
||||||
|
:close-icon="i.closeIcon ?? undefined"
|
||||||
|
/>
|
||||||
|
</VOverlay>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.alerts {
|
||||||
|
flex-direction: column-reverse;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert {
|
||||||
|
margin: 0.5em;
|
||||||
|
}
|
||||||
|
</style>
|
51
components/entryEditor.vue
Normal file
51
components/entryEditor.vue
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from "vue";
|
||||||
|
|
||||||
|
type optionalMap<Optional> = Optional extends true ? undefined : string | number;
|
||||||
|
// type typeMap<Type extends string = {};
|
||||||
|
|
||||||
|
export type fieldDefinition<Optional extends boolean = boolean> = {
|
||||||
|
key: string,
|
||||||
|
label?: string,
|
||||||
|
type: "text" | "number",
|
||||||
|
optional?: Optional,
|
||||||
|
value?: optionalMap<Optional>,
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
fields: Array<fieldDefinition>,
|
||||||
|
modelValue?: any,
|
||||||
|
}>();
|
||||||
|
|
||||||
|
// eslint-disable-next-line func-call-spacing
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(e: "update:modelValue", value: any): void,
|
||||||
|
(e: "updateSubModelValue", key: fieldDefinition["key"], value: fieldDefinition["value"]): void,
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const modelValue = ref<{[key: string]: string | number | undefined}>({});
|
||||||
|
|
||||||
|
for (const i of props.fields) {
|
||||||
|
modelValue.value[i.key] = i.value;
|
||||||
|
emit("updateSubModelValue", i.key, i.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateModel(key: string, v: any) {
|
||||||
|
modelValue.value[key] = v;
|
||||||
|
emit("update:modelValue", modelValue.value);
|
||||||
|
emit("updateSubModelValue", key, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit("update:modelValue", modelValue.value);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<VTextField
|
||||||
|
v-for="i of fields"
|
||||||
|
:key="i.key"
|
||||||
|
:model-value="modelValue[i.key]"
|
||||||
|
:label="i.label"
|
||||||
|
:type="i.type"
|
||||||
|
@update:model-value="v => updateModel(i.key, v)"
|
||||||
|
/>
|
||||||
|
</template>
|
|
@ -13,18 +13,20 @@ const navOpen = ref(!mobile.value);
|
||||||
<VToolbarTitle>Database Project</VToolbarTitle>
|
<VToolbarTitle>Database Project</VToolbarTitle>
|
||||||
</VAppBar>
|
</VAppBar>
|
||||||
<VNavigationDrawer v-model="navOpen" :temporary="mobile">
|
<VNavigationDrawer v-model="navOpen" :temporary="mobile">
|
||||||
<VList>
|
<!-- <VList>
|
||||||
<VListItem
|
<VListItem
|
||||||
prepend-avatar="https://cdn.discordapp.com/avatars/317001025074757632/248f503c44bc95ac75f55f5835e6ff9f.png?size=512"
|
prepend-avatar="https://cdn.discordapp.com/avatars/317001025074757632/248f503c44bc95ac75f55f5835e6ff9f.png?size=512"
|
||||||
title="Anonymous"
|
title="Anonymous"
|
||||||
/>
|
/>
|
||||||
</VList>
|
</VList> -->
|
||||||
|
|
||||||
<VDivider />
|
<VDivider />
|
||||||
|
|
||||||
<VList density="compact" nav>
|
<VList density="compact" nav>
|
||||||
<VListItem prepend-icon="mdi-home" title="Home" value="/" @click="navigateTo('/')" />
|
|
||||||
<VListItem prepend-icon="mdi-login" title="Login" value="/login" @click="navigateTo('/login')" />
|
<VListItem prepend-icon="mdi-login" title="Login" value="/login" @click="navigateTo('/login')" />
|
||||||
|
<VListItem prepend-icon="mdi-account" title="Clients" value="/clients" @click="navigateTo('/clients')" />
|
||||||
|
<VDivider />
|
||||||
|
<VListItem prepend-icon="mdi-home" title="Home" value="/" @click="navigateTo('/')" />
|
||||||
<VListItem prepend-icon="mdi-table" title="Table Example" value="/tableExample" @click="navigateTo('/tableExample')" />
|
<VListItem prepend-icon="mdi-table" title="Table Example" value="/tableExample" @click="navigateTo('/tableExample')" />
|
||||||
<VListItem prepend-icon="mdi-form-textarea" title="Echo Form" value="/forms" @click="navigateTo('/forms')" />
|
<VListItem prepend-icon="mdi-form-textarea" title="Echo Form" value="/forms" @click="navigateTo('/forms')" />
|
||||||
</VList>
|
</VList>
|
||||||
|
|
56
components/orderView.vue
Normal file
56
components/orderView.vue
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { order as orderType } from '~/utils/types/database';
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
order?: orderType
|
||||||
|
}>();
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<!-- <VRow v-if="props.order">
|
||||||
|
{{ props.order.id }}
|
||||||
|
</VRow> -->
|
||||||
|
<VCol>
|
||||||
|
<div v-if="props.order && props.order.work.length !== 0">
|
||||||
|
<span class="text-h4">Works</span>
|
||||||
|
<VTable class="noScroll">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>fulfilled</th>
|
||||||
|
<th>offer</th>
|
||||||
|
<th>price</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="i in props.order.work" :key="i.id">
|
||||||
|
<td>{{ i.is_fulfilled }}</td>
|
||||||
|
<td>
|
||||||
|
{{ i.offer.name }}
|
||||||
|
</td>
|
||||||
|
<td>{{ i.price }} PLN</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</VTable>
|
||||||
|
</div>
|
||||||
|
<div v-if="props.order && props.order.imported_products.length !== 0">
|
||||||
|
<span class="text-h4">Imported products</span>
|
||||||
|
<VTable>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>link</th>
|
||||||
|
<th>price</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="i in props.order.imported_products" :key="i.id">
|
||||||
|
<td>{{ i.name }}</td>
|
||||||
|
<td>{{ i.link }}</td>
|
||||||
|
<td>{{ i.price }} PLN</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</VTable>
|
||||||
|
</div>
|
||||||
|
</VCol>
|
||||||
|
</template>
|
|
@ -2,6 +2,7 @@
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
records: Array<any>,
|
records: Array<any>,
|
||||||
recordKey: string,
|
recordKey: string,
|
||||||
|
recordValue?: string,
|
||||||
variant?: "default" | "inset" | "accordion" | "popout",
|
variant?: "default" | "inset" | "accordion" | "popout",
|
||||||
modelValue?: any,
|
modelValue?: any,
|
||||||
}>();
|
}>();
|
||||||
|
@ -22,6 +23,7 @@ defineEmits<{
|
||||||
v-for="record in records"
|
v-for="record in records"
|
||||||
:key="record[recordKey]"
|
:key="record[recordKey]"
|
||||||
:variant="props.variant ?? 'default'"
|
:variant="props.variant ?? 'default'"
|
||||||
|
:value="recordValue !== undefined ? record[recordValue] : undefined"
|
||||||
>
|
>
|
||||||
<template #title>
|
<template #title>
|
||||||
<slot name="title" :record="record" />
|
<slot name="title" :record="record" />
|
||||||
|
|
|
@ -53,4 +53,8 @@ defineEmits<{
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
flex-wrap: nowrap;
|
flex-wrap: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tr:hover {
|
||||||
|
background-color: rgba(var(--v-theme-on-background), calc(var(--v-hover-opacity) * var(--v-theme-overlay-multiplier)));
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
/* global $fetch */
|
||||||
import { useRoute, useFetch, createError } from "nuxt/app";
|
import { useRoute, useFetch, createError } from "nuxt/app";
|
||||||
import { Ref } from "vue";
|
import { ref, Ref } from "vue";
|
||||||
|
|
||||||
|
import { VBtn, VForm } from "vuetify/components";
|
||||||
import PagedList from "~/components/pagedList.vue";
|
import PagedList from "~/components/pagedList.vue";
|
||||||
import { client as clientType } from "~/utils/types/database";
|
import Snowflake from "~/utils/snowflake";
|
||||||
|
import { client as clientType, order, orderSummary } from "~/utils/types/database";
|
||||||
|
import OrderView from "~/components/orderView.vue";
|
||||||
|
import EntryEditor, { fieldDefinition } from "~/components/entryEditor.vue";
|
||||||
|
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const id = route.params.id;
|
const id = route.params.id;
|
||||||
|
@ -11,11 +16,116 @@ const id = route.params.id;
|
||||||
const clientRequest = await useFetch(`/api/clients/${id}`);
|
const clientRequest = await useFetch(`/api/clients/${id}`);
|
||||||
if (clientRequest.error.value) throw createError(clientRequest.error.value?.data ?? "");
|
if (clientRequest.error.value) throw createError(clientRequest.error.value?.data ?? "");
|
||||||
const client = clientRequest.data as Ref<clientType>;
|
const client = clientRequest.data as Ref<clientType>;
|
||||||
console.log(client);
|
|
||||||
|
|
||||||
|
const clientOrdersRequest = await useFetch(`/api/clients/${id}/orders`);
|
||||||
|
if (clientOrdersRequest.error.value) throw createError(clientOrdersRequest.error.value?.data ?? "");
|
||||||
|
const clientOrders = clientOrdersRequest.data as Ref<Array<orderSummary>>;
|
||||||
|
|
||||||
|
const orders = ref<Map<string, {
|
||||||
|
loading: boolean,
|
||||||
|
value?: order
|
||||||
|
}>>(new Map());
|
||||||
|
|
||||||
|
for (const i of clientOrders.value)
|
||||||
|
orders.value.set(i.id, { loading: false });
|
||||||
|
|
||||||
|
async function loadOrder(id: string) {
|
||||||
|
const entry = orders.value.get(id);
|
||||||
|
if (!entry) throw createError(`excepted order entry for ${id}`);
|
||||||
|
entry.loading = true;
|
||||||
|
// @ts-expect-error
|
||||||
|
entry.value = await $fetch(`/api/orders/${id}` as "/api/order/:id", {
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
entry.loading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastPagedListVModel: Array<string> = [];
|
||||||
|
|
||||||
|
function updatePagedListVModel(element: Array<string>) {
|
||||||
|
const justOpened = element.filter(e => !lastPagedListVModel.includes(e));
|
||||||
|
for (const i of justOpened) loadOrder(i);
|
||||||
|
lastPagedListVModel = element;
|
||||||
|
}
|
||||||
|
|
||||||
|
const editMode = ref<boolean>(route.query?.edit === "1");
|
||||||
|
function editorFields(): Array<fieldDefinition> {
|
||||||
|
return [
|
||||||
|
{ key: "name", type: "text", label: "Name", value: client.value.name ?? undefined },
|
||||||
|
{ key: "address", type: "text", label: "Address", value: client.value.address ?? undefined },
|
||||||
|
{ key: "phone", type: "text", label: "Phone", value: client.value.phone ?? undefined },
|
||||||
|
{ key: "email", type: "text", label: "E-mail", value: client.value.email ?? undefined },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
const submitting = ref<boolean>(false);
|
||||||
|
|
||||||
|
// const updateForm = ref<VForm | null>(null);
|
||||||
|
const formButton = ref<VBtn | null>(null);
|
||||||
|
const formData = ref<any>({});
|
||||||
|
|
||||||
|
function normalizeForm() {
|
||||||
|
for (const i in formData.value)
|
||||||
|
formData.value[i] = formData.value[i] === "" ? null : formData.value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
submitting.value = true;
|
||||||
|
normalizeForm();
|
||||||
|
try {
|
||||||
|
const result = await $fetch(
|
||||||
|
`/api/clients/${client.value.id}` as "/api/clients/:id", {
|
||||||
|
method: "PATCH",
|
||||||
|
body: formData.value,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
client.value = result;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
submitting.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
submitting.value = false;
|
||||||
|
editMode.value = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCreationDate() {
|
||||||
|
const date = new Date(Number(new Snowflake(BigInt(client.value.id)).timestamp.toString())).toLocaleDateString();
|
||||||
|
return date;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<VDialog
|
||||||
|
v-model="editMode"
|
||||||
|
:persistent="submitting"
|
||||||
|
:activator="formButton as unknown as (Element | null) ?? undefined"
|
||||||
|
width="auto"
|
||||||
|
>
|
||||||
|
<VCard width="400px" :loading="submitting">
|
||||||
|
<VCardTitle>
|
||||||
|
Edit client
|
||||||
|
</VCardTitle>
|
||||||
|
<VForm
|
||||||
|
ref="updateForm"
|
||||||
|
:disabled="submitting"
|
||||||
|
class="px-4"
|
||||||
|
>
|
||||||
|
<EntryEditor
|
||||||
|
:fields="editorFields()"
|
||||||
|
@update-sub-model-value="(k, v) => { formData[k] = v; }"
|
||||||
|
/>
|
||||||
|
</VForm>
|
||||||
|
<VCardActions>
|
||||||
|
<VBtn
|
||||||
|
color="primary"
|
||||||
|
@click="handleSubmit"
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</VBtn>
|
||||||
|
</VCardActions>
|
||||||
|
</VCard>
|
||||||
|
</VDialog>
|
||||||
<VRow>
|
<VRow>
|
||||||
<VCol cols="12">
|
<VCol cols="12">
|
||||||
<div
|
<div
|
||||||
|
@ -28,7 +138,7 @@ console.log(client);
|
||||||
</VRow>
|
</VRow>
|
||||||
<VRow>
|
<VRow>
|
||||||
<VCol md="4" cols="12">
|
<VCol md="4" cols="12">
|
||||||
<VCard>
|
<VCard class="mx-auto">
|
||||||
<VList>
|
<VList>
|
||||||
<VListItem
|
<VListItem
|
||||||
v-if="client.address"
|
v-if="client.address"
|
||||||
|
@ -38,14 +148,6 @@ console.log(client);
|
||||||
{{ client.address }}
|
{{ client.address }}
|
||||||
</VListItemTitle>
|
</VListItemTitle>
|
||||||
</VListItem>
|
</VListItem>
|
||||||
<VListItem
|
|
||||||
v-if="client.email"
|
|
||||||
prepend-icon="mdi-email"
|
|
||||||
>
|
|
||||||
<VListItemTitle class="text-wrap">
|
|
||||||
{{ client.email }}
|
|
||||||
</VListItemTitle>
|
|
||||||
</VListItem>
|
|
||||||
<VListItem
|
<VListItem
|
||||||
v-if="client.phone"
|
v-if="client.phone"
|
||||||
prepend-icon="mdi-phone"
|
prepend-icon="mdi-phone"
|
||||||
|
@ -54,19 +156,52 @@ console.log(client);
|
||||||
{{ client.phone }}
|
{{ client.phone }}
|
||||||
</VListItemTitle>
|
</VListItemTitle>
|
||||||
</VListItem>
|
</VListItem>
|
||||||
|
<VListItem
|
||||||
|
v-if="client.email"
|
||||||
|
prepend-icon="mdi-email"
|
||||||
|
>
|
||||||
|
<VListItemTitle class="text-wrap">
|
||||||
|
{{ client.email }}
|
||||||
|
</VListItemTitle>
|
||||||
|
</VListItem>
|
||||||
</VList>
|
</VList>
|
||||||
|
<template #actions>
|
||||||
|
<VBtn
|
||||||
|
ref="formButton"
|
||||||
|
>
|
||||||
|
edit
|
||||||
|
</VBtn>
|
||||||
|
</template>
|
||||||
</VCard>
|
</VCard>
|
||||||
|
<span class="font-italic text-caption">Created {{ getCreationDate() }}</span>
|
||||||
</VCol>
|
</VCol>
|
||||||
<VCol cols="12" md="8">
|
<VCol cols="12" md="8">
|
||||||
<PagedList
|
<PagedList
|
||||||
:records="[{a: 'owo'}, {a: 'uwu'}, {a: 'qwq'}]"
|
:records="clientOrders"
|
||||||
record-key="a"
|
record-key="id"
|
||||||
|
record-value="id"
|
||||||
|
@update:model-value="updatePagedListVModel"
|
||||||
>
|
>
|
||||||
<template #text="i">
|
|
||||||
{{ i }}
|
|
||||||
</template>
|
|
||||||
<template #title="i">
|
<template #title="i">
|
||||||
{{ i }}
|
<VRow>
|
||||||
|
<VCol>{{ new Date(Number(new Snowflake(BigInt(((i.record) as orderSummary).id)).timestamp)).toLocaleDateString() }}</VCol>
|
||||||
|
<VCol>{{ ((i.record) as orderSummary).value }} PLN</VCol>
|
||||||
|
<VCol>
|
||||||
|
{{ ((i.record) as orderSummary).imported_products_count }}
|
||||||
|
products,
|
||||||
|
{{ ((i.record) as orderSummary).work_count }}
|
||||||
|
works
|
||||||
|
</VCol>
|
||||||
|
</VRow>
|
||||||
|
</template>
|
||||||
|
<template #text="i">
|
||||||
|
<VProgressLinear
|
||||||
|
:height="orders.get((i.record as orderSummary).id)?.loading ?? true ? undefined : 0"
|
||||||
|
absolute
|
||||||
|
:progress="orders.get((i.record as orderSummary).id)?.loading ?? true"
|
||||||
|
:indeterminate="orders.get((i.record as orderSummary).id)?.loading ?? true"
|
||||||
|
/>
|
||||||
|
<OrderView :order="(orders.get((i.record as orderSummary).id)?.value as order | undefined)" />
|
||||||
</template>
|
</template>
|
||||||
</PagedList>
|
</PagedList>
|
||||||
</VCol>
|
</VCol>
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
/* global $fetch */
|
/* global $fetch */
|
||||||
import { useFetch, createError } from "nuxt/app";
|
import { useFetch, createError, navigateTo, NuxtError, useRoute } from "nuxt/app";
|
||||||
import { ref, Ref } from "vue";
|
import { ref, Ref, reactive } from "vue";
|
||||||
|
import { VBtn } from "vuetify/components";
|
||||||
import { definePageMeta } from "~/.nuxt/imports";
|
import { definePageMeta } from "~/.nuxt/imports";
|
||||||
|
|
||||||
import { client as clientType } from "~/utils/types/database";
|
import { client as clientType } from "~/utils/types/database";
|
||||||
import pagedTable from "~/components/pagedTable.vue";
|
import pagedTable from "~/components/pagedTable.vue";
|
||||||
|
import Alerts, { AlertData } from "~/components/alerts.vue";
|
||||||
|
import { fieldDefinition } from "~/components/entryEditor.vue";
|
||||||
|
|
||||||
definePageMeta({ middleware: ["auth"] });
|
definePageMeta({ middleware: ["auth"] });
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
|
const alerts = ref<Array<AlertData>>([]);
|
||||||
|
|
||||||
const clientsRequest = await useFetch("/api/clients");
|
const clientsRequest = await useFetch("/api/clients");
|
||||||
if (clientsRequest.error.value) throw createError(clientsRequest.error.value?.data ?? "");
|
if (clientsRequest.error.value) throw createError(clientsRequest.error.value?.data ?? "");
|
||||||
|
@ -16,8 +23,11 @@ const countRequest = await useFetch("/api/clients/count");
|
||||||
if (countRequest.error.value) throw createError(countRequest.error.value?.data ?? "");
|
if (countRequest.error.value) throw createError(countRequest.error.value?.data ?? "");
|
||||||
const count = countRequest.data as Ref<NonNullable<typeof countRequest.data.value>>;
|
const count = countRequest.data as Ref<NonNullable<typeof countRequest.data.value>>;
|
||||||
|
|
||||||
function rowClicked(client: string, edit = false) {
|
async function rowClicked(client: string, edit = false) {
|
||||||
console.log(client);
|
await navigateTo({
|
||||||
|
path: `/client/${client}`,
|
||||||
|
query: { edit: edit ? 1 : undefined },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function rowDelete(client: string) {
|
async function rowDelete(client: string) {
|
||||||
|
@ -28,7 +38,7 @@ async function rowDelete(client: string) {
|
||||||
clients.value = clients.value.filter(e => e.id !== client);
|
clients.value = clients.value.filter(e => e.id !== client);
|
||||||
count.value.count--;
|
count.value.count--;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// FIXME: show the error
|
alerts.value.push({ text: (e as NuxtError).message, type: "error" });
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,28 +47,98 @@ const loadingMore = ref<boolean>(false);
|
||||||
async function loadBefore() {
|
async function loadBefore() {
|
||||||
loadingMore.value = true;
|
loadingMore.value = true;
|
||||||
|
|
||||||
clients.value.push(...await $fetch("/api/clients", {
|
try {
|
||||||
query: {
|
clients.value.push(...await $fetch("/api/clients", {
|
||||||
before: clients.value[clients.value.length - 1].id,
|
query: {
|
||||||
},
|
before: clients.value[clients.value.length - 1].id,
|
||||||
}));
|
},
|
||||||
|
}));
|
||||||
|
} catch (e) {
|
||||||
|
alerts.value.push({ text: (e as NuxtError).message });
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
loadingMore.value = false;
|
loadingMore.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createMode = ref<boolean>(route.query?.create === "1");
|
||||||
|
|
||||||
|
function editorFields(): Array<fieldDefinition> {
|
||||||
|
return [
|
||||||
|
{ key: "name", type: "text", label: "Name" },
|
||||||
|
{ key: "address", type: "text", label: "Address" },
|
||||||
|
{ key: "phone", type: "text", label: "Phone" },
|
||||||
|
{ key: "email", type: "text", label: "E-mail" },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
const submitting = ref<boolean>(false);
|
||||||
|
|
||||||
|
// const updateForm = ref<VForm | null>(null);
|
||||||
|
const formButton = ref<VBtn | null>(null);
|
||||||
|
const formData = ref<any>({
|
||||||
|
name: null,
|
||||||
|
address: null,
|
||||||
|
phone: null,
|
||||||
|
email: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
function normalizeForm() {
|
||||||
|
for (const i in formData.value)
|
||||||
|
formData.value[i] = formData.value[i] === "" ? null : formData.value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
submitting.value = true;
|
||||||
|
normalizeForm();
|
||||||
|
try {
|
||||||
|
const result = await $fetch(
|
||||||
|
"/api/clients/", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData.value,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
submitting.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
submitting.value = false;
|
||||||
|
createMode.value = false;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<VOverlay
|
<Alerts :alerts="alerts" />
|
||||||
model-value
|
<VDialog
|
||||||
origin="top center"
|
v-model="createMode"
|
||||||
:scrim="false"
|
:persistent="submitting"
|
||||||
height="fit-content"
|
:activator="formButton as unknown as (Element | null) ?? undefined"
|
||||||
persistent
|
width="auto"
|
||||||
no-click-animation
|
|
||||||
>
|
>
|
||||||
<VAlert class="alert">
|
<VCard width="400px" :loading="submitting">
|
||||||
owowowowowowowowo
|
<VCardTitle>
|
||||||
</VAlert>
|
Create client
|
||||||
</VOverlay>
|
</VCardTitle>
|
||||||
|
<VForm
|
||||||
|
ref="updateForm"
|
||||||
|
:disabled="submitting"
|
||||||
|
class="px-4"
|
||||||
|
>
|
||||||
|
<EntryEditor
|
||||||
|
:fields="editorFields()"
|
||||||
|
@update-sub-model-value="(k, v) => { formData[k] = v; }"
|
||||||
|
/>
|
||||||
|
</VForm>
|
||||||
|
<VCardActions>
|
||||||
|
<VBtn
|
||||||
|
color="primary"
|
||||||
|
@click="handleSubmit"
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</VBtn>
|
||||||
|
</VCardActions>
|
||||||
|
</VCard>
|
||||||
|
</VDialog>
|
||||||
<VRow>
|
<VRow>
|
||||||
<VCol>
|
<VCol>
|
||||||
<VBreadcrumbs :items="['Clients']" />
|
<VBreadcrumbs :items="['Clients']" />
|
||||||
|
@ -66,6 +146,11 @@ async function loadBefore() {
|
||||||
<div class="text-h4">
|
<div class="text-h4">
|
||||||
There are {{ count?.count }} clients in the database.
|
There are {{ count?.count }} clients in the database.
|
||||||
</div>
|
</div>
|
||||||
|
<VBtn
|
||||||
|
ref="formButton"
|
||||||
|
>
|
||||||
|
Create
|
||||||
|
</VBtn>
|
||||||
</VCol>
|
</VCol>
|
||||||
</VRow>
|
</VRow>
|
||||||
<VRow>
|
<VRow>
|
||||||
|
|
|
@ -3,7 +3,8 @@ import Test from '~/components/test.vue';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<h1>Hi mom!</h1><br>
|
<h1>Hi mom!</h1>
|
||||||
|
<br>
|
||||||
<Test />
|
<Test />
|
||||||
<Test />
|
<Test />
|
||||||
<Test />
|
<Test />
|
||||||
|
|
|
@ -124,6 +124,13 @@ updateUserInfo();
|
||||||
>
|
>
|
||||||
Logout
|
Logout
|
||||||
</VBtn>
|
</VBtn>
|
||||||
|
<VBtn
|
||||||
|
v-if="loggedIn"
|
||||||
|
color="primary"
|
||||||
|
@click="navigateTo('/clients')"
|
||||||
|
>
|
||||||
|
go to clients
|
||||||
|
</VBtn>
|
||||||
</template>
|
</template>
|
||||||
</VCard>
|
</VCard>
|
||||||
</template>
|
</template>
|
||||||
|
|
9
server/api/clients/[id]/orders.get.ts
Normal file
9
server/api/clients/[id]/orders.get.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/* global defineEventHandler */
|
||||||
|
|
||||||
|
import { baaWrapper } from "~/server/api/orders.get";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const baa = await baaWrapper.RESTget(e, 50, 200, "`client` = ?", [e.context.params?.id]);
|
||||||
|
console.log(baa);
|
||||||
|
return baa;
|
||||||
|
});
|
|
@ -3,8 +3,8 @@ import crypto from "crypto";
|
||||||
|
|
||||||
import { database, data } from "../utils/database";
|
import { database, data } from "../utils/database";
|
||||||
import { isString } from "../utils/isString";
|
import { isString } from "../utils/isString";
|
||||||
import Snowflake from "../utils/snowflake";
|
|
||||||
import { cookieSettings } from "../utils/rootUtils";
|
import { cookieSettings } from "../utils/rootUtils";
|
||||||
|
import Snowflake from "~/utils/snowflake";
|
||||||
|
|
||||||
export default defineEventHandler(async (e) => {
|
export default defineEventHandler(async (e) => {
|
||||||
if (getCookie(e, "token"))
|
if (getCookie(e, "token"))
|
||||||
|
|
37
server/api/orders.get.ts
Normal file
37
server/api/orders.get.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/* global defineEventHandler */
|
||||||
|
|
||||||
|
import BaaPagination from "../utils/baaPagination";
|
||||||
|
import { data, database } from "../utils/database";
|
||||||
|
import { client, orderSummary } from "~/utils/types/database";
|
||||||
|
|
||||||
|
export const baaWrapper = new BaaPagination<orderSummary, "id">(
|
||||||
|
"orderSummaries",
|
||||||
|
"id",
|
||||||
|
"*, CONVERT(`client`, CHAR) AS `client`, CONVERT(`user`, CHAR) as `user`",
|
||||||
|
);
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const orders = await baaWrapper.RESTget(e, 50, 200);
|
||||||
|
|
||||||
|
const uniqueClients: Array<string> = [];
|
||||||
|
for (const i of orders) {
|
||||||
|
if (!uniqueClients.includes(i.client))
|
||||||
|
uniqueClients.push(database.escape(i.client));
|
||||||
|
}
|
||||||
|
|
||||||
|
const [clients] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"*,",
|
||||||
|
"CONVERT(`id`, CHAR) AS `id`",
|
||||||
|
"FROM `clients`",
|
||||||
|
"WHERE `id` IN",
|
||||||
|
`(${uniqueClients.join(', ')})`,
|
||||||
|
].join(" "),
|
||||||
|
) as data<client>;
|
||||||
|
|
||||||
|
const rvalue: Array<Omit<typeof orders, "client"> | { client?: client }> = [];
|
||||||
|
|
||||||
|
for (const i of orders)
|
||||||
|
rvalue.push({ ...i, client: clients.find(e => i.client === e.id) });
|
||||||
|
return rvalue;
|
||||||
|
});
|
172
server/api/orders.post.ts
Normal file
172
server/api/orders.post.ts
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
/* global defineEventHandler, createError, readBody, setResponseStatus */
|
||||||
|
|
||||||
|
import { createValidationError, handleRecursedValidationError } from "../utils/validation";
|
||||||
|
import { database as db } from "../utils/database";
|
||||||
|
import getRequestingUser from "../utils/getRequestingUser";
|
||||||
|
import { getOrder } from "./orders/[id].get";
|
||||||
|
import Snowflake from "~/utils/snowflake";
|
||||||
|
|
||||||
|
type importedProduct = {
|
||||||
|
name: string | null,
|
||||||
|
link: string,
|
||||||
|
price_imported: number,
|
||||||
|
price: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
type work = {
|
||||||
|
offer: string,
|
||||||
|
price: number,
|
||||||
|
notes: string | null,
|
||||||
|
is_fulfilled: boolean | 0 | 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
type order = {
|
||||||
|
client: string,
|
||||||
|
// user: string,
|
||||||
|
is_draft: boolean | 0 | 1,
|
||||||
|
imported_products: Array<importedProduct>,
|
||||||
|
work: Array<work>,
|
||||||
|
};
|
||||||
|
|
||||||
|
export function checkIsWork<Patch extends boolean = boolean>(
|
||||||
|
value: any,
|
||||||
|
patch: Patch,
|
||||||
|
): value is Patch extends true ? Partial<work> : work {
|
||||||
|
const errors = new Map<string, string>();
|
||||||
|
|
||||||
|
if (typeof value !== "object") {
|
||||||
|
throw createError({
|
||||||
|
message: "Invalid body",
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(typeof value.offer === "string" || (patch && value.offer === undefined))) errors.set("offer", "is not string");
|
||||||
|
if (!(typeof value.price === "number" || (patch && value.price === undefined))) errors.set("price", "is not price");
|
||||||
|
if (!(typeof value.notes === "string" || value.notes === null || (patch && value.notes === undefined))) errors.set("notes", "is not string or null");
|
||||||
|
if (!(typeof value.is_fulfilled === "boolean" || value.is_fulfilled === 0 || value.is_fulfilled === 1 || (patch && value.is_fulfilled === undefined))) errors.set("is_fulfilled", "is not boolean");
|
||||||
|
|
||||||
|
if (errors.size !== 0) throw createValidationError(errors);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkIsImportedProduct<Patch extends boolean = boolean>(
|
||||||
|
value: any,
|
||||||
|
patch: Patch,
|
||||||
|
): value is Patch extends true ? Partial<importedProduct> : importedProduct {
|
||||||
|
const errors = new Map<string, string>();
|
||||||
|
|
||||||
|
if (typeof value !== "object") {
|
||||||
|
throw createError({
|
||||||
|
message: "Invalid body",
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(typeof value.name === "string" || value.name === null || (patch && value.name === undefined))) errors.set("name", "is not string or null");
|
||||||
|
if (!(typeof value.link === "string" || (patch && value.name === undefined))) errors.set("link", "is not string");
|
||||||
|
if (!(typeof value.price_imported === "number" || (patch && value.name === undefined))) errors.set("price_imported", "is not number");
|
||||||
|
if (!(typeof value.price || (patch && value.price === undefined))) errors.set("price", "is not number");
|
||||||
|
|
||||||
|
if (errors.size !== 0) throw createValidationError(errors);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkIsOrder<Patch extends boolean = boolean>(
|
||||||
|
value: any,
|
||||||
|
patch: Patch,
|
||||||
|
): value is Patch extends true ? Partial<Pick<order, "client" | "is_draft">> : order {
|
||||||
|
const errors = new Map<string, string>();
|
||||||
|
|
||||||
|
if (typeof value !== "object") {
|
||||||
|
throw createError({
|
||||||
|
message: "Invalid body",
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(typeof value.client === "string" || (patch && value.client === undefined))) errors.set("client", "is not string");
|
||||||
|
if (!(typeof value.is_draft === "boolean" || value.is_draft === 0 || value.is_draft === 1 || (patch && value.is_draft === undefined))) errors.set("is_draft", "is not boolean");
|
||||||
|
if (!(value.imported_products instanceof Array)) errors.set("imported_products", "is not array");
|
||||||
|
else if (patch && value.imported_products !== undefined) errors.set("imported_products", "cannot patch from order");
|
||||||
|
if (!(value.work instanceof Array)) errors.set("work", "is not array");
|
||||||
|
else if (patch && value.work !== undefined) errors.set("work", "cannot patch from order");
|
||||||
|
|
||||||
|
if (!patch) {
|
||||||
|
const importedProducts = value.imported_products;
|
||||||
|
if (importedProducts instanceof Array) {
|
||||||
|
for (const i in importedProducts) {
|
||||||
|
try {
|
||||||
|
checkIsImportedProduct(importedProducts[i], patch);
|
||||||
|
} catch (e) {
|
||||||
|
handleRecursedValidationError(e, errors, `imported_products[${i}]`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const work = value.work;
|
||||||
|
if (work instanceof Array) {
|
||||||
|
for (const i in work) {
|
||||||
|
try {
|
||||||
|
checkIsWork(work[i], patch);
|
||||||
|
} catch (e) {
|
||||||
|
handleRecursedValidationError(e, errors, `work[${i}]`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errors.size !== 0) throw createValidationError(errors);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const body = await readBody(e);
|
||||||
|
const id = new Snowflake().toString();
|
||||||
|
const user = await getRequestingUser(e);
|
||||||
|
|
||||||
|
if (!checkIsOrder(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||||
|
|
||||||
|
const database = await db.new();
|
||||||
|
await database.beginTransaction();
|
||||||
|
|
||||||
|
await database.query(
|
||||||
|
["INSERT INTO",
|
||||||
|
"`orders`",
|
||||||
|
"VALUES",
|
||||||
|
"(?, ?, ?, ?)",
|
||||||
|
].join(" "),
|
||||||
|
[id, body.client, user.id, body.is_draft],
|
||||||
|
);
|
||||||
|
|
||||||
|
const promises: Array<Promise<any>> = [];
|
||||||
|
for (const i of body.imported_products) {
|
||||||
|
promises.push(database.query(
|
||||||
|
["INSERT INTO",
|
||||||
|
"`imported_products`",
|
||||||
|
"VALUES",
|
||||||
|
"(?, ?, ?, ?, ?, ?)",
|
||||||
|
].join(" "),
|
||||||
|
[new Snowflake().toString(), id, i.name, i.link, i.price_imported, i.price],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const i of body.work) {
|
||||||
|
promises.push(database.query(
|
||||||
|
["INSERT INTO",
|
||||||
|
"`work`",
|
||||||
|
"VALUES",
|
||||||
|
"(?, ?, ?, ?, ?, ?)",
|
||||||
|
].join(" "),
|
||||||
|
[new Snowflake().toString(), id, i.offer, i.price, i.notes, i.is_fulfilled],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
await database.commit();
|
||||||
|
|
||||||
|
setResponseStatus(e, 201);
|
||||||
|
return getOrder(id);
|
||||||
|
});
|
17
server/api/orders/[id].delete.ts
Normal file
17
server/api/orders/[id].delete.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
import { ResultSetHeader } from "mysql2";
|
||||||
|
|
||||||
|
import { database } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const id = e.context.params?.id;
|
||||||
|
|
||||||
|
const [result] = await database.query(
|
||||||
|
"DELETE FROM `orders` WHERE `id` = ?",
|
||||||
|
[id],
|
||||||
|
) as unknown as [ResultSetHeader];
|
||||||
|
|
||||||
|
if (result.affectedRows === 0) throw createError({ statusCode: 404 });
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
108
server/api/orders/[id].get.ts
Normal file
108
server/api/orders/[id].get.ts
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
|
||||||
|
import { offer as offerType, order } from "~/utils/types/database";
|
||||||
|
import { database, data } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export async function orderExists(id: string) {
|
||||||
|
const [[exists]] = await database.query(
|
||||||
|
"SELECT EXISTS(*) AS `exists` FROM `orders` WHERE `id` = ?",
|
||||||
|
[id],
|
||||||
|
) as data<{exists: 0 | 1}>;
|
||||||
|
|
||||||
|
return exists.exists === 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getImportedProducts(id: string) {
|
||||||
|
const [importedProducts] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"CONVERT(`id`, CHAR) AS `id`,",
|
||||||
|
"`name`,",
|
||||||
|
"`link`,",
|
||||||
|
"`price`,",
|
||||||
|
"`price_imported`",
|
||||||
|
"FROM `imported_products`",
|
||||||
|
"WHERE `order` = ?",
|
||||||
|
].join(" "),
|
||||||
|
[id],
|
||||||
|
) as data<{
|
||||||
|
id: string,
|
||||||
|
name: string | null,
|
||||||
|
link: string,
|
||||||
|
price: string,
|
||||||
|
price_imported: string
|
||||||
|
}>;
|
||||||
|
|
||||||
|
return importedProducts;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getWork(id: string) {
|
||||||
|
const [work] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"CONVERT(`id`, CHAR) AS `id`,",
|
||||||
|
"CONVERT(`offer`, CHAR) AS `offer`,",
|
||||||
|
"`price`,",
|
||||||
|
"`notes`,",
|
||||||
|
"`is_fulfilled`",
|
||||||
|
"FROM `work`",
|
||||||
|
"WHERE `order` = ?",
|
||||||
|
].join(" "),
|
||||||
|
[id],
|
||||||
|
) as data<{
|
||||||
|
id: string,
|
||||||
|
offer: offerType,
|
||||||
|
price: number,
|
||||||
|
notes: string | null,
|
||||||
|
is_fulfilled: 0 | 1,
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const [offer] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"CONVERT(`offer`.`id`, CHAR) AS `id`,",
|
||||||
|
"`offer`.`name`,",
|
||||||
|
"`offer`.`description`,",
|
||||||
|
"`offer`.`recommended_price`",
|
||||||
|
"FROM",
|
||||||
|
"`work`",
|
||||||
|
"LEFT JOIN `offer` ON `work`.`offer` = `offer`.`id`",
|
||||||
|
"WHERE `work`.`order` = ?",
|
||||||
|
].join(" "),
|
||||||
|
[id],
|
||||||
|
) as data<offerType>;
|
||||||
|
|
||||||
|
// @ts-ignore i.offer is string, but it needs to be an offer object
|
||||||
|
for (const i of work) i.offer = offer.find(e => e.id === i.offer) as offerType;
|
||||||
|
return work;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getOrder(id: string): Promise<order> {
|
||||||
|
const [[order]] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"CONVERT(`id`, CHAR) AS `id`,",
|
||||||
|
"CONVERT(`client`, CHAR) AS `client`,",
|
||||||
|
"CONVERT(`user`, CHAR) AS `user`, ",
|
||||||
|
"`is_draft`,",
|
||||||
|
"`value`",
|
||||||
|
"FROM `orderSummaries`",
|
||||||
|
"WHERE `id` = ?",
|
||||||
|
].join(" "),
|
||||||
|
[id],
|
||||||
|
) as data<{
|
||||||
|
id: string,
|
||||||
|
client: string,
|
||||||
|
user: string,
|
||||||
|
is_draft: 0 | 1,
|
||||||
|
value: number,
|
||||||
|
}>;
|
||||||
|
|
||||||
|
if (!order) throw createError({ statusCode: 404 });
|
||||||
|
|
||||||
|
const importedProducts = await getImportedProducts(id);
|
||||||
|
const work = await getWork(id);
|
||||||
|
|
||||||
|
return { ...order, imported_products: importedProducts, work };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler((e) => {
|
||||||
|
const key = e.context.params?.id;
|
||||||
|
return getOrder(key as string);
|
||||||
|
});
|
17
server/api/orders/[id].patch.ts
Normal file
17
server/api/orders/[id].patch.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/* global defineEventHandler, readBody, createError */
|
||||||
|
|
||||||
|
import { checkIsOrder } from "../orders.post";
|
||||||
|
import { database as db } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const body = await readBody(e);
|
||||||
|
const id = e.context.params?.id;
|
||||||
|
|
||||||
|
if (!checkIsOrder(e, true)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||||
|
|
||||||
|
const database = await db.new();
|
||||||
|
await database.beginTransaction();
|
||||||
|
|
||||||
|
for (const [k, v] of Object.entries(body))
|
||||||
|
database.query(`UPDATE TABLE \`orders\` SET \`${k}\` = ? WHERE \`id\` = ?`, [v, id]);
|
||||||
|
});
|
12
server/api/orders/[id]/imported_products.get.ts
Normal file
12
server/api/orders/[id]/imported_products.get.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
|
||||||
|
import { orderExists, getImportedProducts } from "../[id].get";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const id = e.context.params?.id as string;
|
||||||
|
|
||||||
|
if (!orderExists(id)) throw createError({ statusCode: 404 });
|
||||||
|
|
||||||
|
const importedProducts = await getImportedProducts(id);
|
||||||
|
return importedProducts;
|
||||||
|
});
|
27
server/api/orders/[id]/imported_products.post.ts
Normal file
27
server/api/orders/[id]/imported_products.post.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/* global defineEventHandler, readBody, createError, setResponseStatus */
|
||||||
|
|
||||||
|
import { checkIsImportedProduct } from "../../orders.post";
|
||||||
|
import { getImportedProducts, orderExists } from "../[id].get";
|
||||||
|
import Snowflake from "~/utils/snowflake";
|
||||||
|
import { database } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const body = await readBody(e);
|
||||||
|
const idOrder = e.context.params?.id as string;
|
||||||
|
const idImportedProducts = new Snowflake().toString();
|
||||||
|
|
||||||
|
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
|
||||||
|
if (!checkIsImportedProduct(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||||
|
|
||||||
|
await database.query(
|
||||||
|
["INSERT INTO",
|
||||||
|
"`imported_products`",
|
||||||
|
"VALUES",
|
||||||
|
"(?, ?, ?, ?, ?, ?)",
|
||||||
|
].join(" "),
|
||||||
|
[idImportedProducts, idOrder, body.name, body.link, body.price_imported, body.price],
|
||||||
|
);
|
||||||
|
|
||||||
|
setResponseStatus(e, 201);
|
||||||
|
return getImportedProducts(idOrder);
|
||||||
|
});
|
12
server/api/orders/[id]/work.get.ts
Normal file
12
server/api/orders/[id]/work.get.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
|
||||||
|
import { orderExists, getWork } from "../[id].get";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const id = e.context.params?.id as string;
|
||||||
|
|
||||||
|
if (!orderExists(id)) throw createError({ statusCode: 404 });
|
||||||
|
|
||||||
|
const work = await getWork(id);
|
||||||
|
return work;
|
||||||
|
});
|
28
server/api/orders/[id]/work.post.ts
Normal file
28
server/api/orders/[id]/work.post.ts
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
/* global defineEventHandler, readBody, createError, setResponseStatus */
|
||||||
|
|
||||||
|
import { checkIsWork } from "../../orders.post";
|
||||||
|
import { getWork, orderExists } from "../[id].get";
|
||||||
|
import Snowflake from "~/utils/snowflake";
|
||||||
|
import { database } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const body = await readBody(e);
|
||||||
|
const idOrder = e.context.params?.id as string;
|
||||||
|
const idWork = new Snowflake().toString();
|
||||||
|
|
||||||
|
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
|
||||||
|
if (!checkIsWork(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||||
|
|
||||||
|
await database.query(
|
||||||
|
["INSERT INTO",
|
||||||
|
"`work`",
|
||||||
|
"VALUES",
|
||||||
|
"(?, ?, ?, ?, ?, ?)",
|
||||||
|
].join(" "),
|
||||||
|
[idWork, idOrder, body.offer, body.price, body.notes, body.is_fulfilled],
|
||||||
|
);
|
||||||
|
|
||||||
|
setResponseStatus(e, 201);
|
||||||
|
|
||||||
|
return getWork(idWork);
|
||||||
|
});
|
20
server/api/orders/[id]/work/[idWork].delete.ts
Normal file
20
server/api/orders/[id]/work/[idWork].delete.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
|
||||||
|
import { ResultSetHeader } from "mysql2";
|
||||||
|
import { orderExists } from "../../[id].get";
|
||||||
|
import { database } from "~/server/utils/database";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const idOrder = e.context.params?.id as string;
|
||||||
|
const idWork = e.context.params?.idWork as string;
|
||||||
|
|
||||||
|
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
|
||||||
|
|
||||||
|
const [response] = await database.query(
|
||||||
|
"DELETE FROM `work` WHERE `id` = ?",
|
||||||
|
[idWork],
|
||||||
|
) as unknown as [ResultSetHeader];
|
||||||
|
|
||||||
|
if (response.affectedRows === 0) throw createError({ statusCode: 404 });
|
||||||
|
return null;
|
||||||
|
});
|
11
server/api/orders/[id]/work/[idWork].get.ts
Normal file
11
server/api/orders/[id]/work/[idWork].get.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/* global defineEventHandler, createError */
|
||||||
|
|
||||||
|
import { orderExists, getWork } from "../../[id].get";
|
||||||
|
|
||||||
|
export default defineEventHandler((e) => {
|
||||||
|
const idOrder = e.context.params?.id as string;
|
||||||
|
const idWork = e.context.params?.idWork as string;
|
||||||
|
|
||||||
|
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
|
||||||
|
return getWork(idWork);
|
||||||
|
});
|
|
@ -5,7 +5,7 @@ import { ResultSetHeader } from "mysql2/promise";
|
||||||
|
|
||||||
import { data, database } from "./database";
|
import { data, database } from "./database";
|
||||||
import { isString } from "./isString";
|
import { isString } from "./isString";
|
||||||
import Snowflake from "./snowflake";
|
import Snowflake from "~/utils/snowflake";
|
||||||
import { client } from "~/utils/types/database";
|
import { client } from "~/utils/types/database";
|
||||||
|
|
||||||
type queryType = {
|
type queryType = {
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
import mysql from "mysql2/promise";
|
import mysql, { Connection } from "mysql2/promise";
|
||||||
|
|
||||||
export const database = await mysql.createConnection({
|
const connectionOptions: mysql.ConnectionOptions = {
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: Number(process.env.DB_PORT),
|
port: Number(process.env.DB_PORT),
|
||||||
user: process.env.DB_USER,
|
user: process.env.DB_USER,
|
||||||
password: process.env.DB_PASSWORD,
|
password: process.env.DB_PASSWORD,
|
||||||
database: process.env.DB_SCHEMA,
|
database: process.env.DB_SCHEMA,
|
||||||
});
|
decimalNumbers: true,
|
||||||
|
supportBigNumbers: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const database =
|
||||||
|
await mysql.createConnection(connectionOptions) as Connection & { new: () => Promise<Connection> };
|
||||||
|
database.new = () => { return mysql.createConnection(connectionOptions); };
|
||||||
|
|
||||||
export type data<T> = [T[], mysql.FieldPacket[]];
|
export type data<T> = [T[], mysql.FieldPacket[]];
|
||||||
|
|
25
server/utils/getRequestingUser.ts
Normal file
25
server/utils/getRequestingUser.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/* global getCookie, createError */
|
||||||
|
import { H3Event } from "h3";
|
||||||
|
|
||||||
|
import { database, data } from "./database";
|
||||||
|
import { user } from "~/utils/types/database";
|
||||||
|
|
||||||
|
export default async function getRequestingUser(e: H3Event) {
|
||||||
|
const cookie = getCookie(e, "token");
|
||||||
|
const [[user]] = await database.query(
|
||||||
|
["SELECT",
|
||||||
|
"CONVERT(`users`.`id`, CHAR) as `id`,",
|
||||||
|
"`users`.`username`,",
|
||||||
|
"`users`.`email`,",
|
||||||
|
"`users`.`display_name`",
|
||||||
|
"FROM",
|
||||||
|
"`sessions`",
|
||||||
|
"LEFT JOIN `users` ON `sessions`.`user` = `users`.`id`",
|
||||||
|
"WHERE `sessions`.`id` = ?",
|
||||||
|
].join(" "),
|
||||||
|
[cookie],
|
||||||
|
) as data<user>;
|
||||||
|
|
||||||
|
if (!user) throw createError("User not found");
|
||||||
|
return user;
|
||||||
|
}
|
31
server/utils/validation.ts
Normal file
31
server/utils/validation.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/* global createError */
|
||||||
|
|
||||||
|
export function createValidationError(errors: Map<string, string>) {
|
||||||
|
let message = "Invalid parameters: ";
|
||||||
|
for (const i in errors)
|
||||||
|
message += i + ", ";
|
||||||
|
message = message.slice(0, -2);
|
||||||
|
return createError({
|
||||||
|
statusCode: 400,
|
||||||
|
message,
|
||||||
|
data: {
|
||||||
|
errors: Object.fromEntries(errors),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleRecursedValidationError(e: unknown, errors: Map<string, string>, element: string) {
|
||||||
|
if (typeof e !== "object") throw e;
|
||||||
|
if (!e) throw e;
|
||||||
|
if (!(e as any).data || !(e as any).message) throw e;
|
||||||
|
const upstreamErrors = (e as any).data.errors as any;
|
||||||
|
const upstreamMessage = (e as any).message;
|
||||||
|
if (upstreamErrors) {
|
||||||
|
for (const j in upstreamErrors)
|
||||||
|
errors.set(`${element}.${j}`, String(upstreamErrors[j]));
|
||||||
|
} else if (upstreamMessage) {
|
||||||
|
errors.set(`${element}`, String(upstreamMessage));
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,7 +31,11 @@ export default class Snowflake {
|
||||||
this.state = state + (value << 0n);
|
this.state = state + (value << 0n);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor(value?: bigint) {
|
||||||
|
if (value) {
|
||||||
|
this.state = BigInt.asUintN(64, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.set_timestamp(Date.now());
|
this.set_timestamp(Date.now());
|
||||||
this.set_processid(1);
|
this.set_processid(1);
|
||||||
this.set_increment(Snowflake.increment());
|
this.set_increment(Snowflake.increment());
|
||||||
|
@ -40,4 +44,8 @@ export default class Snowflake {
|
||||||
public toString() {
|
public toString() {
|
||||||
return this.state.toString();
|
return this.state.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get timestamp() {
|
||||||
|
return BigInt.asUintN(64 - 22, this.state >> 22n);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,68 +1,99 @@
|
||||||
export interface client {
|
export interface client {
|
||||||
id: string,
|
id: string;
|
||||||
name: string | null,
|
name: string | null;
|
||||||
address: string | null,
|
address: string | null;
|
||||||
phone: string | null,
|
phone: string | null;
|
||||||
email: string | null,
|
email: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface user {
|
export interface user {
|
||||||
id: string,
|
id: string;
|
||||||
username: string,
|
username: string;
|
||||||
email: string,
|
email: string;
|
||||||
display_name?: string,
|
display_name?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface session {
|
export interface session {
|
||||||
id: string,
|
id: string;
|
||||||
user: string,
|
user: string;
|
||||||
expiry_date: string,
|
expiry_date: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface imported_product {
|
export interface imported_product {
|
||||||
id: string,
|
id: string;
|
||||||
// order: string,
|
// order: string,
|
||||||
name?: string,
|
name?: string;
|
||||||
link: string,
|
link: string;
|
||||||
price_imported: string,
|
price_imported: string;
|
||||||
price: string,
|
price: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface offer {
|
export interface offer {
|
||||||
id: string,
|
id: string;
|
||||||
name: string,
|
name: string;
|
||||||
description?: string,
|
description?: string;
|
||||||
recommended_price?: string,
|
recommended_price?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface work {
|
export interface work {
|
||||||
id: string,
|
id: string;
|
||||||
// order: string,
|
// order: string,
|
||||||
offer: string|offer,
|
offer: string | offer;
|
||||||
price: string,
|
price: string;
|
||||||
notes: string,
|
notes: string;
|
||||||
is_fulfilled: boolean,
|
is_fulfilled: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface order {
|
export interface order {
|
||||||
id: string,
|
imported_products: Array<{
|
||||||
client: client|string,
|
id: string;
|
||||||
user: user|string,
|
name: string | null;
|
||||||
is_draft: boolean,
|
link: string;
|
||||||
imported_products: imported_product[],
|
price: string;
|
||||||
work: work[],
|
price_imported: string;
|
||||||
|
}>;
|
||||||
|
work: {
|
||||||
|
id: string;
|
||||||
|
offer: offer;
|
||||||
|
price: number;
|
||||||
|
notes: string | null;
|
||||||
|
is_fulfilled: 0 | 1;
|
||||||
|
}[];
|
||||||
|
id: string;
|
||||||
|
client: string;
|
||||||
|
user: string;
|
||||||
|
is_draft: 0 | 1;
|
||||||
|
value: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface orderSummary {
|
||||||
|
id: string;
|
||||||
|
client: string;
|
||||||
|
user: string;
|
||||||
|
is_draft: 0 | 1;
|
||||||
|
value: string;
|
||||||
|
imported_products_count: number;
|
||||||
|
work_count: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface work_template {
|
export interface work_template {
|
||||||
id: string,
|
id: string;
|
||||||
// order_template: string,
|
// order_template: string,
|
||||||
offer: string|offer,
|
offer: string | offer;
|
||||||
price: string,
|
price: string;
|
||||||
notes?: string,
|
notes?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface order_template {
|
export interface order_template {
|
||||||
id: string,
|
id: string;
|
||||||
name: string,
|
name: string;
|
||||||
description?: string,
|
description?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1 is true, 0 is false
|
||||||
|
export type Dboolean =
|
||||||
|
| boolean
|
||||||
|
| 0 // false
|
||||||
|
| 1; // true
|
||||||
|
|
||||||
|
export type Dnumber = number | `${number}`;
|
||||||
|
|
Loading…
Reference in a new issue