using Khaemhat.Core.Entities;
namespace Khaemhat.Core.Instance;
/// Where an installed licence comes from.
public enum LicenseSource
{
/// A signed .khl instance licence.
File = 0,
/// A signed activation code for one module.
ActivationCode = 1,
/// A fictitious code of SPEC §8.3, accepted only in test mode.
TestCode = 2,
}
/// An installed licence or activation code (sys_licenses).
public sealed class LicenseRecord : Entity
{
/// Where the licence comes from.
public LicenseSource Source { get; set; }
/// The licence text (.khl content or code), as installed.
public string Payload { get; set; } = string.Empty;
/// The licence holder.
public string? Licensee { get; set; }
/// The module codes, comma separated.
public string Modules { get; set; } = string.Empty;
/// Concurrent users granted (0 = not stated by this licence).
public int MaxConcurrentUsers { get; set; }
/// Companies granted (0 = not stated by this licence).
public int MaxCompanies { get; set; }
/// When the licence was issued.
public DateTime? IssuedAt { get; set; }
/// When the licence expires; for no expiry.
public DateTime? ExpiresAt { get; set; }
/// Days of full use after the expiry.
public int GracePeriodDays { get; set; }
/// Whether the licence counts (a removed licence stays for the history).
public bool IsActive { get; set; } = true;
}
/// One step in the history of licences (sys_license_activations).
public sealed class LicenseActivation : Entity
{
/// What happened: activated, deactivated, rejected, verified, expired.
public string Action { get; set; } = string.Empty;
/// The licence source.
public LicenseSource Source { get; set; }
/// The modules concerned, comma separated.
public string? Modules { get; set; }
/// The outcome code, for example ok, license_test_only, signature_invalid.
public string Outcome { get; set; } = string.Empty;
/// A short explanation.
public string? Message { get; set; }
}
/// An ASP.NET Core DataProtection key kept in the system database (sys_data_protection_keys).
public sealed class DataProtectionKey : Entity
{
/// The key name given by DataProtection.
public string FriendlyName { get; set; } = string.Empty;
/// The key XML. The key material inside is encrypted with the instance key.
public string Xml { get; set; } = string.Empty;
}
/// One event of the application log (sys_log, SPEC §9.5).
public sealed class LogEntry : IEntity
{
///
public long Id { get; set; }
/// When the event happened (UTC).
public DateTime At { get; set; }
/// The level: Trace, Debug, Information, Warning, Error, Critical.
public string Level { get; set; } = string.Empty;
/// The section (SPEC §9.5), for example Authentication.
public string Section { get; set; } = string.Empty;
/// The logger category.
public string Source { get; set; } = string.Empty;
/// The message.
public string Message { get; set; } = string.Empty;
/// The exception with its stack, if any.
public string? Exception { get; set; }
/// The user name.
public string? UserName { get; set; }
/// The company code.
public string? CompanyCode { get; set; }
/// The trace identifier shown to the user as the error code.
public string? TraceId { get; set; }
/// The request (method and path).
public string? Request { get; set; }
/// The entity concerned.
public string? Entity { get; set; }
/// The identifier of the entity concerned.
public string? EntityId { get; set; }
/// Further structured data as JSON.
public string? Data { get; set; }
}
///
/// A security or instance action (sys_security_audit): sign-ins, permissions, licences, databases, maintenance.
/// Rows are never updated; each one carries the hash of the previous row, so a removed or changed row breaks the chain.
///
public sealed class SecurityAuditEntry : IEntity
{
///
public long Id { get; set; }
/// When it happened (UTC).
public DateTime At { get; set; }
/// The action code, for example login.succeeded.
public string Action { get; set; } = string.Empty;
/// Who acted.
public string? UserName { get; set; }
/// The company concerned.
public string? CompanyCode { get; set; }
/// The client address.
public string? IpAddress { get; set; }
/// What the action was about (a user, a module, a database...).
public string? Target { get; set; }
/// Details as JSON (never secrets).
public string? Details { get; set; }
/// The hash of the previous row (empty for the first).
public string PreviousHash { get; set; } = string.Empty;
/// SHA-256 of this row's content and .
public string Hash { get; set; } = string.Empty;
}
/// A change to company data (audit_log, SPEC §7.3), shown in the "Storico" of a record.
public sealed class AuditEntry : IEntity
{
///
public long Id { get; set; }
/// When it happened (UTC).
public DateTime At { get; set; }
/// The entity (table) changed.
public string Entity { get; set; } = string.Empty;
/// The identifier of the record.
public string EntityId { get; set; } = string.Empty;
/// insert, update or delete.
public string Action { get; set; } = string.Empty;
/// Who changed it.
public string? UserName { get; set; }
/// The changed fields as JSON: {"field": {"old": ..., "new": ...}}.
public string? Diff { get; set; }
/// The trace of the request that made the change.
public string? TraceId { get; set; }
}
/// The outcome of a job run.
public enum JobOutcome
{
/// Still running.
Running = 0,
/// Completed.
Succeeded = 1,
/// Failed after the retries.
Failed = 2,
/// Stopped by the timeout.
TimedOut = 3,
/// Not started because another run held the lock.
Skipped = 4,
}
/// A scheduled task (sys_scheduled_tasks): a system job or an activity set up by administrators.
public sealed class ScheduledTask : Entity
{
/// The task code, unique, for example system.log-retention.
public string Code { get; set; } = string.Empty;
/// The name shown in the interface.
public string Name { get; set; } = string.Empty;
/// The job type that runs it.
public string JobType { get; set; } = string.Empty;
/// The company it works on; for the instance.
public string? CompanyCode { get; set; }
/// The cron expression (five fields).
public string Cron { get; set; } = string.Empty;
/// The time zone of the cron expression (IANA name).
public string TimeZone { get; set; } = "Europe/Rome";
/// The longest a run may last, in seconds.
public int TimeoutSeconds { get; set; } = 600;
/// How many times a failed run is retried.
public int MaxRetries { get; set; }
/// Whether the task runs.
public bool IsEnabled { get; set; } = true;
/// A system job, which administrators can suspend but not delete.
public bool IsSystem { get; set; }
/// The parameters as JSON.
public string? Parameters { get; set; }
/// The last start.
public DateTime? LastRunAt { get; set; }
/// The next planned start.
public DateTime? NextRunAt { get; set; }
/// The outcome of the last run.
public JobOutcome? LastOutcome { get; set; }
}
/// One run of a scheduled task (sys_job_runs).
public sealed class JobRun : IEntity
{
///
public long Id { get; set; }
/// The task code.
public string TaskCode { get; set; } = string.Empty;
/// When the run started (UTC).
public DateTime StartedAt { get; set; }
/// When the run ended.
public DateTime? FinishedAt { get; set; }
/// The outcome.
public JobOutcome Outcome { get; set; }
/// The attempt number, from 1.
public int Attempt { get; set; } = 1;
/// A short result or error message.
public string? Message { get; set; }
/// The trace identifier of the run, to find its log.
public string? TraceId { get; set; }
}
///
/// A lock held by a job run (sys_job_locks): a row per running task, with an expiry so that a process that died
/// does not block the task for ever.
///
public sealed class JobLock : IEntity
{
/// The lock key (the task code).
public string Id { get; set; } = string.Empty;
/// The process that holds the lock.
public string Owner { get; set; } = string.Empty;
/// When the lock was taken.
public DateTime AcquiredAt { get; set; }
/// When the lock lapses.
public DateTime ExpiresAt { get; set; }
}
/// The weight of a notification.
public enum NotificationSeverity
{
/// Information.
Info = 0,
/// Something completed.
Success = 1,
/// Needs attention.
Warning = 2,
/// Something failed.
Error = 3,
}
/// An in-app notification (SPEC §9; mockup 010).
public abstract class NotificationRecord : Entity
{
/// The recipient; for every user of the scope (all administrators, all users).
public long? RecipientUserId { get; set; }
/// The area: security, jobs, system, licenses...
public string Category { get; set; } = string.Empty;
/// The weight.
public NotificationSeverity Severity { get; set; }
/// The title.
public string Title { get; set; } = string.Empty;
/// The text.
public string? Body { get; set; }
/// A link inside the application.
public string? Link { get; set; }
/// When the recipient read it.
public DateTime? ReadAt { get; set; }
}
/// A notification for instance accounts (sys_notifications).
public sealed class SystemNotification : NotificationRecord;
/// A notification for company users (notifications).
public sealed class Notification : NotificationRecord;