-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from hinagiku-dev/feat/hostview-individual
Feat/hostview individual
- Loading branch information
Showing
3 changed files
with
180 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/routes/api/session/[id]/group/[group_number]/join/[participant]/+server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { adminDb } from '$lib/server/firebase'; | ||
import { error, json } from '@sveltejs/kit'; | ||
import type { RequestHandler } from './$types'; | ||
|
||
export const DELETE: RequestHandler = async ({ params }) => { | ||
try { | ||
const { id, group_number, participant } = params; | ||
if (!participant) { | ||
throw error(400, '缺少參與者ID'); | ||
} | ||
|
||
// 找到群組 | ||
const groupsRef = adminDb.collection('sessions').doc(id).collection('groups'); | ||
const groupDoc = await groupsRef.doc(group_number).get(); | ||
|
||
if (!groupDoc.exists) { | ||
throw error(404, '找不到群組'); | ||
} | ||
|
||
const groupData = groupDoc.data(); | ||
if (!groupData) { | ||
throw error(404, '找不到群組資料'); | ||
} | ||
|
||
// 確認參與者在群組中 | ||
if (!groupData.participants.includes(participant)) { | ||
throw error(400, '參與者不在此群組中'); | ||
} | ||
|
||
// 計算移除參與者後的新參與者列表 | ||
const updatedParticipants = groupData.participants.filter((p: string) => p !== participant); | ||
|
||
if (updatedParticipants.length === 0) { | ||
// 如果群組將變成空的,直接刪除整個群組 | ||
await groupDoc.ref.delete(); | ||
} else { | ||
// 否則只更新參與者列表 | ||
await groupDoc.ref.update({ | ||
participants: updatedParticipants | ||
}); | ||
} | ||
|
||
// 刪除參與者的對話記錄 | ||
const conversationsRef = groupDoc.ref.collection('conversations'); | ||
const conversationQuery = await conversationsRef.where('userId', '==', participant).get(); | ||
|
||
const deletePromises = conversationQuery.docs.map((doc) => doc.ref.delete()); | ||
await Promise.all(deletePromises); | ||
|
||
return json({ success: true }); | ||
} catch (e) { | ||
console.error('移除參與者時發生錯誤:', e); | ||
throw error(500, '無法移除參與者'); | ||
} | ||
}; |