From b27bd21acca54754b934549236ebdbddb43b1fca Mon Sep 17 00:00:00 2001
From: anatole <anatole.romon@polytechnique.edu>
Date: Fri, 13 Apr 2018 03:08:34 +0200
Subject: [PATCH] resolver de recipients

---
 ...80413024152_new_group_message_relations.js | 25 +++++++++++++++++++
 db/seeds/07_make_event.js                     |  7 ++++++
 src/graphql/connectors/connectors.js          | 10 ++++++++
 src/graphql/resolvers.js                      | 10 ++++----
 4 files changed, 47 insertions(+), 5 deletions(-)
 create mode 100644 db/migrations/20180413024152_new_group_message_relations.js

diff --git a/db/migrations/20180413024152_new_group_message_relations.js b/db/migrations/20180413024152_new_group_message_relations.js
new file mode 100644
index 0000000..28c5037
--- /dev/null
+++ b/db/migrations/20180413024152_new_group_message_relations.js
@@ -0,0 +1,25 @@
+
+exports.up = async function(knex, Promise) {
+    let relations = await knex.select().from('group_message_relationships');
+    await knex('group_message_relationships').del();    
+    await knex.schema.table('group_message_relationships', table => {
+        table.dropColumn('status');
+    });
+    await knex.schema.table('group_message_relationships', table => {
+        table.enum('status', ['join', 'host', 'publish', 'recieve', 'internal']);
+    });
+    return knex('group_message_relationships').insert(relations);
+};
+
+exports.down = async function(knex, Promise) {
+    let relations = await knex.select().from('group_message_relationships')
+        .whereIn('status', ['join', 'host', 'publish']);
+    await knex('group_message_relationships').del();    
+    await knex.schema.table('group_message_relationships', table => {
+        table.dropColumn('status');
+    });
+    await knex.schema.table('group_message_relationships', table => {
+        table.enum('status', ['join', 'host', 'publish']);
+    });
+    return knex('group_message_relationships').insert(relations);
+};
diff --git a/db/seeds/07_make_event.js b/db/seeds/07_make_event.js
index d743087..31c17a3 100644
--- a/db/seeds/07_make_event.js
+++ b/db/seeds/07_make_event.js
@@ -2,6 +2,7 @@
 exports.seed = async function(knex, Promise) {
   // Deletes ALL existing entries
   await knex('events').del()
+  await knex('group_message_relationships').del()
       // Inserts seed entries
   await knex('events').insert([
     {
@@ -20,7 +21,13 @@ exports.seed = async function(knex, Promise) {
       message : 42,
       group : 'br',
       status : 'host'
+    },
+    {
+      message : 42,
+      group : 'kes',
+      status : 'recieve'
     }
+    
   ]);
     
 };
diff --git a/src/graphql/connectors/connectors.js b/src/graphql/connectors/connectors.js
index 80e4c8e..432fc9c 100644
--- a/src/graphql/connectors/connectors.js
+++ b/src/graphql/connectors/connectors.js
@@ -400,7 +400,17 @@ export async function getMessageAuthor(user, messageID){
 }
 
 export async function getMessageRecipient(user, messageID){
+    let GroupReciepients = await getGroupsFromCallbacks(user, qb  => {
+        return qb.select({uid: 'group'}).from('group_message_relationships')
+          .where('message', messageID).where('status', 'recieve')
+        })
+    let recipientObject = Object()
+    if(GroupReciepients.length == 1)
+        recipientObject.singleGroup = GroupReciepients[0]
+    else
+        authorObject.multipleGroups = GroupReciepients
 
+    return recipientObject
 }
 
 /**
diff --git a/src/graphql/resolvers.js b/src/graphql/resolvers.js
index 5ce33b5..310a738 100644
--- a/src/graphql/resolvers.js
+++ b/src/graphql/resolvers.js
@@ -113,7 +113,7 @@ export const resolvers = {
         },
 
         test: async function(obj, args, context){
-            return connectors.getSimpleGroup(context.user, "br")
+            return connectors.getSimpleGroup(context.user, "br");
         }
     },
 
@@ -219,11 +219,11 @@ export const resolvers = {
         },
 
         authors: async function (obj, args, context){
-            return connectors.getMessageAuthor(context.user, obj.id)
+            return connectors.getMessageAuthor(context.user, obj.id);
         },
 
         recipient: async function (obj, args, context){
-            return connectors.getMessageRecipient(context.user, obj.id)
+            return connectors.getMessageRecipient(context.user, obj.id);
         }
     },
 
@@ -247,11 +247,11 @@ export const resolvers = {
         },
 
         authors: async function (obj, args, context){
-            return connectors.getMessageAuthor(context.user, obj.id)
+            return connectors.getMessageAuthor(context.user, obj.id);
         },
 
         recipient: async function (obj, args, context){
-            return connectors.getMessageRecipient(context.user, obj.id)
+            return connectors.getMessageRecipient(context.user, obj.id);
         }
     },
 
-- 
GitLab