mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
refactor controllers
This commit is contained in:
parent
166a113467
commit
d7a1656c00
@ -1,12 +1,14 @@
|
||||
import { Role, User } from '../../models'
|
||||
import { selectValues } from "../../lib"
|
||||
import { Schematized } from "../../lib"
|
||||
|
||||
export const RolesController = {
|
||||
get: selectValues(["user_id", "username"], async (req, res) => {
|
||||
const { user_id, username } = req.selectedValues
|
||||
export default {
|
||||
get: Schematized({
|
||||
select: ["user_id", "username"],
|
||||
}, async (req, res) => {
|
||||
const { user_id, username } = req.selection
|
||||
|
||||
if (typeof user_id !== "undefined" || typeof username !== "undefined") {
|
||||
const user = await User.findOne(req.selectedValues)
|
||||
const user = await User.findOne(req.selection)
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "No user founded" })
|
||||
}
|
||||
@ -31,6 +33,4 @@ export const RolesController = {
|
||||
return res.json(true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default RolesController
|
||||
}
|
@ -2,7 +2,7 @@ import { Session } from '../../models'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import { Token } from '../../lib'
|
||||
|
||||
export const SessionController = {
|
||||
export default {
|
||||
regenerate: async (req, res) => {
|
||||
jwt.verify(req.jwtToken, req.jwtStrategy.secretOrKey, async (err, decoded) => {
|
||||
if (err && !decoded?.allowRegenerate) {
|
||||
@ -99,6 +99,4 @@ export const SessionController = {
|
||||
|
||||
return res.json(sessions)
|
||||
},
|
||||
}
|
||||
|
||||
export default SessionController
|
||||
}
|
@ -3,27 +3,83 @@ import bcrypt from 'bcrypt'
|
||||
|
||||
import { User } from '../../models'
|
||||
import SessionController from '../SessionController'
|
||||
import { Token, selectValues } from '../../lib'
|
||||
import { Token, Schematized } from '../../lib'
|
||||
import AvatarController from 'dicebar_lib'
|
||||
|
||||
export const UserController = {
|
||||
import _ from 'lodash'
|
||||
|
||||
export default {
|
||||
isAuth: (req, res) => {
|
||||
return res.json(`You look nice today 😎`)
|
||||
},
|
||||
getSelf: (req, res) => {
|
||||
return res.json(req.user)
|
||||
},
|
||||
get: selectValues(["_id", "username"], async (req, res) => {
|
||||
const user = await User.find(req.selectedValues, { username: 1, fullName: 1, _id: 1, roles: 1, avatar: 1 })
|
||||
get: Schematized({
|
||||
select: ["_id", "username"],
|
||||
}, async (req, res) => {
|
||||
let result = []
|
||||
let selectQueryKeys = []
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "User not exists" })
|
||||
if (Array.isArray(req.selection._id)) {
|
||||
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) => {
|
||||
const user = await User.findOne(req.selectedValues)
|
||||
getOne: Schematized({
|
||||
select: ["_id", "username"],
|
||||
}, async (req, res) => {
|
||||
const user = await User.findOne(req.selection)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "User not exists" })
|
||||
@ -204,6 +260,4 @@ export const UserController = {
|
||||
|
||||
return SessionController.delete(req, res, next)
|
||||
},
|
||||
}
|
||||
|
||||
export default UserController
|
||||
}
|
@ -4,7 +4,6 @@ import mongoose from 'mongoose'
|
||||
import passport from 'passport'
|
||||
import { User } from './models'
|
||||
import socketIo from 'socket.io'
|
||||
import http from "http"
|
||||
|
||||
const b64Decode = global.b64Decode = (data) => {
|
||||
return Buffer.from(data, 'base64').toString('utf-8')
|
||||
@ -37,8 +36,7 @@ class Server {
|
||||
})
|
||||
|
||||
this.server = this.instance.httpServer
|
||||
this.ioHttp = http.createServer()
|
||||
this.io = new socketIo.Server(this.ioHttp, {
|
||||
this.io = new socketIo.Server(3001, {
|
||||
maxHttpBufferSize: 100000000,
|
||||
connectTimeout: 5000,
|
||||
transports: ['websocket', 'polling'],
|
||||
@ -78,8 +76,6 @@ class Server {
|
||||
console.log(socket.id)
|
||||
})
|
||||
|
||||
this.ioHttp.listen(9001)
|
||||
|
||||
await this.instance.init()
|
||||
}
|
||||
|
||||
@ -92,7 +88,7 @@ class Server {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
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) })
|
||||
.catch((err) => { return reject(err) })
|
||||
} catch (err) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user