mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
added admin endpoints
This commit is contained in:
parent
1c5c213b96
commit
56dbba5f5a
@ -0,0 +1,39 @@
|
||||
import { FeaturedWallpaper } from "@db_models"
|
||||
import momentTimezone from "moment-timezone"
|
||||
|
||||
export default {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async (req, res) => {
|
||||
const data = req.body.wallpaper
|
||||
|
||||
if (!data) {
|
||||
return res.status(400).json({
|
||||
error: "Invalid data",
|
||||
})
|
||||
}
|
||||
|
||||
// try to find if data._id exists, else create a new one
|
||||
let wallpaper = null
|
||||
|
||||
if (data._id) {
|
||||
wallpaper = await FeaturedWallpaper.findOne({
|
||||
_id: data._id,
|
||||
})
|
||||
} else {
|
||||
wallpaper = new FeaturedWallpaper()
|
||||
}
|
||||
|
||||
const current_timezone = momentTimezone.tz.guess()
|
||||
|
||||
wallpaper.active = data.active ?? wallpaper.active ?? true
|
||||
wallpaper.date =
|
||||
data.date ??
|
||||
momentTimezone.tz(Date.now(), current_timezone).format()
|
||||
wallpaper.url = data.url ?? wallpaper.url
|
||||
wallpaper.author = data.author ?? wallpaper.author
|
||||
|
||||
await wallpaper.save()
|
||||
|
||||
return res.json(wallpaper)
|
||||
},
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
const os = require("os")
|
||||
|
||||
function getUsage() {
|
||||
let usage = process.cpuUsage()
|
||||
|
||||
usage.time = process.uptime() * 1000
|
||||
usage.percent = (usage.system + usage.user) / (usage.time * 10)
|
||||
|
||||
return usage
|
||||
}
|
||||
|
||||
export default {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async () => {
|
||||
const cpus = os.cpus()
|
||||
|
||||
// get process info, memory usage, etc
|
||||
const processInfo = {
|
||||
memoryUsage: process.memoryUsage(),
|
||||
cpuUsage: getUsage(),
|
||||
uptime: process.uptime(),
|
||||
memoryUsage: process.memoryUsage(),
|
||||
cpus: cpus,
|
||||
}
|
||||
|
||||
return processInfo
|
||||
},
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
import { User, Session, Post } from "@db_models"
|
||||
|
||||
export default {
|
||||
method: "GET",
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async (req) => {
|
||||
// get number of users registered,
|
||||
const users = await User.countDocuments()
|
||||
|
||||
// calculate the last 5 days logins from diferent users
|
||||
let last5D_logins = await Session.find({
|
||||
date: {
|
||||
$gte: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000),
|
||||
$lte: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
const last5D_logins_counts = []
|
||||
|
||||
// filter from different users
|
||||
last5D_logins.forEach((session) => {
|
||||
if (!last5D_logins_counts.includes(session.user_id)) {
|
||||
last5D_logins_counts.push(session.user_id)
|
||||
}
|
||||
})
|
||||
|
||||
// calculate active users within 1 week (using postings)
|
||||
const active_1w_posts_users = await Post.countDocuments({
|
||||
user_id: {
|
||||
$in: last5D_logins_counts,
|
||||
},
|
||||
created_at: {
|
||||
$gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
|
||||
$lte: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
// calculate total posts
|
||||
const total_posts = await Post.countDocuments()
|
||||
|
||||
// calculate total post (1week)
|
||||
const total_posts_1w = await Post.countDocuments({
|
||||
created_at: {
|
||||
$gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
|
||||
$lte: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
accounts_registered: users,
|
||||
last5D_logins: last5D_logins_counts.length,
|
||||
active_1w_posts_users: active_1w_posts_users,
|
||||
total_posts: total_posts,
|
||||
total_posts_1w: total_posts_1w,
|
||||
}
|
||||
},
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
import { User } from "@db_models"
|
||||
|
||||
const filterKeys = ["_id", "__v", "password"]
|
||||
|
||||
export default {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async (req, res) => {
|
||||
const targetUserId = req.params.user_id
|
||||
|
||||
let user = await User.findById(targetUserId).catch((err) => {
|
||||
return false
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
throw new OperationError(400, "User not found")
|
||||
}
|
||||
|
||||
user = user.toObject()
|
||||
|
||||
const updateKeys = Object.keys(req.body.update)
|
||||
|
||||
updateKeys.forEach((key) => {
|
||||
user[key] = req.body.update[key]
|
||||
})
|
||||
|
||||
const updatedData = Object.keys(user)
|
||||
.filter((key) => !filterKeys.includes(key))
|
||||
.reduce((acc, key) => {
|
||||
acc[key] = user[key]
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
console.log(updatedData)
|
||||
|
||||
await User.findByIdAndUpdate(targetUserId, updatedData)
|
||||
|
||||
return user
|
||||
},
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
import { User } from "@db_models"
|
||||
|
||||
import bcrypt from "bcrypt"
|
||||
|
||||
export default {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async (req, res) => {
|
||||
const { password } = req.body
|
||||
|
||||
if (!password) {
|
||||
throw new OperationError(400, "Missing password")
|
||||
}
|
||||
|
||||
const { user_id } = req.params
|
||||
|
||||
const user = await User.findById(user_id).select("+password")
|
||||
|
||||
if (!user) {
|
||||
throw new OperationError(404, "User not found")
|
||||
}
|
||||
|
||||
// hash the password
|
||||
const hash = bcrypt.hashSync(
|
||||
password,
|
||||
parseInt(process.env.BCRYPT_ROUNDS ?? 3),
|
||||
)
|
||||
|
||||
user.password = hash
|
||||
|
||||
await user.save()
|
||||
|
||||
return {
|
||||
status: "ok",
|
||||
message: "Password updated successfully",
|
||||
}
|
||||
},
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user