.ts
TypeScript
(application/typescript)
// 1st-party
import type { ReqHandler } from "@ethicdevs/react-monolith";
// app
import { AppRoute, AppRouteParams } from "../../routes.defs";
// app services
import { makeOrganizationService } from "../../services/organization";
import { makeRepositoryService } from "../../services/repository";
import { makeUsersService } from "../../services/user";
// app views
import RepositoryForkView, {
  RepositoryForkViewProps,
} from "../../views/repository/RepositoryForkView";

type RouteParams = AppRouteParams[AppRoute.REPOSITORY_FORK];

const getRepositoryForkView: ReqHandler = async (request, reply) => {
  if (
    request.session.data.authenticated === false ||
    request.session.data.curr_user_uid == null
  ) {
    reply.redirect(302, request.namedViewsPathMap[AppRoute.AUTH_LOGIN]);
    return reply;
  }

  const { orgSlug, repoSlug } = request.params as RouteParams["params"];
  // const {} = request.body as RouteParams["body"];

  const reqHandler = reply.makeRequestHandler(request, reply);
  const organizationService = makeOrganizationService({ request });
  const repoService = makeRepositoryService({ request });
  const usersService = makeUsersService({ request });

  const sourceParentOrg = await organizationService.getOrganizationBySlug(
    orgSlug
  );

  if (sourceParentOrg == null) {
    return reply.status(404).callNotFound();
  }

  const sourceRepo = await repoService.getRepository(orgSlug, repoSlug);

  if (sourceRepo == null) {
    return reply.status(404).callNotFound();
  }

  let availableParentOrgs = await usersService.getUserOrganizations(
    request.session.data.curr_user_uid
  );

  let isForkable: boolean = true;
  let errorMessage: null | string = null;

  if (availableParentOrgs.length <= 0) {
    /**
     * TODO(user_experience, error_recovery, bad_state):
     * @mission: Analyse/design a solution how to solve this.
     * @context: User may have transformed its PERSONAL Organization into
     * a COMPANY Organization and this organization ownership could have
     * transferred to another user by the previous current user, which may leave
     * us/it in a state where no parent organisations are available for forking.
     */
    isForkable = false;
    errorMessage =
      "You do not own any organization. Please contact the support so we fix your account (contact@gitfoss.io)";
  }

  return reqHandler<RepositoryForkViewProps>(RepositoryForkView.name, {
    availableParentOrgs,
    errorMessage,
    isForkable,
    initialValues: {
      target_repo_display_name: sourceRepo.displayName,
      target_repo_slug: sourceRepo.slug,
      target_repo_visibility: sourceRepo.visibility,
    },
    sourceRepo,
    sourceParentOrg,
  });
};

export default getRepositoryForkView;