# Path: core/validation/permissions.py
# def get_role_approval_step(group):
# 
#     # Approvation roles 
#     approvation_roles = [ s['approver'] if 'approver' in s else '' for s in conf_approval_flow ]
# 
#     # Administrator can see every expence, return 0
#     if group == 'administrator':
#         return 0
#     # Roles that can approves can see only expences >= owned step.
#     elif group in approvation_roles[1:-1]:
#         return approvation_roles.index(group)
#     # Else, return draft step 
#     else:
#         return len(approvation_roles)-1
# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):
# 
# Path: core/config.py



from core.validation.permissions import get_role_approval_step
from core.api.crud import db
from bson.objectid import ObjectId
from core.config import conf_approval_flow, conf_notifications
import cherrypy, logging
    records = { 'trips' : [], 'expences' : [] }
    
    pending_count = 0
    
    for aggregation_type in aggregations_types:

        # If is administrator, can see whole ranges
        if owner_status == 0:
            match_project_status = { '%s.status' % aggregation_type : { '$gt' : 0 } }
        # If it is a permitted user, can see only specific status
        else:
            match_project_status = { '%s.status' % aggregation_type : owner_status }

            # If user is not in the approvations chain, filter also user_id parameter
        if cherrypy.session['_ts_user']['group'] == 'employee':
            match_project_status.update({ '%s.user_id' % aggregation_type : str(cherrypy.session['_ts_user']['_id']) })


        projects_requested = [ ObjectId(p) for p in cherrypy.session['_ts_user']['managed_projects'] ]
        if projects_requested:
            match_project_status.update({ '_id' : { '$in' : projects_requested } })

        aggregation_pipe = [  
                            { '$unwind' : '$%s' % aggregation_type },
                            { '$match' : match_project_status },
                            { '$group' : { '_id' : None, 'count' : { '$sum' : 1 } } }
        ]

        cherrypy.log('%s' % (aggregation_pipe), context = 'TS.PENDING_NOTIFICATIONS.aggregation_pipe', severity = logging.INFO)


# Path: core/validation/permissions.py
# def get_role_approval_step(group):
# 
#     # Approvation roles 
#     approvation_roles = [ s['approver'] if 'approver' in s else '' for s in conf_approval_flow ]
# 
#     # Administrator can see every expence, return 0
#     if group == 'administrator':
#         return 0
#     # Roles that can approves can see only expences >= owned step.
#     elif group in approvation_roles[1:-1]:
#         return approvation_roles.index(group)
#     # Else, return draft step 
#     else:
#         return len(approvation_roles)-1
# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):
# 
# Path: core/config.py



# Path: core/notifications/notifications.py
from core.validation.permissions import get_role_approval_step
from core.api.crud import db
from bson.objectid import ObjectId
from core.config import conf_approval_flow, conf_notifications
import cherrypy, logging

    records = { 'trips' : [], 'expences' : [] }
    
    pending_count = 0
    
    for aggregation_type in aggregations_types:

        # If is administrator, can see whole ranges
        if owner_status == 0:
            match_project_status = { '%s.status' % aggregation_type : { '$gt' : 0 } }
        # If it is a permitted user, can see only specific status
        else:
            match_project_status = { '%s.status' % aggregation_type : owner_status }

            # If user is not in the approvations chain, filter also user_id parameter
        if cherrypy.session['_ts_user']['group'] == 'employee':
            match_project_status.update({ '%s.user_id' % aggregation_type : str(cherrypy.session['_ts_user']['_id']) })


        projects_requested = [ ObjectId(p) for p in cherrypy.session['_ts_user']['managed_projects'] ]
        if projects_requested:
            match_project_status.update({ '_id' : { '$in' : projects_requested } })

        aggregation_pipe = [  
                            { '$unwind' : '$%s' % aggregation_type },
                            { '$match' : match_project_status },
                            { '$group' : { '_id' : None, 'count' : { '$sum' : 1 } } }
        ]

        cherrypy.log('%s' % (aggregation_pipe), context = 'TS.PENDING_NOTIFICATIONS.aggregation_pipe', severity = logging.INFO)


        result = db.project.aggregate(aggregation_pipe)['result']