mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import type { ActionFunction } from "@remix-run/node";
|
|
import { requireUserId } from "~/features/auth/core/user.server";
|
|
import { notFoundIfFalsy, parseRequestPayload, validate } from "~/utils/remix";
|
|
import { assertUnreachable } from "~/utils/types";
|
|
import * as TeamRepository from "../TeamRepository.server";
|
|
import {
|
|
teamParamsSchema,
|
|
teamProfilePageActionSchema,
|
|
} from "../team-schemas.server";
|
|
import { isTeamMember, isTeamOwner } from "../team-utils";
|
|
|
|
export const action: ActionFunction = async ({ request, params }) => {
|
|
const user = await requireUserId(request);
|
|
const data = await parseRequestPayload({
|
|
request,
|
|
schema: teamProfilePageActionSchema,
|
|
});
|
|
|
|
const { customUrl } = teamParamsSchema.parse(params);
|
|
const team = notFoundIfFalsy(await TeamRepository.findByCustomUrl(customUrl));
|
|
|
|
validate(
|
|
isTeamMember({ user, team }) && !isTeamOwner({ user, team }),
|
|
"You are not a regular member of this team",
|
|
);
|
|
|
|
switch (data._action) {
|
|
case "LEAVE_TEAM": {
|
|
await TeamRepository.removeTeamMember({
|
|
teamId: team.id,
|
|
userId: user.id,
|
|
});
|
|
break;
|
|
}
|
|
case "MAKE_MAIN_TEAM": {
|
|
await TeamRepository.switchMainTeam({
|
|
userId: user.id,
|
|
teamId: team.id,
|
|
});
|
|
break;
|
|
}
|
|
default: {
|
|
assertUnreachable(data);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|