import OpenSourceDeployment from "../../Models/DatabaseModels/OpenSourceDeployment";
import URL from "../../Types/API/URL";
import API from "../../Utils/API";
import OpenSourceDeploymentService from "../Services/OpenSourceDeploymentService";
import { OpenSourceDeploymentWebhookUrl } from "../EnvironmentConfig";
import logger, { getLogAttributesFromRequest } from "../Utils/Logger";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
export default class OpenSourceDeploymentAPI extends BaseAPI {
    constructor() {
        var _a;
        super(OpenSourceDeployment, OpenSourceDeploymentService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/register`, async (req, res, next) => {
            var _a, _b, _c, _d, _e, _f;
            try {
                const body = req.body;
                const deployment = new OpenSourceDeployment();
                deployment.email = body["email"] || "";
                deployment.name = body["name"] || "";
                deployment.companyName = body["companyName"] || "";
                deployment.companyPhoneNumber =
                    body["companyPhoneNumber"] || "";
                deployment.oneuptimeVersion =
                    body["oneuptimeVersion"] || "unknown";
                deployment.instanceUrl = body["instanceUrl"] || "";
                // Skip localhost instances - these are default/unconfigured deployments.
                if (deployment.instanceUrl === "http://localhost/" ||
                    deployment.instanceUrl === "http://localhost") {
                    return Response.sendEmptySuccessResponse(req, res);
                }
                await OpenSourceDeploymentService.create({
                    data: deployment,
                    props: {
                        isRoot: true,
                    },
                });
                if (OpenSourceDeploymentWebhookUrl) {
                    API.post({
                        url: URL.fromString(OpenSourceDeploymentWebhookUrl),
                        data: {
                            email: ((_a = deployment.email) === null || _a === void 0 ? void 0 : _a.toString()) || "",
                            name: ((_b = deployment.name) === null || _b === void 0 ? void 0 : _b.toString()) || "",
                            companyName: ((_c = deployment.companyName) === null || _c === void 0 ? void 0 : _c.toString()) || "",
                            companyPhoneNumber: ((_d = deployment.companyPhoneNumber) === null || _d === void 0 ? void 0 : _d.toString()) || "",
                            oneuptimeVersion: ((_e = deployment.oneuptimeVersion) === null || _e === void 0 ? void 0 : _e.toString()) || "",
                            instanceUrl: ((_f = deployment.instanceUrl) === null || _f === void 0 ? void 0 : _f.toString()) || "",
                        },
                    }).catch((err) => {
                        logger.error(err, getLogAttributesFromRequest(req));
                    });
                }
                return Response.sendEmptySuccessResponse(req, res);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
//# sourceMappingURL=OpenSourceDeploymentAPI.js.map