forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create logic get all user products and spec in cor/logic b00tc4mp#101
- Loading branch information
1 parent
801fb63
commit 9b8e9cc
Showing
3 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
staff/lucas-orts/project/cor/logic/getAllUserProducts.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |