API: strukturalny adres silowni (ulica/kod/miasto/kraj); location derivowane, adres wymagany przy tworzeniu REAL
This commit is contained in:
@@ -5,6 +5,7 @@ import { publicUser } from '../lib/publicUser.js';
|
||||
import { badRequest, conflict, forbidden, notFound } from '../lib/errors.js';
|
||||
import { removeGymClaimFiles, removeGymPhotoFiles } from '../lib/uploads.js';
|
||||
import { gymPhotoUrl, listGymClaimIds } from './gyms.js';
|
||||
import { formatAddress, type AddressParts } from '../lib/address.js';
|
||||
import {
|
||||
resolveMuscleConflicts,
|
||||
toPrismaAvailability,
|
||||
@@ -15,16 +16,25 @@ import {
|
||||
|
||||
export interface CreateGymInput {
|
||||
name: string;
|
||||
location?: string | null;
|
||||
availability?: Availability | null;
|
||||
capacity?: number | null;
|
||||
/** Administrator wyznacza właściciela nowej siłowni (rola OWNER). */
|
||||
ownerId: string;
|
||||
/** Strukturalny adres (REAL); `location` jest z niego derivowany. */
|
||||
addressLine1: string;
|
||||
addressLine2?: string | null;
|
||||
postalCode: string;
|
||||
city: string;
|
||||
countryCode?: string;
|
||||
availability?: Availability | null;
|
||||
capacity?: number | null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -144,13 +154,19 @@ export async function removeGymOwner(prisma: PrismaClient, gymId: string, userId
|
||||
}
|
||||
}
|
||||
|
||||
/** Tworzy siłownię; wyznaczony przez admina `ownerId` otrzymuje członkostwo z rolą OWNER. */
|
||||
/** Tworzy siłownię REAL z adresem; `location` derivowany, wyznaczony `ownerId` dostaje rolę OWNER. */
|
||||
export async function createGym(prisma: PrismaClient, data: CreateGymInput) {
|
||||
const { availability, ownerId, ...rest } = data;
|
||||
await ensureUser(prisma, ownerId);
|
||||
const location = formatAddress(rest);
|
||||
const gym = await prisma.$transaction(async (tx) => {
|
||||
const created = await tx.gym.create({
|
||||
data: { ...rest, availability: toPrismaAvailability(availability) },
|
||||
data: {
|
||||
...rest,
|
||||
countryCode: (data.countryCode ?? 'PL').toUpperCase(),
|
||||
location,
|
||||
availability: toPrismaAvailability(availability),
|
||||
},
|
||||
});
|
||||
await tx.gymMembership.create({
|
||||
data: { userId: ownerId, gymId: created.id, roles: ['OWNER'] },
|
||||
@@ -160,12 +176,42 @@ export async function createGym(prisma: PrismaClient, data: CreateGymInput) {
|
||||
return { gym };
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualizacja siłowni; przy zmianie któregokolwiek pola adresu reszta jest
|
||||
* scalana ze stanem w bazie, a display string `location` liczony od nowa.
|
||||
*/
|
||||
export async function updateGym(prisma: PrismaClient, id: string, data: UpdateGymInput) {
|
||||
try {
|
||||
const { availability, ...rest } = data;
|
||||
const {
|
||||
availability,
|
||||
addressLine1,
|
||||
addressLine2,
|
||||
postalCode,
|
||||
city,
|
||||
countryCode,
|
||||
...rest
|
||||
} = data;
|
||||
let addressData: Prisma.GymUpdateInput = {};
|
||||
const hasAddress = [addressLine1, addressLine2, postalCode, city, countryCode].some(
|
||||
(v) => v !== undefined,
|
||||
);
|
||||
if (hasAddress) {
|
||||
const existing = await prisma.gym.findUniqueOrThrow({ where: { id } });
|
||||
const merged: AddressParts = {
|
||||
addressLine1: addressLine1 !== undefined ? addressLine1 : existing.addressLine1,
|
||||
addressLine2: addressLine2 !== undefined ? addressLine2 : existing.addressLine2,
|
||||
postalCode: postalCode !== undefined ? postalCode : existing.postalCode,
|
||||
city: city !== undefined ? city : existing.city,
|
||||
countryCode:
|
||||
countryCode !== undefined
|
||||
? countryCode ? countryCode.trim().toUpperCase() : null
|
||||
: existing.countryCode,
|
||||
};
|
||||
addressData = { ...merged, location: formatAddress(merged) };
|
||||
}
|
||||
const gym = await prisma.gym.update({
|
||||
where: { id },
|
||||
data: { ...rest, availability: toPrismaAvailability(availability) },
|
||||
data: { ...rest, ...addressData, availability: toPrismaAvailability(availability) },
|
||||
});
|
||||
return { gym };
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user