import { __awaiter } from "tslib";
import { QoreClient } from "@qorebase/client";
import { qoreConfig } from "../../app/hooks/qoreStorageClient";
export default function handler(req, res) {
    return __awaiter(this, void 0, void 0, function* () {
        const { method, body: { appId, projectId }, } = req;
        if (method !== "POST") {
            res.setHeader("Allow", ["POST"]);
            res.status(405).json(`Method ${method} Not Allowed`);
            return;
        }
        const client = new QoreClient(qoreConfig);
        client.project.axios.defaults.headers["x-qore-engine-admin-secret"] =
            qoreConfig.adminSecret;
        try {
            if (!appId || !projectId)
                throw new Error("app id and project id required");
            const { data: app } = yield client
                .table("revisions")
                .readRows({
                condition: {
                    apprevision: appId,
                },
                orderBy: { updated_at: "DESC" },
                limit: 1,
            })
                .toPromise();
            const stgConfigId = app.nodes[0].data.config.id;
            const { data: prodApp } = yield client
                .table("applications")
                .readRows({
                condition: {
                    status: "published",
                    app_environment: { name: "production", project: projectId },
                },
            })
                .toPromise();
            const prodConfigIds = yield Promise.all(prodApp.nodes.map((app) => __awaiter(this, void 0, void 0, function* () {
                const { data: getRevision } = yield client
                    .table("revisions")
                    .readRows({
                    limit: 1,
                    orderBy: { updated_at: "DESC" },
                    condition: {
                        apprevision: app.id,
                    },
                })
                    .toPromise();
                return getRevision.nodes[0].data.config.id || "";
            })));
            res.status(200).json({
                status: prodConfigIds.includes(stgConfigId),
            });
        }
        catch (error) {
            res.status(400).json({ message: "failed get restore status" });
        }
    });
}
//# sourceMappingURL=restore-status.js.map