diff --git a/app/features/sendouq-match/ReportedWeaponRepository.server.ts b/app/features/sendouq-match/ReportedWeaponRepository.server.ts index 281207cf1..4423274c3 100644 --- a/app/features/sendouq-match/ReportedWeaponRepository.server.ts +++ b/app/features/sendouq-match/ReportedWeaponRepository.server.ts @@ -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"][], diff --git a/app/features/sendouq-match/actions/q.match.$id.server.ts b/app/features/sendouq-match/actions/q.match.$id.server.ts index bd0ab3cf1..d034f0242 100644 --- a/app/features/sendouq-match/actions/q.match.$id.server.ts +++ b/app/features/sendouq-match/actions/q.match.$id.server.ts @@ -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": { diff --git a/app/features/sendouq-match/components/SendouQMatchActionTab.tsx b/app/features/sendouq-match/components/SendouQMatchActionTab.tsx index a6df614dc..ce129c226 100644 --- a/app/features/sendouq-match/components/SendouQMatchActionTab.tsx +++ b/app/features/sendouq-match/components/SendouQMatchActionTab.tsx @@ -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" }, ); diff --git a/app/features/sendouq-match/q-match-schemas.ts b/app/features/sendouq-match/q-match-schemas.ts index d374a96aa..4e6430f07 100644 --- a/app/features/sendouq-match/q-match-schemas.ts +++ b/app/features/sendouq-match/q-match-schemas.ts @@ -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"),