2024-09-03 20:19:33 +08:00
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
2025-01-06 08:45:23 +08:00
|
|
|
import { db, InitAppConfigs, InitRoles, InitTaxonomies, ObjectType } from "@nice/common";
|
2024-09-03 20:19:33 +08:00
|
|
|
import { AuthService } from '@server/auth/auth.service';
|
2024-12-30 08:26:40 +08:00
|
|
|
import { MinioService } from '@server/utils/minio/minio.service';
|
|
|
|
import { AppConfigService } from '@server/models/app-config/app-config.service';
|
|
|
|
import { GenDevService } from './gendev.service';
|
2024-09-03 20:19:33 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InitService {
|
|
|
|
private readonly logger = new Logger(InitService.name);
|
2024-12-30 08:26:40 +08:00
|
|
|
constructor(
|
|
|
|
private readonly appConfigService: AppConfigService,
|
|
|
|
private readonly minioService: MinioService,
|
|
|
|
private readonly authService: AuthService,
|
|
|
|
private readonly genDevService: GenDevService
|
|
|
|
) { }
|
2024-09-03 20:19:33 +08:00
|
|
|
private async createRoles() {
|
|
|
|
this.logger.log('Checking existing system roles');
|
|
|
|
for (const role of InitRoles) {
|
|
|
|
const existingRole = await db.role.findUnique({
|
|
|
|
where: { name: role.name },
|
|
|
|
});
|
|
|
|
if (!existingRole) {
|
|
|
|
this.logger.log(`Creating role: ${role.name}`);
|
|
|
|
await db.role.create({
|
|
|
|
data: { ...role, system: true },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.logger.log(`Role already exists: ${role.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-30 08:26:40 +08:00
|
|
|
private async createOrUpdateTaxonomy() {
|
2024-09-03 20:19:33 +08:00
|
|
|
this.logger.log('Checking existing taxonomies');
|
2024-12-30 08:26:40 +08:00
|
|
|
|
2024-09-03 20:19:33 +08:00
|
|
|
const existingTaxonomies = await db.taxonomy.findMany();
|
2024-12-30 08:26:40 +08:00
|
|
|
const existingTaxonomyMap = new Map(existingTaxonomies.map(taxonomy => [taxonomy.name, taxonomy]));
|
|
|
|
|
2024-09-03 20:19:33 +08:00
|
|
|
for (const [index, taxonomy] of InitTaxonomies.entries()) {
|
2024-12-30 08:26:40 +08:00
|
|
|
const existingTaxonomy = existingTaxonomyMap.get(taxonomy.name);
|
|
|
|
|
|
|
|
if (!existingTaxonomy) {
|
|
|
|
// Create new taxonomy
|
2024-09-03 20:19:33 +08:00
|
|
|
await db.taxonomy.create({
|
|
|
|
data: {
|
|
|
|
...taxonomy,
|
|
|
|
order: index,
|
|
|
|
},
|
|
|
|
});
|
2024-12-30 08:26:40 +08:00
|
|
|
this.logger.log(`Created new taxonomy: ${taxonomy.name}`);
|
2024-09-03 20:19:33 +08:00
|
|
|
} else {
|
2024-12-30 08:26:40 +08:00
|
|
|
// Check for differences and update if necessary
|
|
|
|
const differences = Object.keys(taxonomy).filter(key => taxonomy[key] !== existingTaxonomy[key]);
|
|
|
|
|
|
|
|
if (differences.length > 0) {
|
|
|
|
await db.taxonomy.update({
|
|
|
|
where: { id: existingTaxonomy.id },
|
|
|
|
data: {
|
|
|
|
...taxonomy,
|
|
|
|
order: index,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.logger.log(`Updated taxonomy: ${taxonomy.name}`);
|
|
|
|
} else {
|
|
|
|
this.logger.log(`No changes for taxonomy: ${taxonomy.name}`);
|
|
|
|
}
|
2024-09-03 20:19:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private async createRoot() {
|
|
|
|
this.logger.log('Checking for root account');
|
2024-12-30 08:26:40 +08:00
|
|
|
|
2024-09-10 10:31:24 +08:00
|
|
|
const rootAccountExists = await db.staff.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
2024-12-30 08:26:40 +08:00
|
|
|
phoneNumber: process.env.ADMIN_PHONE_NUMBER || '000000',
|
2024-09-10 10:31:24 +08:00
|
|
|
},
|
|
|
|
{
|
2024-12-30 08:26:40 +08:00
|
|
|
username: 'root',
|
|
|
|
},
|
|
|
|
],
|
2024-09-10 10:31:24 +08:00
|
|
|
},
|
2024-09-03 20:19:33 +08:00
|
|
|
});
|
2024-12-30 08:26:40 +08:00
|
|
|
|
2024-09-03 20:19:33 +08:00
|
|
|
if (!rootAccountExists) {
|
|
|
|
this.logger.log('Creating root account');
|
2024-09-10 10:31:24 +08:00
|
|
|
const rootStaff = await this.authService.signUp({
|
2024-09-03 20:19:33 +08:00
|
|
|
username: 'root',
|
2024-12-30 08:26:40 +08:00
|
|
|
password: 'root',
|
|
|
|
});
|
2024-09-03 20:19:33 +08:00
|
|
|
const rootRole = await db.role.findUnique({
|
|
|
|
where: { name: '根管理员' },
|
|
|
|
});
|
2024-12-30 08:26:40 +08:00
|
|
|
|
2024-09-03 20:19:33 +08:00
|
|
|
if (rootRole) {
|
|
|
|
this.logger.log('Assigning root role to root account');
|
|
|
|
await db.roleMap.create({
|
|
|
|
data: {
|
|
|
|
objectType: ObjectType.STAFF,
|
|
|
|
objectId: rootStaff.id,
|
|
|
|
roleId: rootRole.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.logger.error('Root role does not exist');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.logger.log('Root account already exists');
|
|
|
|
}
|
|
|
|
}
|
2024-12-30 08:26:40 +08:00
|
|
|
private async createBucket() {
|
|
|
|
await this.minioService.createBucket('app')
|
|
|
|
}
|
|
|
|
private async initAppConfigs() {
|
|
|
|
const existingConfigs = await db.appConfig.findMany();
|
|
|
|
const existingConfigSlugs = existingConfigs.map((config) => config.slug);
|
|
|
|
for (const [index, config] of InitAppConfigs.entries()) {
|
|
|
|
if (!existingConfigSlugs.includes(config.slug)) {
|
|
|
|
this.logger.log(`create Option Page ${config.title}`);
|
|
|
|
await this.appConfigService.create({ data: config });
|
|
|
|
} else {
|
|
|
|
this.logger.log(`AppConfig already exists: ${config.title}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 20:19:33 +08:00
|
|
|
async init() {
|
|
|
|
this.logger.log('Initializing system roles');
|
|
|
|
await this.createRoles();
|
|
|
|
this.logger.log('Initializing root account');
|
|
|
|
await this.createRoot();
|
|
|
|
this.logger.log('Initializing taxonomies');
|
2024-12-30 08:26:40 +08:00
|
|
|
await this.createOrUpdateTaxonomy()
|
2024-09-10 11:36:09 +08:00
|
|
|
this.logger.log('Initialize minio')
|
|
|
|
await this.createBucket()
|
2024-12-30 08:26:40 +08:00
|
|
|
this.logger.log('Initializing appConfigs');
|
|
|
|
await this.initAppConfigs();
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
try {
|
|
|
|
await this.genDevService.genDataEvent();
|
|
|
|
} catch (err: any) {
|
|
|
|
this.logger.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-03 20:19:33 +08:00
|
|
|
}
|
|
|
|
}
|