Skip to content

Commit

Permalink
Manage basket addition from Alias #382
Browse files Browse the repository at this point in the history
  • Loading branch information
ithiame committed Feb 22, 2024
1 parent 891b255 commit 8874d4e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/routes/(app)/cart/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { oneMaxPerLine } from '$lib/types/Product.js';
import { UrlDependency } from '$lib/types/UrlDependency.js';
import CmsDesign from '$lib/components/CmsDesign.svelte';
import { POS_ROLE_ID, SUPER_ADMIN_ROLE_ID } from '$lib/types/User';
import { CUSTOMER_ROLE_ID } from '$lib/types/User';
export let data;
Expand Down Expand Up @@ -43,6 +43,7 @@
});
let alias = '';
let formAlias: HTMLFormElement;
const { t, locale, countryName } = useI18n();
</script>

Expand Down Expand Up @@ -70,12 +71,13 @@
{/if}
<div class="w-full rounded-xl p-6 flex flex-col gap-6 body-mainPlan border-gray-300">
<h1 class="page-title body-title">{t('cart.items')}</h1>
{#if data.roleId === POS_ROLE_ID || data.roleId === SUPER_ADMIN_ROLE_ID}
{#if data.roleId && data.roleId !== CUSTOMER_ROLE_ID}
<form
action="/product/{alias}?/addToCart"
method="post"
class="flex flex-col gap-2"
bind:this={formAlias}
use:enhance
>
<div class="gap-4 flex flex-col md:flex-row">
<label class="form-label w-[20em]">
Expand Down
10 changes: 4 additions & 6 deletions src/routes/(app)/product/[id]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,19 @@ async function addToCart({ params, request, locals }: RequestEvent) {
alias: formData.get('alias') || undefined
});
if (!product && alias) {
throw redirect(303, request.headers.get('referer') || 'cart');
}
if (product && product.availableDate && !product.preorder && product.availableDate > new Date()) {
throw redirect(303, request.headers.get('referer') || 'cart');
return { errorNotExistingAlias: true };
}

const cart = await getCartFromDb({ user: userIdentifier(locals) });
if (product) {
const availableAmount = await computeAvailableAmount(product, cart);
if (availableAmount <= 0) {
throw redirect(303, request.headers.get('referer') || 'cart');
return { errorOutOfStock: true };
}
}

if (product && product.availableDate && !product.preorder && product.availableDate > new Date()) {
throw redirect(303, request.headers.get('referer') || 'cart');
return { errorAvailableDate: true };
}
if (!product) {
throw error(404, 'Product not found');
Expand Down

0 comments on commit 8874d4e

Please sign in to comment.