Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add create cfp #595

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions server/schema/cfps/CfpsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const CfpsSchema = new Schema(
},
slug: {
type: String,
require: true,
require: false,
},
organizer: {
type: String,
Expand All @@ -35,7 +35,7 @@ const CfpsSchema = new Schema(
require: true,
},
tags: {
type: [String],
type: [{ id: String, name: String }],
require: true,
},
logo: {
Expand All @@ -46,12 +46,12 @@ const CfpsSchema = new Schema(
type: String,
require: true,
},
deadline: {
type: Date,
required: true,
},
// deadline: {
// type: Date,
// required: true,
// },
topics: {
type: [String],
type: [{ name: String, details: String }],
required: true,
},
guidelines: {
Expand Down
20 changes: 17 additions & 3 deletions server/src/cpfs/cfp.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,21 @@ cfpRouter.post(
});
}

const data = req.body;
const data = {};
data.name = req.body.name;
data.organizer = req.body.organizer;
data.description = req.body.description;
data.address = JSON.parse(req.body.address);
data.date = new Date(req.body.date);
data.duration = parseInt(req.body.duration);
data.tags = JSON.parse(req.body.tags);
data.topics = JSON.parse(req.body.topics);
data.link = req.body.link;
data.image = req.body.image;
data.logo = req.body.logo;
data.guidelines = req.body.guidelines;
const cfps = await CfpService.createCfp(data);
res.status(200).send({ success: true, cfps });
res.status(200).send({ success: true, cfps: cfps });
} catch (error) {
res
.status(500)
Expand All @@ -108,7 +120,8 @@ cfpRouter.post(
// }

const { id } = req.params;
const data = {};

let data = {};
data.name = req.body.name;
data.organizer = req.body.organizer;
data.description = req.body.description;
Expand All @@ -121,6 +134,7 @@ cfpRouter.post(
data.image = req.body.image;
data.logo = req.body.logo;
data.guidelines = req.body.guidelines;

const cfps = await CfpService.updateCfp(id, data);
res.status(200).send({ success: true, cfps });
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/cpfs/cfp.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const getCfpsBySlug = async (slug) => {
}
};

const createCfp = async (eventInfo) => {
const createCfp = async (cfpInfo) => {
try {
const cfp = new CfpModel(eventInfo);
const cfp = new CfpModel(cfpInfo);
await cfp.save();
return cfp;
} catch (error) {
Expand Down
100 changes: 47 additions & 53 deletions server/src/validator-schema/cfpValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,32 @@ const createCfpSchema = {
errorMessage: 'Name cannot be empty',
},
},
slug: { in: ['body'], isString: true, notEmpty: true },
organizer: { in: ['body'], isString: true, notEmpty: true },
description: { in: ['body'], isString: true, notEmpty: true },

address: {
in: ['body'],
isObject: true,
notEmpty: {
errorMessage: 'address cannot be empty',
},
},
'address.isOnline': {
in: ['body'],
isBoolean: {
errorMessage: 'Invalid value for isOnline',
},
notEmpty: {
errorMessage: 'isOnline cannot be empty',
},
},
'address.location': {
in: ['body'],
custom: {
options: (value, { req }) => {
if (!req.body.address.isOnline && !value) {
throw new Error('location is required when isOnline is false');
options: (value) => {
if (typeof JSON.parse(value) !== 'object') {
throw new Error('Address must be an object');
}
if (
JSON.parse(value).isOnline === false &&
JSON.parse(value).location.length === 0
) {
throw new Error('If event is offline it should have a location');
}
return true;
},
},
notEmpty: {
errorMessage: 'Address cannot be empty',
},
},
image: {
in: ['body'],
custom: {
options: (value) => {
if (!/\.(png|jpg|jpeg)$/.test(value)) {
throw new Error('Image must have a valid extension png, jpg, jpeg');
}
return true;
},
},
isString: true,
optional: true,
},
date: {
Expand All @@ -87,54 +72,63 @@ const createCfpSchema = {
},
duration: {
in: ['body'],
isNumeric: {
errorMessage: 'Invalid duration value',
},
isNumeric: true,
notEmpty: {
errorMessage: 'duration cannot be empty',
},
},
tags: {
in: ['body'],
isArray: {
errorMessage: 'Tags must be an array',
options: { min: 1 },
},
notEmpty: {
errorMessage: 'Tags array must not be empty',
},
},
logo: {
in: ['body'],
custom: {
options: (value) => {
if (!/\.(png|jpg|jpeg)$/.test(value)) {
throw new Error('Logo must have a valid extension png, jpg, jpeg');
if (!Array.isArray(JSON.parse(value))) {
throw new Error('Tags must be an array');
}
if (JSON.parse(value).length < 1) {
throw new Error('Tags array must contain at least one item');
}
return true;
},
notEmpty: {
errorMessage: 'Tags array must not be empty',
},
},
optional: true,
},
link: {
logo: {
in: ['body'],
isURL: true,
notEmpty: true,
isString: true,
optional: true,
},
deadline: {
link: {
in: ['body'],
isDate: true,
notEmpty: true,
isString: true,
notEmpty: {
errorMessage: 'Link cannot be empty',
},
},
topics: {
in: ['body'],
isArray: true,
notEmpty: true,
custom: {
options: (value) => {
if (!Array.isArray(JSON.parse(value))) {
throw new Error('Topics must be an array');
}
if (JSON.parse(value).length < 1) {
throw new Error('Topics array must contain at least one item');
}
return true;
},
notEmpty: {
errorMessage: 'Topics array must not be empty',
},
},
},
guidelines: {
in: ['body'],
isString: true,
notEmpty: true,
notEmpty: {
errorMessage: 'Guidelines cannot be empty',
},
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/NewsLetter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,4 @@ const NewsLetter = () => {
);
};

export default NewsLetter;
export default NewsLetter;
32 changes: 32 additions & 0 deletions src/pages/api/cfps/addCfp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { NextApiRequest, NextApiResponse } from 'next';

export const config = {
api: {
bodyParser: true,
},
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const response = await fetch(
(process.env.NEXT_PUBLIC_API_ENDPOINT as string) + '/api/cfps/create',
{
method: 'post',
body: JSON.stringify(req.body),
headers: {
'Content-type': 'application/json',
},
}
).then((r) => {
if (r.status === 500) {
throw 'Some error occurrend';
}
return r.json();
});
res.status(200).json(response);
} catch (error) {
res.status(500).json({ success: false, error: error });
}
}
Loading