From 8a43b1795b3e47ed36df6929efc269562c4d57a3 Mon Sep 17 00:00:00 2001 From: richarjiang Date: Thu, 4 Dec 2025 18:56:01 +0800 Subject: [PATCH] fix: invite --- .../health-profiles.controller.ts | 15 --------- .../services/family-health.service.ts | 32 +++++++++++++++---- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/health-profiles/health-profiles.controller.ts b/src/health-profiles/health-profiles.controller.ts index 796a6ed..8a41e1f 100644 --- a/src/health-profiles/health-profiles.controller.ts +++ b/src/health-profiles/health-profiles.controller.ts @@ -30,7 +30,6 @@ import { HealthHistoryProgressResponseDto, } from './dto/health-history.dto'; import { - CreateFamilyGroupDto, GenerateInviteCodeDto, JoinFamilyGroupDto, UpdateFamilyMemberDto, @@ -114,20 +113,6 @@ export class HealthProfilesController { return { code: ResponseCode.SUCCESS, message: 'success', data }; } - @Post('family/group') - @HttpCode(HttpStatus.OK) - @ApiOperation({ summary: '创建家庭组' }) - @ApiBody({ type: CreateFamilyGroupDto }) - @ApiResponse({ status: 200, type: GetFamilyGroupResponseDto }) - async createFamilyGroup( - @Body() createDto: CreateFamilyGroupDto, - @CurrentUser() user: AccessTokenPayload, - ): Promise { - this.logger.log(`创建家庭组 - 用户ID: ${user.sub}`); - const data = await this.familyHealthService.createFamilyGroup(user.sub, createDto); - return { code: ResponseCode.SUCCESS, message: 'success', data }; - } - @Post('family/group/invite') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: '生成邀请码' }) diff --git a/src/health-profiles/services/family-health.service.ts b/src/health-profiles/services/family-health.service.ts index 6c9dfc4..c13ff16 100644 --- a/src/health-profiles/services/family-health.service.ts +++ b/src/health-profiles/services/family-health.service.ts @@ -117,31 +117,49 @@ export class FamilyHealthService { } /** - * 生成邀请码 + * 获取或生成邀请码 + * 如果用户没有家庭组,自动创建一个 + * 如果已有有效邀请码,直接返回 */ async generateInviteCode( userId: string, dto: GenerateInviteCodeDto, ): Promise<{ familyGroupId: string; inviteCode: string; expiresAt: string; qrCodeUrl: string }> { - const membership = await this.familyMemberModel.findOne({ + let membership = await this.familyMemberModel.findOne({ where: { userId }, }); + // 如果用户没有家庭组,自动创建一个 if (!membership) { - throw new NotFoundException('您还没有家庭组'); + this.logger.log(`用户 ${userId} 没有家庭组,自动创建`); + await this.createFamilyGroup(userId, { name: '我的家庭' }); + membership = await this.familyMemberModel.findOne({ + where: { userId }, + }); } // 只有 owner 和 admin 可以生成邀请码 - if (membership.role === FamilyRole.MEMBER) { + if (membership!.role === FamilyRole.MEMBER) { throw new ForbiddenException('只有管理员可以生成邀请码'); } - const familyGroup = await this.familyGroupModel.findByPk(membership.familyGroupId); + const familyGroup = await this.familyGroupModel.findByPk(membership!.familyGroupId); if (!familyGroup) { throw new NotFoundException('家庭组不存在'); } - // 生成邀请码 + // 如果已有有效邀请码,直接返回 + if (familyGroup.inviteCode && familyGroup.inviteCodeExpiresAt && dayjs(familyGroup.inviteCodeExpiresAt).isAfter(dayjs())) { + this.logger.log(`用户 ${userId} 获取家庭组 ${familyGroup.id} 的现有邀请码 ${familyGroup.inviteCode}`); + return { + familyGroupId: familyGroup.id, + inviteCode: familyGroup.inviteCode, + expiresAt: familyGroup.inviteCodeExpiresAt.toISOString(), + qrCodeUrl: `outlive://family/join?code=${familyGroup.inviteCode}`, + }; + } + + // 生成新邀请码 const inviteCode = this.generateUniqueInviteCode(); const expiresAt = dayjs().add(dto.expiresInHours || 24, 'hour').toDate(); @@ -155,7 +173,7 @@ export class FamilyHealthService { familyGroupId: familyGroup.id, inviteCode, expiresAt: expiresAt.toISOString(), - qrCodeUrl: `outlive://family/join?code=${inviteCode}`, // 可以根据实际需求生成二维码 URL + qrCodeUrl: `outlive://family/join?code=${inviteCode}`, }; }