forked from b00tc4mp/isdi-bootcamp-202409
-
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.
update username in homepage, start working with logicals of front ass…
…ign pack, create new logics and endpoint for find user by email or username b00tc4mp#219
- Loading branch information
Showing
23 changed files
with
204 additions
and
55 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
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
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 |
---|---|---|
|
@@ -25,7 +25,6 @@ export default (userId) => { | |
basePack.id = basePack._id.toString() | ||
delete basePack._id | ||
}) | ||
|
||
return basePack | ||
}) | ||
} |
25 changes: 25 additions & 0 deletions
25
staff/francisco-sanchez/project/api/logic/users/getUserByEmail.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,25 @@ | ||
import { User } from "dat"; | ||
import { validate, errors } from "com"; | ||
|
||
const { SystemError, NotFoundError } = errors; | ||
|
||
export default async (userId, userEmail) => { | ||
// Validaciones de entrada | ||
validate.id(userId, "userId"); | ||
validate.email(userEmail, "userEmail"); | ||
|
||
try { | ||
// Busca el usuario principal por ID | ||
const user = await User.findById(userId).lean(); | ||
if (!user) throw new NotFoundError("User not found"); | ||
|
||
// Busca el usuario objetivo por email | ||
const targetUser = await User.findOne({ email: userEmail }).lean(); | ||
if (!targetUser) throw new NotFoundError("Email not found"); | ||
|
||
// Devuelve el _id del usuario objetivo | ||
return targetUser._id.toString(); | ||
} catch (error) { | ||
throw new SystemError(error.message); | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
16
staff/francisco-sanchez/project/api/logic/users/getUserByEmail.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,16 @@ | ||
import 'dotenv/config' | ||
import db from 'dat' | ||
|
||
import getUserByEmail from './getUserByEmail.js' | ||
|
||
//await db.connect(process.env.MONGO_URL) | ||
await db.connect('mongodb://127.0.0.1:27017/hourify') | ||
|
||
try { | ||
const userId = await getUserByEmail('675036c010473f3d809e5359', 'greygandalf4@themiddleearth.com') | ||
console.log(userId) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
await db.disconnect() | ||
} |
25 changes: 25 additions & 0 deletions
25
staff/francisco-sanchez/project/api/logic/users/getUserByUserame.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,25 @@ | ||
import { User } from "dat"; | ||
import { validate, errors } from "com"; | ||
|
||
const { SystemError, NotFoundError } = errors; | ||
|
||
export default async (userId, userUsername) => { | ||
// Validaciones de entrada | ||
validate.id(userId, "userId"); | ||
validate.username(userUsername, "username"); | ||
|
||
try { | ||
// Busca el usuario principal por ID | ||
const user = await User.findById(userId).lean(); | ||
if (!user) throw new NotFoundError("User not found"); | ||
|
||
// Busca el usuario objetivo por email | ||
const targetUser = await User.findOne({ username: userUsername }).lean(); | ||
if (!targetUser) throw new NotFoundError("Target user not found"); | ||
|
||
// Devuelve el _id del usuario objetivo | ||
return targetUser._id.toString(); | ||
} catch (error) { | ||
throw new SystemError(error.message); | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
16
staff/francisco-sanchez/project/api/logic/users/getUserByUsername.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,16 @@ | ||
import 'dotenv/config' | ||
import db from 'dat' | ||
|
||
import getUserByUserame from './getUserByUserame.js' | ||
|
||
//await db.connect(process.env.MONGO_URL) | ||
await db.connect('mongodb://127.0.0.1:27017/hourify') | ||
|
||
try { | ||
const userId = await getUserByUserame('675036c010473f3d809e5359', 'greygandalf2') | ||
console.log(userId) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
await db.disconnect() | ||
} |
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
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
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
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
15 changes: 15 additions & 0 deletions
15
staff/francisco-sanchez/project/api/routes/users/handlers/findUserByEmailHandler.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,15 @@ | ||
import logic from "../../../logic/index.js"; | ||
|
||
import { createFunctionalHandler } from "../../helpers/index.js"; | ||
|
||
export default createFunctionalHandler((req, res) => { | ||
const { userId } = req; // Esto viene del token | ||
const { email } = req.params; // Esto viene de la URL | ||
|
||
return logic.getUserByEmail(userId, email) | ||
.then(userId => res.json(userId)) | ||
.catch(error => { | ||
console.error('Error in findUserByEmailHandler:', error.message); | ||
throw error; | ||
}); | ||
}) |
15 changes: 15 additions & 0 deletions
15
staff/francisco-sanchez/project/api/routes/users/handlers/findUserByUsernameHandler.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,15 @@ | ||
import logic from "../../../logic/index.js"; | ||
|
||
import { createFunctionalHandler } from "../../helpers/index.js"; | ||
|
||
export default createFunctionalHandler((req, res) => { | ||
const { userId } = req; // Esto viene del token | ||
const { username } = req.params; // Esto viene de la URL | ||
|
||
return logic.getUserByUserame(userId, username) | ||
.then(userId => res.json(userId)) | ||
.catch(error => { | ||
console.error('Error in findUserByUsernameHandler:', error.message); | ||
throw error; | ||
}); | ||
}) |
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
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 |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import { Router, json } from "express" | ||
|
||
import { authorizationHandler, jsonBodyParser } from "../helpers/index.js" | ||
import { authenticateUserHandler, registerUserHandler, getUserNameHandler, getCustomersHandler } from "./handlers/index.js" | ||
import { | ||
authenticateUserHandler, | ||
registerUserHandler, | ||
getUserNameHandler, | ||
getCustomersHandler, | ||
findUserByEmailHandler, | ||
findUserByUsernameHandler | ||
} from "./handlers/index.js" | ||
|
||
const usersRouter = Router() | ||
|
||
usersRouter.post('/auth', jsonBodyParser, authenticateUserHandler) | ||
usersRouter.post('/register', jsonBodyParser, registerUserHandler) | ||
usersRouter.get('/:targetUserId/name', authorizationHandler, getUserNameHandler) //TODO: Diferencia entre esto y la llamada al getBasePacks de packs | ||
usersRouter.get('/:targetUserId/name', authorizationHandler, getUserNameHandler) | ||
usersRouter.get('/customers', getCustomersHandler) | ||
usersRouter.get('/findbyemail/:email', authorizationHandler, findUserByEmailHandler) | ||
usersRouter.get('/findbyusername/:username', authorizationHandler, findUserByUsernameHandler) | ||
|
||
export default usersRouter |
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,19 @@ | ||
const xhr = new XMLHttpRequest(); | ||
|
||
// Callback para manejar la respuesta de la solicitud | ||
xhr.addEventListener('load', () => { | ||
console.log(`Status: ${xhr.status}`); | ||
console.log(`Response: ${xhr.response}`); | ||
}); | ||
|
||
// Configura la solicitud | ||
xhr.open('GET', 'http://localhost:8080/users/675036c010473f3d809e5359/name'); | ||
|
||
// Define el token JWT | ||
const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NzUwMzZjMDEwNDczZjNkODA5ZTUzNTkiLCJyb2xlIjoic3RhbmRhcmQiLCJpYXQiOjE3MzM4Mjg0MTYsImV4cCI6MTczMzg1NzIxNn0.VancAL12ZxtfLX1i96PqkszViUjsA534bkUN8H1oR4c'; // Sustituye por tu token válido | ||
|
||
// Añade el encabezado Authorization con Bearer Token | ||
xhr.setRequestHeader('Authorization', `Bearer ${jwtToken}`); | ||
|
||
// Envía la solicitud | ||
xhr.send(); |
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 |
---|---|---|
@@ -1 +1 @@ | ||
curl -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NzUwMzZjMDEwNDczZjNkODA5ZTUzNTkiLCJyb2xlIjoic3RhbmRhcmQiLCJpYXQiOjE3MzM3NDMxODcsImV4cCI6MTczMzc3MTk4N30.NmuT8VrU_heulQg10FTX9VuzYUSnzyvbtp9kKv6L8BU' http://localhost:8080/users/675036c010473f3d809e5359/name -v | ||
curl -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NzUwMzZjMDEwNDczZjNkODA5ZTUzNTkiLCJyb2xlIjoic3RhbmRhcmQiLCJpYXQiOjE3MzM4Mjg0MTYsImV4cCI6MTczMzg1NzIxNn0.VancAL12ZxtfLX1i96PqkszViUjsA534bkUN8H1oR4c' http://localhost:8080/users/675036c010473f3d809e5359/name -v |
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
7 changes: 7 additions & 0 deletions
7
staff/francisco-sanchez/project/app/src/util/getCurrencySymbol.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,7 @@ | ||
export default function getCurrencySymbol(basePack) { | ||
if (!basePack || !basePack.currency) { | ||
throw new Error('Currency not provided or basePack is invalid'); | ||
} | ||
|
||
return basePack.currency === 'EUR' ? '€' : '$'; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import extractPayloadJWt from "./extractPayloadJWt.js"; | ||
import getCurrencySymbol from "./getCurrencySymbol.js"; | ||
|
||
export { | ||
extractPayloadJWt | ||
extractPayloadJWt, | ||
getCurrencySymbol | ||
} |
Oops, something went wrong.