Created
March 14, 2017 10:24
-
-
Save Wardormeur/f21ec5102cf4ecd6e1c82df32010488a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
/** | |
* Search next occurence of an event by id and with a minimum | |
* @param {String} id Event id | |
* @param {String} dojoId Dojo id | |
* @param {String} startTime First occurence | |
* @return {Event} | |
* @example '' | |
*/ | |
function searchNextOccurence (args, callback) { | |
var seneca = this; | |
var ENTITY_NS = 'cd/events'; | |
var events = seneca.make$(ENTITY_NS); | |
// We don't use the query format as it's not using seneca-entity | |
var id = args.id; | |
var startTime = args.startTime; | |
var dojoId = args.dojoId; | |
var query = 'SELECT distinct ve.id,' + | |
'min(start_time) OVER (PARTITION BY ve.id) as next_date,' + | |
'e.name,' + | |
'e.country::jsonb,' + | |
'e.city::jsonb,' + | |
'e.address,' + | |
'e.created_at,' + | |
'e.created_by,' + | |
'e.type,' + | |
'e.description,' + | |
'e.dojo_id,' + | |
'e.position::jsonb,' + | |
'e.public,' + | |
'e.status,' + | |
'e.recurring_type,' + | |
'e.dates::jsonb[],' + | |
'e.ticket_approval' + | |
' FROM cd_v_event_occurence ve JOIN cd_events e ON ve.id = e.id' + | |
' WHERE start_time > $1'; | |
var params = [startTime]; | |
if (!startTime) return callback(new Error('Missing required parameter for searchNextOccurence')); | |
if (id) { | |
params.push(id); | |
query += 'AND ve.id = $' + params.length; | |
} | |
if (dojoId) { | |
params.push(dojoId); | |
query += 'AND e.dojo_id = $' + params.length; | |
} | |
console.log(query, params); | |
events.native$(function (err, client, release) { | |
if (err) { | |
return callback(err); | |
} | |
client.query(query, params, function (err, event) { | |
if (err) { | |
release(); | |
return callback(err); | |
} | |
callback(null, event); | |
}); | |
}); | |
} | |
module.exports = searchNextOccurence; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment