"use strict";
/*
If the given public path is unlisted and has a license associated to it,
apply it to the given project.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const misc_1 = require("@cocalc/util/misc");
const public_path_1 = require("@cocalc/server/licenses/public-path");
const database_1 = require("@cocalc/database");
const get_params_1 = __importDefault(require("lib/api/get-params"));
const util_1 = require("@cocalc/server/projects/control/util");
async function handle(req, res) {
    const { public_path_id, project_id } = (0, get_params_1.default)(req);
    try {
        if (!(0, misc_1.isValidUUID)(project_id)) {
            throw Error("project_id must be a valid uuid");
        }
        if (public_path_id?.length != 40) {
            throw Error("public_path_id must be a sha1 hash");
        }
        const site_license_id = await (0, public_path_1.associatedLicense)(public_path_id);
        if (site_license_id) {
            // These are the only conditions under which we would apply a license.
            // Apply site_license_id to project_id.
            await (0, database_1.db)().add_license_to_project(project_id, site_license_id);
            (0, util_1.restartProjectIfRunning)(project_id);
        }
        res.json({});
    }
    catch (err) {
        res.json({ error: err.message });
    }
}
exports.default = handle;
//# sourceMappingURL=public-path-license.js.map