85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
import { scaffoldAssetUrl } from '@/src/lib/s3-client'
|
|
import { ApiError } from '@/src/lib/api-error'
|
|
import { getTagsByStoreId } from '@/src/stores/product-tag-store'
|
|
import {
|
|
getUserStoreSummaries as getUserStoreSummariesInDb,
|
|
getUserStoreDetail as getUserStoreDetailInDb,
|
|
} from '@/src/dbService'
|
|
import type {
|
|
UserStoresResponse,
|
|
UserStoreDetail,
|
|
UserStoreSummary,
|
|
} from '@packages/shared'
|
|
|
|
export async function scaffoldStores(): Promise<UserStoresResponse> {
|
|
const storesData = await getUserStoreSummariesInDb()
|
|
|
|
const storesWithDetails: UserStoreSummary[] = storesData.map((store) => {
|
|
const signedImageUrl = store.imageUrl ? scaffoldAssetUrl(store.imageUrl) : null
|
|
const sampleProducts = store.sampleProducts.map((product) => ({
|
|
id: product.id,
|
|
name: product.name,
|
|
signedImageUrl: product.images && product.images.length > 0
|
|
? scaffoldAssetUrl(product.images[0])
|
|
: null,
|
|
}))
|
|
|
|
return {
|
|
id: store.id,
|
|
name: store.name,
|
|
description: store.description,
|
|
signedImageUrl,
|
|
productCount: store.productCount,
|
|
sampleProducts,
|
|
}
|
|
})
|
|
|
|
return {
|
|
stores: storesWithDetails,
|
|
}
|
|
}
|
|
|
|
export async function scaffoldStoreWithProducts(storeId: number): Promise<UserStoreDetail> {
|
|
const storeDetail = await getUserStoreDetailInDb(storeId)
|
|
|
|
if (!storeDetail) {
|
|
throw new ApiError('Store not found', 404)
|
|
}
|
|
|
|
const signedImageUrl = storeDetail.store.imageUrl
|
|
? scaffoldAssetUrl(storeDetail.store.imageUrl)
|
|
: null
|
|
|
|
const productsWithSignedUrls = storeDetail.products.map((product) => ({
|
|
id: product.id,
|
|
name: product.name,
|
|
shortDescription: product.shortDescription,
|
|
price: product.price,
|
|
marketPrice: product.marketPrice,
|
|
incrementStep: product.incrementStep,
|
|
unit: product.unit,
|
|
unitNotation: product.unitNotation,
|
|
images: scaffoldAssetUrl(product.images || []),
|
|
isOutOfStock: product.isOutOfStock,
|
|
productQuantity: product.productQuantity,
|
|
}))
|
|
|
|
const tags = await getTagsByStoreId(storeId)
|
|
|
|
return {
|
|
store: {
|
|
id: storeDetail.store.id,
|
|
name: storeDetail.store.name,
|
|
description: storeDetail.store.description,
|
|
signedImageUrl,
|
|
},
|
|
products: productsWithSignedUrls,
|
|
tags: tags.map(tag => ({
|
|
id: tag.id,
|
|
tagName: tag.tagName,
|
|
tagDescription: tag.tagDescription,
|
|
imageUrl: tag.imageUrl,
|
|
productIds: tag.productIds,
|
|
})),
|
|
}
|
|
}
|