42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { db } from '../db/db_index'
|
|
import { deliverySlotInfo, productMarketStats } from '../db/schema'
|
|
import { asc, eq } from 'drizzle-orm'
|
|
import type { InferSelectModel } from 'drizzle-orm'
|
|
import type { UserDeliverySlot, UserSlotAvailability } from '@packages/shared'
|
|
|
|
type SlotRow = InferSelectModel<typeof deliverySlotInfo>
|
|
|
|
const mapSlot = (slot: SlotRow): UserDeliverySlot => ({
|
|
id: slot.id,
|
|
deliveryTime: slot.deliveryTime!,
|
|
freezeTime: slot.freezeTime!,
|
|
isActive: slot.isActive,
|
|
isFlash: slot.isFlash,
|
|
isCapacityFull: slot.isCapacityFull,
|
|
deliverySequence: slot.deliverySequence,
|
|
groupIds: slot.groupIds,
|
|
})
|
|
|
|
export async function getActiveSlotsList(): Promise<UserDeliverySlot[]> {
|
|
const slots = await db.query.deliverySlotInfo.findMany({
|
|
where: eq(deliverySlotInfo.isActive, true),
|
|
orderBy: asc(deliverySlotInfo.deliveryTime),
|
|
})
|
|
|
|
return slots.map(mapSlot)
|
|
}
|
|
|
|
export async function getProductAvailability(): Promise<UserSlotAvailability[]> {
|
|
const stats = await db.query.productMarketStats.findMany({
|
|
where: eq(productMarketStats.isSuspended, false),
|
|
columns: {
|
|
skuId: true,
|
|
isOutOfStock: true,
|
|
},
|
|
})
|
|
|
|
return stats.map((stat) => ({
|
|
id: stat.skuId,
|
|
isOutOfStock: stat.isOutOfStock,
|
|
}))
|
|
}
|