import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import ApexDomain from "../../../common/ApexDomain.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import EmailAddressService from "../EmailAddressService.js"; import ClusterDnsCache from "./ClusterDnsCache.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; @RegisterScoped export default class WebHostDnsService { @Inject private db: SocialMailContext; @Inject private eas: EmailAddressService; @Inject private clusterDnsCache: ClusterDnsCache; async update(host: string, folderID: number) { const { db } = this; const existing = await db.hostedZoneRecords .where({ host }, (x, p) => x.fqdn === p.host && x.type === "A" || x.type === "AAAA" || x.type === "CNAME") .orderBy((x) => (x.type === "A") ? 0 : (x.type === "CNAME" ? 1 : 2)) .first(); if(existing) { await db.websiteFolderHosts.where({ host }, (x, p) => x.host === p.host) .update(existing, (x, p) => ({ hostedZoneRecordID: Sql.cast.asBigInt(p.recordID) })); return; } const zone = await ApexDomain.getApexDomain(host); const zoneID = await this.eas.getOrCreateDomain(zone.domain); const now = DateTime.now; await db.hostedZones.withKeys({ zoneID }) .selectOrInsert({ dateUpdated: now }); const cr = await db.hostedZoneRecords.withKeys({ zoneID, type: "CNAME", name: zone.subdomain || "@", }).updateOrInsert({ fqdn: host, value: "$HOST$", dateCreated: now, dateUpdated: now, folderID, ttl: 8600 }); const { recordID } = cr; await db.websiteFolderHosts.where({ host, recordID }, (x, p) => x.host === p.host) .update({ recordID }, (x, p) => ({ hostedZoneRecordID: Sql.cast.asBigInt(p.recordID) })); // sync to parent... await this.clusterDnsCache.save(cr); } }