Skip to content

Commit

Permalink
new stable release
Browse files Browse the repository at this point in the history
new stable release
  • Loading branch information
alesancor1 authored Nov 6, 2021
2 parents e9e944d + 2b02e24 commit f0b0938
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 44 deletions.
11 changes: 7 additions & 4 deletions api/oas-doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,13 @@ components:
type: string
format: date-time
eventTags:
type: string
type: array
items:
type: string
eventOrganizer:
type: string
eventSpeaker:
type: integer
format: int32
eventLocation:
type: string
speaker:
type: object
required:
Expand All @@ -233,6 +234,8 @@ components:
type: string
speakerUrls:
type: array
items:
type: string
post:
type: object
required:
Expand Down
25 changes: 18 additions & 7 deletions controllers/apiv1eventsControllerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
const db = require('../utils/dbConnection');

module.exports.getEvents = function getEvents (req, res, next) {
const sql = `SELECT posts.ID,post_title,start,end,time_start,time_end
FROM wp_posts posts RIGHT JOIN wp_mec_events ev on posts.ID = ev.post_id
WHERE post_type = 'mec-events' and post_status not like 'auto-draft';`;
const sql = `SELECT ID,post_title,
MAX(CASE WHEN meta_key='mec_start_date' THEN meta_value END) AS start,
MAX(CASE WHEN meta_key='mec_end_date' THEN meta_value END) AS end,
MAX(CASE WHEN meta_key='mec_start_day_seconds' THEN meta_value END) AS time_start,
MAX(CASE WHEN meta_key='mec_end_day_seconds' THEN meta_value END) AS time_end,
MAX(CASE WHEN meta_key='mec_organizer_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS organizer,
MAX(CASE WHEN meta_key='mec_location_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS location
FROM wp_posts
RIGHT JOIN wp_postmeta ON ID=post_id
WHERE post_type = 'mec-events' and post_status not like 'auto-draft'
GROUP BY ID;`;

db.query(sql).then((result) => {
const events = result.map((item) => {
const start = item.start === '0000-00-00' ? new Date() : new Date(`${item.start}`);
const end = item.end === '0000-00-00' ? new Date(`${item.start}`) : new Date(`${item.end}`);
const events = result.filter((item) => item.start && item.time_start && item.end && item.time_end).map((item) => {
const start = new Date(`${item.start}`);
const end = new Date(`${item.end}`);
start.setSeconds(item.time_start);
end.setSeconds(item.time_end);

return {
const response = {
eventId: item.ID,
eventName: item.post_title,
eventStartDateTime: start.toISOString(),
eventEndDateTime: end.toISOString()
};
if (item.organizer && !item.organizer.match(/Sin categor.*/)) response.eventOrganizer = item.organizer;
if (item.location && !item.location.match(/Sin categor.*/)) response.eventLocation = item.location;
return response;
});
res.status(200).send(events);
}).catch((err) => {
Expand Down
28 changes: 20 additions & 8 deletions controllers/apiv1eventseventIdControllerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@
const db = require('../utils/dbConnection');

module.exports.findEventByEventId = function findEventByEventId (req, res, next) {
const sql = `SELECT posts.ID,post_title,start,end,time_start,time_end
FROM wp_posts posts RIGHT JOIN wp_mec_events ev on posts.ID = ev.post_id
WHERE post_type = 'mec-events' and post_status not like 'auto-draft' and posts.ID=?;`;
const sql = `SELECT ID,post_title,
MAX(CASE WHEN meta_key='mec_start_date' THEN meta_value END) AS start,
MAX(CASE WHEN meta_key='mec_end_date' THEN meta_value END) AS end,
MAX(CASE WHEN meta_key='mec_start_day_seconds' THEN meta_value END) AS time_start,
MAX(CASE WHEN meta_key='mec_end_day_seconds' THEN meta_value END) AS time_end,
MAX(CASE WHEN meta_key='mec_organizer_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS organizer,
MAX(CASE WHEN meta_key='mec_location_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS location
FROM wp_posts
RIGHT JOIN wp_postmeta ON ID=post_id
WHERE post_type = 'mec-events' and post_status not like 'auto-draft' and ID=?
GROUP BY ID;`;

if (req.eventId.value && req.eventId.value.match(/^[0-9]+$/)) {
db.query(sql, [req.eventId.value]).then((result) => {
if (result.length === 0) {
res.status(404).send({ message: 'Event not found' });
} else if (!result[0].start || !result[0].time_start || !result[0].end || !result[0].time_end) {
res.status(406).send({ message: 'Event has invalid fields' });
} else {
let event = result[0];
const start = event.start === '0000-00-00' ? new Date() : new Date(`${event.start}`);
const end = event.end === '0000-00-00' ? new Date(`${event.start}`) : new Date(`${event.end}`);
const event = result[0];
const start = new Date(`${event.start}`);
const end = new Date(`${event.end}`);
start.setSeconds(event.time_start);
end.setSeconds(event.time_end);

event = {
const response = {
eventId: event.ID,
eventName: event.post_title,
eventStartDateTime: start.toISOString(),
eventEndDateTime: end.toISOString()
};
if (event.organizer && !event.organizer.match(/Sin categor.*/)) response.eventOrganizer = event.organizer;
if (event.location && !event.location.match(/Sin categor.*/)) response.eventLocation = event.location;

res.status(200).send(event);
res.status(200).send(response);
}
}).catch((err) => {
res.status(500).send({ message: err.message });
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@
"ignore": [
"/tests/**"
]
},
"nyc": {
"exclude": [
"utils/dbConnection.js",
"tests",
"server.js"
]
}
}
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const deploy = (env) => {
http.createServer(app).listen(serverPort, function () {
if (env !== 'test') {
console.log('________________________________________________________________');
console.log('App running at http://localhost:' + serverPort);
console.log('App running!');
console.log('________________________________________________________________');
if (options.docs !== false) {
console.log('API docs (Swagger UI) available on http://localhost:' + serverPort + '/docs');
console.log('API docs (Swagger UI) available on /docs');
console.log('________________________________________________________________');
}
}
Expand Down
58 changes: 35 additions & 23 deletions tests/test.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$defs" : {
"queries" : {
"events" : "SELECT posts.ID,post_title,start,end,time_start,time_end \n FROM wp_posts posts RIGHT JOIN wp_mec_events ev on posts.ID = ev.post_id \n WHERE post_type = 'mec-events' and post_status not like 'auto-draft';",
"eventId": "SELECT posts.ID,post_title,start,end,time_start,time_end \n FROM wp_posts posts RIGHT JOIN wp_mec_events ev on posts.ID = ev.post_id \n WHERE post_type = 'mec-events' and post_status not like 'auto-draft' and posts.ID=?;",
"events" : "SELECT ID,post_title,\n MAX(CASE WHEN meta_key='mec_start_date' THEN meta_value END) AS start,\n MAX(CASE WHEN meta_key='mec_end_date' THEN meta_value END) AS end,\n MAX(CASE WHEN meta_key='mec_start_day_seconds' THEN meta_value END) AS time_start,\n MAX(CASE WHEN meta_key='mec_end_day_seconds' THEN meta_value END) AS time_end,\n MAX(CASE WHEN meta_key='mec_organizer_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS organizer,\n MAX(CASE WHEN meta_key='mec_location_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS location\n FROM wp_posts\n RIGHT JOIN wp_postmeta ON ID=post_id\n WHERE post_type = 'mec-events' and post_status not like 'auto-draft'\n GROUP BY ID;",
"eventId": "SELECT ID,post_title,\n MAX(CASE WHEN meta_key='mec_start_date' THEN meta_value END) AS start,\n MAX(CASE WHEN meta_key='mec_end_date' THEN meta_value END) AS end,\n MAX(CASE WHEN meta_key='mec_start_day_seconds' THEN meta_value END) AS time_start,\n MAX(CASE WHEN meta_key='mec_end_day_seconds' THEN meta_value END) AS time_end,\n MAX(CASE WHEN meta_key='mec_organizer_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS organizer,\n MAX(CASE WHEN meta_key='mec_location_id' THEN (SELECT name FROM wp_terms WHERE term_id=meta_value) END) AS location\n FROM wp_posts\n RIGHT JOIN wp_postmeta ON ID=post_id\n WHERE post_type = 'mec-events' and post_status not like 'auto-draft' and ID=?\n GROUP BY ID;",
"posts": "SELECT p.ID,post_title,post_content,post_date,display_name FROM wp_posts p \n RIGHT JOIN wp_users u ON post_author = u.ID WHERE post_type='post' AND post_status='publish' \n ORDER BY p.post_date DESC;",
"postId": "SELECT p.ID,post_title,post_content,post_date,display_name FROM wp_posts p \n RIGHT JOIN wp_users u ON post_author = u.ID WHERE post_type='post' AND post_status='publish' \n AND p.ID=? ORDER BY p.post_date DESC;",
"speakers": "SELECT \n ID,\n MAX(CASE WHEN meta_key='_cl_first_name' THEN meta_value END) AS NAME,\n MAX(CASE WHEN meta_key='_cl_last_name' THEN meta_value ELSE '' END) AS SURNAME,\n MAX(CASE WHEN meta_key='_cl_job_title' THEN meta_value END) AS JOBTITLE,\n MAX(CASE WHEN meta_key='_cl_custom_field_1' THEN meta_value END) AS DESCRIPTION,\n MAX(CASE WHEN meta_key='_cl_email' THEN meta_value END) AS EMAIL,\n MAX(CASE WHEN meta_key='_cl_linkedin_url' THEN meta_value END) AS LINKEDIN,\n MAX(CASE WHEN meta_key='_cl_twitter_url' THEN meta_value END) AS TWITTER,\n MAX(CASE WHEN meta_key='_cl_facebook_url' THEN meta_value END) AS FACEBOOK\n FROM wp_posts RIGHT JOIN wp_postmeta ON ID=post_id \n WHERE post_type=\"contact\" AND post_status=\"publish\" GROUP BY ID;",
Expand All @@ -17,7 +17,9 @@
"start": "2016-01-01",
"end": "2016-01-01",
"time_start": "30600",
"time_end": "30800"
"time_end": "30800",
"organizer": "Test Person",
"location" : "Sin categoria"
}
],
"two_events" : [
Expand All @@ -27,23 +29,35 @@
"start": "2016-01-01",
"end": "2016-01-01",
"time_start": "30600",
"time_end": "30800"
"time_end": "30800",
"organizer": "Sin categoria"
},
{
"ID": "2",
"post_title": "Event 2",
"start": "2016-01-01",
"end": "2016-01-01",
"time_start": "30600",
"time_end": "30800"
"time_end": "30800",
"location": "Test Location"
}
],
"weird_date_event" : [
"null_date_events" : [
{
"ID": "1",
"post_title": "Event 1",
"start": "2016-01-01",
"end": "0000-00-00",
"end": null,
"time_start": null,
"time_end": null
}
],
"wrong_date_event": [
{
"ID": "3",
"post_title": "Wrong event",
"start": "20162-0101",
"end": "201116-r01-01",
"time_start": "30600",
"time_end": "30800"
}
Expand Down Expand Up @@ -91,7 +105,6 @@
"ID": "1",
"NAME": "John",
"SURNAME": "Doe",
"JOBTITLE": "Engineer",
"DESCRIPTION": "This is a description of Jon Doe",
"EMAIL": null,
"LINKEDIN" : "https://link-to-linkedin.test",
Expand All @@ -118,7 +131,8 @@
"eventId": "1",
"eventName": "Event 1",
"eventStartDateTime": "2016-01-01T08:30:00.000Z",
"eventEndDateTime": "2016-01-01T08:33:20.000Z"
"eventEndDateTime": "2016-01-01T08:33:20.000Z",
"eventOrganizer": "Test Person"
}
],
"two_events" : [
Expand All @@ -132,15 +146,8 @@
"eventId": "2",
"eventName": "Event 2",
"eventStartDateTime": "2016-01-01T08:30:00.000Z",
"eventEndDateTime": "2016-01-01T08:33:20.000Z"
}
],
"weird_date_event": [
{
"eventId": "1",
"eventName": "Event 1",
"eventStartDateTime": "2016-01-01T08:30:00.000Z",
"eventEndDateTime": "2016-01-01T08:33:20.000Z"
"eventEndDateTime": "2016-01-01T08:33:20.000Z",
"eventLocation" : "Test Location"
}
],
"one_post" : [
Expand Down Expand Up @@ -182,7 +189,6 @@
"speakerId": "1",
"speakerName": "John Doe",
"speakerDescription": "This is a description of Jon Doe",
"speakerJobTitle": "Engineer",
"speakerUrls" : [ "https://link-to-linkedin.test", "https://link-to-twitter.test" ]
},
{
Expand Down Expand Up @@ -220,9 +226,9 @@
"response" : { "$ref" : "#/$defs/responses/two_events" }
},
{
"description" : "Should have end date equal to start date if end date is 0000-00-00",
"result": { "$ref" : "#/$defs/results/weird_date_event" },
"response" : { "$ref" : "#/$defs/responses/weird_date_event" }
"description" : "Should ignore the events when they are invalid",
"result": { "$ref" : "#/$defs/results/null_date_events" },
"response" : { "$ref" : "#/$defs/responses/empty" }
}
],
"negativeCases" : [
Expand Down Expand Up @@ -262,13 +268,19 @@
{
"params" : { "eventId" : "3003" },
"description" : "Should respond with code 500 if method fails",
"result" : "wrong result",
"result" : { "$ref" : "#/$defs/results/wrong_date_event" },
"code" : 500
},
{
"params" : { "eventId" : "abcd" },
"description" : "Should respond with code 400 if param is invalid",
"code" : 400
},
{
"params" : { "eventId" : "2" },
"description" : "Should respond with code 406 result contain invalid fields",
"result" : { "$ref" : "#/$defs/results/null_date_events" },
"code" : 406
}
]
},
Expand Down

0 comments on commit f0b0938

Please sign in to comment.