payload-validation #30

Merged
y merged 2 commits from payload-validation into staging 2025-04-02 09:47:26 +00:00
5 changed files with 42 additions and 23 deletions

@ -45,11 +45,11 @@ nostrRouter.post('/', async (req: Request, res: Response) => {
const { id, pubkey, created_at, kind, tags, content } = event const { id, pubkey, created_at, kind, tags, content } = event
const events = await collections.nostrEvents const existingEvent = await collections.nostrEvents?.findOne({
?.find({ nostrId: id }) nostrId: id
.toArray() })
if (events?.length) { if (existingEvent) {
throw new Error('nostr event with provided "id" exists') throw new Error('nostr event with provided "id" exists')
} }

@ -1,6 +1,8 @@
import express, { Request, Response } from 'express' import express, { Request, Response } from 'express'
import { collections } from '../services/database.service' import { collections } from '../services/database.service'
import { Review } from '../models' import { Review } from '../models'
import { reviewValidation, handleReqError, handleReqSuccess } from '../utils'
import Joi from 'joi'
// Global Config // Global Config
export const reviewsRouter = express.Router() export const reviewsRouter = express.Router()
@ -25,22 +27,28 @@ reviewsRouter.get('/', async (_req: Request, res: Response) => {
// POST // POST
reviewsRouter.post('/', async (req: Request, res: Response) => { reviewsRouter.post('/', async (req: Request, res: Response) => {
try { 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 existingReview = await collections.reviews?.findOne({
eventId: review.eventId
})
if (existingReview) {
throw new Error('review with provided "eventId" exists')
}
const result = await collections.reviews?.insertOne(review) const result = await collections.reviews?.insertOne(review)
if (result) { handleReqSuccess(res, result, 'review')
res
.status(201)
.send(`Successfully created a new review with id ${result.insertedId}`)
} else {
res.status(500).send('Failed to create a new review.')
}
} catch (error: unknown) { } catch (error: unknown) {
console.error(error) handleReqError(res, error)
if (error instanceof Error) {
res.status(400).send(error.message)
}
} }
}) })

@ -40,18 +40,18 @@ usersRouter.post('/', async (req: Request, res: Response) => {
} }
if (typeof newUser.npub === 'string') { if (typeof newUser.npub === 'string') {
const users = await collections.users const existingUser = await collections.users?.findOne({
?.find({ npub: newUser.npub }) npub: newUser.npub
.toArray() })
if (users?.length) { if (existingUser) {
throw new Error('user with provided "npub" exists') throw new Error('user with provided "npub" exists')
} }
} else { } else {
for (const npub of newUser.npub) { 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') throw new Error('user with provided "npub" exists')
} }
} }

@ -1,2 +1,3 @@
export * from './user' export * from './user'
export * from './nostr' export * from './nostr'
export * from './review'

@ -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)