Skip to content

Commit

Permalink
fixup! リアクション機能をAPIに実装
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Jan 12, 2025
1 parent a79ca61 commit 1a83275
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions functions/src/routers/reactionRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express, { Request, Response } from "express";
import admin from "firebase-admin";
import { logger } from "firebase-functions";
import { Reaction, reactionTypeMap } from "../types";
import { Reaction, ReactionTypeMap } from "../types";

const router = express.Router();
const db = admin.firestore();
Expand All @@ -27,15 +27,16 @@ router.put("/:goalId", async (req: Request, res: Response) => {

const goalData = goalDoc.data();

const currentReactions = goalData?.post?.reaction || {};
const currentReactions = goalData?.reaction || {}; // 元のリアクションデータ
if (!reactionType) {
// リアクション削除
delete currentReactions[userId];
} else {
// リアクション追加
// リアクションタイプが正しいか確認
const validReactionTypes = [
...reactionTypeMap.success,
...reactionTypeMap.failed,
...ReactionTypeMap.success,
...ReactionTypeMap.failed,
];
if (!validReactionTypes.includes(reactionType)) {
return res.status(400).json({ message: "Invalid reactionType" });
Expand All @@ -45,10 +46,8 @@ router.put("/:goalId", async (req: Request, res: Response) => {
}

await goalRef.update({
post: {
...goalData?.post,
reaction: currentReactions,
},
...goalData,
reaction: currentReactions,
});

return res.json({ message: "Reaction updated successfully", goalId });
Expand Down
2 changes: 1 addition & 1 deletion functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ReactionType {
failed: "sad" | "angry" | "muscle";
}

export const reactionTypeMap = {
export const ReactionTypeMap = {
success: ["laugh", "surprised", "clap"] as const,
failed: ["sad", "angry", "muscle"] as const,
};

0 comments on commit 1a83275

Please sign in to comment.