stuff done
This commit is contained in:
107
actions/admin/entity.ts
Normal file
107
actions/admin/entity.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
'use server'
|
||||
|
||||
import {EntityLocale, EntityType, Meta} from '@prisma/client'
|
||||
import {z} from 'zod'
|
||||
|
||||
import {i18nLocalesCodes} from '@/i18n-config'
|
||||
import {STORE_ID} from '@/lib/config/constants'
|
||||
import {db} from '@/lib/db/prisma/client'
|
||||
import {createEntityFormSchema} from '@/lib/schemas/admin/entity'
|
||||
import {
|
||||
cleanEmptyParams,
|
||||
dbErrorHandling,
|
||||
slug as slugger,
|
||||
toEmptyParams
|
||||
} from '@/lib/utils'
|
||||
|
||||
export const onEntityCreateEditAction = async (
|
||||
formData: z.infer<typeof createEntityFormSchema>
|
||||
) => {
|
||||
const validatedData = createEntityFormSchema.parse(formData)
|
||||
|
||||
if (!validatedData) return {error: 'Недійсні вхідні дані'}
|
||||
|
||||
if (validatedData.locales.length < i18nLocalesCodes.length) {
|
||||
return {error: 'Заповніть всі мови'}
|
||||
}
|
||||
|
||||
const {published, media, type, slug, scopes} = validatedData
|
||||
|
||||
const meta: Meta[] = []
|
||||
|
||||
for (const i in validatedData.meta) {
|
||||
const normalizedMeta: any = cleanEmptyParams(validatedData.meta[i])
|
||||
|
||||
meta.push(normalizedMeta)
|
||||
}
|
||||
|
||||
const locales: EntityLocale[] = []
|
||||
|
||||
for (const i in validatedData.locales) {
|
||||
const locale = validatedData.locales[i]
|
||||
const {title, lang} = locale
|
||||
const slug = slugger(title, lang)
|
||||
|
||||
//const result = await getProductBySlug({slug, lang})
|
||||
const result = null
|
||||
|
||||
if (!result) {
|
||||
const normalized: any = cleanEmptyParams({slug, ...locale})
|
||||
|
||||
locales.push(normalized)
|
||||
} else {
|
||||
return {error: `Сутність з такою назвою ${title} вже існує`}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const newEntity = await db.entity.create({
|
||||
data: {
|
||||
published,
|
||||
scopes: scopes ? JSON.parse(scopes) : null,
|
||||
type: type as EntityType,
|
||||
slug: slug || null,
|
||||
media: media || null,
|
||||
locales: {
|
||||
create: locales
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return {success: JSON.stringify(newEntity, null, 2)}
|
||||
} catch (error) {
|
||||
return dbErrorHandling(error)
|
||||
}
|
||||
}
|
||||
|
||||
export const getBlockEntity = async (scope: string) => {
|
||||
return db.entity.findMany({
|
||||
where: {
|
||||
published: true,
|
||||
storeId: STORE_ID,
|
||||
type: 'block',
|
||||
scopes: {
|
||||
array_contains: [scope]
|
||||
}
|
||||
},
|
||||
include: {
|
||||
locales: true
|
||||
},
|
||||
orderBy: {
|
||||
position: 'asc'
|
||||
}
|
||||
})
|
||||
}
|
||||
export const getPageEntityBySlug = async (slug: string) => {
|
||||
return db.entity.findFirst({
|
||||
where: {
|
||||
published: true,
|
||||
storeId: STORE_ID,
|
||||
type: 'page',
|
||||
slug
|
||||
},
|
||||
include: {
|
||||
locales: true
|
||||
}
|
||||
})
|
||||
}
|
||||
44
actions/admin/mailer.ts
Normal file
44
actions/admin/mailer.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
'use server'
|
||||
|
||||
import nodemailer from 'nodemailer'
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: 'smtp.gmail.com',
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: 'vista@ugmail.org',
|
||||
pass: 'hqhowacppifsefxl'
|
||||
}
|
||||
})
|
||||
|
||||
type SendMailProps = {
|
||||
email: string
|
||||
subject: string
|
||||
text: string
|
||||
html: string
|
||||
}
|
||||
|
||||
export async function sendMail({email, subject, text, html}: SendMailProps) {
|
||||
try {
|
||||
const info = await transporter.sendMail({
|
||||
from: `"BeWell" <vista@ugmail.org>`,
|
||||
to: email,
|
||||
bcc: [
|
||||
'yevhen.odynets@gmail.com',
|
||||
'shopping@amok.space',
|
||||
{
|
||||
name: 'Actus Septem',
|
||||
address: 'actus.septem@ukr.net'
|
||||
}
|
||||
],
|
||||
subject,
|
||||
text,
|
||||
html
|
||||
})
|
||||
|
||||
return {ok: true, messageId: info.messageId}
|
||||
} catch (e) {
|
||||
return {ok: false, message: JSON.stringify(e)}
|
||||
}
|
||||
}
|
||||
24
actions/admin/order.tsx
Normal file
24
actions/admin/order.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import {db} from '@/lib/db/prisma/client'
|
||||
|
||||
export const getAllOrders = async () => {
|
||||
return db.order.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: 'desc'
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export const getOrdersByUserId = async (userId: number) => {
|
||||
return db.order.findMany({
|
||||
where: {
|
||||
userId
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: 'desc'
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
77
actions/admin/place-order.ts
Normal file
77
actions/admin/place-order.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
'use server'
|
||||
|
||||
import {DeliveryOption, Lang, Order} from '@prisma/client'
|
||||
import {z} from 'zod'
|
||||
|
||||
import {sendMail} from '@/actions/admin/mailer'
|
||||
import {STORE_ID} from '@/lib/config/constants'
|
||||
import dayjs from '@/lib/config/dayjs'
|
||||
import {db} from '@/lib/db/prisma/client'
|
||||
import {createOrderFormSchema} from '@/lib/schemas/admin/order'
|
||||
import {dbErrorHandling} from '@/lib/utils'
|
||||
|
||||
const generateOrderNo = (): string => {
|
||||
const hex = Math.floor(Math.random() * 16777215)
|
||||
.toString(16)
|
||||
.slice(0, 3)
|
||||
.toUpperCase()
|
||||
|
||||
return `${dayjs().format('YYMM')}-${hex}`
|
||||
}
|
||||
|
||||
export const onPlacingOrder = async (
|
||||
formData: z.infer<typeof createOrderFormSchema>
|
||||
) => {
|
||||
const fields = createOrderFormSchema.parse(formData)
|
||||
if (!fields) return {error: 'Недійсні вхідні дані'}
|
||||
|
||||
const orderNo = generateOrderNo()
|
||||
|
||||
try {
|
||||
const newOrder: Order = await db.order.create({
|
||||
data: {
|
||||
storeId: STORE_ID,
|
||||
lang: fields.lang as Lang,
|
||||
orderNo,
|
||||
isQuick: fields.is_quick,
|
||||
userId: fields.user_id ? parseInt(fields.user_id) : null,
|
||||
firstName: fields.first_name,
|
||||
surname: fields.surname,
|
||||
deliveryOption: fields.delivery_option as DeliveryOption,
|
||||
phone: fields.phone,
|
||||
email: fields.email,
|
||||
address: fields.address.length > 10 ? JSON.parse(fields.address) : null,
|
||||
notes: fields.notes?.toString().trim() !== '' ? fields.notes : null,
|
||||
details: fields.details ? JSON.parse(fields.details) : null
|
||||
}
|
||||
})
|
||||
|
||||
const text = JSON.stringify(newOrder, null, 2)
|
||||
|
||||
const result = await sendMail({
|
||||
email: `${newOrder.firstName} ${newOrder.surname} <${newOrder.email as string}>`,
|
||||
subject: `Замовлення № ${orderNo}`,
|
||||
text,
|
||||
html: `<pre>${text}</pre>`
|
||||
})
|
||||
|
||||
const updated = await db.order.update({
|
||||
where: {
|
||||
id: newOrder.id
|
||||
},
|
||||
data: {
|
||||
emailSent: result.ok
|
||||
}
|
||||
})
|
||||
|
||||
if (result.ok) {
|
||||
return {success: newOrder.orderNo}
|
||||
} else {
|
||||
return {
|
||||
error: result.message
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return dbErrorHandling(error)
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ export const onProductCreateAction = async (
|
||||
}
|
||||
})
|
||||
|
||||
return {success: 'JSON.stringify(newProduct, null, 2)'}
|
||||
return {success: JSON.stringify(newProduct, null, 2)}
|
||||
} catch (error) {
|
||||
return dbErrorHandling(error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user