From 25b0b4ee74babeda91a483d27a923e57f5817e80 Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Wed, 9 Oct 2024 18:58:00 +0200 Subject: [PATCH] feat!: address and paymentMethod (#236) * fix: limit on increment * feat: country code * feat!: address and paymentMethod --- .../app/components/Checkout/DeliveryForm.vue | 179 +++++++++++------- apps/food/app/components/ui/input/Input.vue | 2 +- .../app/components/ui/textarea/Textarea.vue | 2 +- apps/food/app/pages/checkout/index.vue | 78 ++++++-- apps/food/app/utils/phoneHelpers.ts | 5 +- apps/food/prisma/schema.prisma | 78 +++++--- .../server/api/checkout/add/index.post.ts | 8 - .../server/api/checkout/line/[id].post.ts | 8 + apps/food/types/index.d.ts | 29 +++ 9 files changed, 267 insertions(+), 122 deletions(-) diff --git a/apps/food/app/components/Checkout/DeliveryForm.vue b/apps/food/app/components/Checkout/DeliveryForm.vue index 9818db2e..8aa74260 100644 --- a/apps/food/app/components/Checkout/DeliveryForm.vue +++ b/apps/food/app/components/Checkout/DeliveryForm.vue @@ -1,96 +1,147 @@ diff --git a/apps/food/app/components/ui/input/Input.vue b/apps/food/app/components/ui/input/Input.vue index 6a9482d9..1ada421b 100644 --- a/apps/food/app/components/ui/input/Input.vue +++ b/apps/food/app/components/ui/input/Input.vue @@ -1,5 +1,5 @@ diff --git a/apps/food/app/utils/phoneHelpers.ts b/apps/food/app/utils/phoneHelpers.ts index 219b449a..d8afbcf6 100644 --- a/apps/food/app/utils/phoneHelpers.ts +++ b/apps/food/app/utils/phoneHelpers.ts @@ -1,5 +1,3 @@ -import type { - CountryCode } from 'libphonenumber-js' import { AsYouType, formatIncompletePhoneNumber, @@ -33,7 +31,6 @@ export function formatPhoneNumber(value: string, countryCode: CountryCode) { return value } -export function checkPhoneNumberValidity(value: string, - countryCode: CountryCode) { +export function checkPhoneNumberValidity(value: string, countryCode: CountryCode) { return isValidPhoneNumber(value, countryCode) } diff --git a/apps/food/prisma/schema.prisma b/apps/food/prisma/schema.prisma index 38da9606..8ccf0631 100644 --- a/apps/food/prisma/schema.prisma +++ b/apps/food/prisma/schema.prisma @@ -8,23 +8,37 @@ datasource db { } model Channel { - id String @id - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @default(now()) @map("updated_at") - slug String - name String - description String? - currencyCode String @map("currency_code") - isActive Boolean @default(true) @map("is_active") - menus Menu[] - products Product[] - checkouts Checkout[] - users User[] - warehouses Warehouse[] + id String @id + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") + slug String + name String + description String? + countryCode String @map("country_code") + currencyCode String @map("currency_code") + isActive Boolean @default(true) @map("is_active") + paymentMethods PaymentMethod[] + warehouses Warehouse[] + menus Menu[] + products Product[] + checkouts Checkout[] + users User[] @@map("channel") } +model PaymentMethod { + id String @id + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") + name String + type String + channelId String @map("channel_id") + channel Channel @relation(fields: [channelId], references: [id]) + + @@map("payment_method") +} + model Media { id String @id createdAt DateTime @default(now()) @map("created_at") @@ -99,16 +113,20 @@ model ProductVariant { } model Checkout { - id String @id - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @default(now()) @map("updated_at") - deliveryMethod String @map("delivery_method") - shippingPrice Float @default(0) @map("shipping_price") - totalPrice Float @default(0) @map("total_price") - discount Float? - channelId String @map("channel_id") - channel Channel @relation(fields: [channelId], references: [id]) - lines CheckoutLine[] + id String @id + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") + deliveryMethod String @map("delivery_method") + paymentMethodId String @map("payment_method_id") + shippingPrice Float @default(0) @map("shipping_price") + totalPrice Float @default(0) @map("total_price") + discount Float? + warehouseId String? @map("warehouse_id") + addressId String? @map("address_id") + note String? + channelId String @map("channel_id") + channel Channel @relation(fields: [channelId], references: [id]) + lines CheckoutLine[] @@map("checkout") } @@ -182,3 +200,17 @@ model Warehouse { @@map("warehouse") } + +model Address { + id String @id + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") + street String + flat String? + doorphone String? + entrance String? + floor String? + note String? + + @@map("address") +} diff --git a/apps/food/server/api/checkout/add/index.post.ts b/apps/food/server/api/checkout/add/index.post.ts index 26838136..ad251417 100644 --- a/apps/food/server/api/checkout/add/index.post.ts +++ b/apps/food/server/api/checkout/add/index.post.ts @@ -67,14 +67,6 @@ export default defineEventHandler(async (event) => { }, }) } else { - // Limit - if (line.quantity >= 99) { - throw createError({ - statusCode: 400, - statusMessage: 'Limit reached', - }) - } - // Add +1 await prisma.checkoutLine.update({ where: { id: line.id }, diff --git a/apps/food/server/api/checkout/line/[id].post.ts b/apps/food/server/api/checkout/line/[id].post.ts index 32889e9b..4f5a17ef 100644 --- a/apps/food/server/api/checkout/line/[id].post.ts +++ b/apps/food/server/api/checkout/line/[id].post.ts @@ -45,6 +45,14 @@ export default defineEventHandler(async (event) => { } if (method === 'increment') { + // Limit + if (line.quantity >= 99) { + throw createError({ + statusCode: 400, + statusMessage: 'Limit reached', + }) + } + await prisma.checkoutLine.update({ where: { id }, data: { diff --git a/apps/food/types/index.d.ts b/apps/food/types/index.d.ts index f4b8ad3d..c3bcb280 100644 --- a/apps/food/types/index.d.ts +++ b/apps/food/types/index.d.ts @@ -7,11 +7,24 @@ declare global { name: string description: string | null currencyCode: CurrencyCode + countryCode: CountryCode isActive: boolean } type CurrencyCode = 'USD' | 'EUR' | 'RUB' + type CountryCode = 'RU' | 'US' | 'GB' | 'GR' | 'GE' | 'UA' | 'BY' | 'KZ' + + interface PaymentMethod { + id: string + createdAt: Date + updatedAt: Date + name: string + type: PaymentMethodType + } + + type PaymentMethodType = 'CASH' | 'CARD' | 'CUSTOM' + interface Media { id: string createdAt: Date @@ -86,9 +99,13 @@ declare global { createdAt: Date updatedAt: Date deliveryMethod: CheckoutDeliveryMethod + paymentMethodId: string shippingPrice: number totalPrice: number discount: number | null + note: string | null + warehouseId: string | null + addressId: string | null channelId: string } @@ -147,6 +164,18 @@ declare global { address: string channelId: string } + + interface Address { + id: string + createdAt: Date + updatedAt: Date + street: string + flat: string | null + doorphone: string | null + entrance: string | null + floor: string | null + note: string | null + } } export {}