perf: 支持迁移个人数据到企业项目中

This commit is contained in:
xiaojunnuo
2026-03-09 23:34:11 +08:00
parent 853fdc70a2
commit c6ca832737
8 changed files with 321 additions and 70 deletions
@@ -0,0 +1,42 @@
import { BaseController, Constants } from '@certd/lib-server';
import { Controller, Inject, Post, Provide } from '@midwayjs/core';
import { TransferService } from '../../../modules/sys/enterprise/service/transfer-service.js';
/**
*/
@Provide()
@Controller('/api/enterprise/transfer')
export class TransferController extends BaseController {
@Inject()
service: TransferService;
getService(): TransferService {
return this.service;
}
/**
* 我自己的资源
* @param body
* @returns
*/
@Post('/selfResources', { summary: Constants.per.authOnly })
async selfResources() {
const userId = this.getUserId();
const res = await this.service.getUserResources(userId);
return this.ok(res);
}
/**
* 迁移项目
* @param body
* @returns
*/
@Post('/doTransfer', { summary: Constants.per.authOnly })
async doTransfer() {
const {projectId} = await this.getProjectUserIdRead();
const userId = this.getUserId();
await this.service.transferAll(userId,projectId);
return this.ok();
}
}
@@ -0,0 +1,90 @@
import { isEnterprise } from "@certd/lib-server";
import { ApplicationContext, IMidwayContainer, Provide, Scope, ScopeEnum } from "@midwayjs/core";
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class TransferService {
@ApplicationContext()
appCtx: IMidwayContainer;
async getServices() {
const getService = async (key: string) => {
return await this.appCtx.getAsync(key);
}
const serviceNames = [
"pipeline",
"certInfo",
"siteInfo",
"domain",
"cnameRecord",
"group",
"pipelineGroup",
"notification",
"subDomain",
"template",
"openKey",
"siteIp",
"access",
]
const services: any = {}
for (const key of serviceNames) {
services[key] = await getService(`${key}Service`);
}
return services;
}
/**
* 获取用户资源
* @param userId
* @returns
*/
async getUserResources(userId: number) {
const query = {
userId,
}
const services = await this.getServices();
const counts: any = {}
let totalCount = 0;
for (const key of Object.keys(services)) {
const count = await services[key].repository.count({ where: query });
counts[key] = count;
totalCount += count;
}
return {
...counts,
totalCount,
}
}
async transferAll(userId: number, projectId: number) {
if (!isEnterprise()) {
throw new Error('当前非企业模式,不支持资源迁移到项目');
}
if (projectId === 0) {
throw new Error('项目ID不能为0');
}
if (userId == null) {
throw new Error('用户ID不能为空');
}
const query = {
userId,
}
const services = await this.getServices();
for (const key of Object.keys(services)) {
await services[key].repository.update(query, {
userId: -1,
projectId,
});
}
}
}