Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Product Transformation Utilities
*
* Transforms database products (with ProductImage[] and Review[]) to
* legacy Product type used by existing components.
*
* IMPORTANT: This transforms FROM the superior DB structure TO the legacy format.
* The database structure remains unchanged.
*/
import type { Product } from '@/types/product';
import type { Product as DBProduct, ProductImage, Review } from '@prisma/client';
/**
* Type for DB product with all relations loaded
*/
export type DBProductWithRelations = DBProduct & {
images: ProductImage[];
reviews: Review[];
_count?: {
reviews?: number;
};
};
/**
* Transform ProductImage[] to legacy imgs object
*
* Uses thumbnailUrl and url fields, sorted by order field.
*/
export function formatProductImages(images: ProductImage[]): {
thumbnails: string[];
previews: string[];
} {
// Sort by order field
const sorted = [...images].sort((a, b) => a.order - b.order);
return {
thumbnails: sorted.map((img) => img.thumbnailUrl || img.url),
previews: sorted.map((img) => img.url)};
}
/**
* Calculate review statistics from Review[]
*
* Returns count and average rating.
*/
export function calculateReviewStats(reviews: Review[]): {
count: number;
averageRating: number;
} {
if (!reviews || reviews.length === 0) {
return { count: 0, averageRating: 0 };
}
const sum = reviews.reduce((acc, review) => acc + review.rating, 0);
return {
count: reviews.length,
averageRating: Math.round((sum / reviews.length) * 10) / 10, // Round to 1 decimal
};
}
/**
* Main transformation function: DB → Legacy
*
* Transforms a database product (with relations) to the legacy Product type
* that existing components expect.
*
* @param dbProduct - Product from database with images and reviews relations
* @returns Legacy Product object
*
* @example
* ```ts
* const dbProduct = await prisma.product.findUnique({
* where: { id: 1 },
* include: { images: true, reviews: true }
* });
*
* const legacyProduct = transformDBProductToLegacy(dbProduct);
* // Now compatible with existing components
* ```
*/
export function transformDBProductToLegacy(
dbProduct: DBProductWithRelations
): Product {
const reviewStats = calculateReviewStats(dbProduct.reviews);
return {
id: dbProduct.id,
title: dbProduct.title,
description: dbProduct.description ?? undefined,
price: dbProduct.price,
discountedPrice: dbProduct.discountedPrice,
stock: dbProduct.stock,
reviews: reviewStats.count, // Convert Review[] to count
rating: reviewStats.averageRating > 0 ? reviewStats.averageRating : undefined,
imgs: formatProductImages(dbProduct.images), // Convert ProductImage[] to legacy format
};
}
/**
* Batch transform for multiple products
*
* Transforms an array of database products to legacy format.
*
* @param dbProducts - Array of products from database
* @returns Array of legacy Product objects
*
* @example
* ```ts
* const dbProducts = await prisma.product.findMany({
* include: { images: true, reviews: true }
* });
*
* const legacyProducts = transformDBProductsToLegacy(dbProducts);
* ```
*/
export function transformDBProductsToLegacy(
dbProducts: DBProductWithRelations[]
): Product[] {
return dbProducts.map(transformDBProductToLegacy);
}
/**
* Transform with _count optimization
*
* When using Prisma's _count, we can avoid loading all reviews just for the count.
* This is more efficient for lists where we don't need full review data.
*
* @param dbProduct - Product with _count but not full reviews relation
* @returns Legacy Product object
*
* @example
* ```ts
* const dbProducts = await prisma.product.findMany({
* include: {
* images: true,
* _count: { select: { reviews: true } }
* }
* });
*
* // More efficient - doesn't load all review data
* const products = dbProducts.map(transformWithCount);
* ```
*/
export function transformWithCount(
dbProduct: DBProduct & {
images: ProductImage[];
_count?: { reviews: number };
reviews?: Review[];
}
): Product {
// If we have full reviews, use them for average rating
if (dbProduct.reviews && dbProduct.reviews.length > 0) {
return transformDBProductToLegacy(
dbProduct as DBProductWithRelations
);
}
// Otherwise, use _count for reviews (no rating available)
return {
id: dbProduct.id,
title: dbProduct.title,
description: dbProduct.description ?? undefined,
price: dbProduct.price,
discountedPrice: dbProduct.discountedPrice,
stock: dbProduct.stock,
reviews: dbProduct._count?.reviews ?? 0,
rating: undefined, // Can't calculate without review data
imgs: formatProductImages(dbProduct.images)};
}
/**
* Helper to check if product has images
*/
export function hasImages(product: DBProductWithRelations): boolean {
return product.images && product.images.length > 0;
}
/**
* Helper to check if product has reviews
*/
export function hasReviews(product: DBProductWithRelations): boolean {
return product.reviews && product.reviews.length > 0;
}
/**
* Get first thumbnail image URL
*/
export function getFirstThumbnail(product: DBProductWithRelations): string | null {
const images = formatProductImages(product.images);
return images.thumbnails[0] ?? null;
}
/**
* Get first preview image URL
*/
export function getFirstPreview(product: DBProductWithRelations): string | null {
const images = formatProductImages(product.images);
return images.previews[0] ?? null;
}
|