diff --git a/backend/services/authorization-service/src/application/services/authorization-application.service.ts b/backend/services/authorization-service/src/application/services/authorization-application.service.ts index ccf2cfa0..a48d34ed 100644 --- a/backend/services/authorization-service/src/application/services/authorization-application.service.ts +++ b/backend/services/authorization-service/src/application/services/authorization-application.service.ts @@ -2865,9 +2865,9 @@ export class AuthorizationApplicationService { throw new ApplicationError('您已拥有社区授权') } - // 直接创建授权(自助申请的社区授权直接生效) + // 直接创建授权(自助申请的社区授权直接生效,状态为 AUTHORIZED) const userId = UserId.create(command.userId, command.accountSequence) - const authorization = AuthorizationRole.createCommunityAuth({ + const authorization = AuthorizationRole.createSelfAppliedCommunity({ userId, communityName: command.communityName!, }) @@ -2906,9 +2906,9 @@ export class AuthorizationApplicationService { throw new ApplicationError('该市已有授权市公司') } - // 创建授权市公司授权 + // 创建授权市公司授权(自助申请直接生效,状态为 AUTHORIZED) const userId = UserId.create(command.userId, command.accountSequence) - const authorization = AuthorizationRole.createAuthCityCompany({ + const authorization = AuthorizationRole.createSelfAppliedAuthCityCompany({ userId, cityCode: command.cityCode!, cityName: command.cityName!, @@ -2948,9 +2948,9 @@ export class AuthorizationApplicationService { throw new ApplicationError('该省已有授权省公司') } - // 创建授权省公司授权 + // 创建授权省公司授权(自助申请直接生效,状态为 AUTHORIZED) const userId = UserId.create(command.userId, command.accountSequence) - const authorization = AuthorizationRole.createAuthProvinceCompany({ + const authorization = AuthorizationRole.createSelfAppliedAuthProvinceCompany({ userId, provinceCode: command.provinceCode!, provinceName: command.provinceName!, diff --git a/backend/services/authorization-service/src/domain/aggregates/authorization-role.aggregate.ts b/backend/services/authorization-service/src/domain/aggregates/authorization-role.aggregate.ts index 96933dfe..4255f5bc 100644 --- a/backend/services/authorization-service/src/domain/aggregates/authorization-role.aggregate.ts +++ b/backend/services/authorization-service/src/domain/aggregates/authorization-role.aggregate.ts @@ -283,6 +283,50 @@ export class AuthorizationRole extends AggregateRoot { return auth } + // 工厂方法 - 自助申请社区授权(直接生效,无需审核) + static createSelfAppliedCommunity(params: { userId: UserId; communityName: string }): AuthorizationRole { + const now = new Date() + const auth = new AuthorizationRole({ + authorizationId: AuthorizationId.generate(), + userId: params.userId, + roleType: RoleType.COMMUNITY, + regionCode: RegionCode.create(params.communityName), + regionName: params.communityName, + status: AuthorizationStatus.AUTHORIZED, // 自助申请直接生效 + displayTitle: params.communityName, + authorizedAt: now, + authorizedBy: null, // 自助申请无管理员 + revokedAt: null, + revokedBy: null, + revokeReason: null, + assessmentConfig: AssessmentConfig.forCommunity(), + requireLocalPercentage: 0, + exemptFromPercentageCheck: true, + benefitActive: false, // 权益需要达到考核目标后激活 + benefitActivatedAt: null, + benefitDeactivatedAt: null, + benefitValidUntil: null, + lastAssessmentMonth: null, + monthlyTreesAdded: 0, + lastMonthTreesAdded: 0, + currentMonthIndex: 0, + deletedAt: null, + createdAt: now, + updatedAt: now, + }) + + auth.addDomainEvent( + new CommunityAuthorizedEvent({ + authorizationId: auth.authorizationId.value, + userId: params.userId.value, + communityName: params.communityName, + authorizedBy: null, // 自助申请 + }), + ) + + return auth + } + // 工厂方法 - 管理员直接授权社区 static createCommunity(params: { userId: UserId @@ -381,6 +425,55 @@ export class AuthorizationRole extends AggregateRoot { return auth } + // 工厂方法 - 自助申请授权省公司(直接生效,无需审核) + static createSelfAppliedAuthProvinceCompany(params: { + userId: UserId + provinceCode: string + provinceName: string + }): AuthorizationRole { + const now = new Date() + const auth = new AuthorizationRole({ + authorizationId: AuthorizationId.generate(), + userId: params.userId, + roleType: RoleType.AUTH_PROVINCE_COMPANY, + regionCode: RegionCode.create(params.provinceCode), + regionName: params.provinceName, + status: AuthorizationStatus.AUTHORIZED, // 自助申请直接生效 + displayTitle: `授权${params.provinceName}`, + authorizedAt: now, + authorizedBy: null, // 自助申请无管理员 + revokedAt: null, + revokedBy: null, + revokeReason: null, + assessmentConfig: AssessmentConfig.forAuthProvince(), + requireLocalPercentage: 5.0, + exemptFromPercentageCheck: false, + benefitActive: false, // 权益需要达到考核目标后激活 + benefitActivatedAt: null, + benefitDeactivatedAt: null, + benefitValidUntil: null, + lastAssessmentMonth: null, + monthlyTreesAdded: 0, + lastMonthTreesAdded: 0, + currentMonthIndex: 0, + deletedAt: null, + createdAt: now, + updatedAt: now, + }) + + auth.addDomainEvent( + new AuthProvinceCompanyGrantedEvent({ + authorizationId: auth.authorizationId.value, + userId: params.userId.value, + provinceCode: params.provinceCode, + provinceName: params.provinceName, + authorizedBy: null, // 自助申请 + }), + ) + + return auth + } + // 工厂方法 - 创建正式省公司 // 授权即激活,开始按月考核(150, 300, 600, 1200, 2400, 4800, 9600, 19200, 11750) static createProvinceCompany(params: { @@ -481,6 +574,55 @@ export class AuthorizationRole extends AggregateRoot { return auth } + // 工厂方法 - 自助申请授权市公司(直接生效,无需审核) + static createSelfAppliedAuthCityCompany(params: { + userId: UserId + cityCode: string + cityName: string + }): AuthorizationRole { + const now = new Date() + const auth = new AuthorizationRole({ + authorizationId: AuthorizationId.generate(), + userId: params.userId, + roleType: RoleType.AUTH_CITY_COMPANY, + regionCode: RegionCode.create(params.cityCode), + regionName: params.cityName, + status: AuthorizationStatus.AUTHORIZED, // 自助申请直接生效 + displayTitle: `授权${params.cityName}`, + authorizedAt: now, + authorizedBy: null, // 自助申请无管理员 + revokedAt: null, + revokedBy: null, + revokeReason: null, + assessmentConfig: AssessmentConfig.forAuthCity(), + requireLocalPercentage: 5.0, + exemptFromPercentageCheck: false, + benefitActive: false, // 权益需要达到考核目标后激活 + benefitActivatedAt: null, + benefitDeactivatedAt: null, + benefitValidUntil: null, + lastAssessmentMonth: null, + monthlyTreesAdded: 0, + lastMonthTreesAdded: 0, + currentMonthIndex: 0, + deletedAt: null, + createdAt: now, + updatedAt: now, + }) + + auth.addDomainEvent( + new AuthCityCompanyGrantedEvent({ + authorizationId: auth.authorizationId.value, + userId: params.userId.value, + cityCode: params.cityCode, + cityName: params.cityName, + authorizedBy: null, // 自助申请 + }), + ) + + return auth + } + // 工厂方法 - 创建正式市公司 // 授权即激活,开始按月考核(30, 60, 120, 240, 480, 960, 1920, 3840, 2350) static createCityCompany(params: { diff --git a/backend/services/authorization-service/src/domain/events/authorization-events.ts b/backend/services/authorization-service/src/domain/events/authorization-events.ts index f2576dcd..80789378 100644 --- a/backend/services/authorization-service/src/domain/events/authorization-events.ts +++ b/backend/services/authorization-service/src/domain/events/authorization-events.ts @@ -1,7 +1,7 @@ import { DomainEvent } from './domain-event.base' import { RoleType } from '@/domain/enums' -// 社区授权事件(管理员直接授权) +// 社区授权事件(管理员直接授权或自助申请) export class CommunityAuthorizedEvent extends DomainEvent { readonly eventType = 'authorization.community.authorized' readonly aggregateId: string @@ -9,14 +9,14 @@ export class CommunityAuthorizedEvent extends DomainEvent { authorizationId: string userId: string communityName: string - authorizedBy: string + authorizedBy: string | null // null 表示自助申请 } constructor(data: { authorizationId: string userId: string communityName: string - authorizedBy: string + authorizedBy: string | null }) { super() this.aggregateId = data.authorizationId @@ -146,7 +146,7 @@ export class AuthProvinceCompanyGrantedEvent extends DomainEvent { userId: string provinceCode: string provinceName: string - authorizedBy: string + authorizedBy: string | null // null 表示自助申请 } constructor(data: { @@ -154,7 +154,7 @@ export class AuthProvinceCompanyGrantedEvent extends DomainEvent { userId: string provinceCode: string provinceName: string - authorizedBy: string + authorizedBy: string | null }) { super() this.aggregateId = data.authorizationId @@ -171,7 +171,7 @@ export class AuthCityCompanyGrantedEvent extends DomainEvent { userId: string cityCode: string cityName: string - authorizedBy: string + authorizedBy: string | null // null 表示自助申请 } constructor(data: { @@ -179,7 +179,7 @@ export class AuthCityCompanyGrantedEvent extends DomainEvent { userId: string cityCode: string cityName: string - authorizedBy: string + authorizedBy: string | null }) { super() this.aggregateId = data.authorizationId