2026-04-17 15:45:54 +08:00
|
|
|
// src/api/invitations.ts
|
|
|
|
|
import pb from './pocketbase'
|
2026-04-17 16:30:05 +08:00
|
|
|
import type { Invitation } from '@/types'
|
2026-04-17 15:45:54 +08:00
|
|
|
|
|
|
|
|
// 发送邀请
|
|
|
|
|
export async function sendInvitation(data: {
|
|
|
|
|
to: string
|
|
|
|
|
teamSession: string
|
|
|
|
|
}) {
|
|
|
|
|
const user = pb.authStore.model
|
|
|
|
|
if (!user) throw new Error('未登录')
|
|
|
|
|
|
|
|
|
|
// 检查是否已有待处理邀请
|
|
|
|
|
const existing = await pb.collection('invitations').getList(1, 1, {
|
|
|
|
|
filter: `from="${user.id}" && to="${data.to}" && teamSession="${data.teamSession}" && status="pending"`
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (existing.items.length > 0) {
|
|
|
|
|
throw new Error('已有待处理的邀请')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pb.collection('invitations').create({
|
|
|
|
|
...data,
|
|
|
|
|
from: user.id,
|
|
|
|
|
status: 'pending'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量发送邀请
|
|
|
|
|
export async function sendBulkInvitations(recipients: string[], teamSessionId: string) {
|
|
|
|
|
const promises = recipients.map(to =>
|
|
|
|
|
sendInvitation({ to, teamSession: teamSessionId })
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return Promise.allSettled(promises)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户的待处理邀请
|
|
|
|
|
export async function getPendingInvitations(): Promise<Invitation[]> {
|
|
|
|
|
const user = pb.authStore.model
|
|
|
|
|
if (!user) return []
|
|
|
|
|
|
|
|
|
|
const result = await pb.collection('invitations').getList(1, 50, {
|
|
|
|
|
filter: `to="${user.id}" && status="pending"`,
|
|
|
|
|
sort: '-created',
|
|
|
|
|
expand: 'from,teamSession'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return result.items as unknown as Invitation[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取我发送的邀请
|
|
|
|
|
export async function getMySentInvitations(): Promise<Invitation[]> {
|
|
|
|
|
const user = pb.authStore.model
|
|
|
|
|
if (!user) return []
|
|
|
|
|
|
|
|
|
|
const result = await pb.collection('invitations').getList(1, 50, {
|
|
|
|
|
filter: `from="${user.id}"`,
|
|
|
|
|
sort: '-created',
|
|
|
|
|
expand: 'to,teamSession'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return result.items as unknown as Invitation[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 响应邀请
|
|
|
|
|
export async function respondInvitation(
|
|
|
|
|
invitationId: string,
|
|
|
|
|
response: 'accepted' | 'rejected',
|
|
|
|
|
rejectReason?: string
|
|
|
|
|
) {
|
2026-04-17 16:30:05 +08:00
|
|
|
const updateData: Record<string, unknown> = {
|
|
|
|
|
status: response
|
2026-04-17 15:45:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response === 'rejected' && rejectReason) {
|
|
|
|
|
updateData.rejectReason = rejectReason
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 16:30:05 +08:00
|
|
|
// 后端 hook 会自动处理:加入 team members + 更新用户状态
|
2026-04-17 15:45:54 +08:00
|
|
|
await pb.collection('invitations').update(invitationId, updateData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 订阅邀请变更
|
|
|
|
|
export function subscribeInvitations(callback: (invitation: Invitation) => void) {
|
|
|
|
|
const user = pb.authStore.model
|
|
|
|
|
if (!user) return () => {}
|
|
|
|
|
|
|
|
|
|
return pb.collection('invitations').subscribe('*', (payload) => {
|
|
|
|
|
const invite = payload.record as unknown as Invitation
|
|
|
|
|
if (invite.to === user.id || invite.from === user.id) {
|
|
|
|
|
callback(invite)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|