forked from b00tc4mp/isdi-parttime-202309
-
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.
add logics: deleteUser, deleteFile, registerAdmin with handlers | add…
… html and spec testing b00tc4mp#361
- Loading branch information
Abel Prieto
committed
Mar 10, 2024
1 parent
33f89ee
commit 6f577be
Showing
29 changed files
with
521 additions
and
53 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
38 changes: 38 additions & 0 deletions
38
staff/abel-prieto/PROYECT/API/handlers/deleteFileHandler.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,38 @@ | ||
import jwt from 'jsonwebtoken' | ||
import deleteFile from "../logic/deleteFile.js" | ||
import { errors } from 'com' | ||
const { NotFoundError, ContentError, AuthorizationError, TokenError } = errors | ||
const { JsonWebTokenError } = jwt | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const { fileId } = req.params | ||
try { | ||
await deleteFile(userId, fileId) | ||
res.status(201).send() | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof AuthorizationError) { | ||
status = 401 | ||
} | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof TypeError || error instanceof ContentError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
staff/abel-prieto/PROYECT/API/handlers/downloadFileHandler.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,42 @@ | ||
import jwt from 'jsonwebtoken' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import downloadFile from '../logic/downloadFile.js' | ||
import { errors } from 'com' | ||
|
||
const { JsonWebTokenError } = jwt | ||
const { NotFoundError, ContentError, TokenError, AuthorizationError } = errors | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const { fileId } = req.params | ||
|
||
try { | ||
const file = await downloadFile(userId, fileId) | ||
|
||
res.download(file) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof AuthorizationError) { | ||
status = 401 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
staff/abel-prieto/PROYECT/API/handlers/retrieveFilesHandler.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,34 @@ | ||
import jwt from 'jsonwebtoken' | ||
import retrieveFiles from '../logic/retrieveFiles.js' | ||
import { errors } from 'com' | ||
|
||
const { JsonWebTokenError } = jwt | ||
const { NotFoundError, ContentError, TokenError } = errors | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
try { | ||
const files = await retrieveFiles(userId) | ||
res.json(files) | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import { User, File } from '../data/models.js' | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, AuthorizationError } = errors | ||
|
||
export default async function deleteFile(userId, fileId) { | ||
validate.id(userId, 'ID User') | ||
validate.id(fileId, 'ID File') | ||
|
||
try { | ||
const user = await User.findById(userId).lean() | ||
if (!user) { | ||
throw new NotFoundError('User not found') | ||
} | ||
|
||
const file = await File.findById(fileId).lean() | ||
if (!file) { | ||
throw new NotFoundError('File not found') | ||
} | ||
|
||
if (file.owner[0] === user.id || user.role[0] === 'admin') { | ||
const filePath = `./uploads/${file._id.toString()}` | ||
|
||
await fs.unlink(filePath) | ||
await File.findByIdAndDelete(fileId) | ||
|
||
} else { | ||
throw new AuthorizationError('Authorization denied. Try again') | ||
} | ||
|
||
} catch (error) { | ||
if (error instanceof NotFoundError || error instanceof AuthorizationError) { | ||
throw error | ||
} | ||
|
||
throw new SystemError(error.message) | ||
} | ||
} |
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,41 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { User, File } from '../data/models.js' | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, AuthorizationError } = errors | ||
|
||
export default async function downloadFile(userId, fileId) { | ||
validate.id(userId, 'ID User') | ||
validate.id(fileId, 'ID File') | ||
|
||
try { | ||
const user = await User.findById(userId) | ||
|
||
if (!user) { | ||
throw new NotFoundError('User not found') | ||
} | ||
|
||
const file = await File.findById(fileId) | ||
|
||
if (!file) { | ||
throw new NotFoundError('File not found') | ||
} | ||
|
||
if (file.owner === user.id || user.role[0] === 'admin') { | ||
const originalName = file.originalName | ||
const filePath = `./uploads/${file._id.toString()}` | ||
|
||
// Devolvemos la info de la ruta y el nombre original | ||
return { filePath, originalName } | ||
} else { | ||
throw new AuthorizationError('Authorization denied. Try again') | ||
} | ||
|
||
} catch (error) { | ||
if (error instanceof NotFoundError || error instanceof AuthorizationError) { | ||
throw error | ||
} | ||
|
||
throw new SystemError(error.message) | ||
} | ||
} |
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,42 +1,32 @@ | ||
import { User, Group } from '../data/models.js'; | ||
import bcrypt from 'bcrypt'; | ||
import { errors } from 'com'; | ||
import { validate } from 'com'; | ||
import { User, Group } from '../data/models.js' | ||
import bcrypt from 'bcrypt' | ||
import { errors } from 'com' | ||
import { validate } from 'com' | ||
|
||
const { SystemError, DuplicityError } = errors; | ||
const { SystemError, DuplicityError } = errors | ||
|
||
async function registerUser(username, email, password) { | ||
try { | ||
// Validación de datos | ||
validate.text(username, 'Username'); | ||
validate.email(email, 'Email'); | ||
validate.password(password, 'Password'); | ||
validate.text(username, 'Username') | ||
validate.email(email, 'Email') | ||
validate.password(password, 'Password') | ||
|
||
// Hash de la contraseña | ||
const hash = await bcrypt.hash(password, 5); | ||
const hash = await bcrypt.hash(password, 5) | ||
const user = await User.create({ username, email, password: hash, group: 'localhost', role: 'user' }) | ||
|
||
// Crear el usuario | ||
const user = await User.create({ username, email, password: hash, group: 'localhost', role: 'user' }); | ||
const group = await Group.findOne({ name: 'localhost' }) | ||
group.members.push(user._id) | ||
await group.save() | ||
|
||
// Buscar o crear el grupo 'localhost' | ||
let group = await Group.findOne({ name: 'localhost' }); | ||
|
||
// Agregar el ID del usuario al array de miembros | ||
group.members.push(user._id); | ||
|
||
// Guardar el grupo | ||
await group.save(); | ||
|
||
// Devolver el usuario creado o cualquier otro valor necesario | ||
return user; | ||
return user | ||
|
||
} catch (error) { | ||
if (error.code === 11000) { | ||
throw new DuplicityError('Account already exists. Try again'); | ||
throw new DuplicityError('Account already exist. Try again') | ||
} | ||
|
||
throw new SystemError(error.message); | ||
throw new SystemError(error.message) | ||
} | ||
} | ||
|
||
export default registerUser; | ||
export default registerUser |
Oops, something went wrong.