Skip to content

Commit

Permalink
loggerを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 27, 2024
1 parent 842e50b commit d74ca68
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cors from "cors";
import express from "express";
import { rateLimit } from "express-rate-limit";
import admin from "firebase-admin";
import * as logger from "firebase-functions/logger";
import { logger } from "firebase-functions";
import { onRequest } from "firebase-functions/v2/https";
import helmet from "helmet";
import serviceAccount from "./serviceAccountKey.json";
Expand Down
7 changes: 7 additions & 0 deletions functions/src/routers/goalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Request, Response } from "express";
import admin from "firebase-admin";
import { logger } from "firebase-functions";
import { Goal } from "./types";

const router = express.Router();
Expand All @@ -26,6 +27,7 @@ router.get("/", async (req: Request, res: Response) => {

return res.json(goalData);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching goals" });
}
});
Expand Down Expand Up @@ -60,6 +62,7 @@ router.get("/:userId", async (req: Request, res: Response) => {

return res.json(goals);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching goals" });
}
});
Expand All @@ -75,6 +78,7 @@ router.post("/", async (req: Request, res: Response) => {
try {
({ userId, deadline, text } = req.body as Goal);
} catch (error) {
logger.error(error);
return res.status(400).json({ message: "Invalid request body" });
}

Expand All @@ -98,6 +102,7 @@ router.post("/", async (req: Request, res: Response) => {
.status(201)
.json({ message: "Goal created successfully", goalId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error creating goal" });
}
});
Expand Down Expand Up @@ -132,6 +137,7 @@ router.put("/:goalId", async (req: Request, res: Response) => {
await db.collection("goal").doc(goalId).update(updateData);
return res.json({ message: "Goal updated successfully", goalId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error updating goal" });
}
});
Expand All @@ -148,6 +154,7 @@ router.delete("/:goalId", async (req: Request, res: Response) => {
await db.collection("goal").doc(goalId).delete();
return res.json({ message: "Goal deleted successfully", goalId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error deleting goal" });
}
});
Expand Down
7 changes: 7 additions & 0 deletions functions/src/routers/postRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Request, Response } from "express";
import admin from "firebase-admin";
import * as logger from "firebase-functions/logger";
import { Post } from "./types";

const router = express.Router();
Expand Down Expand Up @@ -28,6 +29,7 @@ router.get("/", async (req: Request, res: Response) => {

return res.json(postData);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching posts" });
}
});
Expand Down Expand Up @@ -63,6 +65,7 @@ router.get("/:userId", async (req: Request, res: Response) => {

return res.json(posts);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching user's posts" });
}
});
Expand All @@ -80,6 +83,7 @@ router.post("/", async (req: Request, res: Response) => {
try {
({ userId, storedId, text, goalId, submittedAt } = req.body);
} catch (error) {
logger.error(error);
return res.status(400).json({ message: "Invalid request body" });
}

Expand Down Expand Up @@ -107,6 +111,7 @@ router.post("/", async (req: Request, res: Response) => {

return res.json({ message: "Post created successfully", postId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error creating post" });
}
});
Expand Down Expand Up @@ -140,6 +145,7 @@ router.put("/:postId", async (req: Request, res: Response) => {
await db.collection("post").doc(postId).update(updateData);
return res.json({ message: "Post updated successfully", postId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error updating post" });
}
});
Expand All @@ -156,6 +162,7 @@ router.delete("/:postId", async (req: Request, res: Response) => {
await db.collection("post").doc(postId).delete();
return res.json({ message: "Post deleted successfully", postId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error deleting post" });
}
});
Expand Down
3 changes: 2 additions & 1 deletion functions/src/routers/resultRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Request, Response } from "express";
import admin from "firebase-admin";
import { logger } from "firebase-functions";
import { GoalWithId, SuccessResult } from "./types";

const router = express.Router();
Expand Down Expand Up @@ -86,7 +87,7 @@ router.get("/:userId?", async (req: Request, res: Response) => {
const results = await getResults(limit, offset, userId);
res.json(results);
} catch (error) {
console.error(error);
logger.info(error);
res.status(500).json({ message: "Internal server error" });
}
});
Expand Down
8 changes: 8 additions & 0 deletions functions/src/routers/userRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Request, Response } from "express";
import admin from "firebase-admin";
import { logger } from "firebase-functions";
import { User } from "./types";

const router = express.Router();
Expand All @@ -25,6 +26,7 @@ router.get("/", async (req: Request, res: Response) => {

return res.json(userData);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching user data" });
}
});
Expand Down Expand Up @@ -54,6 +56,7 @@ router.get("/id/:userId", async (req: Request, res: Response) => {

return res.json(userData);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching user data" });
}
});
Expand Down Expand Up @@ -84,6 +87,7 @@ router.get("/name/:userName", async (req: Request, res: Response) => {

return res.json(userData);
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error fetching user data" });
}
});
Expand All @@ -98,6 +102,7 @@ router.post("/", async (req: Request, res: Response) => {
try {
({ name, userId, streak = 0, fcmToken = "" } = req.body);
} catch (error) {
logger.error(error);
return res.status(400).json({ message: "Invalid request body" });
}

Expand All @@ -116,6 +121,7 @@ router.post("/", async (req: Request, res: Response) => {
.status(201)
.json({ message: "User created successfully", userId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error creating user" });
}
});
Expand Down Expand Up @@ -146,6 +152,7 @@ router.put("/:userId", async (req: Request, res: Response) => {
await db.collection("user").doc(userId).update(updateData);
return res.json({ message: "User updated successfully", userId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error updating user" });
}
});
Expand All @@ -162,6 +169,7 @@ router.delete("/:userId", async (req: Request, res: Response) => {
await db.collection("user").doc(userId).delete();
return res.json({ message: "User deleted successfully", userId });
} catch (error) {
logger.error(error);
return res.status(500).json({ message: "Error deleting user" });
}
});
Expand Down
5 changes: 2 additions & 3 deletions functions/src/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CloudTasksClient } from "@google-cloud/tasks";
import * as admin from "firebase-admin";
import * as logger from "firebase-functions/logger";
import { logger } from "firebase-functions";
import {
onDocumentCreated,
onDocumentDeleted,
Expand Down Expand Up @@ -45,7 +45,6 @@ export const createTasksOnGoalCreate = onDocumentCreated(
notification: {
title: `${marginTime}分以内に目標を完了し写真をアップロードしましょう!`,
body: goalData.text,
click_action: "https://todo-real-c28fa.web.app/mycontent/",
},
},
};
Expand Down Expand Up @@ -73,7 +72,7 @@ export const createTasksOnGoalCreate = onDocumentCreated(
});
logger.info("Task created for goalId:", goalId);
} catch (error) {
logger.info("Error scheduling task:", error);
logger.error("Error scheduling task:", error);
}
}
);
Expand Down

0 comments on commit d74ca68

Please sign in to comment.