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 203 204 205 206 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 10x 10x 10x 10x 1x 1x 1x 1x 2x 2x 1x 1x 1x | /**
* Public Hero API
* Returns active hero carousel items and feature cards for homepage display
*/
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import {
withErrorHandling,
successResponse,
ApiSuccessResponse,
ApiErrorResponse } from "@/lib/api";
export const dynamic = "force-dynamic";
// Cache for 5 minutes
export const revalidate = 300;
async function handleGet(): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const now = new Date();
// Fetch active carousel items
const carouselItems = await prisma.heroCarouselItem.findMany({
where: {
isActive: true,
OR: [
// No scheduling - always active
{ startDate: null, endDate: null },
// Within scheduled period
{
startDate: { lte: now },
endDate: { gte: now } },
// Started but no end date
{
startDate: { lte: now },
endDate: null },
// Not started yet but no start date, with end date in future
{
startDate: null,
endDate: { gte: now } },
] },
orderBy: { order: "asc" },
include: {
product: {
select: {
id: true,
title: true,
description: true,
price: true,
discountedPrice: true,
images: {
select: {
id: true,
url: true,
thumbnailUrl: true,
order: true },
orderBy: { order: "asc" },
take: 1 } } } } });
// Fetch active feature cards
const featureCards = await prisma.heroFeatureCard.findMany({
where: {
isActive: true },
orderBy: [
{ position: "asc" }, // 'bottom' comes after 'top' alphabetically, but we want top first
],
include: {
product: {
select: {
id: true,
title: true,
price: true,
discountedPrice: true,
images: {
select: {
id: true,
url: true,
thumbnailUrl: true,
order: true },
orderBy: { order: "asc" },
take: 1 } } } } });
// Sort feature cards: top first, then bottom
const sortedFeatureCards = featureCards.sort((a, b) => {
if (a.position === "top" && b.position === "bottom") return -1;
if (a.position === "bottom" && b.position === "top") return 1;
return 0;
});
// Fetch active promo banners
const promoBanners = await prisma.promoBanner.findMany({
where: {
isActive: true,
OR: [
// No scheduling - always active
{ startDate: null, endDate: null },
// Within scheduled period
{
startDate: { lte: now },
endDate: { gte: now },
},
// Started but no end date
{
startDate: { lte: now },
endDate: null,
},
// Not started yet but no start date, with end date in future
{
startDate: null,
endDate: { gte: now },
},
],
},
orderBy: { order: "asc" },
include: {
product: {
select: {
id: true,
title: true,
price: true,
discountedPrice: true,
images: {
select: {
id: true,
url: true,
thumbnailUrl: true,
},
orderBy: { order: "asc" },
take: 1,
},
},
},
},
});
// Transform data for frontend consumption
// Note: slug is the product ID as the product page uses /product/[id]
const response = {
carousel: carouselItems.map((item) => ({
id: item.id,
product: {
id: item.product.id,
title: item.product.title,
slug: item.product.id.toString(),
price: item.product.price,
discountedPrice: item.product.discountedPrice,
image: item.product.images[0] || null },
headline: item.headline || item.product.title,
subheadline: item.subheadline || item.product.description?.slice(0, 100),
badgeText: item.badgeText || calculateDiscount(item.product.price, item.product.discountedPrice),
badgeSubtext: item.badgeSubtext || (item.badgeText ? "Sale Off" : null),
ctaText: item.ctaText,
order: item.order })),
featureCards: sortedFeatureCards.map((card) => ({
id: card.id,
product: {
id: card.product.id,
title: card.product.title,
slug: card.product.id.toString(),
price: card.product.price,
discountedPrice: card.product.discountedPrice,
image: card.product.images[0] || null },
title: card.title || card.product.title,
subtitle: card.subtitle || "limited time offer",
position: card.position as "top" | "bottom" })),
promoBanners: promoBanners.map((banner) => ({
id: banner.id,
title: banner.title,
headline: banner.headline,
description: banner.description,
ctaText: banner.ctaText,
ctaLink: banner.productId
? `/product/${banner.productId}`
: banner.ctaLink,
product: banner.product
? {
id: banner.product.id,
title: banner.product.title,
price: banner.product.price,
discountedPrice: banner.product.discountedPrice,
image: banner.product.images[0] || null,
}
: null,
imageUrl: banner.imageUrl,
backgroundColor: banner.backgroundColor,
textColor: banner.textColor,
darkBgColor: banner.darkBgColor,
darkTextColor: banner.darkTextColor,
size: banner.size as "large" | "small",
imagePosition: banner.imagePosition as "left" | "right",
order: banner.order,
})),
};
return successResponse(response);
}
export const GET = withErrorHandling(handleGet);
// Helper to calculate discount percentage
function calculateDiscount(price: number, discountedPrice: number): string | null {
if (!discountedPrice || discountedPrice >= price) return null;
const discount = Math.round(((price - discountedPrice) / price) * 100);
return `${discount}%`;
}
|