优化组织和用户创建逻辑,简化插入数据结构并处理可选描述字段

This commit is contained in:
2026-02-07 02:04:17 +08:00
parent 7dfa96d165
commit 0bd634faf2
9 changed files with 302 additions and 947 deletions

View File

@@ -3,7 +3,7 @@ import { resolvePath } from '@kevisual/use-config';
const entry = 'src/index.ts';
const naming = 'app';
const external = ['sequelize', 'pg', 'sqlite3', 'ioredis', 'pm2', 'bun'];
const external = ['pg', 'sqlite3', 'ioredis', 'pm2'];
/**
* @type {import('bun').BuildConfig}
*/

View File

@@ -59,11 +59,9 @@
"drizzle-orm": "^0.45.1",
"drizzle-zod": "^0.8.3",
"eventemitter3": "^5.0.4",
"ioredis": "^5.9.2",
"pg": "^8.18.0",
"pm2": "^6.0.14",
"send": "^1.2.1",
"sequelize": "^6.37.7",
"ws": "npm:@kevisual/ws",
"xml2js": "^0.6.2"
},
@@ -95,14 +93,11 @@
"es-toolkit": "^1.44.0",
"ioredis": "^5.9.2",
"jsonwebtoken": "^9.0.3",
"minio": "^8.0.6",
"nanoid": "^5.1.6",
"nodemon": "^3.1.11",
"p-queue": "^9.1.0",
"pg": "^8.18.0",
"pm2": "^6.0.14",
"semver": "^7.7.4",
"sequelize": "^6.37.7",
"zod": "^4.3.6"
},
"resolutions": {

1141
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -178,7 +178,16 @@ export class Org {
* 创建组织
*/
static async create(data: { username: string; description?: string; users: OrgUser[] }): Promise<Org> {
const inserted = await db.insert(orgsTable).values(data).returning();
const insertData: any = {
username: data.username,
users: data.users,
};
if (data.description !== undefined && data.description !== null) {
insertData.description = data.description;
}
const inserted = await db.insert(orgsTable).values(insertData).returning();
return new Org(inserted[0]);
}

View File

@@ -259,13 +259,19 @@ export class UserSecret {
userId = tokenUser.uid;
orgId = tokenUser.id;
}
const inserted = await db.insert(userSecretsTable).values({
const insertData: Partial<typeof userSecretsTable.$inferInsert> = {
userId,
orgId,
token,
title: tokenUser.title || randomString(6),
expiredTime: UserSecret.getExpiredTime(expireDays).toISOString(),
}).returning();
};
if (orgId !== null && orgId !== undefined) {
insertData.orgId = orgId;
}
const inserted = await db.insert(userSecretsTable).values(insertData).returning();
return new UserSecret(inserted[0]);
}

View File

@@ -6,6 +6,8 @@ import { oauth } from '../oauth/auth.ts';
import { cryptPwd } from '../oauth/salt.ts';
import { OauthUser } from '../oauth/oauth.ts';
import { db } from '../../modules/db.ts';
import { Org } from './org.ts';
import { cfUser, cfOrgs, cfUserSecrets } from '../../db/drizzle/schema.ts';
import { eq, sql, InferSelectModel, InferInsertModel } from 'drizzle-orm';
@@ -188,15 +190,28 @@ export class User {
password = password || '123456';
const cPassword = cryptPwd(password, salt);
const inserted = await db.insert(usersTable).values({
const insertData: any = {
username,
password: cPassword,
description,
salt,
needChangePassword,
}).returning();
};
// 只在需要时才设置非默认值
if (needChangePassword) {
insertData.needChangePassword = true;
}
if (description !== undefined && description !== null) {
insertData.description = description;
}
try {
const inserted = await db.insert(usersTable).values(insertData).returning();
return new User(inserted[0]);
} catch (e) {
console.log(e)
throw e
}
return new User(inserted[0]);
}
static async createOrg(username: string, owner: string, description?: string) {
@@ -212,16 +227,21 @@ export class User {
throw new CustomError('Owner type is not user');
}
const { Org } = await import('./org.ts');
const org = await Org.create({ username, description, users: [{ uid: owner, role: 'owner' }] });
const inserted = await db.insert(usersTable).values({
const insertData: any = {
username,
password: '',
description,
type: 'org',
owner,
orgId: org.id,
}).returning();
};
if (description !== undefined && description !== null) {
insertData.description = description;
}
const inserted = await db.insert(usersTable).values(insertData).returning();
// owner add
await redis.del(`user:${me.id}:orgs`);
@@ -304,6 +324,7 @@ export class User {
*/
async getOrgs() {
let id = this.id;
console.log('type', this.type, this.tokenUser)
if (this.type === 'org') {
if (this.tokenUser && this.tokenUser.uid) {
id = this.tokenUser.uid;

View File

@@ -92,9 +92,7 @@ app
}
try {
const user = await User.findOne({
where: {
id: tokenUser.id,
},
id: tokenUser.id,
});
if (!user) {
ctx.throw(404, 'user not found');
@@ -133,9 +131,7 @@ app
try {
const user = await User.findOne({
where: {
id: tokenUser.id,
},
id: tokenUser.id,
});
if (!user) {
ctx.throw(404, 'user not found');

View File

@@ -1,4 +1,3 @@
// import { Op } from 'sequelize';
// import { app } from '@/app.ts';
// import { FileSyncModel } from './model.ts';
// app

View File

@@ -24,7 +24,7 @@ export const checkUsernameShort = (username: string) => {
}
};
export const toChangeName = async (opts: { id: number; newName: string; admin?: boolean, ctx: any }) => {
export const toChangeName = async (opts: { id: string; newName: string; admin?: boolean, ctx: any }) => {
const { id, newName, admin = false, ctx } = opts;
if (!admin) {
checkUsernameShort(newName);
@@ -35,7 +35,7 @@ export const toChangeName = async (opts: { id: number; newName: string; admin?:
}
const oldName = user.username;
checkUsername(newName);
const findUserByUsername = await User.findOne({ where: { username: newName } });
const findUserByUsername = await User.findOne({ username: newName });
if (findUserByUsername) {
ctx.throw(400, 'Username already exists');
}