From 35b2cec312c85997b05afa9e6fc2280798cacb0b Mon Sep 17 00:00:00 2001 From: nostrdev-com <support@nostrdev.com> Date: Wed, 2 Apr 2025 12:45:17 +0300 Subject: [PATCH 1/2] feat(review): added review validation --- src/routes/nostr.router.ts | 8 ++++---- src/routes/reviews.router.ts | 34 +++++++++++++++++++++------------- src/routes/users.router.ts | 12 ++++++------ src/utils/validation/index.ts | 1 + src/utils/validation/review.ts | 10 ++++++++++ 5 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 src/utils/validation/review.ts diff --git a/src/routes/nostr.router.ts b/src/routes/nostr.router.ts index 3d46cad..c30e5a5 100644 --- a/src/routes/nostr.router.ts +++ b/src/routes/nostr.router.ts @@ -45,11 +45,11 @@ nostrRouter.post('/', async (req: Request, res: Response) => { const { id, pubkey, created_at, kind, tags, content } = event - const events = await collections.nostrEvents - ?.find({ nostrId: id }) - .toArray() + const existingEvent = await collections.nostrEvents?.findOne({ + nostrId: id + }) - if (events?.length) { + if (existingEvent) { throw new Error('nostr event with provided "id" exists') } diff --git a/src/routes/reviews.router.ts b/src/routes/reviews.router.ts index 5ca69f2..c5b7054 100644 --- a/src/routes/reviews.router.ts +++ b/src/routes/reviews.router.ts @@ -1,6 +1,8 @@ import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { Review } from '../models' +import { reviewValidation, handleReqError, handleReqSuccess } from '../utils' +import Joi from 'joi' // Global Config export const reviewsRouter = express.Router() @@ -25,22 +27,28 @@ reviewsRouter.get('/', async (_req: Request, res: Response) => { // POST reviewsRouter.post('/', async (req: Request, res: Response) => { try { - const review = req.body as Review + const { + error, + value: review + }: { error: Joi.ValidationError | undefined; value: Review } = + reviewValidation(req.body) + + if (error) { + throw error.details[0].message + } + + const reviews = await collections.reviews?.findOne({ + eventId: review.eventId + }) + + if (reviews) { + throw new Error('review with provided "eventId" exists') + } const result = await collections.reviews?.insertOne(review) - if (result) { - res - .status(201) - .send(`Successfully created a new review with id ${result.insertedId}`) - } else { - res.status(500).send('Failed to create a new review.') - } + handleReqSuccess(res, result, 'review') } catch (error: unknown) { - console.error(error) - - if (error instanceof Error) { - res.status(400).send(error.message) - } + handleReqError(res, error) } }) diff --git a/src/routes/users.router.ts b/src/routes/users.router.ts index a203b37..50cdc22 100644 --- a/src/routes/users.router.ts +++ b/src/routes/users.router.ts @@ -40,18 +40,18 @@ usersRouter.post('/', async (req: Request, res: Response) => { } if (typeof newUser.npub === 'string') { - const users = await collections.users - ?.find({ npub: newUser.npub }) - .toArray() + const existingUser = await collections.users?.findOne({ + npub: newUser.npub + }) - if (users?.length) { + if (existingUser) { throw new Error('user with provided "npub" exists') } } else { for (const npub of newUser.npub) { - const users = await collections.users?.find({ npub }).toArray() + const existingUser = await collections.users?.findOne({ npub }) - if (users?.length) { + if (existingUser) { throw new Error('user with provided "npub" exists') } } diff --git a/src/utils/validation/index.ts b/src/utils/validation/index.ts index 7a791a3..baa0c5a 100644 --- a/src/utils/validation/index.ts +++ b/src/utils/validation/index.ts @@ -1,2 +1,3 @@ export * from './user' export * from './nostr' +export * from './review' diff --git a/src/utils/validation/review.ts b/src/utils/validation/review.ts new file mode 100644 index 0000000..7e77c8d --- /dev/null +++ b/src/utils/validation/review.ts @@ -0,0 +1,10 @@ +import Joi from 'joi' + +export const reviewValidation = (data: unknown): Joi.ValidationResult => + Joi.object({ + eventId: Joi.string().required(), + productId: Joi.string().required(), + rating: Joi.number().required(), + reviewText: Joi.string().required(), + tastingNotes: Joi.array().items(Joi.string()) + }).validate(data) -- 2.43.0 From 14b61fa0f3f00fc4a934b02e1c13d327f415aac3 Mon Sep 17 00:00:00 2001 From: nostrdev-com <support@nostrdev.com> Date: Wed, 2 Apr 2025 12:46:36 +0300 Subject: [PATCH 2/2] chore: fixed variable name --- src/routes/reviews.router.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/reviews.router.ts b/src/routes/reviews.router.ts index c5b7054..02fa1b9 100644 --- a/src/routes/reviews.router.ts +++ b/src/routes/reviews.router.ts @@ -37,11 +37,11 @@ reviewsRouter.post('/', async (req: Request, res: Response) => { throw error.details[0].message } - const reviews = await collections.reviews?.findOne({ + const existingReview = await collections.reviews?.findOne({ eventId: review.eventId }) - if (reviews) { + if (existingReview) { throw new Error('review with provided "eventId" exists') } -- 2.43.0