GitFOSS
.ts
TypeScript
(application/typescript)
// 1st-party
import type { ReqHandler } from "@ethicdevs/react-monolith";
// app
import { AppRoute } from "../../routes.defs";
import { makeUsersService } from "../../services/user";
// app views
import RepositoryCreateView, {
  RepositoryCreateViewProps,
} from "../../views/repository/RepositoryCreateView";

const getRepositoryCreateView: 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 reqHandler = reply.makeRequestHandler(request, reply);
  const usersService = makeUsersService({ request });

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

  if (availableParentOrgs.length <= 0) {
    /**
     * TODO(ux): Decide 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 create.
     */
  }

  return reqHandler<RepositoryCreateViewProps>(RepositoryCreateView.name, {
    availableParentOrgs,
  });
};

export default getRepositoryCreateView;