import { isValidUUID } from "@cocalc/util/misc";
import { associatedLicense } from "@cocalc/server/licenses/public-path";
import getParams from "lib/api/get-params";
import { restartProjectIfRunning } from "@cocalc/server/projects/control/util";
import addLicenseToProject from "@cocalc/server/licenses/add-to-project";
export default async function handle(req, res) {
const { public_path_id, project_id } = getParams(req);
try {
if (!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 associatedLicense(public_path_id);
if (site_license_id) {
await addLicenseToProject({ project_id, license_id: site_license_id });
restartProjectIfRunning(project_id);
}
res.json({});
} catch (err) {
res.json({ error: err.message });
}
}