Skip to content

Commit

Permalink
Fix main team switch handling when leaving/deleting team + add second…
Browse files Browse the repository at this point in the history
…ary team tests
  • Loading branch information
Sendouc committed Oct 9, 2024
1 parent 182d2fa commit 0932e8a
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 34 deletions.
1 change: 1 addition & 0 deletions app/db/seed/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const ADMIN_TEST_AVATAR = "6fc41a44b069a0d2152ac06d1e496c6c";
export const NZAP_TEST_DISCORD_ID = "455039198672453645";
export const NZAP_TEST_AVATAR = "f809176af93132c3db5f0a5019e96339"; // https://cdn.discordapp.com/avatars/455039198672453645/f809176af93132c3db5f0a5019e96339.webp?size=160
export const NZAP_TEST_ID = 2;
export const REGULAR_USER_TEST_ID = 2;

export const AMOUNT_OF_CALENDAR_EVENTS = 200;
11 changes: 7 additions & 4 deletions app/features/lfg/LFGRepository.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sub } from "date-fns";
import { type NotNull, sql } from "kysely";
import { type NotNull, type Transaction, sql } from "kysely";
import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
import { db } from "~/db/sql";
import type { TablesInsertable } from "~/db/tables";
import type { DB, TablesInsertable } from "~/db/tables";
import { databaseTimestampNow, dateToDatabaseTimestamp } from "~/utils/dates";
import { COMMON_USER_FIELDS } from "~/utils/kysely.server";
import { LFG } from "./lfg-constants";
Expand Down Expand Up @@ -143,6 +143,9 @@ export function deletePost(id: number) {
return db.deleteFrom("LFGPost").where("id", "=", id).execute();
}

