cfff74e2db
Souq Marketplace: - Native souq pages: browse+filters, create listing, detail+seller profile, orders+chat - New API: reviews (POST), sellers/[id], upload (multipart), polar-checkout webhook - Listings API enhanced: minPrice, maxPrice, sortBy, deliveryDays filters - Seller workflow: start order, mark delivered, upload files, confirm delivery - Wallet: 5 Polar.sh FLH tiers, production checkout, mock fallback, success banner - Fix: order redirect /souq/history → /souq/orders Auth System: - Unified auth page, removed OAuth provider routing (2 files deleted) - Deleted oauth.ts (319 lines) — simplified auth chain - Streamlined login/register API routes Prisma Schema (+179 lines): - New models: Listing, Purchase, Review, Group/GroupMember - Gamification: DailyStreak, XpTransaction, Achievement, UserLevel - Learning: LearnCourse/Module/Enrollment/Certificate - Notifications, Referrals, Dhikr, PremiumBenefits Deleted legacy code: - src/app/api/marketplace/* (3 files) — replaced by /api/souq/* - src/app/api/files/[listingId]/route.ts — replaced by /api/souq/upload - src/app/api/seller/[sellerId]/route.ts — replaced by /api/souq/sellers/[id] Infrastructure: - Dockerfile: standalone output, Prisma runtime migration - docker-compose.yml: Polar env vars, healthcheck fix - docker-compose.staging.yml: staging on port 4014 - .gitea/workflows/ci.yml: CI pipeline
183 lines
4.9 KiB
TypeScript
183 lines
4.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAuth } from "@/lib/auth";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const category = searchParams.get("category");
|
|
const search = searchParams.get("search");
|
|
const featured = searchParams.get("featured");
|
|
const sellerId = searchParams.get("sellerId");
|
|
const minPrice = searchParams.get("minPrice");
|
|
const maxPrice = searchParams.get("maxPrice");
|
|
const sortBy = searchParams.get("sortBy");
|
|
const deliveryDays = searchParams.get("deliveryDays");
|
|
const page = parseInt(searchParams.get("page") || "1", 10);
|
|
const limit = parseInt(searchParams.get("limit") || "20", 10);
|
|
const skip = (page - 1) * limit;
|
|
|
|
const where: Record<string, unknown> = {};
|
|
|
|
// Only return active listings by default
|
|
where.status = "active";
|
|
|
|
if (category) {
|
|
where.category = category;
|
|
}
|
|
|
|
if (search) {
|
|
where.OR = [
|
|
{ title: { contains: search } },
|
|
{ description: { contains: search } },
|
|
];
|
|
}
|
|
|
|
if (featured === "true") {
|
|
where.featured = true;
|
|
}
|
|
|
|
if (sellerId) {
|
|
where.sellerId = sellerId;
|
|
}
|
|
|
|
// Price range filter
|
|
if (minPrice || maxPrice) {
|
|
const priceFilter: Record<string, number> = {};
|
|
if (minPrice) priceFilter.gte = parseInt(minPrice, 10);
|
|
if (maxPrice) priceFilter.lte = parseInt(maxPrice, 10);
|
|
where.priceFlh = priceFilter;
|
|
}
|
|
|
|
// Delivery days filter
|
|
if (deliveryDays) {
|
|
where.deliveryDays = { lte: parseInt(deliveryDays, 10) };
|
|
}
|
|
|
|
// Build orderBy based on sortBy
|
|
let orderBy: Record<string, unknown>[];
|
|
switch (sortBy) {
|
|
case "price_asc":
|
|
orderBy = [{ priceFlh: "asc" }, { featured: "desc" }, { createdAt: "desc" }];
|
|
break;
|
|
case "price_desc":
|
|
orderBy = [{ priceFlh: "desc" }, { featured: "desc" }, { createdAt: "desc" }];
|
|
break;
|
|
case "rating":
|
|
orderBy = [{ rating: "desc" }, { featured: "desc" }, { createdAt: "desc" }];
|
|
break;
|
|
case "popular":
|
|
orderBy = [{ salesCount: "desc" }, { featured: "desc" }, { createdAt: "desc" }];
|
|
break;
|
|
case "newest":
|
|
orderBy = [{ createdAt: "desc" }, { featured: "desc" }];
|
|
break;
|
|
default:
|
|
orderBy = [{ featured: "desc" }, { createdAt: "desc" }];
|
|
break;
|
|
}
|
|
|
|
const [listings, total] = await Promise.all([
|
|
prisma.listing.findMany({
|
|
where,
|
|
include: {
|
|
seller: {
|
|
select: { id: true, name: true, email: true, avatar: true },
|
|
},
|
|
},
|
|
orderBy,
|
|
skip,
|
|
take: limit,
|
|
}),
|
|
prisma.listing.count({ where }),
|
|
]);
|
|
|
|
return NextResponse.json({
|
|
listings,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
totalPages: Math.ceil(total / limit),
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("GET /api/souq/listings error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const auth = await requireAuth(request);
|
|
if (!auth) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({ where: { id: auth.userId } });
|
|
if (!user) {
|
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
|
}
|
|
|
|
const {
|
|
title,
|
|
description,
|
|
category,
|
|
subcategory,
|
|
priceFlh,
|
|
packages,
|
|
tags,
|
|
images,
|
|
deliveryDays,
|
|
fileType,
|
|
} = await request.json();
|
|
|
|
if (!title || !description || !category || !priceFlh) {
|
|
return NextResponse.json(
|
|
{ error: "Missing required fields: title, description, category, priceFlh" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (typeof priceFlh !== "number" || priceFlh < 0) {
|
|
return NextResponse.json(
|
|
{ error: "priceFlh must be a non-negative number" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const listing = await prisma.listing.create({
|
|
data: {
|
|
title,
|
|
description,
|
|
category,
|
|
subcategory: subcategory || null,
|
|
priceFlh,
|
|
packages: packages ? JSON.stringify(packages) : null,
|
|
tags: tags ? JSON.stringify(tags) : null,
|
|
images: images ? JSON.stringify(images) : null,
|
|
deliveryDays: deliveryDays || null,
|
|
fileType: fileType || null,
|
|
sellerId: user.id,
|
|
status: "active",
|
|
},
|
|
include: {
|
|
seller: {
|
|
select: { id: true, name: true, email: true, avatar: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ listing }, { status: 201 });
|
|
} catch (error) {
|
|
console.error("POST /api/souq/listings error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|