Skip to content

Commit

Permalink
🍁 Refactor Resource Form: Create new broadcast upon successful resour…
Browse files Browse the repository at this point in the history
…ce form submission (#1031)

* Refactor Resource Form: Create new broadcast upon successful resource form submission

* Refactor Resource Form: Create new broadcast upon successful resource form submission

* Refactor Resource Form: Create new broadcast upon successful resource form submission

* Refactor Resource Form: Create new broadcast upon successful resource form submission
  • Loading branch information
shivamgaur99 authored Jun 9, 2024
1 parent fe73f78 commit 70d21f4
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 952 deletions.
4 changes: 4 additions & 0 deletions backend/app/models/Broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const broadcastSchema = new Schema(
tags: {
type: Array,
},
isApproved: {
type: Boolean,
default: false,
},
},
{ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }
);
Expand Down
34 changes: 0 additions & 34 deletions backend/app/models/resource.js

This file was deleted.

19 changes: 10 additions & 9 deletions backend/app/routes/resources/@validationSchema/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const Joi = require('joi');

const ResourcesValidationSchema = Joi.object().keys({
name: Joi.string().trim().required().min(3),
email: Joi.string().trim().email().required(),
url: Joi.string().trim().required().min(10),
description: Joi.string().trim().required().min(5),
trustLevel: Joi.number().integer().required(),
expiryDate: Joi.date().required(),
additionalInfo: Joi.string().trim().min(5),
const postResourceValidationSchema = Joi.object().keys({
title: Joi.string().required(),
content: Joi.string().required(),
link: Joi.string().uri().required(),
expiresOn: Joi.date()
.min(new Date(new Date() - 100000))
.required(),
imageUrl: Joi.array().min(1).items(Joi.string().uri()).required(),
tags: Joi.array().min(1).items(Joi.string()).required(),
});

module.exports = ResourcesValidationSchema;
module.exports = postResourceValidationSchema;
48 changes: 0 additions & 48 deletions backend/app/routes/resources/addResource.js

This file was deleted.

14 changes: 0 additions & 14 deletions backend/app/routes/resources/deleteResource.js

This file was deleted.

11 changes: 0 additions & 11 deletions backend/app/routes/resources/getResource.js

This file was deleted.

13 changes: 4 additions & 9 deletions backend/app/routes/resources/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const router = require('express').Router({ mergeParams: true });
const addaResource = require('./addResource');
const deleteResource = require('./deleteResource');
const getResource = require('./getResource');
const postResource = require('./postResource');
const validation = require('../../../helpers/middlewares/validation');
const ResourcesValidationSchema = require('./@validationSchema');
const { authMiddleware } = require('../../../helpers/middlewares/auth');
const postResourceValidationSchema = require('./@validationSchema');

router.post('/', validation(ResourcesValidationSchema), addaResource);
// adding resouce in broadcast schema
router.post('/', validation(postResourceValidationSchema), postResource);

// Route for deleting a resource
router.delete('/deleteResource', deleteResource);
router.get('/getresources', authMiddleware, getResource);
module.exports = router;
21 changes: 21 additions & 0 deletions backend/app/routes/resources/postResource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const to = require('await-to-js').default;
const Broadcast = require('../../models/Broadcast');
const { ErrorHandler } = require('../../../helpers/error');
const constants = require('../../../constants');

module.exports = async (req, res, next) => {
const [err, response] = await to(Broadcast.create({ ...req.body }));
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Mongo Error: Insertion Failed',
errStack: err,
});
return next(error);
}
res.status(200).send({
message: 'Resource added successfully',
response: response,
});
return next();
};
35 changes: 0 additions & 35 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,12 @@ const cluster = require('./helpers/cluster');
const joinUs = require('./app/models/joinUs');
const contactUs = require('./app/models/contactUs');
const sendEmail = require('./utility/sendEmail');
const Resource = require('./app/models/resource');
const {
JoinUsCronJobMailTemplate,
ContactUsCronJobMailTemplate,
ResourceDeletedMailTemplate,
} = require('./utility/emailTemplates');
const Admin = require('./app/models/Admin');

