85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
import { Injectable } from '@nestjs/common'
|
|
import { StudioConfig as PrismaStudioConfig } from '@prisma/client'
|
|
import { DEFAULT_OWNER_INTRODUCTION, DEFAULT_OWNER_CERTIFICATIONS } from '@mp-pilates/shared'
|
|
import { PrismaService } from '../prisma/prisma.service'
|
|
import { UpdateStudioDto } from './dto/update-studio.dto'
|
|
|
|
@Injectable()
|
|
export class StudioService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async getInfo() {
|
|
const existing = await this.prisma.studioConfig.findFirst()
|
|
|
|
if (existing) {
|
|
return this.normalizeStudioConfig(existing)
|
|
}
|
|
|
|
const created = await this.prisma.studioConfig.create({
|
|
data: {
|
|
name: '普拉提工作室',
|
|
},
|
|
})
|
|
|
|
return this.normalizeStudioConfig(created)
|
|
}
|
|
|
|
async updateInfo(dto: UpdateStudioDto) {
|
|
const existing = await this.prisma.studioConfig.findFirst({
|
|
select: { id: true },
|
|
})
|
|
|
|
const data = {
|
|
...dto,
|
|
photos: dto.photos ? this.normalizeStringList(dto.photos) : undefined,
|
|
ownerIntroduction: dto.ownerIntroduction?.trim(),
|
|
ownerCertifications: dto.ownerCertifications === undefined ? undefined : this.normalizeStringList(dto.ownerCertifications),
|
|
}
|
|
|
|
const record = existing
|
|
? await this.prisma.studioConfig.update({
|
|
where: { id: existing.id },
|
|
data,
|
|
})
|
|
: await this.prisma.studioConfig.create({
|
|
data: { name: '普拉提工作室', ...data },
|
|
})
|
|
|
|
return this.normalizeStudioConfig(record)
|
|
}
|
|
|
|
private normalizeStudioConfig(config: PrismaStudioConfig) {
|
|
return {
|
|
...config,
|
|
latitude: config.latitude == null ? null : Number(config.latitude),
|
|
longitude: config.longitude == null ? null : Number(config.longitude),
|
|
photos: this.normalizeStringList(config.photos),
|
|
ownerIntroduction: config.ownerIntroduction ?? DEFAULT_OWNER_INTRODUCTION,
|
|
ownerCertifications: config.ownerCertifications == null
|
|
? [...DEFAULT_OWNER_CERTIFICATIONS]
|
|
: this.normalizeStringList(config.ownerCertifications),
|
|
}
|
|
}
|
|
|
|
private normalizeStringList(value: unknown): string[] {
|
|
if (!Array.isArray(value)) {
|
|
return []
|
|
}
|
|
|
|
const deduped = new Set<string>()
|
|
|
|
value.forEach((item) => {
|
|
if (typeof item !== 'string') {
|
|
return
|
|
}
|
|
|
|
const trimmed = item.trim()
|
|
if (trimmed) {
|
|
deduped.add(trimmed)
|
|
}
|
|
})
|
|
|
|
return [...deduped]
|
|
}
|
|
}
|