diff --git a/src/models/nostrEvent.ts b/src/models/nostrEvent.ts index 351ca36..201c6ec 100644 --- a/src/models/nostrEvent.ts +++ b/src/models/nostrEvent.ts @@ -7,6 +7,6 @@ export class NostrEvent { public kind: number, // event type, e.g., review, article, comment public tags: string[][], // array of keywords or hashtags public content: string, // text content of the event - public id?: ObjectId + public id?: ObjectId // database object id ) {} } diff --git a/src/models/user.ts b/src/models/user.ts index 93fe1ca..d25245b 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -3,9 +3,9 @@ import { UserRole } from '../types' export class User { constructor( - public name: string, - public npub: string | string[], - public role: UserRole, - public id?: ObjectId + public name: string, // name + public npub: string, // npub + public role: UserRole, // user role (user, reviewer, producer) + public id?: ObjectId // database object id ) {} } diff --git a/src/models/wine.ts b/src/models/wine.ts index 1bc19cb..4a1ab9c 100644 --- a/src/models/wine.ts +++ b/src/models/wine.ts @@ -22,8 +22,8 @@ export class Wine { public viticulture: Viticulture, // two-letter country codes public sulfites: number, // parts per million public filtered: boolean, // is wine filtered (fined (egg or fish)) - public vegan: boolean, - public kosher: boolean, + public vegan: boolean, // if wine is vegan + public kosher: boolean, // if wine is kosher public closure: BottleClosure, // cork, crown-seal, screwcap public RRPamount: number, // 20 public RRPcurrency: CurrencyCode, // USD diff --git a/src/routes/coffee.router.ts b/src/routes/coffee.router.ts index 9cfa014..acbc9e6 100644 --- a/src/routes/coffee.router.ts +++ b/src/routes/coffee.router.ts @@ -2,7 +2,6 @@ import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { Coffee } from '../models' -// Global Config export const coffeeRouter = express.Router() coffeeRouter.use(express.json()) diff --git a/src/routes/nostr.router.ts b/src/routes/nostr.router.ts index c30e5a5..ede0f06 100644 --- a/src/routes/nostr.router.ts +++ b/src/routes/nostr.router.ts @@ -1,4 +1,3 @@ -// External Dependencies import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { NostrEvent } from '../models' @@ -10,7 +9,6 @@ import { import { Event } from 'nostr-tools' import Joi from 'joi' -// Global Config export const nostrRouter = express.Router() nostrRouter.use(express.json()) diff --git a/src/routes/reviews.router.ts b/src/routes/reviews.router.ts index 02fa1b9..91becfb 100644 --- a/src/routes/reviews.router.ts +++ b/src/routes/reviews.router.ts @@ -4,7 +4,6 @@ import { Review } from '../models' import { reviewValidation, handleReqError, handleReqSuccess } from '../utils' import Joi from 'joi' -// Global Config export const reviewsRouter = express.Router() reviewsRouter.use(express.json()) diff --git a/src/routes/sake.router.ts b/src/routes/sake.router.ts index 4bdda38..42a24e4 100644 --- a/src/routes/sake.router.ts +++ b/src/routes/sake.router.ts @@ -2,7 +2,6 @@ import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { Sake } from '../models' -// Global Config export const sakeRouter = express.Router() sakeRouter.use(express.json()) diff --git a/src/routes/spirits.router.ts b/src/routes/spirits.router.ts index 6904a61..d0b1246 100644 --- a/src/routes/spirits.router.ts +++ b/src/routes/spirits.router.ts @@ -2,7 +2,6 @@ import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { Spirit } from '../models' -// Global Config export const spiritsRouter = express.Router() spiritsRouter.use(express.json()) diff --git a/src/routes/users.router.ts b/src/routes/users.router.ts index 50cdc22..75bf5fa 100644 --- a/src/routes/users.router.ts +++ b/src/routes/users.router.ts @@ -1,11 +1,9 @@ -// External Dependencies import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { User } from '../models' import { userValidation, handleReqError, handleReqSuccess } from '../utils' import Joi from 'joi' -// Global Config export const usersRouter = express.Router() usersRouter.use(express.json()) @@ -39,22 +37,12 @@ usersRouter.post('/', async (req: Request, res: Response) => { throw error.details[0].message } - if (typeof newUser.npub === 'string') { - const existingUser = await collections.users?.findOne({ - npub: newUser.npub - }) + const existingUser = await collections.users?.findOne({ + npub: newUser.npub + }) - if (existingUser) { - throw new Error('user with provided "npub" exists') - } - } else { - for (const npub of newUser.npub) { - const existingUser = await collections.users?.findOne({ npub }) - - if (existingUser) { - throw new Error('user with provided "npub" exists') - } - } + if (existingUser) { + throw new Error('user with provided "npub" exists') } const result = await collections.users?.insertOne(newUser) diff --git a/src/routes/wines.router.ts b/src/routes/wines.router.ts index 7d081bf..e83e397 100644 --- a/src/routes/wines.router.ts +++ b/src/routes/wines.router.ts @@ -2,7 +2,6 @@ import express, { Request, Response } from 'express' import { collections } from '../services/database.service' import { Wine } from '../models' -// Global Config export const winesRouter = express.Router() winesRouter.use(express.json()) diff --git a/src/utils/validation/user.ts b/src/utils/validation/user.ts index ddef1f7..657364e 100644 --- a/src/utils/validation/user.ts +++ b/src/utils/validation/user.ts @@ -2,26 +2,21 @@ import Joi from 'joi' import { UserRole } from '../../types' import { npubToHex, validateHex } from '../nostr' -const npubValidation = (value: unknown, helper: Joi.CustomHelpers<unknown>) => { - const hex = npubToHex(value as string) - - if (!hex || !validateHex(hex)) { - return helper.message({ - custom: Joi.expression('"npub" contains an invalid value') - }) - } - - return hex -} - export const userValidation = (data: unknown): Joi.ValidationResult => Joi.object({ name: Joi.string().required(), - npub: Joi.alternatives() - .try( - Joi.array().items(Joi.string().custom(npubValidation)), - Joi.string().custom(npubValidation) - ) + npub: Joi.string() + .custom((value, helper) => { + const hex = npubToHex(value as string) + + if (!hex || !validateHex(hex)) { + return helper.message({ + custom: Joi.expression('"npub" contains an invalid value') + }) + } + + return hex + }) .required(), role: Joi.string() .valid(...Object.values(UserRole))