// Cron job to delete all resource data for 2months
cronJob('0 0 2 * *', async (req, res, next) => {
try {
await Resource.deleteMany({});
console.log('deleting resource data');
const [err, response] = await to(Admin.find().select('email username'));
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'admin not found',
errStack: err,
});
return next(error);
}
try {
response.map(async (admin) => {
// eslint-disable-next-line no-unused-expressions, no-sequences
admin.email, 'Notifcation : Resource data deleted', ResourceDeletedMailTemplate(admin.username);
});
} catch (e) {
const error = new ErrorHandler(constants.ERRORS.EMAIL, {
statusCode: 500,
message: 'Sendgrid Error',
errStack: e,
});
return next(error);
}
} catch (error) {
return res.json(error);
}
return next();
});
// Running Join Us cronjob for 2 months - 0 0 2 * *
cronJob('0 0 2 * *', async (req, res, next) => {
try {
await joinUs.deleteMany({});
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/common/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const POST_SUCCUSS = "Successfully Post data!";
const POST_SUCCESS = "Successfully Post data!";
const POST_FAIL = "Unable to post data!";
const GET_SUCCESS = "Successfully get data!";
const GET_FAIL = "Unable to get data!";
Expand All @@ -7,4 +8,4 @@ const PATCH_FAIL = "Unable to Update data!";
const DELETE_SUCCESS = "Successfully Delete data!";
const DELETE_FAIL = "Unable to delete data!";

export { POST_SUCCUSS, POST_FAIL, GET_SUCCESS, GET_FAIL, PATCH_SUCCESS, PATCH_FAIL, DELETE_SUCCESS, DELETE_FAIL };
export { POST_SUCCUSS, POST_SUCCESS, POST_FAIL, GET_SUCCESS, GET_FAIL, PATCH_SUCCESS, PATCH_FAIL, DELETE_SUCCESS, DELETE_FAIL };
16 changes: 0 additions & 16 deletions frontend/src/pages/Admin/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ManageTeams } from "./Components/MangeTeams";
import { AddTeamMember } from "./Components/AddTeamMember";
import { About } from "./Components/About";
import { JoinUs } from "./Components/JoinUs";
import { Resource } from "./Components/Resource";
import { Faq } from "./Components/Faq";
import { Setting } from "./Components/Setting";
import { Invite } from "./Components/Setting/Invite";
Expand Down Expand Up @@ -164,19 +163,6 @@ export const Admin = (props) => {
<div className={style["span"]}>Join Us</div>
</div>
</li>
<li onClick={closeMobileMenu}>
<div
className={
tab === 12
? style["features-icons"]
: style["features-icons1"]
}
onClick={() => setTab(12)}
>
<i className="fas fa-book fa-fw fa-lg" aria-hidden="true"></i>
<div className={style["span"]}>Resources</div>
</div>
</li>
<li onClick={closeMobileMenu}>
<div
className={
Expand Down Expand Up @@ -234,8 +220,6 @@ export const Admin = (props) => {
<About setTab={setTab} />
) : tab === 11 ? (
<JoinUs />
) : tab === 12 ? (
<Resource />
) : tab === 5 ? (
<Faq setTab={setTab} />
) : tab === 6 ? (
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/pages/Admin/Components/Dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ export const Dashboard = (props) => {
icon: <i className="fa fa-handshake fa-lg" aria-hidden="true"></i>,
tab: 11,
},
{
name: "Resources",
icon: <i className="fa fa-book fa-lg" aria-hidden="true"></i>,
tab: 12,
},
{
name: "FAQ",
icon: <LiveHelpIcon style={{ fontSize: 23 }} />,
Expand Down
Loading

0 comments on commit 70d21f4

Please sign in to comment.