Simplify action

This commit is contained in:
Kalle
2026-04-15 18:27:23 +03:00
parent b6851288f3
commit 11107d8bd8
4 changed files with 29 additions and 54 deletions

View File

@@ -11,6 +11,23 @@ export function createMany(
return (trx ?? db).insertInto("ReportedWeapon").values(weapons).execute();
}
export async function upsertOne({
groupMatchMapId,
userId,
weaponSplId,
}: TablesInsertable["ReportedWeapon"] & { groupMatchMapId: number }) {
await db
.deleteFrom("ReportedWeapon")
.where("groupMatchMapId", "=", groupMatchMapId)
.where("userId", "=", userId)
.execute();
await db
.insertInto("ReportedWeapon")
.values({ groupMatchMapId, userId, weaponSplId })
.execute();
}
export async function replaceByMatchId(
matchId: number,
weapons: TablesInsertable["ReportedWeapon"][],

View File

@@ -1,6 +1,5 @@
import type { ActionFunctionArgs } from "react-router";
import { redirect } from "react-router";
import type { ReportedWeapon } from "~/db/tables";
import { requireUser } from "~/features/auth/core/user.server";
import * as ChatSystemMessage from "~/features/chat/ChatSystemMessage.server";
import * as RoomLinkRepository from "~/features/chat/RoomLinkRepository.server";
@@ -26,7 +25,6 @@ import {
} from "~/utils/remix.server";
import { assertUnreachable } from "~/utils/types";
import { SENDOUQ_PREPARING_PAGE, sendouQMatchPage } from "~/utils/urls";
import { mergeReportedWeapons } from "../core/reported-weapons.server";
import { matchSchema, qMatchPageParamsSchema } from "../q-match-schemas";
export const action = async ({ request, params }: ActionFunctionArgs) => {
@@ -135,8 +133,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
throw redirect(SENDOUQ_PREPARING_PAGE);
}
// xxx: why not REPORT_WEAPON
case "REPORT_WEAPONS": {
case "REPORT_WEAPON": {
const match = notFoundIfFalsy(await SQMatchRepository.findById(matchId));
const members = [
@@ -148,26 +145,12 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
"User is not a member of any group",
);
const oldReportedWeapons =
(await ReportedWeaponRepository.findByMatchId(matchId)) ?? [];
const mergedWeapons = mergeReportedWeapons({
oldWeapons: oldReportedWeapons,
newWeapons: data.weapons as (ReportedWeapon & {
mapIndex: number;
groupMatchMapId: number;
})[],
await ReportedWeaponRepository.upsertOne({
groupMatchMapId: data.groupMatchMapId,
userId: user.id,
weaponSplId: data.weaponSplId,
});
await ReportedWeaponRepository.replaceByMatchId(
matchId,
mergedWeapons.map((w) => ({
groupMatchMapId: w.groupMatchMapId,
userId: w.userId,
weaponSplId: w.weaponSplId,
})),
);
break;
}
case "UNDO_WEAPON_REPORT": {

View File

@@ -185,15 +185,9 @@ export function SendouQMatchActionTab({
const map = data.match.mapList[mapIndex];
weaponFetcher.submit(
{
_action: "REPORT_WEAPONS",
weapons: JSON.stringify([
{
weaponSplId,
userId: ownUserId,
mapIndex,
groupMatchMapId: map.id,
},
]),
_action: "REPORT_WEAPON",
weaponSplId: String(weaponSplId),
groupMatchMapId: String(map.id),
},
{ method: "post" },
);

View File

@@ -1,27 +1,7 @@
import { z } from "zod";
import { SENDOUQ } from "~/features/sendouq/q-constants";
import {
_action,
falsyToNull,
id,
safeJSONParse,
weaponSplId,
} from "~/utils/zod";
import { _action, falsyToNull, id, weaponSplId } from "~/utils/zod";
const weapons = z.preprocess(
safeJSONParse,
z
.array(
z.object({
weaponSplId,
userId: id,
mapIndex: z.number().int().nonnegative(),
groupMatchMapId: id,
}),
)
.optional()
.default([]),
);
export const matchSchema = z.union([
z.object({
_action: _action("REPORT_SCORE"),
@@ -33,8 +13,9 @@ export const matchSchema = z.union([
previousGroupId: id,
}),
z.object({
_action: _action("REPORT_WEAPONS"),
weapons,
_action: _action("REPORT_WEAPON"),
weaponSplId,
groupMatchMapId: id,
}),
z.object({
_action: _action("ADD_PRIVATE_USER_NOTE"),