Repository navigation
OM-268: 1º Parte #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OM-268: 1º Parte #75
Changes from all commits
df3172f
07693cd
3ced818
051f48f
1849662
9224d6f
78ec05c
ab6c73a
88cdd92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "Products" ADD COLUMN "image_url" VARCHAR(500); | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "Users" ADD COLUMN "avatar_url" VARCHAR(500); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const requiredEnvVars = [ | ||
| 'DATABASE_URL', | ||
| 'DIRECT_URL', | ||
| 'JWT_SECRET', | ||
| 'SUPABASE_URL', | ||
| 'SUPABASE_SERVICE_ROLE_KEY', | ||
| 'SUPABASE_BUCKET_PRODUCTS', | ||
| 'SUPABASE_BUCKET_USERS', | ||
| 'SUPABASE_BUCKET_STORES', | ||
| ] | ||
|
|
||
| export function validateEnv() { | ||
| const missing = requiredEnvVars.filter(key => !process.env[key]) | ||
|
|
||
| if (missing.length > 0) { | ||
| console.error('❌ Faltan las siguientes variables de entorno:') | ||
| missing.forEach(key => console.error(` - ${key}`)) | ||
| process.exit(1) | ||
|
Check failure on line 18 in src/config/env.config.js
|
||
| } | ||
|
|
||
| console.log('✅ Variables de entorno validadas correctamente') | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import 'dotenv/config' | ||
| import { createClient } from '@supabase/supabase-js' | ||
|
|
||
| let supabase | ||
|
|
||
| export function getSupabase() { | ||
| if (supabase) return supabase | ||
|
|
||
| const url = process.env.SUPABASE_URL | ||
| const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY | ||
|
|
||
| if (!url || !serviceRoleKey) { | ||
| throw new Error('Falta configurar SUPABASE_URL y SUPABASE_SERVICE_ROLE_KEY') | ||
| } | ||
|
|
||
| supabase = createClient(url, serviceRoleKey) | ||
| return supabase | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,13 @@ | ||
| import "dotenv/config"; | ||
| import pkg from '@prisma/client'; | ||
| import { PrismaPg } from "@prisma/adapter-pg"; | ||
| import pg from "pg"; | ||
| import { PrismaClient } from '@prisma/client' | ||
| import { PrismaPg } from '@prisma/adapter-pg' | ||
| import pg from 'pg' | ||
|
|
||
| const { PrismaClient } = pkg; | ||
| const { Pool } = pg; | ||
| const { Pool } = pg | ||
|
|
||
| const pool = new Pool({ | ||
| connectionString: process.env.DATABASE_URL, | ||
| }); | ||
| }) | ||
|
|
||
| const adapter = new PrismaPg(pool); | ||
| const adapter = new PrismaPg(pool) | ||
|
|
||
| export const prisma = new PrismaClient({ | ||
| adapter, | ||
| }); | ||
| export const prisma = new PrismaClient({ adapter }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { ForbiddenError } from '../lib/errors.js' | ||
|
|
||
| /** | ||
| * Verifica que el usuario autenticado tenga uno de los roles permitidos. | ||
| * Debe usarse DESPUÉS de authenticate. | ||
| * @param {...string} roles - Roles permitidos (ej: 'ADMIN', 'SELLER') | ||
| */ | ||
| export const requireRole = (...roles) => (req, res, next) => { | ||
| if (!req.user) { | ||
| return next(new ForbiddenError('Usuario no autenticado')) | ||
| } | ||
| if (!roles.includes(req.user.role)) { | ||
| return next(new ForbiddenError('No tenés permisos para realizar esta acción')) | ||
| } | ||
| next() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import multer from 'multer' | ||
| import { ValidationError } from '../lib/errors.js' | ||
| import { IMAGE } from '../utils/contants/image.constant.js' | ||
|
|
||
| export const upload = multer({ | ||
| storage: multer.memoryStorage(), | ||
| limits: { fileSize: IMAGE.MAX_SIZE_MB }, | ||
| fileFilter: (req, file, cb) => { | ||
| if (IMAGE.ALLOWED_TYPES.includes(file.mimetype)) { | ||
| cb(null, true) | ||
| } else { | ||
| cb(new ValidationError('Formato no permitido. Usá JPG, PNG o WEBP.')) | ||
| } | ||
| } | ||
| }) | ||
|
CrisNAC marked this conversation as resolved.
Resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as productImageService from '../services/product-image.service.js' | ||
|
|
||
| export async function getProductImage(req, res, next) { | ||
| try { | ||
| const image_url = await productImageService.getProductImage(req.params.id) | ||
| return res.json({ image_url }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function uploadProductImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const image_url = await productImageService.upsertProductImage(req.params.id, req.file, req.user) | ||
| return res.status(201).json({ image_url }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function updateProductImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const image_url = await productImageService.upsertProductImage(req.params.id, req.file, req.user) | ||
| return res.status(200).json({ image_url }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| export async function deleteProductImage(req, res, next) { | ||
| try { | ||
| await productImageService.removeProductImage(req.params.id, req.user) | ||
| return res.status(200).json({ message: 'Imagen eliminada correctamente' }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as storeImageService from '../services/store-image.service.js' | ||
|
|
||
| export async function getStoreImage(req, res, next) { | ||
| try { | ||
| const logo = await storeImageService.getStoreImage(req.params.id) | ||
| return res.json({ logo }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function uploadStoreImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const logo = await storeImageService.upsertStoreImage(req.params.id, req.file, req.user) | ||
| return res.status(201).json({ logo }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function updateStoreImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const logo = await storeImageService.upsertStoreImage(req.params.id, req.file, req.user) | ||
| return res.status(200).json({ logo }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| export async function deleteStoreImage(req, res, next) { | ||
| try { | ||
| await storeImageService.removeStoreImage(req.params.id, req.user) | ||
| return res.status(200).json({ message: 'Logo eliminado correctamente' }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as userImageService from '../services/user-image.service.js' | ||
|
|
||
| export async function getUserImage(req, res, next) { | ||
| try { | ||
| const avatar_url = await userImageService.getUserImage(req.params.id) | ||
| return res.json({ avatar_url }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function uploadUserImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const avatar_url = await userImageService.upsertUserImage(req.params.id, req.file, req.user) | ||
| return res.status(201).json({ avatar_url }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
|
||
| export async function updateUserImage(req, res, next) { | ||
| try { | ||
| if (!req.file) return res.status(400).json({ message: 'No se recibió ningún archivo' }) | ||
| const avatar_url = await userImageService.upsertUserImage(req.params.id, req.file, req.user) | ||
| return res.status(200).json({ avatar_url }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| export async function deleteUserImage(req, res, next) { | ||
| try { | ||
| await userImageService.removeUserImage(req.params.id, req.user) | ||
| return res.status(200).json({ message: 'Avatar eliminado correctamente' }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.