Skip to content

Commit

Permalink
feat: prisma 6 adaption
Browse files Browse the repository at this point in the history
  • Loading branch information
elishahung committed Feb 25, 2025
1 parent 872701f commit 57859bf
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 154 deletions.
1 change: 0 additions & 1 deletion src/components/admin/Commodities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default function Commodities() {
const { showFormDialog, formDialog } = useFormDialog()
const [supplierId, setSupplierId] = useState<number | undefined>(undefined)
const { data, error, isError, isLoading } = trpc.commodity.getList.useQuery({
includeMenus: true,
onlyFromSupplierId: supplierId,
includeStatistics: true,
})
Expand Down
38 changes: 16 additions & 22 deletions src/lib/server/database/commodity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ type GetCommoditiesFull = Prisma.CommodityGetPayload<
}
}

export async function getCommodities<
FMenu extends boolean,
FStatistics extends boolean,
>(props: {
includeMenus?: FMenu
export async function getCommodities<FStatistics extends boolean>(props: {
includeIds?: number[]
onlyFromSupplierId?: number
includeStatistics?: FStatistics
Expand Down Expand Up @@ -179,25 +175,23 @@ export async function getCommodities<
name: true,
},
},
onMenus: props.includeMenus
? {
onMenus: {
select: {
limitPerUser: true,
stock: true,
menu: {
select: {
limitPerUser: true,
stock: true,
menu: {
select: {
id: true,
name: true,
type: true,
date: true,
},
},
},
where: {
isDeleted: false,
id: true,
name: true,
type: true,
date: true,
},
}
: undefined,
},
},
where: {
isDeleted: false,
},
},
},
orderBy: {
id: 'asc',
Expand Down
177 changes: 57 additions & 120 deletions src/lib/server/database/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,67 +274,6 @@ type GetMenuWithComsArgs = {
excludeCartItems?: boolean
client?: Prisma.TransactionClient | PrismaClient
}
type MenuWithComs = Prisma.MenuGetPayload<{
select: {
id: true
date: true
name: true
type: true
description: true
limitPerUser: true
publishedDate: true
closedDate: true
commodities: {
select: {
limitPerUser: true
stock: true
commodity: {
select: {
id: true
name: true
description: true
price: true
optionSets: true
image: {
select: {
path: true
}
}
categories: {
select: {
name: true
order: true
rootCategory: {
select: {
name: true
order: true
}
}
}
}
}
}
cartItems:
| {
select: {
quantity: true
}
}
| undefined
orderItems: {
select: {
order: {
select: {
userId: true
}
}
quantity: true
}
}
}
}
}
}>
/** Get menu and return with unavailableReasons from validation */
export async function getMenuWithComs({
menuId,
Expand Down Expand Up @@ -369,88 +308,86 @@ export async function getMenuWithComs({
limitPerUser: true,
publishedDate: true,
closedDate: true,
commodities: userId
? {
where: {
commodityId: limitCommodityIds
? { in: limitCommodityIds }
: undefined,
isDeleted: false,
commodity: {
isDeleted: false,
},
},
commodities: {
where: {
commodityId: limitCommodityIds
? { in: limitCommodityIds }
: undefined,
isDeleted: false,
commodity: {
isDeleted: false,
},
},
select: {
limitPerUser: true,
stock: true,
commodity: {
select: {
limitPerUser: true,
stock: true,
commodity: {
id: true,
name: true,
description: true,
price: true,
optionSets: true,
image: {
select: {
path: true,
},
},
categories: {
select: {
id: true,
name: true,
description: true,
price: true,
optionSets: true,
image: {
select: {
path: true,
},
},
categories: {
order: true,
rootCategory: {
select: {
name: true,
order: true,
rootCategory: {
select: {
name: true,
order: true,
},
},
},
},
},
},
cartItems: excludeCartItems
? undefined
: {
where: {
userId,
invalid: false,
},
select: {
quantity: true,
},
},
orderItems: {
},
},
cartItems: excludeCartItems
? undefined
: {
where: {
createdAt: isLive // only show today's order for stock validation
? {
gte: new Date(new Date().setHours(0, 0, 0, 0)),
}
: undefined,
order: {
timeCanceled: null,
},
userId,
invalid: false,
},
select: {
order: {
select: {
userId: true,
},
},
quantity: true,
},
},
orderItems: {
where: {
createdAt: isLive // only show today's order for stock validation
? {
gte: new Date(new Date().setHours(0, 0, 0, 0)),
}
: undefined,
order: {
timeCanceled: null,
},
},
select: {
order: {
select: {
userId: true,
},
},
quantity: true,
},
}
: undefined,
},
},
},
},
})

if (!rawMenu) {
throw new Error('menu not found')
}
// XXX: onditional select hotfix, need to find a better way
const menu = rawMenu as unknown as ConvertPrismaJson<MenuWithComs>

const menu = rawMenu as ConvertPrismaJson<typeof rawMenu>

// Validate menu and coms status
let menuOrderedCount = 0
Expand Down
1 change: 0 additions & 1 deletion src/lib/trpc/api/commodity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export const CommodityRouter = router({
getList: staffProcedure
.input(
z.object({
includeMenus: z.boolean().optional(),
includeIds: z.array(z.number()).optional(),
onlyFromSupplierId: z.number().optional(),
includeStatistics: z.boolean().optional(),
Expand Down
20 changes: 10 additions & 10 deletions src/pages/deposit/[depositId].tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
import { DepositStatus } from '@prisma/client'
import {
CheckCircleIcon,
InformationCircleIcon,
ExclamationCircleIcon,
InformationCircleIcon,
} from '@heroicons/react/24/outline'
import { DepositStatus } from '@prisma/client'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'

import trpc, { DepositData } from '@/lib/client/trpc'
import Button from '@/components/core/Button'
import Error from '@/components/core/Error'
import { SpinnerBlock } from '@/components/core/Spinner'
import Title from '@/components/core/Title'
import Button from '@/components/core/Button'
import { useStore } from '@/lib/client/store'
import trpc, { DepositData } from '@/lib/client/trpc'

export default function PageDepositDetail() {
const router = useRouter()
Expand Down Expand Up @@ -50,16 +50,16 @@ export default function PageDepositDetail() {
if (isError) return <Error description={error.message} />
if (isLoading) return <SpinnerBlock />

const isSuccess = data.status === DepositStatus.SUCCESS
const isPending = data.status === DepositStatus.PENDING
const isSuccess = data?.status === DepositStatus.SUCCESS
const isPending = data?.status === DepositStatus.PENDING

return (
<div className='flex h-full w-full flex-col items-center justify-center'>
<Title prefix={`儲值紀錄 ${depositId}`} />
<div className='flex flex-col items-center justify-center rounded-2xl border p-4 shadow-lg sm:p-8'>
<p className='mb-1 font-mono font-bold text-stone-400'>{depositId}</p>
<p className='mb-4 text-xs text-stone-400'>
{data.createdAt.toLocaleString()}
{data?.createdAt.toLocaleString()}
</p>
{/* deposit status with icon on full page */}
{isSuccess ? (
Expand Down Expand Up @@ -92,7 +92,7 @@ export default function PageDepositDetail() {
'付款手續出現錯誤,如有問題請聯繫公司財務。'
)}
</p>
<p className='text-stone-400'>儲值 {data.amount}</p>
<p className='text-stone-400'>儲值 {data?.amount}</p>
</div>
{typeof redirectUrl === 'string' && (
<Button
Expand Down

0 comments on commit 57859bf

Please sign in to comment.