diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 4432e69..3755e50 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -33,7 +33,7 @@ export const protect = catchAsync( // 2) Verification token const decoded: any = await jwtVerifyPromisified(token, config.JWT_SECRET); //const decoded = await promisify(jwt.verify)(token, process.env.JWT_SECRET); - console.log(decoded); + // 3) Check if user still exists const currentUser = await UserModel.findById(decoded.id); if (!currentUser) { diff --git a/src/controllers/pub.controller.ts b/src/controllers/pub.controller.ts index 07d5bca..1b55a2c 100644 --- a/src/controllers/pub.controller.ts +++ b/src/controllers/pub.controller.ts @@ -1,6 +1,13 @@ import { NextFunction, Request, Response } from "express"; +import PubModel from "../models/pub.model"; import { createPubInput } from "../schema/pub.schema"; -import { createPub, deletePub, getAllPubs } from "../services/pub.service"; +import { + createPub, + deletePub, + getAllPubs, + getPub, + updatePub, +} from "../services/pub.service"; import AppError from "../utils/appError"; import catchAsync from "../utils/catchAsync"; @@ -48,3 +55,38 @@ export const getAllPubsHandler = catchAsync( }); } ); + +export const getPubHandler = catchAsync( + async (req: Request, res: Response, next: NextFunction) => { + const pub: object = await getPub(req.params.pubId); + if (!pub) { + return next(new AppError("No pub found with that ID", 404)); + } + res.status(200).json({ + status: "success", + data: { + pub, + }, + }); + } +); + +export const updatePubHandler = catchAsync( + async (req: Request, res: Response, next: NextFunction) => { + const pub: object = await updatePub( + req.params.pubId, + req.body, + res.locals.user + ); + + if (!pub) { + return next(new AppError("No pub found with that ID", 404)); + } + res.status(200).json({ + status: "success", + data: { + pub, + }, + }); + } +); diff --git a/src/models/pub.model.ts b/src/models/pub.model.ts index f75c822..4a3af48 100644 --- a/src/models/pub.model.ts +++ b/src/models/pub.model.ts @@ -27,6 +27,13 @@ pubSchema.pre("save", async function (next) { this.user = userId; next(); }); */ +/* pubSchema.pre(/^findOneAnd/, async function (next) { + const pub = await this.findOne(); + this.title = pub.title; + this.description = pub.description; + this.status = pub.status; + next(); +}); */ const PubModel = mongoose.model("Pub", pubSchema); diff --git a/src/routes.ts b/src/routes.ts index 6f3a565..6847e9f 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -9,6 +9,8 @@ import { createPubHandler, deletePubHandler, getAllPubsHandler, + getPubHandler, + updatePubHandler, } from "./controllers/pub.controller"; import { createPubSchema } from "./schema/pub.schema"; import { setPubUserId } from "./services/pub.service"; @@ -28,7 +30,8 @@ function routes(app: Express) { setPubUserId, createPubHandler ); - app.patch("/api/pubs/:pubId"); + app.get("/api/pubs/:pubId", protect, getPubHandler); + app.patch("/api/pubs/:pubId", protect, updatePubHandler); app.delete("/api/pubs/:pubId", protect, deletePubHandler); app.get("/api/users"); app.get("/api/users/:userId"); diff --git a/src/services/pub.service.ts b/src/services/pub.service.ts index f6aec52..d775793 100644 --- a/src/services/pub.service.ts +++ b/src/services/pub.service.ts @@ -55,3 +55,35 @@ export async function getAllPubs() { return new AppError(error, 404); } } + +export async function getPub(id: any) { + try { + const pub = await PubModel.findById(id); + return pub; + } catch (error) { + return new AppError(error, 404); + } +} + +export async function updatePub(id: any, body: object, user: any) { + try { + const pubToUpdate = await PubModel.findById(id); + const pubToUpdateUserId = pubToUpdate?.user.toString(); + const userId = user._id.toString(); + + //return pub; + if (user.role === "user") { + if (pubToUpdateUserId === userId) { + await PubModel.findByIdAndUpdate(id, body); + const pubUpdated = await PubModel.findById(id); + return pubUpdated; + } + } else if (user.role === "admin") { + await PubModel.findByIdAndUpdate(id, body); + const pubUpdated = await PubModel.findById(id); + return pubUpdated; + } + } catch (error) { + return new AppError(error, 404); + } +} diff --git a/src/utils/connect.ts b/src/utils/connect.ts index 2d71b7a..3ea5f53 100644 --- a/src/utils/connect.ts +++ b/src/utils/connect.ts @@ -6,7 +6,6 @@ dotenv.config({ path: "../../config.env" }); async function connect() { const dbUrl = config.DATABASE; - console.log(dbUrl); try { await mongoose.connect(dbUrl);