Import source: backend/src/lib/serializers
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
import type {
|
||||
Database,
|
||||
DnsRecord,
|
||||
Domain,
|
||||
Mailbox,
|
||||
Permission,
|
||||
ResourceUsage,
|
||||
Server,
|
||||
Session,
|
||||
User,
|
||||
UserPermission,
|
||||
Website,
|
||||
} from "@prisma/client";
|
||||
|
||||
export function iso(value: Date | null | undefined) {
|
||||
return value?.toISOString() ?? new Date(0).toISOString();
|
||||
}
|
||||
|
||||
export function toRuntime(value: Website["runtime"]) {
|
||||
if (value === "Node_js") return "Node.js";
|
||||
if (value === "Reverse_proxy") return "Reverse proxy";
|
||||
if (value === "Custom_service") return "Custom service";
|
||||
return value;
|
||||
}
|
||||
|
||||
export function fromRuntime(value: string | undefined) {
|
||||
if (!value) return "PHP";
|
||||
if (value === "Node.js") return "Node_js";
|
||||
if (value === "Reverse proxy") return "Reverse_proxy";
|
||||
if (value === "Custom service") return "Custom_service";
|
||||
return value;
|
||||
}
|
||||
|
||||
export function toPermission(value: Permission) {
|
||||
return value.replaceAll("_", ":");
|
||||
}
|
||||
|
||||
export function fromPermission(value: string) {
|
||||
return value.replaceAll(":", "_") as Permission;
|
||||
}
|
||||
|
||||
export function userDto(user: User & { permissions?: UserPermission[] }) {
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
permissions: (user.permissions ?? []).map((entry) => toPermission(entry.permission)),
|
||||
lastLogin: iso(user.lastLogin),
|
||||
avatarInitials: user.avatarInitials,
|
||||
twoFactorEnabled: user.twoFactorEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
export function websiteDto(website: Website) {
|
||||
return {
|
||||
id: website.id,
|
||||
name: website.name,
|
||||
domain: website.domain,
|
||||
runtime: toRuntime(website.runtime),
|
||||
status: website.status,
|
||||
ssl: website.ssl,
|
||||
repository: website.repository ?? undefined,
|
||||
environment: website.environment,
|
||||
cpuLimit: website.cpuLimit,
|
||||
memoryLimit: website.memoryLimit,
|
||||
lastDeployment: iso(website.lastDeployment),
|
||||
};
|
||||
}
|
||||
|
||||
export function domainDto(domain: Domain) {
|
||||
return {
|
||||
id: domain.id,
|
||||
name: domain.name,
|
||||
type: domain.type,
|
||||
documentRoot: domain.documentRoot,
|
||||
sslStatus: domain.sslStatus,
|
||||
dnssec: domain.dnssec,
|
||||
propagation: domain.propagation,
|
||||
};
|
||||
}
|
||||
|
||||
export function dnsRecordDto(record: DnsRecord) {
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
ttl: record.ttl,
|
||||
class: "IN" as const,
|
||||
type: record.type,
|
||||
record: record.record,
|
||||
priority: record.priority ?? undefined,
|
||||
warning: record.warning ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function serverDto(server: Server) {
|
||||
return {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
hostname: server.hostname,
|
||||
os: server.os,
|
||||
panelVersion: server.panelVersion,
|
||||
load: [server.load1, server.load5, server.load15] as [number, number, number],
|
||||
ipAddress: server.ipAddress,
|
||||
status: server.status,
|
||||
location: server.location,
|
||||
};
|
||||
}
|
||||
|
||||
export function databaseDto(database: Database & { users?: Array<{ username: string }> }) {
|
||||
return {
|
||||
id: database.id,
|
||||
name: database.name,
|
||||
engine: database.engine,
|
||||
size: Number(database.size),
|
||||
users: (database.users ?? []).map((entry) => entry.username),
|
||||
tables: database.tables,
|
||||
connections: database.connections,
|
||||
lastBackup: iso(database.lastBackup),
|
||||
};
|
||||
}
|
||||
|
||||
export function mailboxDto(mailbox: Mailbox & { domain?: Domain }) {
|
||||
return {
|
||||
id: mailbox.id,
|
||||
address: mailbox.address,
|
||||
domain: mailbox.domain?.name ?? mailbox.address.split("@")[1] ?? "",
|
||||
used: Number(mailbox.used),
|
||||
quota: Number(mailbox.quota),
|
||||
lastLogin: iso(mailbox.lastLogin),
|
||||
forwarding: mailbox.forwarding,
|
||||
spamScore: mailbox.spamScore,
|
||||
authStatus: mailbox.authStatus,
|
||||
};
|
||||
}
|
||||
|
||||
export function resourceUsageDto(resource: ResourceUsage) {
|
||||
return {
|
||||
key: resource.key,
|
||||
label: resource.label,
|
||||
used: resource.used,
|
||||
limit: resource.limit,
|
||||
unit: resource.unit,
|
||||
status: resource.status,
|
||||
};
|
||||
}
|
||||
|
||||
export type SessionContext = Session & {
|
||||
user: User & { permissions: UserPermission[]; accounts: Array<{ accountId: string }> };
|
||||
};
|
||||
Reference in New Issue
Block a user