import { Request, Response, NextFunction } from 'express'; import path from 'path'; import { PutObjectCommand } from '@aws-sdk/client-s3'; import { s3Client, BUCKET_NAME } from '../utils/s3'; import prisma from '../utils/db'; export class EcosystemController { public listOfferings = async (req: Request, res: Response, next: NextFunction) => { try { const user = (req as any).user; const isAdmin = user?.role === 'ADMIN'; const offerings = await prisma.ecosystemOffering.findMany({ where: isAdmin ? undefined : { isActive: true }, orderBy: { orderIndex: 'asc' } }); res.status(200).json(offerings); } catch (err) { next(err); } }; public createOffering = async (req: Request, res: Response, next: NextFunction) => { try { const data = req.body; const created = await prisma.ecosystemOffering.create({ data: { name: data.name, type: data.type, tagline: data.tagline, description: data.description, benefits: data.benefits || [], websiteUrl: data.websiteUrl, ctaText: data.ctaText || 'Visit Website', logoIcon: data.logoIcon || 'Globe', logoUrl: data.logoUrl || null, mediaUrl: data.mediaUrl || null, mediaType: data.mediaType || null, orderIndex: data.orderIndex !== undefined ? data.orderIndex : 0, isActive: data.isActive !== undefined ? data.isActive : true, } }); res.status(201).json(created); } catch (err) { next(err); } }; public updateOffering = async (req: Request, res: Response, next: NextFunction) => { try { const { id } = req.params; const data = req.body; const updated = await prisma.ecosystemOffering.update({ where: { id }, data: { name: data.name, type: data.type, tagline: data.tagline, description: data.description, benefits: data.benefits, websiteUrl: data.websiteUrl, ctaText: data.ctaText, logoIcon: data.logoIcon, logoUrl: data.logoUrl, mediaUrl: data.mediaUrl, mediaType: data.mediaType, orderIndex: data.orderIndex, isActive: data.isActive, } }); res.status(200).json(updated); } catch (err) { next(err); } }; public deleteOffering = async (req: Request, res: Response, next: NextFunction) => { try { const { id } = req.params; await prisma.ecosystemOffering.delete({ where: { id } }); res.status(200).json({ message: 'Offering deleted successfully' }); } catch (err) { next(err); } }; public uploadFile = async (req: Request, res: Response, next: NextFunction) => { try { if (!req.file) { return res.status(400).json({ error: 'No file uploaded' }); } const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9); const filename = uniqueSuffix + path.extname(req.file.originalname); await s3Client.send(new PutObjectCommand({ Bucket: BUCKET_NAME, Key: filename, Body: req.file.buffer, ContentType: req.file.mimetype, })); const fileUrl = `/uploads/${filename}`; res.status(200).json({ url: fileUrl }); } catch (err) { next(err); } }; }