Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ const Users = sequelize.define(
disk_size: {
type: DataTypes.DOUBLE,
allowNull: true,
defaultValue: 0,
defaultValue: 100000000,
},
used_capacity: {
type: DataTypes.DOUBLE,
allowNull: true,
defaultValue: 100000000,
},
is_admin: {
type: DataTypes.BOOLEAN,
Expand Down
25 changes: 21 additions & 4 deletions routers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ const Router = require("koa-router");
const redisClient = require("../redis");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const { filesize } = require("filesize");
const checkAdminAuth = require("../middleware/checkAdminAuth");
require("dotenv").config({ path: ".env.local" });
const Users = require("../models/users");
const { USERS_LOGIN_POST, USER_REST_ID } = require("../types/schema/users");
const {
USERS_LOGIN_POST,
USER_REST_PARAMS_PATCH,
} = require("../types/schema/users");

const { validateBody, validateParams } = require("../types");
const { USER_STATUS, USER_ACTION_TYPES } = require("../constants/users");

Expand Down Expand Up @@ -77,9 +82,18 @@ router.post("/users", validateBody(USERS_LOGIN_POST), async (ctx) => {
const { id, disk_size, status, created_at, login_at } = await Users.create({
username,
password: hashedPassword,
created_by: ctx.state?.user?.id ?? null,
});

ctx.status = 201;
ctx.body = { id, disk_size, status, created_at, username, login_at };
ctx.body = {
id,
disk_size: filesize(disk_size),
status,
created_at,
username,
login_at,
};
} catch (error) {
console.error(error);
ctx.status = 500;
Expand All @@ -94,7 +108,10 @@ router.get("/users/info", async (ctx) => {
});
if (user) {
ctx.status = 200; // 确保状态码为 200
ctx.body = user.dataValues;
ctx.body = {
...user.dataValues,
disk_size: filesize(user.dataValues?.disk_size),
};
return;
}
if (!user) {
Expand Down Expand Up @@ -139,7 +156,7 @@ router.delete("/sessions", async (ctx) => {
// 禁用用户
router.patch(
"/users/:id/:action",
validateParams(USER_REST_ID),
validateParams(USER_REST_PARAMS_PATCH),
checkAdminAuth,
async (ctx) => {
const { id, action } = ctx.params;
Expand Down
9 changes: 7 additions & 2 deletions types/schema/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ const USERS_LOGIN_POST = Joi.object({
password: Joi.string().required(),
});

const USER_REST_ID = Joi.object({
const USER_REST_PARAMS_PATCH = Joi.object({
id: Joi.string().required(),
action: Joi.string()
.valid(...Object.keys(USER_ACTION_TYPES))
.required(),
});

const USER_REST_ID = Joi.object({
id: Joi.string().required(),
});

module.exports = {
USER_REST_ID,
USER_REST_PARAMS_PATCH,
USERS_LOGIN_POST,
USER_REST_ID,
};