API: adres strukturalny w endpoints /gyms (REAL wymaga ulicy/kodu/miasta, location derivowane, merge przy PATCH)
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
import { z } from 'zod';
|
||||
import { availabilitySchema } from '../me.js';
|
||||
|
||||
// Adres strukturalny (wzorzec Google/UPU); wymóg kompletności dla REAL egzekwuje serwis.
|
||||
export const gymAddressFields = {
|
||||
addressLine1: z.string().trim().min(1).max(200).nullable().optional(),
|
||||
addressLine2: z.string().trim().max(200).nullable().optional(),
|
||||
postalCode: z
|
||||
.string()
|
||||
.trim()
|
||||
.regex(/^\d{2}-\d{3}$/, 'Format kodu pocztowego: 00-001')
|
||||
.nullable()
|
||||
.optional(),
|
||||
city: z.string().trim().min(1).max(100).nullable().optional(),
|
||||
countryCode: z.string().trim().regex(/^[a-zA-Z]{2}$/).nullable().optional(),
|
||||
};
|
||||
|
||||
export const createGymSchema = z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
location: z.string().max(500).nullable().optional(),
|
||||
...gymAddressFields,
|
||||
availability: availabilitySchema,
|
||||
capacity: z.number().int().positive().max(100000).nullable().optional(),
|
||||
kind: z.enum(['VIRTUAL', 'REAL']).optional(),
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -204,7 +204,7 @@ export const openapi: Record<string, unknown> = {
|
||||
},
|
||||
post: {
|
||||
tags: ['Gyms'],
|
||||
summary: 'Create a gym (creator becomes its OWNER)',
|
||||
summary: 'Create a gym (creator becomes OWNER; admins only for REAL, REAL requires address)',
|
||||
security: [{ bearerAuth: [] }],
|
||||
requestBody: {
|
||||
required: true,
|
||||
@@ -215,7 +215,17 @@ export const openapi: Record<string, unknown> = {
|
||||
required: ['name'],
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
location: { type: 'string' },
|
||||
kind: { type: 'string', enum: ['VIRTUAL', 'REAL'], default: 'VIRTUAL' },
|
||||
isPrivate: { type: 'boolean', description: 'Tylko dla VIRTUAL' },
|
||||
location: {
|
||||
type: 'string',
|
||||
description: 'Swobodna lokalizacja (VIRTUAL); dla REAL derivowana z adresu',
|
||||
},
|
||||
addressLine1: { type: 'string', description: 'Ulica i numer (REAL)' },
|
||||
addressLine2: { type: 'string', nullable: true },
|
||||
postalCode: { type: 'string', description: 'Kod pocztowy 00-001 (REAL)' },
|
||||
city: { type: 'string', description: 'Miejscowość (REAL)' },
|
||||
countryCode: { type: 'string', default: 'PL', description: 'ISO 3166-1 alpha-2' },
|
||||
availability: {
|
||||
type: 'array',
|
||||
description: '7 dni (Pon-Niedz), każdy null lub { start, end }',
|
||||
|
||||
@@ -209,7 +209,13 @@ describe('gyms — selection and management', () => {
|
||||
const created = await app
|
||||
.post('/api/v1/gyms')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ name: `Remove Gym ${rand()}`, location: 'Warsaw', kind: 'REAL' });
|
||||
.send({
|
||||
name: `Remove Gym ${rand()}`,
|
||||
kind: 'REAL',
|
||||
addressLine1: 'Złota 5',
|
||||
postalCode: '00-100',
|
||||
city: 'Warszawa',
|
||||
});
|
||||
const gymId = created.body.gym.id as string;
|
||||
const kettlebell = await createEquipment('Kettlebell');
|
||||
await app
|
||||
|
||||
@@ -51,9 +51,49 @@ describe('virtual gyms — creation, listing and ratings', () => {
|
||||
const res = await app
|
||||
.post('/api/v1/gyms')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ name: `Admin Real ${rand()}`, location: 'Wrocław', kind: 'REAL' });
|
||||
.send({
|
||||
name: `Admin Real ${rand()}`,
|
||||
kind: 'REAL',
|
||||
addressLine1: 'Kazimierza 8',
|
||||
postalCode: '50-001',
|
||||
city: 'Wrocław',
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.gym).toMatchObject({ kind: 'REAL', isPrivate: false });
|
||||
expect(res.body.gym).toMatchObject({
|
||||
kind: 'REAL',
|
||||
isPrivate: false,
|
||||
location: 'Kazimierza 8, 50-001 Wrocław',
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a REAL gym without a structured address with 400', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app
|
||||
.post('/api/v1/gyms')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ name: `No Addr ${rand()}`, kind: 'REAL', location: 'Wrocław' });
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('recomputes location when the owner updates the address', async () => {
|
||||
const app = buildApp();
|
||||
const created = await app
|
||||
.post('/api/v1/gyms')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({
|
||||
name: `Addr Edit ${rand()}`,
|
||||
kind: 'REAL',
|
||||
addressLine1: 'Krótka 1',
|
||||
postalCode: '00-001',
|
||||
city: 'Warszawa',
|
||||
});
|
||||
const gymId = created.body.gym.id as string;
|
||||
const res = await app
|
||||
.patch(`/api/v1/gyms/${gymId}`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ postalCode: '02-500' });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.gym).toMatchObject({ location: 'Krótka 1, 02-500 Warszawa' });
|
||||
});
|
||||
|
||||
it('lets an admin create a private VIRTUAL gym', async () => {
|
||||
@@ -110,7 +150,13 @@ describe('virtual gyms — creation, listing and ratings', () => {
|
||||
const created = await app
|
||||
.post('/api/v1/gyms')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ name: `Real Patch ${rand()}`, location: 'Wrocław', kind: 'REAL' });
|
||||
.send({
|
||||
name: `Real Patch ${rand()}`,
|
||||
kind: 'REAL',
|
||||
addressLine1: 'Swobodna 2',
|
||||
postalCode: '50-002',
|
||||
city: 'Wrocław',
|
||||
});
|
||||
const gymId = created.body.gym.id as string;
|
||||
const res = await app
|
||||
.patch(`/api/v1/gyms/${gymId}`)
|
||||
|
||||
Reference in New Issue
Block a user