-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5825d1d
commit a79ca61
Showing
12 changed files
with
143 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
データベースには`user`と`goal`の2つのコレクションがあります。`user`コレクションにはユーザーの情報が、`goal`コレクションにはユーザーが設定した目標と結果の情報が保存されます。 | ||
|
||
# userコレクション | ||
user (コレクション) | ||
├── userId (ドキュメント) | ||
│ ├── fcmToken: string (初期値: "") | ||
│ ├── name: string | ||
│ └── streak: number (初期値: 0, 目標完了時に更新, streakがリセットされる場合はAPI側で0を返す) | ||
├── 他のドキュメント... | ||
|
||
|
||
# goalコレクション | ||
goal (コレクション) | ||
├── goalId (ドキュメント) | ||
│ ├── userId: string | ||
│ ├── deadline: Timestamp (例: 2024年12月18日 23:00:00 UTC+9) | ||
│ ├── text: string (""は禁止) | ||
│ ├── post: map | null (初期値: null) | ||
│ │ ├── storedId: string (storageに保存されたファイルのID) | ||
│ │ ├── submittedAt: Timestamp (目標完了時間) | ||
│ │ └── text: string (""もOK) | ||
│ └── reaction?: map (初期値: undefined) | ||
│ ├── userId: string (リアクションの種類) | ||
│ ├── 他のUserId... | ||
├── 他のドキュメント... |
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
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
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,61 @@ | ||
import express, { Request, Response } from "express"; | ||
import admin from "firebase-admin"; | ||
import { logger } from "firebase-functions"; | ||
import { Reaction, reactionTypeMap } from "../types"; | ||
|
||
const router = express.Router(); | ||
const db = admin.firestore(); | ||
|
||
// PUT: リアクションを更新 | ||
router.put("/:goalId", async (req: Request, res: Response) => { | ||
const goalId = req.params.goalId; | ||
const { userId, reactionType }: Partial<Reaction> = req.body; | ||
|
||
if (!userId || !goalId) { | ||
return res.status(400).json({ | ||
message: "userID and goalId are required", | ||
}); | ||
} | ||
|
||
try { | ||
const goalRef = db.collection("goal").doc(goalId); | ||
const goalDoc = await goalRef.get(); | ||
|
||
if (!goalDoc.exists) { | ||
return res.status(404).json({ message: "Goal not found" }); | ||
} | ||
|
||
const goalData = goalDoc.data(); | ||
|
||
const currentReactions = goalData?.post?.reaction || {}; | ||
if (!reactionType) { | ||
// リアクション削除 | ||
delete currentReactions[userId]; | ||
} else { | ||
// リアクション追加 | ||
const validReactionTypes = [ | ||
...reactionTypeMap.success, | ||
...reactionTypeMap.failed, | ||
]; | ||
if (!validReactionTypes.includes(reactionType)) { | ||
return res.status(400).json({ message: "Invalid reactionType" }); | ||
} | ||
|
||
currentReactions[userId] = reactionType; | ||
} | ||
|
||
await goalRef.update({ | ||
post: { | ||
...goalData?.post, | ||
reaction: currentReactions, | ||
}, | ||
}); | ||
|
||
return res.json({ message: "Reaction updated successfully", goalId }); | ||
} catch (error) { | ||
logger.error(error); | ||
return res.status(500).json({ message: "Error updating Reaction" }); | ||
} | ||
}); | ||
|
||
export default router; |
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
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
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