sendou.ink/app/features/team/actions/t.$customUrl.index.server.ts
Kalle c4c0b6dd50
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Migrate repository functions to use user id via ALS
2026-06-06 14:56:43 +03:00

71 lines
1.8 KiB
TypeScript

import type { ActionFunction } from "react-router";
import { redirect } from "react-router";
import { requireUser } from "~/features/auth/core/user.server";
import {
errorToastIfFalsy,
notFoundIfFalsy,
parseRequestPayload,
} from "~/utils/remix.server";
import { assertUnreachable } from "~/utils/types";
import * as TeamRepository from "../TeamRepository.server";
import {
teamParamsSchema,
teamProfilePageActionSchema,
} from "../team-schemas.server";
import { isTeamMember, isTeamOwner, resolveNewOwner } from "../team-utils";
export const action: ActionFunction = async ({ request, params }) => {
const user = requireUser();
const data = await parseRequestPayload({
request,
schema: teamProfilePageActionSchema,
});
const { customUrl } = teamParamsSchema.parse(params);
const team = notFoundIfFalsy(await TeamRepository.findByCustomUrl(customUrl));
switch (data._action) {
case "LEAVE_TEAM": {
errorToastIfFalsy(
isTeamMember({ user, team }),
"You are not a member of this team",
);
const newOwner = isTeamOwner({ user, team })
? resolveNewOwner(team.members)
: null;
errorToastIfFalsy(
!isTeamOwner({ user, team }) || newOwner,
"You can't leave the team if you are the owner and there is no other member to become the owner",
);
await TeamRepository.handleMemberLeaving({
teamId: team.id,
userId: user.id,
newOwnerUserId: newOwner?.id,
});
break;
}
case "MAKE_MAIN_TEAM": {
await TeamRepository.switchOwnMainTeam(team.id);
break;
}
case "DELETE_TEAM": {
errorToastIfFalsy(
isTeamOwner({ user, team }) || user.roles.includes("ADMIN"),
"You are not the team owner",
);
await TeamRepository.del(team.id);
throw redirect("/");
}
default: {
assertUnreachable(data);
}
}
return null;
};