优化组织和用户创建逻辑,简化插入数据结构并处理可选描述字段
This commit is contained in:
@@ -3,7 +3,7 @@ import { resolvePath } from '@kevisual/use-config';
|
|||||||
|
|
||||||
const entry = 'src/index.ts';
|
const entry = 'src/index.ts';
|
||||||
const naming = 'app';
|
const naming = 'app';
|
||||||
const external = ['sequelize', 'pg', 'sqlite3', 'ioredis', 'pm2', 'bun'];
|
const external = ['pg', 'sqlite3', 'ioredis', 'pm2'];
|
||||||
/**
|
/**
|
||||||
* @type {import('bun').BuildConfig}
|
* @type {import('bun').BuildConfig}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -59,11 +59,9 @@
|
|||||||
"drizzle-orm": "^0.45.1",
|
"drizzle-orm": "^0.45.1",
|
||||||
"drizzle-zod": "^0.8.3",
|
"drizzle-zod": "^0.8.3",
|
||||||
"eventemitter3": "^5.0.4",
|
"eventemitter3": "^5.0.4",
|
||||||
"ioredis": "^5.9.2",
|
|
||||||
"pg": "^8.18.0",
|
"pg": "^8.18.0",
|
||||||
"pm2": "^6.0.14",
|
"pm2": "^6.0.14",
|
||||||
"send": "^1.2.1",
|
"send": "^1.2.1",
|
||||||
"sequelize": "^6.37.7",
|
|
||||||
"ws": "npm:@kevisual/ws",
|
"ws": "npm:@kevisual/ws",
|
||||||
"xml2js": "^0.6.2"
|
"xml2js": "^0.6.2"
|
||||||
},
|
},
|
||||||
@@ -95,14 +93,11 @@
|
|||||||
"es-toolkit": "^1.44.0",
|
"es-toolkit": "^1.44.0",
|
||||||
"ioredis": "^5.9.2",
|
"ioredis": "^5.9.2",
|
||||||
"jsonwebtoken": "^9.0.3",
|
"jsonwebtoken": "^9.0.3",
|
||||||
"minio": "^8.0.6",
|
|
||||||
"nanoid": "^5.1.6",
|
"nanoid": "^5.1.6",
|
||||||
"nodemon": "^3.1.11",
|
|
||||||
"p-queue": "^9.1.0",
|
"p-queue": "^9.1.0",
|
||||||
"pg": "^8.18.0",
|
"pg": "^8.18.0",
|
||||||
"pm2": "^6.0.14",
|
"pm2": "^6.0.14",
|
||||||
"semver": "^7.7.4",
|
"semver": "^7.7.4",
|
||||||
"sequelize": "^6.37.7",
|
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6"
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
|
|||||||
1141
pnpm-lock.yaml
generated
1141
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -178,7 +178,16 @@ export class Org {
|
|||||||
* 创建组织
|
* 创建组织
|
||||||
*/
|
*/
|
||||||
static async create(data: { username: string; description?: string; users: OrgUser[] }): Promise<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]);
|
return new Org(inserted[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -259,13 +259,19 @@ export class UserSecret {
|
|||||||
userId = tokenUser.uid;
|
userId = tokenUser.uid;
|
||||||
orgId = tokenUser.id;
|
orgId = tokenUser.id;
|
||||||
}
|
}
|
||||||
const inserted = await db.insert(userSecretsTable).values({
|
|
||||||
|
const insertData: Partial<typeof userSecretsTable.$inferInsert> = {
|
||||||
userId,
|
userId,
|
||||||
orgId,
|
|
||||||
token,
|
token,
|
||||||
title: tokenUser.title || randomString(6),
|
title: tokenUser.title || randomString(6),
|
||||||
expiredTime: UserSecret.getExpiredTime(expireDays).toISOString(),
|
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]);
|
return new UserSecret(inserted[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import { oauth } from '../oauth/auth.ts';
|
|||||||
import { cryptPwd } from '../oauth/salt.ts';
|
import { cryptPwd } from '../oauth/salt.ts';
|
||||||
import { OauthUser } from '../oauth/oauth.ts';
|
import { OauthUser } from '../oauth/oauth.ts';
|
||||||
import { db } from '../../modules/db.ts';
|
import { db } from '../../modules/db.ts';
|
||||||
|
import { Org } from './org.ts';
|
||||||
|
|
||||||
import { cfUser, cfOrgs, cfUserSecrets } from '../../db/drizzle/schema.ts';
|
import { cfUser, cfOrgs, cfUserSecrets } from '../../db/drizzle/schema.ts';
|
||||||
import { eq, sql, InferSelectModel, InferInsertModel } from 'drizzle-orm';
|
import { eq, sql, InferSelectModel, InferInsertModel } from 'drizzle-orm';
|
||||||
|
|
||||||
@@ -188,15 +190,28 @@ export class User {
|
|||||||
password = password || '123456';
|
password = password || '123456';
|
||||||
const cPassword = cryptPwd(password, salt);
|
const cPassword = cryptPwd(password, salt);
|
||||||
|
|
||||||
const inserted = await db.insert(usersTable).values({
|
const insertData: any = {
|
||||||
username,
|
username,
|
||||||
password: cPassword,
|
password: cPassword,
|
||||||
description,
|
|
||||||
salt,
|
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) {
|
static async createOrg(username: string, owner: string, description?: string) {
|
||||||
@@ -212,16 +227,21 @@ export class User {
|
|||||||
throw new CustomError('Owner type is not 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 org = await Org.create({ username, description, users: [{ uid: owner, role: 'owner' }] });
|
||||||
const inserted = await db.insert(usersTable).values({
|
|
||||||
|
const insertData: any = {
|
||||||
username,
|
username,
|
||||||
password: '',
|
password: '',
|
||||||
description,
|
|
||||||
type: 'org',
|
type: 'org',
|
||||||
owner,
|
owner,
|
||||||
orgId: org.id,
|
orgId: org.id,
|
||||||
}).returning();
|
};
|
||||||
|
|
||||||
|
if (description !== undefined && description !== null) {
|
||||||
|
insertData.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inserted = await db.insert(usersTable).values(insertData).returning();
|
||||||
|
|
||||||
// owner add
|
// owner add
|
||||||
await redis.del(`user:${me.id}:orgs`);
|
await redis.del(`user:${me.id}:orgs`);
|
||||||
@@ -304,6 +324,7 @@ export class User {
|
|||||||
*/
|
*/
|
||||||
async getOrgs() {
|
async getOrgs() {
|
||||||
let id = this.id;
|
let id = this.id;
|
||||||
|
console.log('type', this.type, this.tokenUser)
|
||||||
if (this.type === 'org') {
|
if (this.type === 'org') {
|
||||||
if (this.tokenUser && this.tokenUser.uid) {
|
if (this.tokenUser && this.tokenUser.uid) {
|
||||||
id = this.tokenUser.uid;
|
id = this.tokenUser.uid;
|
||||||
|
|||||||
@@ -92,9 +92,7 @@ app
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const user = await User.findOne({
|
const user = await User.findOne({
|
||||||
where: {
|
id: tokenUser.id,
|
||||||
id: tokenUser.id,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (!user) {
|
if (!user) {
|
||||||
ctx.throw(404, 'user not found');
|
ctx.throw(404, 'user not found');
|
||||||
@@ -133,9 +131,7 @@ app
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await User.findOne({
|
const user = await User.findOne({
|
||||||
where: {
|
id: tokenUser.id,
|
||||||
id: tokenUser.id,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (!user) {
|
if (!user) {
|
||||||
ctx.throw(404, 'user not found');
|
ctx.throw(404, 'user not found');
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// import { Op } from 'sequelize';
|
|
||||||
// import { app } from '@/app.ts';
|
// import { app } from '@/app.ts';
|
||||||
// import { FileSyncModel } from './model.ts';
|
// import { FileSyncModel } from './model.ts';
|
||||||
// app
|
// app
|
||||||
|
|||||||
@@ -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;
|
const { id, newName, admin = false, ctx } = opts;
|
||||||
if (!admin) {
|
if (!admin) {
|
||||||
checkUsernameShort(newName);
|
checkUsernameShort(newName);
|
||||||
@@ -35,7 +35,7 @@ export const toChangeName = async (opts: { id: number; newName: string; admin?:
|
|||||||
}
|
}
|
||||||
const oldName = user.username;
|
const oldName = user.username;
|
||||||
checkUsername(newName);
|
checkUsername(newName);
|
||||||
const findUserByUsername = await User.findOne({ where: { username: newName } });
|
const findUserByUsername = await User.findOne({ username: newName });
|
||||||
if (findUserByUsername) {
|
if (findUserByUsername) {
|
||||||
ctx.throw(400, 'Username already exists');
|
ctx.throw(400, 'Username already exists');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user