Files
helpdesk-texnet/backend/src/services/config.service.ts
pettrop e4f63a135e Initial commit: Helpdesk application setup
- Backend: Node.js/TypeScript with Prisma ORM
- Frontend: Vite + TypeScript
- Project configuration and documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 08:53:22 +01:00

212 lines
5.2 KiB
TypeScript

import prisma from '../config/database';
type CacheValue = {
data: unknown;
expiry: number;
};
export class ConfigService {
private cache = new Map<string, CacheValue>();
private cacheExpiry = 60000; // 1 minute
private getCached<T>(key: string): T | null {
const cached = this.cache.get(key);
if (cached && cached.expiry > Date.now()) {
return cached.data as T;
}
this.cache.delete(key);
return null;
}
private setCache(key: string, data: unknown): void {
this.cache.set(key, {
data,
expiry: Date.now() + this.cacheExpiry,
});
}
async getEquipmentTypes(activeOnly = true) {
const cacheKey = `equipment_types_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const types = await prisma.equipmentType.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, types);
return types;
}
async getRevisionTypes(activeOnly = true) {
const cacheKey = `revision_types_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const types = await prisma.revisionType.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, types);
return types;
}
async getRMAStatuses(activeOnly = true) {
const cacheKey = `rma_statuses_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const statuses = await prisma.rMAStatus.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, statuses);
return statuses;
}
async getRMASolutions(activeOnly = true) {
const cacheKey = `rma_solutions_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const solutions = await prisma.rMASolution.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, solutions);
return solutions;
}
async getTaskStatuses(activeOnly = true) {
const cacheKey = `task_statuses_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const statuses = await prisma.taskStatus.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, statuses);
return statuses;
}
async getPriorities(activeOnly = true) {
const cacheKey = `priorities_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const priorities = await prisma.priority.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, priorities);
return priorities;
}
async getUserRoles(activeOnly = true) {
const cacheKey = `user_roles_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const roles = await prisma.userRole.findMany({
where: activeOnly ? { active: true } : {},
orderBy: { level: 'asc' },
});
this.setCache(cacheKey, roles);
return roles;
}
async getTags(entityType?: string, activeOnly = true) {
const cacheKey = `tags_${entityType || 'all'}_${activeOnly}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const tags = await prisma.tag.findMany({
where: {
...(activeOnly && { active: true }),
...(entityType && { entityType }),
},
orderBy: { order: 'asc' },
});
this.setCache(cacheKey, tags);
return tags;
}
async getSetting<T = unknown>(key: string): Promise<T | null> {
const cacheKey = `setting_${key}`;
const cached = this.getCached<T>(cacheKey);
if (cached !== null) return cached;
const setting = await prisma.systemSetting.findUnique({
where: { key },
});
if (setting) {
this.setCache(cacheKey, setting.value);
return setting.value as T;
}
return null;
}
async getSettingsByCategory(category: string) {
const cacheKey = `settings_category_${category}`;
const cached = this.getCached(cacheKey);
if (cached) return cached;
const settings = await prisma.systemSetting.findMany({
where: { category },
});
this.setCache(cacheKey, settings);
return settings;
}
async getInitialTaskStatus() {
const statuses = await this.getTaskStatuses();
return (statuses as { id: string; isInitial: boolean }[]).find((s) => s.isInitial);
}
async getInitialRMAStatus() {
const statuses = await this.getRMAStatuses();
return (statuses as { id: string; isInitial: boolean }[]).find((s) => s.isInitial);
}
async getDefaultPriority() {
const priorities = await this.getPriorities();
return (priorities as { id: string; code: string }[]).find((p) => p.code === 'MEDIUM');
}
clearCache(): void {
this.cache.clear();
}
clearCacheKey(keyPrefix: string): void {
for (const key of this.cache.keys()) {
if (key.startsWith(keyPrefix)) {
this.cache.delete(key);
}
}
}
}
export const configService = new ConfigService();