Skip to content

Commit

Permalink
feat(users): #28 New registered users increase capacity limit by 100MB
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyMrYan committed Jul 6, 2024
1 parent 2902cc6 commit 7a17dd4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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,
};

0 comments on commit 7a17dd4

Please sign in to comment.