Skip to content

Commit

Permalink
create logic get all user products and spec in cor/logic b00tc4mp#101
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasOrtsBaguena committed Aug 26, 2024
1 parent 801fb63 commit 9b8e9cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions staff/lucas-orts/project/cor/logic/getAllUserProducts.js
Original file line number Diff line number Diff line change
@@ -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
})
})
}
Empty file.
9 changes: 9 additions & 0 deletions staff/lucas-orts/project/cor/logic/getAllUserProducts.test.js
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 9b8e9cc

Please sign in to comment.