2024-09-03 20:19:33 +08:00
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
|
import { db, InitRoles, InitTaxonomies, ObjectType } from "@nicestack/common";
|
|
|
|
import { AuthService } from '@server/auth/auth.service';
|
2024-09-10 11:36:09 +08:00
|
|
|
import { MinioService } from '@server/minio/minio.service';
|
2024-09-03 20:19:33 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InitService {
|
|
|
|
private readonly logger = new Logger(InitService.name);
|
2024-09-10 11:36:09 +08:00
|
|
|
constructor(private readonly authService: AuthService, private readonly minioService: MinioService) { }
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private async createTaxonomy() {
|
|
|
|
this.logger.log('Checking existing taxonomies');
|
|
|
|
const existingTaxonomies = await db.taxonomy.findMany();
|
|
|
|
const existingTaxonomyNames = existingTaxonomies.map(taxonomy => taxonomy.name);
|
|
|
|
for (const [index, taxonomy] of InitTaxonomies.entries()) {
|
|
|
|
if (!existingTaxonomyNames.includes(taxonomy.name)) {
|
|
|
|
this.logger.log(`Creating taxonomy: ${taxonomy.name}`);
|
|
|
|
await db.taxonomy.create({
|
|
|
|
data: {
|
|
|
|
...taxonomy,
|
|
|
|
order: index,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.logger.log(`Taxonomy already exists: ${taxonomy.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-10 11:36:09 +08:00
|
|
|
private async createBucket() {
|
|
|
|
await this.minioService.createBucket('app')
|
|
|
|
}
|
2024-09-03 20:19:33 +08:00
|
|
|
private async createRoot() {
|
|
|
|
this.logger.log('Checking for root account');
|
2024-09-10 10:31:24 +08:00
|
|
|
const rootAccountExists = await db.staff.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
phoneNumber: process.env.ADMIN_PHONE_NUMBER || '000000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
username: 'root'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
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-09-10 10:31:24 +08:00
|
|
|
password: 'root'
|
2024-09-03 20:19:33 +08:00
|
|
|
})
|
|
|
|
const rootRole = await db.role.findUnique({
|
|
|
|
where: { name: '根管理员' },
|
|
|
|
});
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
await this.createTaxonomy();
|
2024-09-10 11:36:09 +08:00
|
|
|
|
|
|
|
this.logger.log('Initialize minio')
|
|
|
|
await this.createBucket()
|
2024-09-03 20:19:33 +08:00
|
|
|
}
|
|
|
|
}
|