Files
corrosion-admin-panel/backend-nest/src/modules/schedules/schedules.service.ts
Vantz Stockwell 78e97babf1
All checks were successful
Test Asgard Runner / test (push) Successful in 3s
fix: Align NestJS entities with actual DB schema — 12 files, 5 entities
Root cause of all remaining 500s: TypeORM entities were scaffolded with
"ideal" column names that don't match the Postgres columns created by
the Rust migrations. Every query generated SQL referencing non-existent
columns.

Entity fixes:
- notifications_config: email_enabled→email_alerts_enabled, removed
  6 phantom columns (email_address, notify_on_start, notify_on_stop,
  notify_on_player_threshold, player_threshold), renamed 4 notify
  columns to match DB (notify_server_crash, notify_wipe_start, etc),
  added 3 missing columns (notify_server_offline, notify_store_purchase,
  notify_player_report)
- team_members: joined_at→accepted_at (nullable, matches DB)
- roles: removed description column (doesn't exist in DB)
- scheduled_tasks: is_enabled→is_active, removed phantom last_run
- wipe_profiles: pre/post_wipe_config nullable→NOT NULL with default

Service/DTO fixes:
- Updated all property references across notifications, team, schedules
  services and DTOs to match corrected entity names
- Added is_active to UpdateTaskDto (frontend sends it, was being
  rejected by forbidNonWhitelisted validation)
- Removed description from CreateRoleDto

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 22:33:55 -05:00

125 lines
3.2 KiB
TypeScript

import {
Injectable,
NotFoundException,
BadRequestException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ScheduledTask } from '../../entities/scheduled-task.entity';
import { CreateTaskDto } from './dto/create-task.dto';
import { UpdateTaskDto } from './dto/update-task.dto';
@Injectable()
export class SchedulesService {
constructor(
@InjectRepository(ScheduledTask)
private taskRepository: Repository<ScheduledTask>,
) {}
async getTasks(licenseId: string): Promise<ScheduledTask[]> {
return await this.taskRepository.find({
where: { license_id: licenseId },
order: { created_at: 'DESC' },
});
}
async createTask(
licenseId: string,
dto: CreateTaskDto,
): Promise<ScheduledTask> {
// Validate cron expression is parseable
// In production, you'd use a cron parser library to validate
// For now, we rely on the regex in the DTO
// Set default timezone if not provided
const timezone = dto.timezone || 'UTC';
const task = this.taskRepository.create({
license_id: licenseId,
task_type: dto.task_type,
task_name: dto.task_name,
cron_expression: dto.cron_expression,
timezone: timezone,
task_config: dto.task_config || {},
is_active: true,
next_run: null, // Would be calculated by scheduler
created_at: new Date(),
});
const saved = await this.taskRepository.save(task);
// TODO: Register task with scheduler (tokio-cron-scheduler in Rust)
// This would send a NATS message to the scheduler service to register the task
return saved;
}
async updateTask(
licenseId: string,
taskId: string,
dto: UpdateTaskDto,
): Promise<ScheduledTask> {
const task = await this.taskRepository.findOne({
where: {
id: taskId,
license_id: licenseId,
},
});
if (!task) {
throw new NotFoundException(`Scheduled task ${taskId} not found`);
}
// Update fields
Object.assign(task, dto);
const updated = await this.taskRepository.save(task);
// TODO: Update task registration with scheduler
// Send NATS message to update the task in tokio-cron-scheduler
return updated;
}
async deleteTask(licenseId: string, taskId: string) {
const task = await this.taskRepository.findOne({
where: {
id: taskId,
license_id: licenseId,
},
});
if (!task) {
throw new NotFoundException(`Scheduled task ${taskId} not found`);
}
await this.taskRepository.delete(taskId);
// TODO: Unregister task from scheduler
// Send NATS message to remove the task from tokio-cron-scheduler
return { deleted: true };
}
async toggleTask(licenseId: string, taskId: string, enabled: boolean) {
const task = await this.taskRepository.findOne({
where: {
id: taskId,
license_id: licenseId,
},
});
if (!task) {
throw new NotFoundException(`Scheduled task ${taskId} not found`);
}
task.is_active = enabled;
const updated = await this.taskRepository.save(task);
// TODO: Enable/disable task in scheduler
// Send NATS message to pause or resume the task
return updated;
}
}