fix: invite
This commit is contained in:
@@ -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<GetFamilyGroupResponseDto> {
|
||||
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: '生成邀请码' })
|
||||
|
||||
@@ -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}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user