-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from badging/event-badging-table
API routes for event badging
- Loading branch information
Showing
13 changed files
with
258 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const Event = require("../models/event.model"); | ||
|
||
const createEvent = async (event) => { | ||
try { | ||
const { event_name, event_URL, badge, reviewers, application } = event; | ||
|
||
const newEvent = await Event.create({ | ||
event_name, | ||
event_URL, | ||
badge, | ||
reviewers, | ||
application, | ||
}); | ||
|
||
console.log(`Event ${newEvent.event_name} created successfully`); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
// Get all Events | ||
const getAllEvents = async (req, res) => { | ||
try { | ||
const events = await Event.findAll(); | ||
res.status(200).json(events); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
// Get event by ID | ||
const getEventById = async (req, res) => { | ||
const { id } = req.params; | ||
try { | ||
const event = await Event.findByPk(id); | ||
if (!event) { | ||
return res.status(404).json({ message: "Event not found" }); | ||
} | ||
res.status(200).json(event); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
createEvent, | ||
getAllEvents, | ||
getEventById, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { DataTypes } = require("sequelize"); | ||
const sequelize = require("../helpers/sequelize"); | ||
|
||
const Event = sequelize.define("events", { | ||
date: { | ||
type: DataTypes.DATE, | ||
defaultValue: DataTypes.NOW, | ||
allowNull: false, | ||
}, | ||
event_name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
event_URL: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
badge: { | ||
type: DataTypes.JSON, | ||
allowNull: false, | ||
}, | ||
reviewers: { | ||
type: DataTypes.JSON, | ||
allowNull: false, | ||
validate: { | ||
isValidReviewers(value) { | ||
if (!Array.isArray(value)) { | ||
throw new Error("Reviewers must be an array."); | ||
} | ||
for (const reviewer of value) { | ||
if (typeof reviewer.name !== "string" || reviewer.name.length > 255) { | ||
throw new Error( | ||
"Reviewer name must be a string with max length 255." | ||
); | ||
} | ||
if (!isValidURL(reviewer.github_profile_link)) { | ||
throw new Error("Invalid GitHub profile link format."); | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
application: { | ||
type: DataTypes.JSON, | ||
allowNull: false, | ||
}, | ||
}); | ||
|
||
// Helper function to validate URL format | ||
function isValidURL(url) { | ||
try { | ||
new URL(url); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = Event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const { createEvent } = require("../../database/controllers/event.controller"); | ||
const calculateBadge = require("./calculate.badge.js"); | ||
|
||
const saveEvent = async (octokit, payload) => { | ||
let { assigned_badge, badge_URL } = await calculateBadge(octokit, payload); // get badge name | ||
|
||
const event_name = payload.issue.title.replace(/\[(.*?)\] /gi, ""); | ||
let event_URL; | ||
if (payload.issue.title.includes("[Virtual Event]")) { | ||
event_URL = await payload.issue.body | ||
.slice( | ||
payload.issue.body.indexOf("- Link to the Event Website: "), | ||
payload.issue.body.indexOf( | ||
"- Provide verification that you are an event organizer: " | ||
) - 2 | ||
) | ||
.replace("- Link to the Event Website: ", ""); | ||
} | ||
|
||
if (payload.issue.title.includes("[In-Person Event]")) { | ||
event_URL = await payload.issue.body | ||
.slice( | ||
payload.issue.body.indexOf("- Link to the Event Website: "), | ||
payload.issue.body.indexOf("- Are you an organizer ") - 2 | ||
) | ||
.replace("- Link to the Event Website: ", ""); | ||
} | ||
// get badge name | ||
const badge = { | ||
name: assigned_badge, | ||
badgeURL: badge_URL, | ||
}; | ||
|
||
// get array of assignees | ||
const reviewers = await payload.issue.assignees.map((assignee) => { | ||
return { | ||
name: assignee.login, | ||
github_profile_link: assignee.html_url, | ||
}; | ||
}); | ||
|
||
const application = { | ||
app_no: payload.issue.number, | ||
app_URL: payload.issue.html_url, | ||
}; | ||
|
||
try { | ||
const newEvent = { | ||
event_name, | ||
event_URL, | ||
badge, | ||
reviewers, | ||
application, | ||
}; | ||
|
||
const event = await createEvent(newEvent); | ||
if (event) { | ||
return event; | ||
} else { | ||
return ""; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return ""; | ||
} | ||
}; | ||
|
||
module.exports = saveEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.