Repository: louislam/uptime-kuma
File: server/model/domain_expiry.js (JavaScript)

Code before:
        }

        const rdap = await getRdapServer(tld.publicSuffix);
        if (!rdap) {
            // Only warn when the monitor actually has domain expiry notifications enabled.
            // The edit monitor page calls this method frequently while the user is typing.
            if (Boolean(monitor.domainExpiryNotification)) {
                log.warn("domain_expiry", `Domain expiry unsupported for '.${tld.publicSuffix}' because its RDAP endpoint is not listed in the IANA database.`);
            }
            throw new TranslatableError("domain_expiry_unsupported_unsupported_tld_no_rdap_endpoint", { publicSuffix: tld.publicSuffix });
        }

        return {
            domain: tld.domain,
            tld: tld.publicSuffix,
        };
    }

    /**
     * @param {string} domainName Domain name
     * @returns {Promise<DomainExpiry>} Domain expiry bean
     */
    static async findByDomainNameOrCreate(domainName) {
        const domain = await DomainExpiry.findByName(domainName);
        if (!domain && domainName) {
            domain = await DomainExpiry.createByName(tld.domain);
        }
        [ "expiry", "lastCheck" ].forEach((key) => {
            if (typeof domain[key] === "string") {
                domain[key] = new Date(domain[key].replace(" ", "T"));
            } else if (typeof domain[key] === "number") {
                domain[key] = new Date(domain[key]);
            }
        });
        return domain;
    }

    /**
     * @returns {number} number of days remaining before expiry
     */
    get daysRemaining() {
        return getDaysRemaining(new Date(), new Date(this.expiry));
    }

    /**
     * @returns {Promise<(Date|null)>} Expiry date from RDAP
     */
    async getExpiryDate() {
        return getRdapDomainExpiryDate(this.domain);
    }


Review comment (suggestion):
```suggestion
            domain = await DomainExpiry.createByName(domainName);
```

Code after:
                );
            }
            throw new TranslatableError("domain_expiry_unsupported_unsupported_tld_no_rdap_endpoint", {
                publicSuffix: tld.publicSuffix,
            });
        }

        return {
            domain: tld.domain,
            tld: tld.publicSuffix,
        };
    }

    /**
     * @param {string} domainName Domain name
     * @returns {Promise<DomainExpiry>} Domain expiry bean
     */
    static async findByDomainNameOrCreate(domainName) {
        let domain = await DomainExpiry.findByName(domainName);
        if (!domain && domainName) {
            domain = await DomainExpiry.createByName(domainName);
        }
        return domain;
    }

    /**
     * @returns {number} number of days remaining before expiry
     */
    get daysRemaining() {
        return dayjs.utc(this.expiry).diff(dayjs.utc(), "day");
    }

    /**
     * @returns {Promise<(Date|null)>} Expiry date from RDAP
     */
    async getExpiryDate() {
        return getRdapDomainExpiryDate(this.domain);
    }

    /**
     * @param {string} domainName Monitor object
     * @throws {TranslatableError} If the domain is not supported
     * @returns {Promise<Date | undefined>} the expiry date
     */
    static async checkExpiry(domainName) {
        let bean = await DomainExpiry.findByDomainNameOrCreate(domainName);
        let expiryDate;

        if (bean?.lastCheck && dayjs.utc(bean.lastCheck).diff(dayjs.utc(), "day") < 1) {
            log.debug("domain_expiry", `Domain expiry already checked recently for ${bean.domain}, won't re-check.`);
            return bean.expiry;