<commit_before>//
//  AlarmService.swift
//  ConnectedAlarmApp
//
//  Created by <NAME> on 5/13/17.
//  Copyright © 2017 connected alarm app. All rights reserved.
//

import UIKit
import Parse

struct AlarmService {

    // Creating singleton instance
    private static let _instance = AlarmService ()
    static var Instance = {
       return _instance
    }()
    
    func fetchParticipantAlarms(offset:Int = 0, limit: Int = 20, alarmType: AlarmStatusType, sucess: @escaping ([Alarm]) -> (), failure: @escaping (Error) -> ()) {
        log.debug("fetching alarms from Parse for alarmType \(alarmType.rawValue)")
        
        let includeKeys: [String] = ["alarm", "alarm.alarmTime", "alarm.adminUser", "alarm.participants.user", "user"]
        var whereKeys: [String : Any] = [:]
        whereKeys.updateValue(alarmType.rawValue, forKey: "status")
        whereKeys.updateValue(PFUser.current()!, forKey: "user")
        log.debug("Current user = \(String(describing: PFUser.current()!.username))")
        
        log.debug("Include keys are \(includeKeys)")
        
        DBManager.Instance.getCollectionFromParse(offset: offset, limit: limit, includeKeys: includeKeys, whereKeys: whereKeys, collectionName: ParticipantAlarm.parseClassName(), sucess: { (data: [PFObject]) in
            
            var alarmData: [Alarm] = []
            
            let participantAlarms: [ParticipantAlarm] = Mapper.Instance.convertFromParticipantAlarmsPFObject(participantAlarm: data)
            
            if participantAlarms.isEmpty == false {
                
                for participantAlarm in participantAlarms {
                    alarmData.append(participantAlarm.alarm)
                }                   
            }
            
            sucess(alarmData)
            
        }) { (error: Error) in
            failure(error)
        }
    }
    
    func updateParticipantAlarmStatus(alarm: Alarm, status: AlarmStatusType, sucess: @escaping (Bool) -> (), failure: @escaping (Error) -> ()) {
        log.debug("Updating alarm status as \(status.rawValue) for alarmid \(String(describing: alarm.objectId))" )

        var whereKeys: [String : Any] = [:]
        whereKeys.updateValue(alarm, forKey: "alarm")
        whereKeys.updateValue(PFUser.current()!, forKey: "user")
        
       DBManager.Instance.getCollectionFromParse(whereKeys: whereKeys, collectionName: ParticipantAlarm.parseClassName(), sucess: { (data: [PFObject]) in<commit_msg>Filtering out admin alarm when admin logs in<commit_after>//
//  AlarmService.swift
//  ConnectedAlarmApp
//
//  Created by <NAME> on 5/13/17.
//  Copyright © 2017 connected alarm app. All rights reserved.
//

import UIKit
import Parse

struct AlarmService {

    // Creating singleton instance
    private static let _instance = AlarmService ()
    static var Instance = {
       return _instance
    }()
    
    func fetchParticipantAlarms(offset:Int = 0, limit: Int = 20, alarmType: AlarmStatusType, sucess: @escaping ([Alarm]) -> (), failure: @escaping (Error) -> ()) {
        log.debug("fetching alarms from Parse for alarmType \(alarmType.rawValue)")
        
        let includeKeys: [String] = ["alarm", "alarm.alarmTime", "alarm.adminUser", "alarm.participants.user", "user"]
        var whereKeys: [String : Any] = [:]
        whereKeys.updateValue(alarmType.rawValue, forKey: "status")
        whereKeys.updateValue(PFUser.current()!, forKey: "user")
        log.debug("Current user = \(String(describing: PFUser.current()!.username))")
        
        log.debug("Include keys are \(includeKeys)")
        
        DBManager.Instance.getCollectionFromParse(offset: offset, limit: limit, includeKeys: includeKeys, whereKeys: whereKeys, collectionName: ParticipantAlarm.parseClassName(), sucess: { (data: [PFObject]) in
            
            var alarmData: [Alarm] = []
            
            let participantAlarms: [ParticipantAlarm] = Mapper.Instance.convertFromParticipantAlarmsPFObject(participantAlarm: data)
            
            if participantAlarms.isEmpty == false {
                
                for participantAlarm in participantAlarms {
                    if participantAlarm.alarm.adminUser.username != PFUser.current()?.username{
                        alarmData.append(participantAlarm.alarm)
                    }
                }
            }
            
            sucess(alarmData)
            
        }) { (error: Error) in
            failure(error)
        }
    }
    
    func updateParticipantAlarmStatus(alarm: Alarm, status: AlarmStatusType, sucess: @escaping (Bool) -> (), failure: @escaping (Error) -> ()) {
        log.debug("Updating alarm status as \(status.rawValue) for alarmid \(String(describing: alarm.objectId))" )

        var whereKeys: [String : Any] = [:]
        whereKeys.updateValue(alarm, forKey: "alarm")
        whereKeys.updateValue(PFUser.current()!, forKey: "user")
        
       DBManager.Instance.getCollectionFromParse(whereKeys: whereKeys, collectionName: ParticipantAlarm.parseClassName(), sucess: { (data: [PFObject]) in
{'fraction_non_alphanumeric': 0.11875737318128195, 'fraction_numerical': 0.0047188360204482895, 'mean_word_length': 2.535093815149409, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7231330', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1417, 'n_words': 321}