Skip to content

Commit

Permalink
Merge branch 'main' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Nov 20, 2023
2 parents 282ccd9 + e933c10 commit e188157
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/controllers/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
5 changes: 5 additions & 0 deletions src/models/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/repository/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/routes/event.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
deleteEvent,
getAllEvents,
getEventById,
getEventBySlug,
getUserEventTicket,
initiateTicketPayment,
requestEventTicket,
Expand All @@ -19,6 +20,7 @@ import { adminProtect, identify, protect } from '@/middleware/auth';
import {
addEventSchema,
eventIdSchema,
eventSlugSchema,
eventTicketIdSchema,
initiateTicketPaymentSchema,
requestTicketSchema,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/services/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/validations/event.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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({
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e188157

Please sign in to comment.