API: adres strukturalny w endpoints /gyms (REAL wymaga ulicy/kodu/miasta, location derivowane, merge przy PATCH)

This commit is contained in:
Tilumca
2026-10-15 00:34:12 +02:00
parent 5db1c7ebbc
commit c56d9d12e6
5 changed files with 133 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
import type { PrismaClient } from '@prisma/client';
import { badRequest, conflict, forbidden, notFound } from '../../lib/errors.js';
import { formatAddress } from '../../lib/address.js';
import { isPrismaNotFound } from '../../lib/prisma.js';
import { removeGymClaimFiles, removeGymPhotoFiles } from '../../lib/uploads.js';
import { toPrismaAvailability, type Availability } from '../me.js';
@@ -11,6 +12,12 @@ export type GymKind = 'VIRTUAL' | 'REAL';
export interface CreateGymInput {
name: string;
location?: string | null;
/** Adres strukturalny — wymagany dla REAL; `location` jest z niego derivowany. */
addressLine1?: string | null;
addressLine2?: string | null;
postalCode?: string | null;
city?: string | null;
countryCode?: string | null;
availability?: Availability | null;
capacity?: number | null;
kind?: GymKind;
@@ -20,6 +27,11 @@ export interface CreateGymInput {
export interface UpdateGymInput {
name?: string;
location?: string | null;
addressLine1?: string | null;
addressLine2?: string | null;
postalCode?: string | null;
city?: string | null;
countryCode?: string | null;
availability?: Availability | null;
capacity?: number | null;
isPrivate?: boolean;
@@ -193,10 +205,26 @@ export async function createGym(
throw badRequest('Location is required for virtual gyms');
const isPrivate = data.isPrivate ?? false;
if (kind === 'REAL' && isPrivate) throw badRequest('Real gyms are always public');
// REAL: adres strukturalny wymagany, display string `location` liczony z adresu (jak w panelu admina).
const hasAddress = !!(data.addressLine1?.trim() && data.postalCode?.trim() && data.city?.trim());
if (kind === 'REAL' && !hasAddress)
throw badRequest('Address (street, postal code, city) is required for real gyms');
const location = kind === 'REAL' ? formatAddress(data) : data.location ?? null;
const address =
kind === 'REAL'
? {
addressLine1: data.addressLine1!.trim(),
addressLine2: data.addressLine2?.trim() || null,
postalCode: data.postalCode!.trim(),
city: data.city!.trim(),
countryCode: (data.countryCode ?? 'PL').toUpperCase(),
}
: {};
const gym = await prisma.gym.create({
data: {
name: data.name,
location: data.location ?? null,
location,
...address,
availability: toPrismaAvailability(data.availability ?? null),
capacity: data.capacity ?? null,
kind,
@@ -231,10 +259,29 @@ export async function updateOwnedGym(
if (data.isPrivate === true && gym.kind === 'REAL')
throw badRequest('Real gyms are always public');
try {
const { availability, ...rest } = data;
const { availability, addressLine1, addressLine2, postalCode, city, countryCode, ...rest } =
data;
// Zmiana dowolnego pola adresu → scalenie ze stanem w bazie i przeliczenie `location`.
let addressData: Record<string, unknown> = {};
const hasAddress = [addressLine1, addressLine2, postalCode, city, countryCode].some(
(v) => v !== undefined,
);
if (hasAddress) {
const merged = {
addressLine1: addressLine1 !== undefined ? addressLine1 : gym.addressLine1,
addressLine2: addressLine2 !== undefined ? addressLine2 : gym.addressLine2,
postalCode: postalCode !== undefined ? postalCode : gym.postalCode,
city: city !== undefined ? city : gym.city,
countryCode:
countryCode !== undefined
? countryCode ? countryCode.trim().toUpperCase() : null
: gym.countryCode,
};
addressData = { ...merged, location: formatAddress(merged) };
}
const updated = await prisma.gym.update({
where: { id: gymId },
data: { ...rest, availability: toPrismaAvailability(availability) },
data: { ...rest, ...addressData, availability: toPrismaAvailability(availability) },
});
return { gym: updated };
} catch (e) {