feat: 支持新版会员档案
This commit is contained in:
@@ -2,6 +2,8 @@ import { BadRequestException, ForbiddenException, Injectable, NotFoundException
|
||||
import { PrismaService } from '../prisma/prisma.service'
|
||||
import { BodyMetricDto, MemberNoteDto, ProgressPhotoDto } from './dto/member-progress.dto'
|
||||
import { ProgressPhotoStorageService } from './progress-photo-storage.service'
|
||||
import { BODY_MEASUREMENT_FIELDS, emptyPostureAssessment, isPostureAssessment, postureAnswerCount } from '@mp-pilates/shared'
|
||||
import { Prisma } from '@prisma/client'
|
||||
@Injectable()
|
||||
export class MemberProgressService {
|
||||
constructor(private readonly prisma: PrismaService, private readonly storage: ProgressPhotoStorageService) {}
|
||||
@@ -29,8 +31,26 @@ export class MemberProgressService {
|
||||
}
|
||||
async addMetric(userId: string, operatorId: string, dto: BodyMetricDto) {
|
||||
await this.member(userId)
|
||||
if ([dto.weight, dto.bodyFat, dto.waist, dto.hip, dto.flexibility].every(v => v == null)) throw new BadRequestException('请至少填写一项体测数据')
|
||||
return this.prisma.bodyMetric.create({ data: { ...dto, userId, operatorId, recordedAt: this.date(dto.recordedAt) } })
|
||||
return this.prisma.bodyMetric.create({ data: { ...this.metricData(dto), userId, operatorId } })
|
||||
}
|
||||
async metric(userId: string, id: string) {
|
||||
const member = await this.member(userId)
|
||||
const record = await this.prisma.bodyMetric.findFirst({ where: { id, userId } })
|
||||
if (!record) throw new NotFoundException('体测记录不存在')
|
||||
return { nickname: member.nickname, record }
|
||||
}
|
||||
async updateMetric(userId: string, id: string, operatorId: string, dto: BodyMetricDto) {
|
||||
const data = this.metricData(dto)
|
||||
const result = await this.prisma.bodyMetric.updateMany({ where: { id, userId }, data: { ...data, operatorId } })
|
||||
if (!result.count) throw new NotFoundException('体测记录不存在')
|
||||
return this.prisma.bodyMetric.findFirst({ where: { id, userId } })
|
||||
}
|
||||
private metricData(dto: BodyMetricDto) {
|
||||
const assessment = dto.assessment ?? emptyPostureAssessment()
|
||||
if (!isPostureAssessment(assessment)) throw new BadRequestException('体态检查内容无效')
|
||||
const measurements = Object.fromEntries(BODY_MEASUREMENT_FIELDS.map(field => [field.key, dto[field.key] ?? null]))
|
||||
if (BODY_MEASUREMENT_FIELDS.every(field => dto[field.key] == null) && !postureAnswerCount(assessment) && !dto.remark?.trim()) throw new BadRequestException('请至少填写一项体测数据、体态检查或评估备注')
|
||||
return { ...measurements, recordedAt: this.date(dto.recordedAt), remark: dto.remark?.trim() || '', assessment: assessment as unknown as Prisma.InputJsonValue }
|
||||
}
|
||||
async addNote(userId: string, operatorId: string, dto: MemberNoteDto) {
|
||||
await this.member(userId)
|
||||
|
||||
Reference in New Issue
Block a user