- 抽取 seed 资源/字典到 tenant-template.ts,initTenant 幂等初始化租户 - 创建租户自动初始化管理员/根组织/角色/资源/字典 - 删除租户级联清理全部业务数据 - 修复 BaseCrudService 对 Tenant 模型的 tenantCode 大小写注入 bug Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
2.8 KiB
TypeScript
74 lines
2.8 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { BaseCrudService } from '../common/base-crud.service.js';
|
|
import { PrismaService } from '../prisma/prisma.service.js';
|
|
import { TENANT_ADMIN_ROLE_CODE } from '../common/constants.js';
|
|
import { initTenant } from '../common/tenant-template.js';
|
|
import { CreateTenantDto, ListTenantQuery, UpdateTenantDto } from './dto.js';
|
|
|
|
@Injectable()
|
|
export class TenantsService extends BaseCrudService {
|
|
constructor(prisma: PrismaService) {
|
|
super(prisma, 'tenant');
|
|
}
|
|
|
|
/** 创建租户并初始化:根组织 + 租户管理员角色 + 管理员账号 + 资源 + 字典 + 授权。 */
|
|
async createTenant(dto: CreateTenantDto) {
|
|
const tenant = await this.create({
|
|
code: dto.code,
|
|
name: dto.name,
|
|
status: dto.status ?? 'enabled',
|
|
remark: dto.remark,
|
|
});
|
|
await initTenant({
|
|
prisma: this.prisma.raw,
|
|
tenantCode: dto.code,
|
|
tenantName: dto.name,
|
|
adminRoleCode: TENANT_ADMIN_ROLE_CODE,
|
|
adminRoleName: '租户管理员',
|
|
adminUsername: dto.adminUsername ?? 'admin',
|
|
adminPassword: dto.adminPassword ?? 'Admin@123456',
|
|
includePlatformPerms: false,
|
|
});
|
|
return tenant;
|
|
}
|
|
|
|
listTenants(q: ListTenantQuery) {
|
|
const where: Record<string, unknown> = {};
|
|
if (q.name) where.name = { contains: q.name };
|
|
if (q.code) where.code = { contains: q.code };
|
|
if (q.status) where.status = q.status;
|
|
return this.paginate(q.page, q.pageSize, where);
|
|
}
|
|
|
|
async updateTenant(id: string, dto: UpdateTenantDto) {
|
|
const existing = await this.findById(id);
|
|
if (!existing) throw new NotFoundException('租户不存在');
|
|
return this.update(id, { ...dto });
|
|
}
|
|
|
|
/** 删除租户并级联清理其全部业务数据。 */
|
|
async removeTenant(id: string) {
|
|
const existing = await this.findById(id);
|
|
if (!existing) throw new NotFoundException('租户不存在');
|
|
const code = existing.code;
|
|
const db = this.prisma.raw;
|
|
await db.$transaction([
|
|
db.userRole.deleteMany({ where: { tenantCode: code } }),
|
|
db.roleResource.deleteMany({ where: { tenantCode: code } }),
|
|
db.roleOrg.deleteMany({ where: { tenantCode: code } }),
|
|
db.refreshToken.deleteMany({ where: { tenantCode: code } }),
|
|
db.loginLog.deleteMany({ where: { tenantCode: code } }),
|
|
db.auditLog.deleteMany({ where: { tenantCode: code } }),
|
|
db.config.deleteMany({ where: { tenantCode: code } }),
|
|
db.dictItem.deleteMany({ where: { tenantCode: code } }),
|
|
db.dictType.deleteMany({ where: { tenantCode: code } }),
|
|
db.org.deleteMany({ where: { tenantCode: code } }),
|
|
db.resource.deleteMany({ where: { tenantCode: code } }),
|
|
db.role.deleteMany({ where: { tenantCode: code } }),
|
|
db.user.deleteMany({ where: { tenantCode: code } }),
|
|
db.tenant.delete({ where: { id } }),
|
|
]);
|
|
return null;
|
|
}
|
|
}
|