mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
refactor controllers
This commit is contained in:
parent
6f50106fd3
commit
775b3ea2e9
@ -1,12 +1,14 @@
|
|||||||
import { Role, User } from '../../models'
|
import { Role, User } from '../../models'
|
||||||
import { selectValues } from "../../lib"
|
import { Schematized } from "../../lib"
|
||||||
|
|
||||||
export const RolesController = {
|
export default {
|
||||||
get: selectValues(["user_id", "username"], async (req, res) => {
|
get: Schematized({
|
||||||
const { user_id, username } = req.selectedValues
|
select: ["user_id", "username"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
const { user_id, username } = req.selection
|
||||||
|
|
||||||
if (typeof user_id !== "undefined" || typeof username !== "undefined") {
|
if (typeof user_id !== "undefined" || typeof username !== "undefined") {
|
||||||
const user = await User.findOne(req.selectedValues)
|
const user = await User.findOne(req.selection)
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ error: "No user founded" })
|
return res.status(404).json({ error: "No user founded" })
|
||||||
}
|
}
|
||||||
@ -31,6 +33,4 @@ export const RolesController = {
|
|||||||
return res.json(true)
|
return res.json(true)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default RolesController
|
|
@ -2,7 +2,7 @@ import { Session } from '../../models'
|
|||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import { Token } from '../../lib'
|
import { Token } from '../../lib'
|
||||||
|
|
||||||
export const SessionController = {
|
export default {
|
||||||
regenerate: async (req, res) => {
|
regenerate: async (req, res) => {
|
||||||
jwt.verify(req.jwtToken, req.jwtStrategy.secretOrKey, async (err, decoded) => {
|
jwt.verify(req.jwtToken, req.jwtStrategy.secretOrKey, async (err, decoded) => {
|
||||||
if (err && !decoded?.allowRegenerate) {
|
if (err && !decoded?.allowRegenerate) {
|
||||||
@ -99,6 +99,4 @@ export const SessionController = {
|
|||||||
|
|
||||||
return res.json(sessions)
|
return res.json(sessions)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SessionController
|
|
@ -3,27 +3,83 @@ import bcrypt from 'bcrypt'
|
|||||||
|
|
||||||
import { User } from '../../models'
|
import { User } from '../../models'
|
||||||
import SessionController from '../SessionController'
|
import SessionController from '../SessionController'
|
||||||
import { Token, selectValues } from '../../lib'
|
import { Token, Schematized } from '../../lib'
|
||||||
import AvatarController from 'dicebar_lib'
|
import AvatarController from 'dicebar_lib'
|
||||||
|
|
||||||
export const UserController = {
|
import _ from 'lodash'
|
||||||
|
|
||||||
|
export default {
|
||||||
isAuth: (req, res) => {
|
isAuth: (req, res) => {
|
||||||
return res.json(`You look nice today 😎`)
|
return res.json(`You look nice today 😎`)
|
||||||
},
|
},
|
||||||
getSelf: (req, res) => {
|
getSelf: (req, res) => {
|
||||||
return res.json(req.user)
|
return res.json(req.user)
|
||||||
},
|
},
|
||||||
get: selectValues(["_id", "username"], async (req, res) => {
|
get: Schematized({
|
||||||
const user = await User.find(req.selectedValues, { username: 1, fullName: 1, _id: 1, roles: 1, avatar: 1 })
|
select: ["_id", "username"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
let result = []
|
||||||
|
let selectQueryKeys = []
|
||||||
|
|
||||||
if (!user) {
|
if (Array.isArray(req.selection._id)) {
|
||||||
return res.status(404).json({ error: "User not exists" })
|
for await (let _id of req.selection._id) {
|
||||||
|
const user = await User.findById(_id).catch(err => {
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
if (user) {
|
||||||
|
result.push(user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = await User.find(req.selection, { username: 1, fullName: 1, _id: 1, roles: 1, avatar: 1 })
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(user)
|
if (req.query.select) {
|
||||||
|
try {
|
||||||
|
req.query.select = JSON.parse(req.query.select)
|
||||||
|
} catch (error) {
|
||||||
|
req.query.select = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
selectQueryKeys = Object.keys(req.query.select)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectQueryKeys.length > 0) {
|
||||||
|
result = result.filter(user => {
|
||||||
|
let pass = false
|
||||||
|
const selectFilter = req.query.select
|
||||||
|
|
||||||
|
selectQueryKeys.forEach(key => {
|
||||||
|
if (Array.isArray(selectFilter[key]) && Array.isArray(user[key])) {
|
||||||
|
// check if arrays includes any of the values
|
||||||
|
pass = selectFilter[key].some(val => user[key].includes(val))
|
||||||
|
} else if (typeof selectFilter[key] === 'object' && typeof user[key] === 'object') {
|
||||||
|
// check if objects includes any of the values
|
||||||
|
Object.keys(selectFilter[key]).forEach(objKey => {
|
||||||
|
pass = user[key][objKey] === selectFilter[key][objKey]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if strings includes any of the values
|
||||||
|
if (typeof selectFilter[key] === 'string' && typeof user[key] === 'string') {
|
||||||
|
pass = selectFilter[key].split(',').some(val => user[key].includes(val))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return pass
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "Users not found" })
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json(result)
|
||||||
}),
|
}),
|
||||||
getOne: selectValues(["_id", "username"], async (req, res) => {
|
getOne: Schematized({
|
||||||
const user = await User.findOne(req.selectedValues)
|
select: ["_id", "username"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
const user = await User.findOne(req.selection)
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ error: "User not exists" })
|
return res.status(404).json({ error: "User not exists" })
|
||||||
@ -204,6 +260,4 @@ export const UserController = {
|
|||||||
|
|
||||||
return SessionController.delete(req, res, next)
|
return SessionController.delete(req, res, next)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export default UserController
|
|
@ -4,7 +4,6 @@ import mongoose from 'mongoose'
|
|||||||
import passport from 'passport'
|
import passport from 'passport'
|
||||||
import { User } from './models'
|
import { User } from './models'
|
||||||
import socketIo from 'socket.io'
|
import socketIo from 'socket.io'
|
||||||
import http from "http"
|
|
||||||
|
|
||||||
const b64Decode = global.b64Decode = (data) => {
|
const b64Decode = global.b64Decode = (data) => {
|
||||||
return Buffer.from(data, 'base64').toString('utf-8')
|
return Buffer.from(data, 'base64').toString('utf-8')
|
||||||
@ -37,8 +36,7 @@ class Server {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.server = this.instance.httpServer
|
this.server = this.instance.httpServer
|
||||||
this.ioHttp = http.createServer()
|
this.io = new socketIo.Server(3001, {
|
||||||
this.io = new socketIo.Server(this.ioHttp, {
|
|
||||||
maxHttpBufferSize: 100000000,
|
maxHttpBufferSize: 100000000,
|
||||||
connectTimeout: 5000,
|
connectTimeout: 5000,
|
||||||
transports: ['websocket', 'polling'],
|
transports: ['websocket', 'polling'],
|
||||||
@ -78,8 +76,6 @@ class Server {
|
|||||||
console.log(socket.id)
|
console.log(socket.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.ioHttp.listen(9001)
|
|
||||||
|
|
||||||
await this.instance.init()
|
await this.instance.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +88,7 @@ class Server {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
console.log("🌐 Trying to connect to DB...")
|
console.log("🌐 Trying to connect to DB...")
|
||||||
mongoose.connect(this.getDBConnectionString(), { useNewUrlParser: true, useUnifiedTopology: true })
|
mongoose.connect(this.getDBConnectionString(), { useNewUrlParser: true, useFindAndModify: false })
|
||||||
.then((res) => { return resolve(true) })
|
.then((res) => { return resolve(true) })
|
||||||
.catch((err) => { return reject(err) })
|
.catch((err) => { return reject(err) })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user