diff --git a/src/controllers/event/index.js b/src/controllers/event/index.js index e3ff818..ff01a4e 100644 --- a/src/controllers/event/index.js +++ b/src/controllers/event/index.js @@ -18,6 +18,11 @@ export const getEventById = async (req, res) => { return makeResponse({ res, data: result, message: 'Event retrieved successfully' }); }; +export const getEventBySlug = async (req, res) => { + const result = await eventService.retrieveBySlug(req.params.slug, req.user); + return makeResponse({ res, data: result, message: 'Event retrieved successfully' }); +}; + export const updateEvent = async (req, res) => { const result = await eventService.update(req.params.event_id, req.body, req.user); return makeResponse({ res, data: result, message: 'Event updated successfully' }); diff --git a/src/models/event/index.js b/src/models/event/index.js index f097e5c..d8787ac 100644 --- a/src/models/event/index.js +++ b/src/models/event/index.js @@ -18,6 +18,11 @@ const EventSchema = new mongoose.Schema( type: Number, required: true }, + slug: { + type: String, + unique: true + }, + guidelines: String, photo_urls: { 'default': String, 'sm': String, diff --git a/src/repository/event/index.js b/src/repository/event/index.js index af062f2..2a4a9da 100644 --- a/src/repository/event/index.js +++ b/src/repository/event/index.js @@ -91,6 +91,13 @@ export const findById = (id, user, filterFields = true) => { return query.exec(); }; +export const findBySlug = (slug, user, filterFields = true) => { + const baseFilters = { slug }; + let query = Event.findOne(eventFilters(user, baseFilters)).lean(); + if (filterFields) query = query.select('-creator_lock'); + return query.exec(); +}; + export const findOneAndUpdate = (filters, data) => Event.findOneAndUpdate(filters, dot(data), { new: true }); export const updateById = (id, data) => findOneAndUpdate({ _id: id }, data); diff --git a/src/routes/event.routes.js b/src/routes/event.routes.js index 626df11..f120333 100644 --- a/src/routes/event.routes.js +++ b/src/routes/event.routes.js @@ -8,6 +8,7 @@ import { deleteEvent, getAllEvents, getEventById, + getEventBySlug, getUserEventTicket, initiateTicketPayment, requestEventTicket, @@ -19,6 +20,7 @@ import { adminProtect, identify, protect } from '@/middleware/auth'; import { addEventSchema, eventIdSchema, + eventSlugSchema, eventTicketIdSchema, initiateTicketPaymentSchema, requestTicketSchema, @@ -51,6 +53,12 @@ events.delete( adminProtect, tracedAsyncHandler(deleteEvent) ); +events.get( + '/slugs/:slug', + identify, + celebrate({ [Segments.PARAMS]: eventSlugSchema }), + tracedAsyncHandler(getEventBySlug) +); events.post( '/:event_id/tickets/request', protect, diff --git a/src/services/event/index.js b/src/services/event/index.js index 54774fd..15aa489 100644 --- a/src/services/event/index.js +++ b/src/services/event/index.js @@ -13,6 +13,12 @@ export const retrieve = async (event_id, user) => { return result; }; +export const retrieveBySlug = async (slug, user) => { + const result = await eventRepository.findBySlug(slug, user); + if (!result) throw new createError(404, "Event doesn't exist or you do not have permission to access this event"); + return result; +}; + export const update = async (event_id, data, user) => { const event = await retrieve(event_id, user); if (data.name) { diff --git a/src/validations/event.js b/src/validations/event.js index 2570949..fb4cdc0 100644 --- a/src/validations/event.js +++ b/src/validations/event.js @@ -1,6 +1,7 @@ import { Joi } from 'celebrate'; const optionals = { + slug: Joi.string().optional(), photo_urls: Joi.object({ 'default': Joi.string().optional(), 'sm': Joi.string().optional(), @@ -10,6 +11,7 @@ const optionals = { '2xl': Joi.string().optional() }).optional(), tags: Joi.array().items(Joi.string()).min(2).max(2), + guidelines: Joi.string().optional(), faqs: Joi.array() .items( Joi.object({ @@ -80,6 +82,10 @@ export const eventIdSchema = { event_id: Joi.string().hex().length(24).required() }; +export const eventSlugSchema = { + slug: Joi.string().required() +}; + export const eventTicketIdSchema = { ...eventIdSchema, ticket_id: Joi.string().hex().length(24).required()