chore: project controller ok

This commit is contained in:
xiaojunnuo
2026-02-13 21:28:17 +08:00
parent 3f87752d1f
commit 4ee6e38a94
42 changed files with 399 additions and 198 deletions
@@ -22,6 +22,9 @@ export class GroupEntity {
@Column({ name: 'type', comment: '类型', length: 512 })
type: string;
@Column({ name: 'project_id', comment: '项目Id' })
projectId: number;
@Column({
name: 'create_time',
comment: '创建时间',
@@ -44,6 +44,9 @@ export class DomainEntity {
@Column({ comment: 'http上传根目录', name: 'http_upload_root_dir', length: 512 })
httpUploadRootDir: string;
@Column({ name: 'project_id', comment: '项目Id' })
projectId: number;
@Column({
comment: '创建时间',
name: 'create_time',
@@ -18,6 +18,7 @@ import { DomainEntity } from '../entity/domain.js';
export interface SyncFromProviderReq {
userId: number;
projectId: number;
dnsProviderType: string;
dnsProviderAccessId: number;
}
@@ -215,9 +216,9 @@ export class DomainService extends BaseService<DomainEntity> {
}
async startDomainImportTask(req: { userId: number, key: string }) {
async startDomainImportTask(req: { userId: number, projectId: number, key: string }) {
const key = req.key
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(req.userId, UserDomainImportSetting)
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(req.userId, req.projectId, UserDomainImportSetting)
const item = setting.domainImportList.find(item => item.key === key)
if (!item) {
@@ -232,6 +233,7 @@ export class DomainService extends BaseService<DomainEntity> {
run: async (task: BackTask) => {
await this._syncFromProvider({
userId: req.userId,
projectId: req.projectId,
dnsProviderType,
dnsProviderAccessId,
}, task)
@@ -240,13 +242,13 @@ export class DomainService extends BaseService<DomainEntity> {
}
private async _syncFromProvider(req: SyncFromProviderReq, task: BackTask) {
const { userId, dnsProviderType, dnsProviderAccessId } = req;
const { userId, projectId, dnsProviderType, dnsProviderAccessId } = req;
const serviceGetter = this.taskServiceBuilder.create({ userId });
const serviceGetter = this.taskServiceBuilder.create({ userId, projectId });
const subDomainGetter = await serviceGetter.getSubDomainsGetter();
const domainParser = new DomainParser(subDomainGetter)
const access = await this.accessService.getById(dnsProviderAccessId, userId);
const access = await this.accessService.getById(dnsProviderAccessId, userId, projectId);
const context = { access, logger, http, utils, domainParser, serviceGetter };
// 翻页查询dns的记录
const dnsProvider = await createDnsProvider({ dnsProviderType, context })
@@ -272,6 +274,7 @@ export class DomainService extends BaseService<DomainEntity> {
where: {
domain,
userId,
projectId,
}
})
if (old) {
@@ -296,6 +299,7 @@ export class DomainService extends BaseService<DomainEntity> {
//添加
await this.add({
userId,
projectId,
domain,
dnsProviderType,
dnsProviderAccess: dnsProviderAccessId,
@@ -314,10 +318,11 @@ export class DomainService extends BaseService<DomainEntity> {
logger.info(`从域名提供商${dnsProviderType}导入域名完成(${key}),共导入${task.total}个域名,跳过${task.getSkipCount()}个域名,成功${task.getSuccessCount()}个域名,失败${task.getErrorCount()}个域名`)
}
async getDomainImportTaskStatus(req: { userId?: number }) {
async getDomainImportTaskStatus(req: { userId?: number ,projectId?: number}) {
const userId = req.userId || 0
const projectId = req.projectId
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, projectId, UserDomainImportSetting)
const list = setting?.domainImportList || []
const taskList: any = []
@@ -335,8 +340,9 @@ export class DomainService extends BaseService<DomainEntity> {
return taskList
}
async getProviderTitle(req: { userId?: number, dnsProviderType: string, dnsProviderAccessId: number }) {
async getProviderTitle(req: { userId?: number, projectId?: number, dnsProviderType: string, dnsProviderAccessId: number }) {
const userId = req.userId || 0
const projectId = req.projectId
const { dnsProviderType, dnsProviderAccessId } = req
const dnsProviderDefine = dnsProviderRegistry.getDefine(dnsProviderType)
if (!dnsProviderDefine) {
@@ -346,6 +352,9 @@ export class DomainService extends BaseService<DomainEntity> {
if (!access || access.userId !== userId) {
throw new Error(`该授权(${dnsProviderAccessId})不存在,请检查是否已被删除`)
}
if (projectId && access.projectId !== projectId) {
throw new Error(`该授权(${dnsProviderAccessId})不存在,请检查是否已被删除`)
}
return {
title: `${dnsProviderDefine.title}_${access.name || ''}`,
//@ts-ignore
@@ -353,21 +362,22 @@ export class DomainService extends BaseService<DomainEntity> {
}
}
async addDomainImportTask(req: { userId?: number, dnsProviderType: string, dnsProviderAccessId: number, index?: number }) {
async addDomainImportTask(req: { userId?: number, projectId?: number, dnsProviderType: string, dnsProviderAccessId: number, index?: number }) {
const userId = req.userId || 0
const projectId = req.projectId
const { dnsProviderType, dnsProviderAccessId, index = 0 } = req
const key = `user_${userId}_${dnsProviderType}_${dnsProviderAccessId}`
const { title, icon } = await this.getProviderTitle(req)
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, projectId, UserDomainImportSetting)
setting.domainImportList = setting.domainImportList || []
if (setting.domainImportList.find(item => item.key === key)) {
throw new Error(`该域名导入任务${key}已存在`)
}
const access = await this.accessService.getAccessById(dnsProviderAccessId, true, userId)
const access = await this.accessService.getAccessById(dnsProviderAccessId, true, userId, projectId)
if (!access) {
throw new Error(`该授权(${dnsProviderAccessId})不存在,请检查是否已被删除`)
}
@@ -380,16 +390,17 @@ export class DomainService extends BaseService<DomainEntity> {
icon: icon || '',
}
setting.domainImportList.splice(index, 0, item)
await this.userSettingService.saveSetting(userId, setting)
await this.userSettingService.saveSetting(userId, projectId, setting)
return item
}
async deleteDomainImportTask(req: { userId?: number, key: string }) {
async deleteDomainImportTask(req: { userId?: number, projectId?: number, key: string }) {
const userId = req.userId || 0
const { key } = req
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
const projectId = req.projectId
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, projectId, UserDomainImportSetting)
setting.domainImportList = setting.domainImportList || []
const index = setting.domainImportList.findIndex(item => item.key === key)
if (index === -1) {
@@ -397,13 +408,14 @@ export class DomainService extends BaseService<DomainEntity> {
}
setting.domainImportList.splice(index, 1)
taskExecutor.clear(DOMAIN_IMPORT_TASK_TYPE, key)
await this.userSettingService.saveSetting(userId, setting)
await this.userSettingService.saveSetting(userId, projectId, setting)
}
async saveDomainImportTask(req: { userId?: number, dnsProviderType: string, dnsProviderAccessId: number, key?: string }) {
async saveDomainImportTask(req: { userId?: number, projectId?: number, dnsProviderType: string, dnsProviderAccessId: number, key?: string }) {
const userId = req.userId || 0
const projectId = req.projectId
const { dnsProviderType, dnsProviderAccessId, key } = req
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, projectId, UserDomainImportSetting)
setting.domainImportList = setting.domainImportList || []
let index = 0
@@ -412,36 +424,46 @@ export class DomainService extends BaseService<DomainEntity> {
if (index === -1) {
throw new Error(`该域名导入任务${key}不存在`)
}
await this.deleteDomainImportTask({ userId, key })
await this.deleteDomainImportTask({ userId, projectId, key })
}
return await this.addDomainImportTask({ userId, dnsProviderType, dnsProviderAccessId, index })
return await this.addDomainImportTask({ userId, projectId, dnsProviderType, dnsProviderAccessId, index })
}
async getSyncExpirationTaskStatus(req: { userId?: number }) {
async getSyncExpirationTaskStatus(req: { userId?: number, projectId?: number }) {
const userId = req.userId ?? 'all'
const key = `user_${userId}`
const projectId = req.projectId
let key = `user_${userId}`
if (projectId!=null) {
key += `_${projectId}`
}
const task = taskExecutor.get(DOMAIN_EXPIRE_TASK_TYPE, key)
return task
}
async startSyncExpirationTask(req: { userId?: number }) {
async startSyncExpirationTask(req: { userId?: number, projectId?: number }) {
const userId = req.userId
const key = `user_${userId ?? 'all'}`
const projectId = req.projectId
let key = `user_${userId ?? 'all'}`
if (projectId!=null) {
key += `_${projectId}`
}
taskExecutor.start(new BackTask({
type: DOMAIN_EXPIRE_TASK_TYPE,
key,
title: `同步注册域名过期时间(${key}))`,
run: async (task: BackTask) => {
await this._syncDomainsExpirationDate({ userId, task })
await this._syncDomainsExpirationDate({ userId, projectId, task })
}
}))
}
private async _syncDomainsExpirationDate(req: { userId?: number, task: BackTask }) {
private async _syncDomainsExpirationDate(req: { userId?: number, projectId?: number, task: BackTask }) {
//同步所有域名的过期时间
const pager = new Pager({
pageNo: 1,
@@ -498,6 +520,9 @@ export class DomainService extends BaseService<DomainEntity> {
if (req.userId != null) {
query.userId = req.userId
}
if (req.projectId != null) {
query.projectId = req.projectId
}
const getDomainPage = async (pager: Pager) => {
const pageRes = await this.page({
query: query,
@@ -549,5 +574,4 @@ export class DomainService extends BaseService<DomainEntity> {
logger.info(`同步用户(${key})注册域名过期时间完成(${req.task.getSuccessCount()}个成功,${req.task.getErrorCount()}个失败)`)
}
}
@@ -31,6 +31,9 @@ export class CnameRecordEntity {
@Column({ comment: '错误信息' })
error: string
@Column({ name: 'project_id', comment: '项目Id' })
projectId: number;
@Column({
comment: '创建时间',
name: 'create_time',
@@ -15,6 +15,9 @@ export class UserSettingsEntity {
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
setting: string;
@Column({ name: 'project_id', comment: '项目Id' })
projectId: number;
@Column({
name: 'create_time',
comment: '创建时间',
@@ -37,7 +37,7 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
};
}
async getByKey(key: string, userId: number): Promise<UserSettingsEntity | null> {
async getByKey(key: string, userId: number, projectId: number): Promise<UserSettingsEntity | null> {
if(!userId){
throw new Error('userId is required');
}
@@ -47,16 +47,17 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
return await this.repository.findOne({
where: {
key,
userId
userId,
projectId
}
});
}
async getSettingByKey(key: string, userId: number): Promise<any | null> {
async getSettingByKey(key: string, userId: number, projectId: number): Promise<any | null> {
if(!userId){
throw new Error('userId is required');
}
const entity = await this.getByKey(key, userId);
const entity = await this.getByKey(key, userId, projectId);
if (!entity) {
return null;
}
@@ -67,7 +68,8 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
const entity = await this.repository.findOne({
where: {
key: bean.key,
userId: bean.userId
userId: bean.userId,
projectId: bean.projectId
}
});
if (entity) {
@@ -80,12 +82,16 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
}
async getSetting<T>( userId: number,type: any, cache:boolean = false): Promise<T> {
async getSetting<T>( userId: number, projectId: number,type: any, cache:boolean = false): Promise<T> {
if(!userId){
throw new Error('userId is required');
}
const key = type.__key__;
const cacheKey = key + '_' + userId;
let cacheKey = key + '_' + userId ;
if (projectId) {
cacheKey += '_' + projectId;
}
if (cache) {
const settings: T = UserSettingCache.get(cacheKey);
if (settings) {
@@ -94,7 +100,7 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
}
let newSetting: T = new type();
const savedSettings = await this.getSettingByKey(key, userId);
const savedSettings = await this.getSettingByKey(key, userId, projectId);
newSetting = merge(newSetting, savedSettings);
if (cache) {
@@ -103,11 +109,11 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
return newSetting;
}
async saveSetting<T extends BaseSettings>(userId:number,bean: T) {
async saveSetting<T extends BaseSettings>(userId:number, projectId: number,bean: T) {
if(!userId){
throw new Error('userId is required');
}
const old = await this.getSetting(userId,bean.constructor)
const old = await this.getSetting(userId, projectId,bean.constructor)
bean = merge(old,bean)
const type: any = bean.constructor;
@@ -115,7 +121,7 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
if(!key){
throw new Error(`${type.name} must have __key__`);
}
const entity = await this.getByKey(key,userId);
const entity = await this.getByKey(key,userId, projectId);
const newEntity = new UserSettingsEntity();
if (entity) {
newEntity.id = entity.id;
@@ -123,6 +129,7 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
newEntity.key = key;
newEntity.title = type.__title__;
newEntity.userId = userId;
newEntity.projectId = projectId;
}
newEntity.setting = JSON.stringify(bean);
await this.repository.save(newEntity);
@@ -17,6 +17,9 @@ export class SubDomainEntity {
@Column({ name: 'disabled', comment: '禁用' })
disabled: boolean;
@Column({ name: 'project_id', comment: '项目Id' })
projectId: number;
@Column({
name: 'create_time',
comment: '创建时间',
@@ -3,22 +3,24 @@ import {NotificationService} from "../notification-service.js";
export class NotificationGetter implements INotificationService {
userId: number;
projectId: number;
notificationService: NotificationService;
constructor(userId: number, notificationService: NotificationService) {
constructor(userId: number, projectId: number, notificationService: NotificationService) {
this.userId = userId;
this.projectId = projectId;
this.notificationService = notificationService;
}
async getDefault() {
return await this.notificationService.getDefault(this.userId);
return await this.notificationService.getDefault(this.userId, this.projectId);
}
async getById(id: any) {
return await this.notificationService.getById(id, this.userId);
return await this.notificationService.getById(id, this.userId, this.projectId);
}
async send(req: NotificationSendReq): Promise<void> {
return await this.notificationService.send(req, this.userId);
return await this.notificationService.send(req, this.userId, this.projectId);
}
}
@@ -15,9 +15,11 @@ const serviceNames = [
]
export class TaskServiceGetter implements IServiceGetter{
private userId: number;
private projectId: number;
private appCtx : IMidwayContainer;
constructor(userId:number,appCtx:IMidwayContainer) {
constructor(userId:number,projectId:number,appCtx:IMidwayContainer) {
this.userId = userId;
this.projectId = projectId;
this.appCtx = appCtx
}
async get<T>(serviceName: string): Promise<T> {
@@ -51,7 +53,7 @@ export class TaskServiceGetter implements IServiceGetter{
async getAccessService(): Promise<AccessGetter> {
const accessService:AccessService = await this.appCtx.getAsync("accessService")
return new AccessGetter(this.userId, accessService.getById.bind(accessService));
return new AccessGetter(this.userId, this.projectId, accessService.getById.bind(accessService));
}
@@ -62,7 +64,7 @@ export class TaskServiceGetter implements IServiceGetter{
async getNotificationService(): Promise<NotificationGetter> {
const notificationService:NotificationService = await this.appCtx.getAsync("notificationService")
return new NotificationGetter(this.userId, notificationService);
return new NotificationGetter(this.userId, this.projectId, notificationService);
}
async getDomainVerifierGetter(): Promise<DomainVerifierGetter> {
@@ -78,12 +80,14 @@ export class TaskServiceBuilder {
create(req:TaskServiceCreateReq){
const userId = req.userId;
return new TaskServiceGetter(userId,this.appCtx)
const projectId = req.projectId;
return new TaskServiceGetter(userId,projectId,this.appCtx)
}
}
export type TaskServiceCreateReq = {
userId: number;
projectId?: number;
}
@@ -84,7 +84,7 @@ export class NotificationService extends BaseService<NotificationEntity> {
}
}
async getById(id: number, userId: number): Promise<NotificationInstanceConfig> {
async getById(id: number, userId: number, projectId?: number): Promise<NotificationInstanceConfig> {
if (!id) {
throw new ValidateException('id不能为空');
}
@@ -95,6 +95,7 @@ export class NotificationService extends BaseService<NotificationEntity> {
where: {
id,
userId,
projectId,
},
});
if (!res) {
@@ -114,10 +115,11 @@ export class NotificationService extends BaseService<NotificationEntity> {
};
}
async getDefault(userId: number): Promise<NotificationInstanceConfig> {
async getDefault(userId: number, projectId?: number): Promise<NotificationInstanceConfig> {
const res = await this.repository.findOne({
where: {
userId,
projectId,
},
order: {
isDefault: 'DESC',
@@ -129,7 +131,7 @@ export class NotificationService extends BaseService<NotificationEntity> {
return this.buildNotificationInstanceConfig(res);
}
async setDefault(id: number, userId: number) {
async setDefault(id: number, userId: number, projectId?: number) {
if (!id) {
throw new ValidateException('id不能为空');
}
@@ -139,6 +141,7 @@ export class NotificationService extends BaseService<NotificationEntity> {
await this.repository.update(
{
userId,
projectId,
},
{
isDefault: false,
@@ -148,6 +151,7 @@ export class NotificationService extends BaseService<NotificationEntity> {
{
id,
userId,
projectId,
},
{
isDefault: true,
@@ -155,8 +159,8 @@ export class NotificationService extends BaseService<NotificationEntity> {
);
}
async getOrCreateDefault(email: string, userId: any) {
const defaultConfig = await this.getDefault(userId);
async getOrCreateDefault(email: string, userId: any, projectId?: number) {
const defaultConfig = await this.getDefault(userId, projectId);
if (defaultConfig) {
return defaultConfig;
}
@@ -169,21 +173,22 @@ export class NotificationService extends BaseService<NotificationEntity> {
name: '邮件通知',
setting: JSON.stringify(setting),
isDefault: true,
projectId,
});
return this.buildNotificationInstanceConfig(res);
}
async send(req: NotificationSendReq, userId?: number) {
async send(req: NotificationSendReq, userId?: number, projectId?: number) {
const logger = req.logger;
let notifyConfig: NotificationInstanceConfig = null;
if (req.id && req.id > 0) {
notifyConfig = await this.getById(req.id, userId);
notifyConfig = await this.getById(req.id, userId, projectId);
if (!notifyConfig) {
logger.warn(`未找到通知配置<${req.id}>,请确认是否已被删除`);
}
}
if (!notifyConfig) {
notifyConfig = await this.getDefault(userId);
notifyConfig = await this.getDefault(userId, projectId);
if (!notifyConfig) {
logger.warn(`未找到默认通知配置`);
}