<commit_before>//
// This is our "Enter OPAT" flow controller
// 
controllers.controller(
    'OPATReferralCtrl',
    function($scope, $modalInstance, $modal, $rootScope, $q,
             growl,
             schema, options,
             Episode){
        
        $scope.model = {
            hospital_number : null
        }
        $scope.patient = null;

        //
        // When we've created an episode with this flow, tag it to the correct
        // teams and then kill the modal.
        // 
        $scope.tag_and_close = function(episode){
            if(!episode.newItem){
                episode = new Episode(episode, schema);
            };
            if(!episode.tagging[0].makeCopy){
                episode.tagging[0] = episode.newItem('tagging');
            }
            var ep = episode.makeCopy()
            var teams = episode.tagging[0].makeCopy();
            var location = episode.location[0].makeCopy();
            
            // 
            // See https://github.com/openhealthcare/elcid/issues/484 -
            //
            // Becuase we have a referrals situation, we have to flush teams.
            //
            _.each(_.keys(teams), function(team){
                if(teams[team]){ teams[team] = false };
            })

                ep.category = 'OPAT'
            teams.opat = true;
            teams.opat_referrals = true;
            location.opat_referral = moment();
            
            //
            // Pre fill some tests:
            //
            var mrsa = episode.newItem('microbiology_test');
            var vte = episode.newItem('microbiology_test');

            $q.all([
                episode.save(ep),
                episode.tagging[0].save(teams),
                episode.location[0].save(location),
                mrsa.save({test: 'MRSA PCR'}),
                vte.save({test: 'VTE Assessment'})
            ]).then(function(){
                episode.active = true;
                $modalInstance.close(episode);
            });                        
        };

        // 
        // We have an initial hospital number - we can now figure out whether to
        // Add or pull over.
        // 
        $scope.find_by_hospital_number = function(){
            Episode.findByHospitalNumber(
                $scope.model.hospital_number,
                {
                    newPatient: $scope.new_patient,
                    newForPatient: $scope.new_for_patient,
                    error: function(){
			            // This shouldn't happen, but we should probably handle it better
			            alert('ERROR: More than one patient found with hospital number');
                        $modalInstance.close(null)
                    }
                }<commit_msg>We should finish the transaction that saves the episode before sending the requests that update it. That way the category stays set as not the default.<commit_after>//
// This is our "Enter OPAT" flow controller
// 
controllers.controller(
    'OPATReferralCtrl',
    function($scope, $modalInstance, $modal, $rootScope, $q,
             growl,
             schema, options,
             Episode){
        
        $scope.model = {
            hospital_number : null
        }
        $scope.patient = null;

        //
        // When we've created an episode with this flow, tag it to the correct
        // teams and then kill the modal.
        // 
        $scope.tag_and_close = function(episode){
            if(!episode.newItem){
                episode = new Episode(episode, schema);
            };
            if(!episode.tagging[0].makeCopy){
                episode.tagging[0] = episode.newItem('tagging');
            }
            var ep = episode.makeCopy()
            var teams = episode.tagging[0].makeCopy();
            var location = episode.location[0].makeCopy();
            
            // 
            // See https://github.com/openhealthcare/elcid/issues/484 -
            //
            // Becuase we have a referrals situation, we have to flush teams.
            //
            _.each(_.keys(teams), function(team){
                if(teams[team]){ teams[team] = false };
            })

                ep.category = 'OPAT'
            teams.opat = true;
            teams.opat_referrals = true;
            location.opat_referral = moment();
            
            //
            // Pre fill some tests:
            //
            var mrsa = episode.newItem('microbiology_test');
            var vte = episode.newItem('microbiology_test');
            
            episode.save(ep).then(function(){
                $q.all([
                    episode.tagging[0].save(teams),
                    episode.location[0].save(location),
                    mrsa.save({test: 'MRSA PCR'}),
                    vte.save({test: 'VTE Assessment'})
                ]).then(function(){
                    episode.active = true;
                    $modalInstance.close(episode);
                });
            });


        };

        // 
        // We have an initial hospital number - we can now figure out whether to
        // Add or pull over.
        // 
        $scope.find_by_hospital_number = function(){
            Episode.findByHospitalNumber(
                $scope.model.hospital_number,
                {
                    newPatient: $scope.new_patient,
                    newForPatient: $scope.new_for_patient,
                    error: function(){
			            // This shouldn't happen, but we should probably handle it better
			            alert('ERROR: More than one patient found with hospital number');
                        $modalInstance.close(null)
                    }
                }
{'fraction_non_alphanumeric': 0.10610312171168011, 'fraction_numerical': 0.003156787092248334, 'mean_word_length': 1.2977437550362612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '127746', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1425, 'n_words': 343}