138 lines
4.8 KiB
TypeScript
138 lines
4.8 KiB
TypeScript
import { db } from '../db/db_index'
|
|
import { productInfo, productSkus, storeInfo } from '../db/schema'
|
|
import { and, asc, eq, inArray, isNull, or } from 'drizzle-orm'
|
|
import type { UserStoreDetailData, UserStoreProductData, UserStoreSummaryData, StoreSummary } from '@packages/shared'
|
|
import { composeSkuName, composeUnitNotation } from '../lib/sku-features'
|
|
import { marketStatsForRegion, isSkuSuspended } from '../lib/market-stats'
|
|
|
|
const getStringArray = (value: unknown): string[] | null => {
|
|
if (!Array.isArray(value)) return null
|
|
return value.map((item) => String(item))
|
|
}
|
|
|
|
export async function getStoreSummaries(regionId: number): Promise<UserStoreSummaryData[]> {
|
|
const storesData = await db.select({
|
|
id: storeInfo.id,
|
|
name: storeInfo.name,
|
|
description: storeInfo.description,
|
|
imageUrl: storeInfo.imageUrl,
|
|
}).from(storeInfo).where(or(eq(storeInfo.regionId, regionId), eq(storeInfo.isGlobal, true)))
|
|
|
|
const skus = await db.query.productSkus.findMany({
|
|
with: { product: true, marketStats: true },
|
|
orderBy: asc(productSkus.id),
|
|
})
|
|
const activeSkus = skus.filter((sku) =>
|
|
!sku.isDeleted &&
|
|
// The region's own products plus global ones (a global store is visible from
|
|
// every region, so its global catalogue must count here too). This mirrors
|
|
// getStoreDetail's product query.
|
|
(sku.product?.regionId === regionId || sku.product?.regionId == null) &&
|
|
!isSkuSuspended(sku.isSuspended, marketStatsForRegion(sku.marketStats, regionId))
|
|
)
|
|
|
|
const skusByStore = new Map<number, typeof skus>()
|
|
for (const sku of activeSkus) {
|
|
const storeId = sku.product?.storeId
|
|
if (storeId == null) continue
|
|
if (!skusByStore.has(storeId)) skusByStore.set(storeId, [])
|
|
skusByStore.get(storeId)!.push(sku)
|
|
}
|
|
|
|
return storesData.map((store) => {
|
|
const storeSkus = skusByStore.get(store.id) || []
|
|
const sampleProducts = storeSkus.slice(0, 3).map((sku) => ({
|
|
id: sku.id,
|
|
name: sku.product?.name ?? sku.name ?? 'Unknown',
|
|
images: getStringArray(sku.images),
|
|
}))
|
|
|
|
return {
|
|
id: store.id,
|
|
name: store.name,
|
|
description: store.description ?? null,
|
|
imageUrl: store.imageUrl ?? null,
|
|
productCount: storeSkus.length,
|
|
sampleProducts,
|
|
}
|
|
})
|
|
}
|
|
|
|
export async function getStoreDetail(regionId: number, storeId: number): Promise<UserStoreDetailData | null> {
|
|
const storeData = await db.query.storeInfo.findFirst({
|
|
where: and(eq(storeInfo.id, storeId), or(eq(storeInfo.regionId, regionId), eq(storeInfo.isGlobal, true))),
|
|
columns: {
|
|
id: true,
|
|
name: true,
|
|
description: true,
|
|
imageUrl: true,
|
|
},
|
|
})
|
|
|
|
if (!storeData) {
|
|
return null
|
|
}
|
|
|
|
const storeProductIds = await db
|
|
.select({ id: productInfo.id })
|
|
.from(productInfo)
|
|
.where(and(eq(productInfo.storeId, storeId), or(eq(productInfo.regionId, regionId), isNull(productInfo.regionId))))
|
|
|
|
const productIdArr = storeProductIds.map((p) => p.id)
|
|
|
|
const skus = productIdArr.length > 0
|
|
? await db.query.productSkus.findMany({
|
|
where: inArray(productSkus.productId, productIdArr),
|
|
with: {
|
|
product: true,
|
|
features: true,
|
|
marketStats: true,
|
|
},
|
|
})
|
|
: []
|
|
|
|
const products: UserStoreProductData[] = skus
|
|
.filter((sku) => !sku.isDeleted && !isSkuSuspended(sku.isSuspended, marketStatsForRegion(sku.marketStats, regionId)))
|
|
.map((sku) => {
|
|
const features = sku.features || []
|
|
const marketStats = marketStatsForRegion(sku.marketStats, regionId)
|
|
return {
|
|
id: sku.id,
|
|
name: composeSkuName(sku.product?.name ?? 'Unknown', features, sku.name),
|
|
shortDescription: sku.product?.shortDescription ?? null,
|
|
price: marketStats?.ourPrice ? String(marketStats.ourPrice) : '0',
|
|
marketPrice: marketStats?.marketPrice ? String(marketStats.marketPrice) : null,
|
|
incrementStep: sku.product?.incrementStep ?? 1,
|
|
unit: composeUnitNotation(features),
|
|
unitNotation: composeUnitNotation(features),
|
|
images: getStringArray(sku.images),
|
|
isOutOfStock: marketStats?.isOutOfStock ?? false,
|
|
productQuantity: 1,
|
|
}
|
|
})
|
|
|
|
return {
|
|
store: {
|
|
id: storeData.id,
|
|
name: storeData.name,
|
|
description: storeData.description ?? null,
|
|
imageUrl: storeData.imageUrl ?? null,
|
|
},
|
|
products,
|
|
}
|
|
}
|
|
|
|
export async function getStoresSummary(regionId: number): Promise<StoreSummary[]> {
|
|
return db.query.storeInfo.findMany({
|
|
// The region's own stores plus every global store (global stores are usable
|
|
// from any region). Also the source for the per-store cache files, so global
|
|
// stores get a regions/{id}/stores/<id>.json like regional ones.
|
|
where: or(eq(storeInfo.regionId, regionId), eq(storeInfo.isGlobal, true)),
|
|
columns: {
|
|
id: true,
|
|
name: true,
|
|
description: true,
|
|
isGlobal: true,
|
|
},
|
|
})
|
|
}
|