diff --git a/staff/lucas-orts/project/cor/logic/getAllUserProducts.js b/staff/lucas-orts/project/cor/logic/getAllUserProducts.js new file mode 100644 index 000000000..813d52ec6 --- /dev/null +++ b/staff/lucas-orts/project/cor/logic/getAllUserProducts.js @@ -0,0 +1,21 @@ +import { Product } from '../data/models.js' +import { validate, errors } from 'com' + +const { SystemError } = errors + +export default userId => { + // Validar el userId + validate.string(userId, 'userId') + + // Buscar productos asociados al farmer (usuario) específico y ordenarlos por el campo name en orden alfabético + return Product.find({ farmer: userId }, { __v: 0 }).sort({ name: 1 }).lean() + .catch(error => { throw new SystemError(error.message) }) + .then(products => { + // Transformar _id a id en cada producto + return products.map(product => { + product.id = product._id.toString() + delete product._id + return product + }) + }) +} \ No newline at end of file diff --git a/staff/lucas-orts/project/cor/logic/getAllUserProducts.spec.js b/staff/lucas-orts/project/cor/logic/getAllUserProducts.spec.js new file mode 100644 index 000000000..e69de29bb diff --git a/staff/lucas-orts/project/cor/logic/getAllUserProducts.test.js b/staff/lucas-orts/project/cor/logic/getAllUserProducts.test.js new file mode 100644 index 000000000..710b45706 --- /dev/null +++ b/staff/lucas-orts/project/cor/logic/getAllUserProducts.test.js @@ -0,0 +1,9 @@ +import 'dotenv/config' +import getAllUserProducts from './getAllUserProducts.js' +import mongoose from 'mongoose' + +mongoose.connect(process.env.MONGODB_URI) + .then(() => getAllUserProducts('66cb76f3220cbfc7ec120782')) + .then(posts => console.log(posts)) + .catch(error => console.error(error)) + .finally(() => mongoose.disconnect()) \ No newline at end of file