From 674b3109361d29dd18ffda6a393c6e839e7a3a56 Mon Sep 17 00:00:00 2001
From: anatole <anatole.romon@polytechnique.edu>
Date: Mon, 16 Apr 2018 02:02:30 +0200
Subject: [PATCH] fonctions de fetch de message

---
 src/graphql/connectors/connectors.js | 147 +++++++++++++++++++--------
 src/graphql/connectors/selectors.js  |  19 ++++
 2 files changed, 126 insertions(+), 40 deletions(-)

diff --git a/src/graphql/connectors/connectors.js b/src/graphql/connectors/connectors.js
index 432fc9c..bd55ad6 100644
--- a/src/graphql/connectors/connectors.js
+++ b/src/graphql/connectors/connectors.js
@@ -335,26 +335,60 @@ export const getUser = (user, uid) => {
     return result;
 };
 
+// All these messages are returned if they are visible
+
+export async function getAnnouncement(user, messageID){
+    let res = await knex.select().from('announcements').where('id', messageID)
+        .whereIn('id', await selectors.visibleAnnouncements(user));
+    if(res[0]){
+        res[0].type = 'Announcement';
+        return res[0];
+    }
+    res = await knex.select().from('events').where('id', messageID)
+    .whereIn('id', await selectors.visibleEvents(user));
+    if(res[0]){
+        res[0].type = 'Announcement';
+        return res[0];
+    }
+    return undefined;
+}
 
+export async function getEvent(user, messageID){
+    let res = await knex.select().from('events').where('id', messageID)
+    .whereIn('id', await selectors.visibleEvents(user));
+    if(res[0]){
+        res[0].type = 'Event';
+        return res[0];
+    }
+    return undefined;
+}
 
-/**
- * @summary Renvoie un message en fonction de son identifiant.
- * @param {*} user - Utilisateur effectuant la requête.
- * @param {*} eventID - Identifiant unique de l'événement.
- * @rights super
- */
-export async function getMessage(user, messageID){
-    let announcement = await knex.select().from('announcements').where('id', messageID);
-    if(announcement){
-        let res = announcement[0];
-        res.type = "Announcement";
-        return res;
+export async function getPrivatePost(user, messageID){
+    let res = await knex.select().from('private_posts').where('id', messageID)
+    .whereIn('id', await selectors.visiblePrivatePosts(user));
+    if(res[0]){
+        res[0].type = 'PrivatePost';
+        return res[0];
     }
-    let event = await knex.select().from('events').where('id', messageID);
-    if(event){
-        let res = event[0];
-        res.type = "Event";
-        return res;
+    return undefined;
+}
+
+export async function getQuestion(user, messageID){
+    let res = await knex.select().from('questions').where('id', messageID)
+        .whereIn('id', await selectors.visibleQuestions(user));
+    if(res[0]){
+        res[0].type = 'Question';
+        return res[0];
+    }
+    return undefined;
+}
+
+export async function getAnswer(user, messageID){
+    let res = await knex.select().from('answers').where('id', messageID)
+        .whereIn('id', await selectors.visibleAnswers(user));
+    if(res[0]){
+        res[0].type = 'Answer';
+        return res[0];
     }
     return undefined;
 }
@@ -365,24 +399,64 @@ export async function getMessage(user, messageID){
  * @param {*} eventID - Identifiant unique de l'événement.
  * @rights super
  */
-export async function getMessageIfVisible(user, messageID){
-    let announcement = await knex
-        .with('visible_announcements', await selectors.visibleAnnouncements(user))
-        .select().from('visible_announcements').where('id', messageID);
-    if(announcement){
-        let res = announcement[0];
-        res.type = "Announcement";
-        return res;
+export async function getMessage(user, messageID){
+    return getEvent(user, messageID) | 
+        getAnnouncement(user, messageID) |
+        getPrivatePost(user, messageID) |
+        getQuestion(user, messageID) |
+        getAnswer(user, messageID);
+}
+
+export async function allVisibleEvents(user){
+    let selection = await selectors.visibleEvents(user);
+    let result = await knex.select().from('events').whereIn('id', selection);
+    for(r of result){
+        r.type = 'Announcement';
     }
-    let event = await knex
-        .with('visible_events', await selectors.visibleEvents(user))
-        .select().from('visible_events').where('id', messageID);
-    if(event){
-        let res = event[0];
-        res.type = "Event";
-        return res;
+    return result;
+}
+
+export async function allVisibleAnnouncements(user){
+    let selection = await selectors.visibleAnnouncements(user);
+    let result = await knex.select().from('announcements').whereIn('id', selection);
+    result = result.concat(
+        await knex.select().from('events').whereIn('id', selection)
+    );
+    for(r of result){
+        r.type = 'Announcement';
     }
-    return undefined;
+    return result;
+}
+
+export async function allVisiblePrivatePosts(user){
+    let selection = await selectors.visiblePrivatePosts(user);
+    let result = await knex.select().from('private_posts').whereIn('id', selection);
+    for(r of result){
+        r.type = 'PrivatePost';
+    }
+    return result;
+}
+
+export async function allVisibleQuestions(user){
+    let selection = await selectors.visibleQuestions(user);
+    let result = await knex.select().from('questions').whereIn('id', selection);
+    for(r of result){
+        r.type = 'Question';
+    }
+    return result;
+}
+
+export async function allVisibleAnswers(user){
+    let selection = await selectors.visibleAnswers(user);
+    let result = await knex.select().from('answers').whereIn('id', selection);
+    for(r of result){
+        r.type = 'Answer';
+    }
+    return result;
+}
+
+export async function visibleMessages(user, messageID){
+
 }
 
 export async function getMessageAuthor(user, messageID){
@@ -449,13 +523,6 @@ export const getMetaGroup = (user, groupUID) => {
     return knex.select().from('meta_groups').where('uid',groupUID).then(results => results [0]);
 };
 
-export async function getEvent(user, eventID){
-    // console.log(eventID);
-    let event_list = await knex('events').select().where('id', eventID);
-    // console.log(event_list);
-    return event_list[0];
-}
-
 /**
  * @summary Refuse une requête d'un groupe voulant rejoindre un évènement
  * @arg {Object} user - Représente l'utilisateur qui a effectué la requête. 
diff --git a/src/graphql/connectors/selectors.js b/src/graphql/connectors/selectors.js
index 40afa3a..5a34589 100644
--- a/src/graphql/connectors/selectors.js
+++ b/src/graphql/connectors/selectors.js
@@ -63,6 +63,25 @@ export async function visibleEvents(user){
     };
 }
 
+export async function visiblePrivatePosts(user){
+    return query_builder => {
+        return query_builder;
+    };
+}
+
+export async function visibleQuestions(user){
+    return query_builder => {
+        return query_builder;
+    };
+}
+
+export async function visibleAnswers(user){
+    return query_builder => {
+        return query_builder;
+    };
+}
+
+
 export async function messageHosts(user, messageID){
     return function(query_builder){
         return query_builder.select({uid : 'group'}).from('group_message_relationships')
-- 
GitLab