116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
import { forwardRef, Inject, Injectable } from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
import { Repository } from "typeorm";
|
|
import { Project } from "./entities/project.entity";
|
|
import { RedisClient } from "src/redis/redis.service";
|
|
import { DatabaseManagerService } from "src/databaseManager/database/database.manager.service";
|
|
import { ProjectSettingService } from "./settings/project.setting.service";
|
|
import { TMeta } from "./constants";
|
|
|
|
@Injectable()
|
|
export class ProjectService {
|
|
constructor(
|
|
@InjectRepository(Project)
|
|
private readonly projectRepository: Repository<Project>,
|
|
@Inject(RedisClient)
|
|
private readonly redisClient: RedisClient,
|
|
@Inject(forwardRef(() => DatabaseManagerService))
|
|
private readonly databaseManagerService: DatabaseManagerService,
|
|
@Inject(ProjectSettingService)
|
|
private readonly projectSettingService: ProjectSettingService
|
|
) {}
|
|
|
|
async createDefaultSettings(projectId: string) {
|
|
const defaultSettings = [{ key: "sessionTTL", value: "3600" }];
|
|
|
|
await Promise.all(
|
|
defaultSettings.map((setting) =>
|
|
this.projectSettingService.create(projectId, setting.key, setting.value)
|
|
)
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
async create(name: string, createDatabase: boolean = true) {
|
|
const project = this.projectRepository.create({ name });
|
|
const projectSaved = await this.projectRepository.save(project);
|
|
|
|
if (createDatabase) {
|
|
await this.databaseManagerService.createDatabase(projectSaved.id);
|
|
}
|
|
|
|
await this.createDefaultSettings(projectSaved.id);
|
|
await this.redisClient.set(`project_${projectSaved.id}`, projectSaved, 300);
|
|
|
|
return projectSaved;
|
|
}
|
|
|
|
async updateMeta(projectId: string, meta: TMeta) {
|
|
return this.projectRepository.update({ id: projectId }, { meta });
|
|
}
|
|
|
|
async getProjectDetails(projectId: string) {
|
|
const project = await this.projectRepository.findOne({
|
|
where: { id: projectId },
|
|
relations: [
|
|
"database",
|
|
"database.migrations",
|
|
"queries",
|
|
"functions",
|
|
"settings",
|
|
],
|
|
});
|
|
|
|
return {
|
|
migrations: project?.database?.migrations || [],
|
|
queries: project?.queries || [],
|
|
functions: project?.functions || [],
|
|
settings: project?.settings || [],
|
|
meta: project?.meta || null,
|
|
name: project?.name || "",
|
|
id: project?.id || "",
|
|
};
|
|
}
|
|
|
|
async findById(id: string) {
|
|
const cached = await this.redisClient.get(`project_${id}`);
|
|
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
const project = await this.projectRepository.findOne({ where: { id: id } });
|
|
|
|
if (project) {
|
|
await this.redisClient.set(`project_${id}`, project, 300);
|
|
}
|
|
|
|
return project;
|
|
}
|
|
|
|
async updateDatabase(projectId: string, databaseId: string) {
|
|
await this.redisClient.del(`project_${projectId}`);
|
|
|
|
return this.projectRepository.update(projectId, {
|
|
database: { id: databaseId },
|
|
});
|
|
}
|
|
|
|
async updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
|
|
await this.redisClient.del(`project_${projectId}`);
|
|
|
|
return this.projectRepository.update(projectId, {
|
|
redisNodes: redisNodeId,
|
|
});
|
|
}
|
|
|
|
async getAllApiTokens(projectId: string) {
|
|
const project = await this.projectRepository.findOne({
|
|
where: { id: projectId },
|
|
relations: ["apiTokens"],
|
|
});
|
|
return project?.apiTokens || [];
|
|
}
|
|
}
|