<commit_before>//
// This is our "Enter OPAT" flow controller
// 
controllers.controller(
    'OPATReferralCtrl',
    function($scope, $modalInstance, $modal, $rootScope,
             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 teams = episode.tagging[0].makeCopy();
            var location = episode.location[0].makeCopy();
            teams.opat = true;
            teams.opat_referrals = true;
            location.category = 'OPAT';

            //
            // Pre fill some tests:
            //
            var mrsa = episode.newItem('microbiology_test');
            var vte = episode.newItem('microbiology_test');

            // TODO - these are promises - the API is nicer than this !
            episode.tagging[0].save(teams).then(function(){
                episode.location[0].save(location).then(function(){
                    mrsa.save({test: 'MRSA PCR'}).then(function(mrsa_test){
                        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>Set OPAT referral to date of referral. Refactor OPAT referral saving to use promise.all() API<commit_after>//
// This is our "Enter OPAT" flow controller
// 
controllers.controller(
    'OPATReferralCtrl',
    function($scope, $modalInstance, $modal, $rootScope, $q,
             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 teams = episode.tagging[0].makeCopy();
            var location = episode.location[0].makeCopy();
            teams.opat = true;
            teams.opat_referrals = true;
            location.category = 'OPAT';
            location.opat_referral = moment();

            //
            // Pre fill some tests:
            //
            var mrsa = episode.newItem('microbiology_test');
            var vte = episode.newItem('microbiology_test');

            $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.10616578195181707, 'fraction_numerical': 0.002449979583503471, 'mean_word_length': 1.3485139022051773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1684382', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1221, 'n_words': 291}