import connection from '../../config/database';

module.exports.register = (server, options, next) => {
  async function getStats(next) {
    try {
      const divisions = await connection
        .table('employees')
        .filter(doc => doc('div').ne(''))('div')
        .distinct()
        .count();

      const directorates = await connection
        .table('employees')
        .filter(doc => doc('bran').ne(''))('bran')
        .distinct()
        .count();

      const branches = await connection
        .table('employees')
        .filter(doc => doc('sect').ne(''))('sect')
        .distinct()
        .count();

      const employees = await connection
        .table('employees')
        .filter(doc => doc('email').ne(''))('email')
        .distinct()
        .count();


      const locations = await connection
        .table('employees')
        .filter(doc => doc('location_name').ne(''))('location_name')
        .distinct()
        .count();

      const stats = { organisation: { branches, directorates, divisions }, employees, locations };

      return next(null, stats);
    } catch (error) {
      return next(error);
    }
  }

  server.method('db.getStats', getStats, {});

  next();
};

module.exports.register.attributes = {
  name: 'method.db.getStats',
};

@@ -141,16 +141,170 @@
   try %7B%0A
+      const groups = await connection%0A        .table('employees')%0A        .filter(doc =%3E doc('grp').ne(''))('grp')%0A        .distinct()%0A        .count();%0A%0A
       co
@@ -552,20 +552,27 @@
 =%3E doc('
-bran
+directorate
 ').ne(''
@@ -575,20 +575,27 @@
 e(''))('
-bran
+directorate
 ')%0A     
@@ -728,28 +728,341 @@
 oc('
-sect').ne(''))('sect
+bran').ne(''))('bran')%0A        .distinct()%0A        .count();%0A%0A      const sections = await connection%0A        .table('employees')%0A        .filter(doc =%3E doc('sect').ne(''))('sect')%0A        .distinct()%0A        .count();%0A%0A      const teams = await connection%0A        .table('employees')%0A        .filter(doc =%3E doc('team').ne(''))('team
 ')%0A 
@@ -1260,17 +1260,16 @@
 unt();%0A%0A
-%0A
       co
@@ -1458,16 +1458,24 @@
 tats = %7B
+%0A       
  organis
@@ -1486,45 +1486,147 @@
 n: %7B
- branches, directorates, divisions %7D,
+%0A          groups,%0A          divisions,%0A          directorates,%0A          branches,%0A          sections,%0A          teams,%0A        %7D,%0A       
  emp
@@ -1632,16 +1632,24 @@
 ployees,
+%0A       
  locatio
@@ -1650,16 +1650,23 @@
 ocations
+,%0A     
  %7D;%0A%0A   

Update Census statistics method to get groups, sections and teams