export function deletePostsByTeamId(teamId: number) {
return db.deleteFrom("LFGPost").where("teamId", "=", teamId).execute();
export function deletePostsByTeamId(teamId: number, trx?: Transaction<DB>) {
return (trx ?? db)
.deleteFrom("LFGPost")
.where("teamId", "=", teamId)
.execute();
}
11 changes: 6 additions & 5 deletions app/features/sendouq/routes/q.looking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ describe("Private user note sorting", () => {
});
const matchAction = wrappedAction<typeof matchSchema>({
action: rawMatchAction,
params: { id: "1" },
});

const matchActionParams = { id: "1" };

test("users with positive note sorted first", async () => {
await matchAction(
{
Expand All @@ -215,7 +216,7 @@ describe("Private user note sorting", () => {
sentiment: "POSITIVE",
comment: "test",
},
{ user: "admin" },
{ user: "admin", params: matchActionParams },
);

const data = await lookingLoader({ user: "admin" });
Expand All @@ -231,7 +232,7 @@ describe("Private user note sorting", () => {
sentiment: "NEGATIVE",
comment: "test",
},
{ user: "admin" },
{ user: "admin", params: matchActionParams },
);

const data = await lookingLoader({ user: "admin" });
Expand All @@ -249,7 +250,7 @@ describe("Private user note sorting", () => {
sentiment: "POSITIVE",
comment: "test",
},
{ user: "admin" },
{ user: "admin", params: matchActionParams },
);
await matchAction(
{
Expand All @@ -258,7 +259,7 @@ describe("Private user note sorting", () => {
sentiment: "NEGATIVE",
comment: "test",
},
{ user: "admin" },
{ user: "admin", params: matchActionParams },
);

const data = await lookingLoader({ user: "admin" });
Expand Down
52 changes: 51 additions & 1 deletion app/features/team/TeamRepository.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { nanoid } from "nanoid";
import { INVITE_CODE_LENGTH } from "~/constants";
import { db } from "~/db/sql";
import type { DB, Tables } from "~/db/tables";
import * as LFGRepository from "~/features/lfg/LFGRepository.server";
import { databaseTimestampNow } from "~/utils/dates";
import invariant from "~/utils/invariant";
import { COMMON_USER_FIELDS } from "~/utils/kysely.server";
Expand Down Expand Up @@ -107,6 +108,7 @@ export async function teamsByMemberUserId(
.select([
"TeamMemberWithSecondary.teamId as id",
"TeamMemberWithSecondary.isOwner",
"TeamMemberWithSecondary.isMainTeam",
])
.where("userId", "=", userId)
.execute();
Expand Down Expand Up @@ -200,6 +202,52 @@ export function switchMainTeam({
});
}

export function del(teamId: number) {
return db.transaction().execute(async (trx) => {
const members = await trx
.selectFrom("TeamMember")
.select(["TeamMember.userId"])
.where("teamId", "=", teamId)
.execute();

// switch main team to another if they at least one secondary team
for (const member of members) {
const currentTeams = await teamsByMemberUserId(member.userId, trx);

const teamToSwitchTo = currentTeams.find((team) => team.id !== teamId);

if (!teamToSwitchTo) continue;

await trx
.updateTable("AllTeamMember")
.set({
isMainTeam: 1,
})
.where("userId", "=", member.userId)
.where("teamId", "=", teamToSwitchTo.id)
.execute();
}

await trx
.updateTable("AllTeamMember")
.set({
isMainTeam: 0,
})
.where("AllTeamMember.teamId", "=", teamId)
.execute();

await LFGRepository.deletePostsByTeamId(teamId, trx);

await trx
.updateTable("AllTeam")
.set({
deletedAt: databaseTimestampNow(),
})
.where("id", "=", teamId)
.execute();
});
}

export function addNewTeamMember({
userId,
teamId,
Expand Down Expand Up @@ -245,8 +293,9 @@ export function removeTeamMember({
invariant(teamToLeave, "User is not a member of this team");
invariant(!teamToLeave.isOwner, "Owner cannot leave the team");

const wasMainTeam = teamToLeave.isMainTeam;
const newMainTeam = currentTeams.find((team) => team.id !== teamId);
if (newMainTeam) {
if (wasMainTeam && newMainTeam) {
await trx
.updateTable("AllTeamMember")
.set({
Expand All @@ -261,6 +310,7 @@ export function removeTeamMember({
.updateTable("AllTeamMember")
.set({
leftAt: databaseTimestampNow(),
isMainTeam: 0,
})
.where("userId", "=", userId)
.where("teamId", "=", teamId)
Expand Down
2 changes: 2 additions & 0 deletions app/features/team/actions/t.$customUrl.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export const action: ActionFunction = async ({ request, params }) => {
teamId: team.id,
userId: user.id,
});

break;
}
case "MAKE_MAIN_TEAM": {
await TeamRepository.switchMainTeam({
userId: user.id,
teamId: team.id,
});

break;
}
default: {
Expand Down
11 changes: 0 additions & 11 deletions app/features/team/queries/deleteTeam.server.ts

This file was deleted.

5 changes: 1 addition & 4 deletions app/features/team/routes/t.$customUrl.edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { Label } from "~/components/Label";
import { Main } from "~/components/Main";
import { SubmitButton } from "~/components/SubmitButton";
import { requireUserId } from "~/features/auth/core/user.server";
import * as LFGRepository from "~/features/lfg/LFGRepository.server";
import { isAdmin } from "~/permissions";
import {
type SendouRouteHandle,
Expand All @@ -36,7 +35,6 @@ import {
uploadImagePage,
} from "~/utils/urls";
import * as TeamRepository from "../TeamRepository.server";
import { deleteTeam } from "../queries/deleteTeam.server";
import { TEAM } from "../team-constants";
import { editTeamSchema, teamParamsSchema } from "../team-schemas.server";
import { canAddCustomizedColors, isTeamOwner } from "../team-utils";
Expand Down Expand Up @@ -89,8 +87,7 @@ export const action: ActionFunction = async ({ request, params }) => {

switch (data._action) {
case "DELETE": {
await LFGRepository.deletePostsByTeamId(team.id);
deleteTeam(team.id);
await TeamRepository.del(team.id);

throw redirect(TEAM_SEARCH_PAGE);
}
Expand Down
Loading

0 comments on commit 0932e8a

Please sign in to comment.