162 lines
4.6 KiB
TypeScript
162 lines
4.6 KiB
TypeScript
import { db } from '../db/db_index'
|
|
import { offers, offerItems, productInfo, units } from '../db/schema'
|
|
import { eq, desc } from 'drizzle-orm'
|
|
|
|
export interface OfferItemInput {
|
|
productId: number
|
|
quantity: string
|
|
}
|
|
|
|
export interface CreateOfferInput {
|
|
name: string
|
|
shortDescription?: string | null
|
|
longDescription?: string | null
|
|
price: string
|
|
marketPrice?: string | null
|
|
images?: string[] | null
|
|
isSuspended?: boolean
|
|
isFlashEnabled?: boolean
|
|
items: OfferItemInput[]
|
|
}
|
|
|
|
export interface UpdateOfferInput extends Partial<CreateOfferInput> {}
|
|
|
|
export async function getAllOffers(): Promise<any[]> {
|
|
const results = await db.query.offers.findMany({
|
|
with: {
|
|
items: {
|
|
with: {
|
|
product: {
|
|
columns: { id: true, name: true, images: true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
orderBy: desc(offers.addedOn),
|
|
})
|
|
|
|
return results.map((offer: any) => ({
|
|
...offer,
|
|
images: offer.images || [],
|
|
items: (offer.items || []).map((item: any) => ({
|
|
id: item.id,
|
|
offerId: item.offerId,
|
|
productId: item.productId,
|
|
quantity: item.quantity,
|
|
product: item.product ? {
|
|
id: item.product.id,
|
|
name: item.product.name,
|
|
images: item.product.images || [],
|
|
} : null,
|
|
})),
|
|
}))
|
|
}
|
|
|
|
export async function getOfferById(id: number): Promise<any | null> {
|
|
const result = await db.query.offers.findFirst({
|
|
where: eq(offers.id, id),
|
|
with: {
|
|
items: {
|
|
with: {
|
|
product: {
|
|
columns: { id: true, name: true, images: true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!result) return null
|
|
|
|
return {
|
|
...result,
|
|
images: result.images || [],
|
|
items: (result.items || []).map((item: any) => ({
|
|
id: item.id,
|
|
offerId: item.offerId,
|
|
productId: item.productId,
|
|
quantity: item.quantity,
|
|
product: item.product ? {
|
|
id: item.product.id,
|
|
name: item.product.name,
|
|
images: item.product.images || [],
|
|
} : null,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export async function createOffer(input: CreateOfferInput): Promise<any> {
|
|
const [offer] = await db.insert(offers).values({
|
|
name: input.name,
|
|
shortDescription: input.shortDescription || null,
|
|
longDescription: input.longDescription || null,
|
|
price: numericStr(input.price),
|
|
marketPrice: input.marketPrice ? numericStr(input.marketPrice) : null,
|
|
images: input.images || [],
|
|
isSuspended: input.isSuspended || false,
|
|
isFlashEnabled: input.isFlashEnabled || false,
|
|
}).returning()
|
|
|
|
if (input.items.length > 0) {
|
|
await db.insert(offerItems).values(
|
|
input.items.map(item => ({
|
|
offerId: offer.id,
|
|
productId: item.productId,
|
|
quantity: item.quantity,
|
|
}))
|
|
)
|
|
}
|
|
|
|
return getOfferById(offer.id)
|
|
}
|
|
|
|
export async function updateOffer(id: number, input: UpdateOfferInput): Promise<any> {
|
|
const updateData: Record<string, any> = {}
|
|
if (input.name !== undefined) updateData.name = input.name
|
|
if (input.shortDescription !== undefined) updateData.shortDescription = input.shortDescription
|
|
if (input.longDescription !== undefined) updateData.longDescription = input.longDescription
|
|
if (input.price !== undefined) updateData.price = numericStr(input.price)
|
|
if (input.marketPrice !== undefined) updateData.marketPrice = input.marketPrice ? numericStr(input.marketPrice) : null
|
|
if (input.images !== undefined) updateData.images = input.images
|
|
if (input.isSuspended !== undefined) updateData.isSuspended = input.isSuspended
|
|
if (input.isFlashEnabled !== undefined) updateData.isFlashEnabled = input.isFlashEnabled
|
|
|
|
if (Object.keys(updateData).length > 0) {
|
|
await db.update(offers).set(updateData).where(eq(offers.id, id))
|
|
}
|
|
|
|
if (input.items !== undefined) {
|
|
await db.delete(offerItems).where(eq(offerItems.offerId, id))
|
|
if (input.items.length > 0) {
|
|
await db.insert(offerItems).values(
|
|
input.items.map(item => ({
|
|
offerId: id,
|
|
productId: item.productId,
|
|
quantity: item.quantity,
|
|
}))
|
|
)
|
|
}
|
|
}
|
|
|
|
return getOfferById(id)
|
|
}
|
|
|
|
export async function deleteOffer(id: number): Promise<void> {
|
|
await db.delete(offerItems).where(eq(offerItems.offerId, id))
|
|
await db.delete(offers).where(eq(offers.id, id))
|
|
}
|
|
|
|
export async function toggleSuspendOffer(id: number): Promise<any> {
|
|
const existing = await getOfferById(id)
|
|
if (!existing) throw new Error('Offer not found')
|
|
|
|
await db.update(offers)
|
|
.set({ isSuspended: !existing.isSuspended })
|
|
.where(eq(offers.id, id))
|
|
|
|
return getOfferById(id)
|
|
}
|
|
|
|
function numericStr(value: string): string {
|
|
return value
|
|
}
|