Skip to content

Commit

Permalink
fix: changes to events
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes committed Feb 27, 2024
1 parent 9da9de9 commit 3c6f89b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
33 changes: 21 additions & 12 deletions routes/events/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const router = express.Router();
const Event = require("../../schema/events/EventSchema");
const { STATUSCODE, STATUSMESSAGE } = require("../../static/Status");
const { setTime } = require("../../utils/SetTime");
const jwt = require("jsonwebtoken");
const User = require("../../schema/user/UserSchema");

router.get("/", async (req, res) => {
try {
Expand Down Expand Up @@ -30,19 +32,21 @@ router.post("/create", async (req, res) => {
try {
const { uid, ...data } = req.body;

// Validate required fields:
if (
!data.name ||
!uid ||
!data.about ||
!data.hostUsername ||
!data.hostName ||
!data.thumbnailImage ||
!data.description ||
!data.coverImage ||
!data.mode ||
!data.date ||
!data.startDate ||
!data.endDate ||
!data.startTime ||
!data.endTime ||
!data.timezone
!data.city ||
!data.state ||
!data.country ||
!data.address ||
!data.mapIframe
) {
console.log(data);
return res
Expand All @@ -58,15 +62,20 @@ router.post("/create", async (req, res) => {
.json({ message: "Already exists" });
}

// Combine sanitized data with timestamp fields:
const { Token } = req.cookies;
const user = await User.findOne({
email: jwt.verify(Token, process.env.JWT_SECRET).User.id,
});

const newEvent = new Event({
...data,
uid,
hostName: user.name,
hostUsername: user.username,
createdAt: setTime(),
updatedAt: setTime(),
});

// Validate data before saving:
const validationErrors = newEvent.validateSync();
if (validationErrors) {
const errors = Object.values(validationErrors.errors).map(
Expand All @@ -77,11 +86,11 @@ router.post("/create", async (req, res) => {
.json({ message: "Event Validation failed", errors });
}

// Save the event:
const savedEvent = await newEvent.save();

// Respond with the created event:
res.status(STATUSCODE.CREATED).json(savedEvent);
res
.status(STATUSCODE.CREATED)
.json({ message: "Event Created", savedEvent });
} catch (error) {
console.error(error);
res
Expand Down
69 changes: 38 additions & 31 deletions schema/events/EventSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ const EventSchema = new mongoose.Schema({
required: true,
unique: true,
},
about: {
description: {
type: String,
required: true,
trim: true,
},

hostUsername: {
type: String,
required: true,
Expand All @@ -26,20 +25,19 @@ const EventSchema = new mongoose.Schema({
required: true,
trim: true,
},
bannerImage: {
coverImage: {
type: String,
trim: true,
},
thumbnailImage: {
type: String,
required: true,
trim: true,
},

// thumbnailImage: {
// type: String,
// required: true,
// trim: true,
// },
mode: {
type: String,
required: true,
enum: ["online", "offline"],
enum: ["Online", "Offline"],
},
address: {
type: String,
Expand All @@ -57,14 +55,14 @@ const EventSchema = new mongoose.Schema({
type: String,
trim: true,
},
iframe: {
mapIframe: {
type: String,
trim: true,
},
date: {
type: Date,
required: true,
},
// date: {
// type: Date,
// required: true,
// },
startTime: {
type: Date,
required: true,
Expand All @@ -73,26 +71,35 @@ const EventSchema = new mongoose.Schema({
type: Date,
required: true,
},
timezone: {
type: String,
required: true,
},
tags: {
type: [String],
default: [],
},
isApproved: {
type: Boolean,
default: false,
},
createdAt: {
startDate: {
type: Date,
default: Date.now,
required: true,
},
updatedAt: {
endDate: {
type: Date,
default: Date.now,
required: true,
},

// timezone: {
// type: String,
// required: true,
// },
// tags: {
// type: [String],
// default: [],
// },
// isApproved: {
// type: Boolean,
// default: false,
// },
// createdAt: {
// type: Date,
// default: Date.now,
// },
// updatedAt: {
// type: Date,
// default: Date.now,
// },
});

// Indexes for efficient querying:
Expand Down

0 comments on commit 3c6f89b

Please sign in to comment.