Class: Auth::Workflow::Assembly

Inherits:
Object
  • Object
show all
Includes:
Concerns::EsConcern, Concerns::WorkflowConcern
Defined in:
app/models/auth/workflow/assembly.rb

Constant Summary collapse

FIELDS_LOCKED_AFTER_ORDER_ADDED =
["master","applicable"]
INDEX_DEFINITION =
{
  index_options:  {
          settings:  {
          index: {
              analysis:  {
                  filter:  {
                      nGram_filter:  {
                          type: "nGram",
                          min_gram: 2,
                          max_gram: 20,
                          token_chars: [
                             "letter",
                             "digit",
                             "punctuation",
                             "symbol"
                          ]
                      }
                  },
                  analyzer:  {
                      nGram_analyzer:  {
                          type: "custom",
                          tokenizer:  "whitespace",
                          filter: [
                              "lowercase",
                              "asciifolding",
                              "nGram_filter"
                          ]
                      },
                      whitespace_analyzer: {
                          type: "custom",
                          tokenizer: "whitespace",
                          filter: [
                              "lowercase",
                              "asciifolding"
                          ]
                      }
                  }
              }
          }
        },
          mappings: {
            "auth/workflow/assembly" => {
            properties: {
                  _all_fields:  {
                      type: "text",
                      analyzer: "nGram_analyzer",
                      search_analyzer: "whitespace_analyzer"
                  },
                  name: {
                    type: "keyword",
                    copy_to: "_all_fields"
                  },
                  description: {
                    type: "keyword",
                    copy_to: "_all_fields"
                  }
            }
          }
      }
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_self(id, signed_in_resource, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/models/auth/workflow/assembly.rb', line 42

def self.find_self(id,signed_in_resource,options={})
  
  return nil unless collection =  Auth.configuration.assembly_class.constantize.where("_id" => id
  )

  collection.first
    
end

.permitted_paramsObject

CLASS METHODS



38
39
40
# File 'app/models/auth/workflow/assembly.rb', line 38

def self.permitted_params
  [{:assembly => [:name,:description,:doc_version,:applicable, :master_assembly_id, :master]},:id]
end

.prepare_nestedObject

FOR TESTING PURPOSES

Auth::Workflow::Assembly.prepare_nested



60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/auth/workflow/assembly.rb', line 60

def self.prepare_nested
  a = Auth::Workflow::Assembly.new
  stage = Auth::Workflow::Stage.new
  sop = Auth::Workflow::Sop.new
  step = Auth::Workflow::Step.new
  sop.steps << step
  stage.sops << sop
  a.stages << stage
  a.save
  a
end

Instance Method Details

#after_schedule_order(arguments = {}) ⇒ Object



1133
1134
1135
# File 'app/models/auth/workflow/assembly.rb', line 1133

def after_schedule_order(arguments={})

end

#as_indexed_json(options = {}) ⇒ Object

create_es_index(INDEX_DEFINITION)



181
182
183
184
185
186
187
# File 'app/models/auth/workflow/assembly.rb', line 181

def as_indexed_json(options={})
   {
      name: name,
      description: description,
      public: public
   }
end

#build_order_query(permitted_params, params) ⇒ Object

> order query and update



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'app/models/auth/workflow/assembly.rb', line 591

def build_order_query(permitted_params,params)

  #puts "permitted params are: #{permitted_params}"

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  order_index = permitted_params[:order_index]
  order_id = params[:id]
  order_doc_version = permitted_params[:doc_version]

=begin
  puts " ------------------ CAME TO ORDER QUERY --------------"
  puts "stage index : #{stage_index}"
  puts "stage id : #{stage_id}"
  puts "stage doc version: #{stage_doc_version}"
  puts "sop index: #{sop_index}"
  puts "sop id: #{sop_id}"
  puts "sop_doc_version : #{sop_doc_version}"
  puts "order index: #{order_index}"
  puts "order id : #{order_id}"
  puts "order doc version: #{order_doc_version}"

=end
  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && order_index && order_id && order_doc_version)

  query =
  
    [
      {
        "stages.#{stage_index}.sops.#{sop_index}.orders.#{order_index}._id" => BSON::ObjectId(order_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.orders.#{order_index}.doc_version" => order_doc_version
      }
    ]

  return query

end

#build_order_update(permitted_params, params) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'app/models/auth/workflow/assembly.rb', line 635

def build_order_update(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  
  order_index = permitted_params[:order_index]
  order_id = params[:id]
  order_doc_version = permitted_params[:doc_version]

=begin
  puts " ------------------ CAME TO ORDER UPDATE --------------"
  puts "stage index : #{stage_index}"
  puts "stage id : #{stage_id}"
  puts "stage doc version: #{stage_doc_version}"
  puts "sop index: #{sop_index}"
  puts "sop id: #{sop_id}"
  puts "sop_doc_version : #{sop_doc_version}"
  puts "order index: #{order_index}"
  puts "order id : #{order_id}"
  puts "order doc version: #{order_doc_version}"
=end

  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && order_index && order_id && order_doc_version)

  #puts 'ORDER UPDATE -------------all required params are present.'

  order = get_order(stage_index,sop_index,order_index)
    
  #puts "step got as : #{step}"

  return unless order


  order.assign_attributes(permitted_params)
  order.doc_version = order_doc_version + 1


  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}.orders.#{order_index}" => order.attributes
    }
  }

  return update

end

#build_requirement_query(permitted_params, params) ⇒ Object

> requirement query and update.



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'app/models/auth/workflow/assembly.rb', line 691

def build_requirement_query(permitted_params,params)

  

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]
  requirement_index = permitted_params[:requirement_index]
  requirement_id = permitted_params[:requirement_id] || params[:id]
  requirement_doc_version = permitted_params[:requirement_doc_version] || permitted_params[:doc_version]


  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && requirement_index && requirement_id && requirement_doc_version)



  query =
  
    [
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}._id" => BSON::ObjectId(requirement_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}.doc_version" => requirement_doc_version
      }
    ]


 
  return query

end

#build_requirement_update(permitted_params, params) ⇒ Object



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'app/models/auth/workflow/assembly.rb', line 731

def build_requirement_update(permitted_params,params)


  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]

  requirement_index = permitted_params[:requirement_index]
  requirement_id = params[:id]
  requirement_doc_version = permitted_params[:doc_version]  


  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && requirement_index && requirement_id && requirement_doc_version)

 # puts 'all required params are present.'

  requirement = get_requirement(stage_index,sop_index,step_index,requirement_index)
    
 # puts "requirement got as : #{requirement}"

  return unless requirement

 # puts "found requirement"

  requirement.assign_attributes(permitted_params)
  requirement.doc_version = requirement_doc_version + 1


  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}" => requirement.attributes
    }
  }

  return update


end

#build_sop_query(permitted_params, params) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'app/models/auth/workflow/assembly.rb', line 461

def build_sop_query(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  ## see the reasons for these pipes in the build_stage_query
  sop_id = permitted_params[:sop_id] || params[:id]
  sop_doc_version = permitted_params[:sop_doc_version] || permitted_params[:doc_version]

  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version)

  query =
  
    [

      {
        "stages.#{stage_index}.sops.#{sop_index}._id" => BSON::ObjectId(sop_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.doc_version" => sop_doc_version
      }

    ]

  return query

end

#build_sop_update(permitted_params, params) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'app/models/auth/workflow/assembly.rb', line 491

def build_sop_update(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = params[:id]
  sop_doc_version =  permitted_params[:doc_version]

  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version)

  sop = get_sop(stage_index,sop_index)
    
  return unless sop

  sop.assign_attributes(permitted_params)
  sop.doc_version = sop_doc_version + 1

  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}" => sop.attributes
    }
  }

  return update

end

#build_stage_query(permitted_params, params) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'app/models/auth/workflow/assembly.rb', line 404

def build_stage_query(permitted_params,params)
  stage_index = permitted_params[:stage_index]
  ## why this pipe here?
  ## because if request was made to the stages controller to update a stage, then the stage_id would be the params[:id]
  ## however if the request was made to the sops controller , then the stage_id would explicitly defined as the stage_id, and the :id would actually refer to the sop_id
  ## so first preference is given to that, and params[:id] is the fallback.
  stage_id = permitted_params[:stage_id] || params[:id]
  stage_doc_version = permitted_params[:stage_doc_version] || permitted_params[:doc_version]
  #puts "stage index: #{stage_index}, stage_id: #{stage_id}, stage_doc_version: #{stage_doc_version}"
  return unless (stage_index && stage_id && stage_doc_version)

  query =
    
    [

      {
        "stages.#{stage_index}._id" => BSON::ObjectId(stage_id)     
      },
      {
        "stages.#{stage_index}.doc_version" => stage_doc_version
      }

    ]

  return query

end

#build_stage_update(permitted_params, params) ⇒ Object

will return nil if the params were not enough will otherwise return hash permitted params are the stage params.



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/models/auth/workflow/assembly.rb', line 435

def build_stage_update(permitted_params,params)
  
  stage_index = permitted_params[:stage_index]
  #stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:doc_version]
  
  return unless (stage_index && stage_doc_version)

  stage = get_stage(stage_index)
  
  return unless stage

  stage.assign_attributes(permitted_params)
  stage.doc_version = stage_doc_version + 1
 
  update = {
    "$set" => {
      "stages.#{stage_index}" => stage.attributes
    }
  }

  return update

end

#build_state_query(permitted_params, params) ⇒ Object

> state query and update.



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'app/models/auth/workflow/assembly.rb', line 869

def build_state_query(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]
  requirement_index = permitted_params[:requirement_index]
  requirement_id = permitted_params[:requirement_id]
  requirement_doc_version = permitted_params[:requirement_doc_version]
  state_id = params[:id]
  state_doc_version = permitted_params[:doc_version]
  state_index = permitted_params[:state_index]

=begin
  puts "stage index: #{stage_index}"
  puts "stage id: #{stage_id}"
  puts "stage doc version: #{stage_doc_version}"

  puts "sop index: #{sop_index}"
  puts "sop id: #{sop_id}"
  puts "sop doc version: #{sop_doc_version}"

  puts "step index: #{step_index}"
  puts "step id: #{step_id}"
  puts "step doc version: #{step_doc_version}"

  puts "requirement index: #{requirement_index}"
  puts "requirement id: #{requirement_id}"
  puts "requirement doc version: #{requirement_doc_version}"

  puts "state index: #{state_index}"
  puts "state id: #{state_id}"
  puts "state doc version: #{state_doc_version}"
=end

  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && requirement_index && requirement_id && requirement_doc_version && state_index && state_id && state_doc_version)



  query =
  
    [
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}.states.#{state_index}._id" => BSON::ObjectId(state_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}.states.#{state_index}.doc_version" => state_doc_version
      }
    ]


  puts "STATE_QUERY: ALL PARAMS PRESENt. ---------------------------------"

  return query

end

#build_state_update(permitted_params, params) ⇒ Object



932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'app/models/auth/workflow/assembly.rb', line 932

def build_state_update(permitted_params,params)

  #puts "came to build state update."
  #puts "the params are:"
  #puts params.to_s

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]

  requirement_index = permitted_params[:requirement_index]
  requirement_id = params[:id]
  requirement_doc_version = permitted_params[:doc_version]  

  state_index = permitted_params[:state_index]
  state_id = params[:id]
  state_doc_version = permitted_params[:doc_version]  

=begin
  puts "the params inside the update are-----------"

  puts "stage index: #{stage_index}"
  puts "stage id: #{stage_id}"
  puts "stage doc version: #{stage_doc_version}"

  puts "sop index: #{sop_index}"
  puts "sop id: #{sop_id}"
  puts "sop doc version: #{sop_doc_version}"

  puts "step index: #{step_index}"
  puts "step id: #{step_id}"
  puts "step doc version: #{step_doc_version}"

  puts "requirement index: #{requirement_index}"
  puts "requirement id: #{requirement_id}"
  puts "requirement doc version: #{requirement_doc_version}"


  puts "state index: #{state_index}"
  puts "state id: #{state_id}"
  puts "state doc version: #{state_doc_version}"
=end
  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && requirement_index && requirement_id && requirement_doc_version && state_index && state_id && state_doc_version)

 # puts 'all required params are present.'

  state = get_state(stage_index,sop_index,step_index,requirement_index,state_index)
    
 # puts "state got as : #{state}"

  return unless state

 # puts "found requirement"

  ## here we can do the redacting.
  state.assign_attributes(permitted_params)
  state.doc_version = state_doc_version + 1


  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.requirements.#{requirement_index}.states.#{state_index}" => state.attributes
    }
  }

  return update


end

#build_step_query(permitted_params, params) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'app/models/auth/workflow/assembly.rb', line 519

def build_step_query(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id] || params[:id]
  step_doc_version = permitted_params[:step_doc_version] ||permitted_params[:doc_version]

  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version)

  query =
  
    [
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}._id" => BSON::ObjectId(step_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.doc_version" => step_doc_version
      }
    ]

  return query

end

#build_step_update(permitted_params, params) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'app/models/auth/workflow/assembly.rb', line 549

def build_step_update(permitted_params,params)

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  
  step_index = permitted_params[:step_index]
  step_id = params[:id]
  step_doc_version = permitted_params[:doc_version]


  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version)

  #puts 'all required params are present.'

  step = get_step(stage_index,sop_index,step_index)
    
  #puts "step got as : #{step}"

  return unless step


  step.assign_attributes(permitted_params)
  step.doc_version = step_doc_version + 1


  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}" => step.attributes
    }
  }

  return update

end

#build_tlocation_query(permitted_params, params) ⇒ Object

> tlocation query and update



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'app/models/auth/workflow/assembly.rb', line 780

def build_tlocation_query(permitted_params,params)

  

  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]
  tlocation_index = permitted_params[:tlocation_index]
  tlocation_id = permitted_params[:tlocation_id] || params[:id]
  tlocation_doc_version = permitted_params[:tlocation_doc_version] || permitted_params[:doc_version]


  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && tlocation_index && tlocation_id && tlocation_doc_version)



  query =
  
    [
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.tlocations.#{tlocation_index}._id" => BSON::ObjectId(tlocation_id)     
      },
      {
        "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.tlocations.#{tlocation_index}.doc_version" => tlocation_doc_version
      }
    ]


 
  return query

end

#build_tlocation_update(permitted_params, params) ⇒ Object



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'app/models/auth/workflow/assembly.rb', line 820

def build_tlocation_update(permitted_params,params)


  stage_index = permitted_params[:stage_index]
  stage_id = permitted_params[:stage_id]
  stage_doc_version = permitted_params[:stage_doc_version]
  
  sop_index = permitted_params[:sop_index]
  sop_id = permitted_params[:sop_id]
  sop_doc_version = permitted_params[:sop_doc_version]
  
  step_index = permitted_params[:step_index]
  step_id = permitted_params[:step_id]
  step_doc_version = permitted_params[:step_doc_version]

  tlocation_index = permitted_params[:tlocation_index]
  tlocation_id = params[:id]
  tlocation_doc_version = permitted_params[:doc_version]  


  return unless (stage_index && stage_id && stage_doc_version && sop_index && sop_id && sop_doc_version && step_index && step_id && step_doc_version && tlocation_index && tlocation_id && tlocation_doc_version)

 # puts 'all required params are present.'

  tlocation = get_tlocation(stage_index,sop_index,step_index,tlocation_index)
    
 # puts "tlocation got as : #{tlocation}"

  return unless tlocation

 # puts "found tlocation"

  tlocation.assign_attributes(permitted_params)
  tlocation.doc_version = tlocation_doc_version + 1


  update = {
    "$set" => {
      "stages.#{stage_index}.sops.#{sop_index}.steps.#{step_index}.tlocations.#{tlocation_index}" => tlocation.attributes
    }
  }

  return update

end

#clone_to_add_cart_items(options) ⇒ Object

TRANSACTION BASED DEFS.

@return of event objects. @params options : one key is expected : ‘order’ => this will be the order object serialized.



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'app/models/auth/workflow/assembly.rb', line 1044

def clone_to_add_cart_items(options)
 
  return nil unless self.master
  return nil if (options[:order].blank?)
  new_assembly = self.clone
  new_assembly.master = false
  if new_assembly && new_assembly.save
    resulting_event = Auth::Transaction::Event.new
    resulting_event.object_class = Auth.configuration.sop_class
    resulting_event.method_to_call = "find_applicable_sops"
    order = Auth.configuration.order_class.constantize.new(JSON.parse(options[:order]))
    order.assembly_id = new_assembly.id.to_s
    order.assembly_doc_version = new_assembly.doc_version
    resulting_event.arguments = {:order => order.to_json(:methods => [:assembly_id,:assembly_doc_version])}
    return [resulting_event]
  end

  return nil

end

#create_order_in_multiple_sops(arguments) ⇒ Object



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'app/models/auth/workflow/assembly.rb', line 1074

def create_order_in_multiple_sops(arguments) 

  ## the arguments are the sops, and order.
  return nil if(arguments[:sops].blank? || arguments[:order].blank?)

  sops = JSON.parse(arguments[:sops]).map{|c| c = Auth.configuration.sop_class.constantize.new(c)}
  order = Auth.configuration.order_class.constantize.new(JSON.parse(arguments[:order]))
  
  assembly_updated = order.create_order_into_all_applicable_sops(sops)
  return false unless assembly_updated
  
  ## get the updated sops only.
  ## from the assembly.
  new_sops = sops.map{|c| 
    c = assembly_updated.stages[c.stage_index].sops[c.sop_index]
  }

  sops.each_with_index{|value,key|
    new_sops[key].stage_index = value.stage_index
    new_sops[key].sop_index = value.sop_index
  }


  e = Auth::Transaction::Event.new
  e.arguments = {:sops => new_sops.to_json(:methods => [:sop_index,:stage_index]), :order => order.to_json}
  #puts e.arguments.to_s
  e.object_class = Auth.configuration.assembly_class
  e.method_to_call = "schedule_order"
  e.object_id = order.assembly_id.to_s
  return [e]

end

#create_with_conditions(params, permitted_params, model) ⇒ Object

MODEL PERSISTENCE DEFINITIONS



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/models/auth/workflow/assembly.rb', line 196

def create_with_conditions(params,permitted_params,model)
  ## in this case the model is a stage model.
  ## we have to set everything other than anything which has an __metadata attribute, and anything else which has
  return false unless (model.valid?)

  ## if the assembly is being created with a master assembly id, then it should 
  if model.master_assembly_id
    ## clone the master
    model = Auth.configuration.assembly_class.constantize.find(master_assembly_id.to_s).clone
  end

  
  assembly_updated = Auth.configuration.assembly_class.constantize.where({
        :_id => model._id
  })
  .find_one_and_update(
    {
      "$set" => model.attributes.except("master")
    },
    {
      :return_document => :after,
      :upsert => true
    }
  )

  #puts assembly_updated.attributes.to_s
  #puts assembly_updated.stages.to_s

  return false unless assembly_updated
  return model


end

#get_order(stage_index, sop_index, order_index) ⇒ Object



387
388
389
# File 'app/models/auth/workflow/assembly.rb', line 387

def get_order(stage_index,sop_index,order_index)
  return self.stages[stage_index].sops[sop_index].orders[order_index]
end

#get_requirement(stage_index, sop_index, step_index, requirement_index) ⇒ Object



391
392
393
# File 'app/models/auth/workflow/assembly.rb', line 391

def get_requirement(stage_index,sop_index,step_index,requirement_index)
  return self.stages[stage_index].sops[sop_index].steps[step_index].requirements[requirement_index]
end

#get_sop(stage_index, sop_index) ⇒ Object

getters used in update functions.



375
376
377
# File 'app/models/auth/workflow/assembly.rb', line 375

def get_sop(stage_index,sop_index)
  return self.stages[stage_index].sops[sop_index]
end

#get_stage(stage_index) ⇒ Object



379
380
381
# File 'app/models/auth/workflow/assembly.rb', line 379

def get_stage(stage_index)
  return self.stages[stage_index]
end

#get_state(stage_index, sop_index, step_index, requirement_index, state_index) ⇒ Object



399
400
401
# File 'app/models/auth/workflow/assembly.rb', line 399

def get_state(stage_index,sop_index,step_index,requirement_index,state_index)
  return self.stages[stage_index].sops[sop_index].steps[step_index].requirements[requirement_index].states[state_index]
end

#get_step(stage_index, sop_index, step_index) ⇒ Object



383
384
385
# File 'app/models/auth/workflow/assembly.rb', line 383

def get_step(stage_index,sop_index,step_index)
  return self.stages[stage_index].sops[sop_index].steps[step_index]
end

#get_tlocation(stage_index, sop_index, step_index, tlocation_index) ⇒ Object



395
396
397
# File 'app/models/auth/workflow/assembly.rb', line 395

def get_tlocation(stage_index,sop_index,step_index,tlocation_index)
  return self.stages[stage_index].sops[sop_index].steps[step_index].tlocations[tlocation_index]
end

#masterObject

set as true if the assembly is to be considered as the master assembly. can it be set on create ? no it cannot. we first clone, for that we provide a master_assembly_id. then we update something else as the master. so master cannot be set, on create.



13
# File 'app/models/auth/workflow/assembly.rb', line 13

field :master, type: Boolean, default: false

#master_assembly_idObject

should the master assembly id be a field, so suppose i entered a master assembly id for this, then it should be easy for us to know that one. and it should never be possible to change that master id field. so that parameter is not to be permitted for update. master is also not be changed once it already exists. in what sense. once something is made a master, it cannot be unmade. also not possible to do that for master_field master_assembly_id once set cannot be changed, so that will have to be included in all the updates?



27
# File 'app/models/auth/workflow/assembly.rb', line 27

field :master_assembly_id, type: String

#master_assembly_id_is_latest_created_masterObject

CUSTOM DEFINITIONS.



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'app/models/auth/workflow/assembly.rb', line 1017

def master_assembly_id_is_latest_created_master
  return unless self.master_assembly_id
  return unless self.doc_version == 0
  latest_assembly = Auth.configuration.assembly_class.constantize.where({
    "master" => true 
  }).order_by(:created_at => 'desc').limit(1)

  self.errors.add(:master_assembly_id,"this is not the latest master assembly, please check for the latest master assembly, before cloning.") unless latest_assembly.first 

  return if self.errors.full_messages.size > 0

  self.errors.add(:master_assembly_id,"this is not the latest master assembly, please check for the latest master assembly, before cloning.") unless latest_assembly.first.id.to_s == self.master_assembly_id.to_s    

end

#schedule_order(arguments = {}) ⇒ Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'app/models/auth/workflow/assembly.rb', line 1107

def schedule_order(arguments={})

  return nil if(arguments[:sops].blank? || arguments[:order].blank?)
  sops = JSON.parse(arguments[:sops]).map{|c| c = Auth.configuration.sop_class.constantize.new(c)}
  order = Auth.configuration.order_class.constantize.new(JSON.parse(arguments[:order]))

  cart_items_latest_time = {}
  requirement_query_hash = {}

  sops.each do |sop|
    response = sop.schedule_order(cart_items_latest_time,requirement_query_hash)

    cart_items_latest_time = response[:cart_items_latest_time]
    requirement_query_hash = response[:requirement_query_hash]
  end
  
  e = Auth::Transaction::Event.new
  e.arguments = {:sops => sops.to_json, :requirement_query_hash => requirement_query_hash.to_json}
  e.object_class = Auth.configuration.assembly_class
  e.method_to_call = "after_schedule_order"
  e.object_id = order.assembly_id.to_s
  return [e]

end

#update_with_conditions(params, permitted_params, model) ⇒ Object

find_self on stage, sop or step -> returns an assembly object so in any controller, for update, the model will always be an assembly object so we only work on update_with_conditions for assembly. permitted params is just the result of calling the permitted params def, and fetching the model from it.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/models/auth/workflow/assembly.rb', line 236

def update_with_conditions(params,permitted_params,model)
  
  ensure_no_orders = false

  puts "the params are:"
  puts params.to_s

  puts "permitted params are:"
  puts permitted_params.to_s

  _id = permitted_params[:assembly_id] || params[:id] 
  doc_version = permitted_params[:assembly_doc_version] || permitted_params[:doc_version]

  return unless (_id && doc_version)

  query_and_conditions = [
    {
          "_id" => BSON::ObjectId(_id.to_s)
    },
    {

          "doc_version" => doc_version
    },
    {
          "master" => false
    }
  ]

  ## whatever the situation, the master has to be false, for any update to go through.
  ##  

  query = { "$and" => query_and_conditions}

  

  if stage_query = build_stage_query(permitted_params,params)
    query["$and"] = query["$and"] + stage_query
    if sop_query = build_sop_query(permitted_params,params)
      query["$and"] = query["$and"] + sop_query
      if step_query = build_step_query(permitted_params,params)
        query["$and"] = query["$and"] + step_query 
        if requirement_query = build_requirement_query(permitted_params,params)
          query["$and"] = query["$and"] + requirement_query
          if state_query = build_state_query(permitted_params,params)
            query["$and"] = query["$and"] + state_query
          end
        elsif tlocation_query = build_tlocation_query(permitted_params,params)
          query["$and"] = query["$and"] + tlocation_query
        end
      elsif order_query = build_order_query(permitted_params,params)
        query["$and"] = query["$and"] + order_query 
      end
    end
  end


  if state_update = build_state_update(permitted_params,params)
    update = state_update
    ensure_no_orders =  Auth.configuration.state_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif requirement_update = build_requirement_update(permitted_params,params)
    update = requirement_update
    ensure_no_orders =  Auth.configuration.requirement_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif tlocation_update = build_tlocation_update(permitted_params,params)
    update = tlocation_update
    ensure_no_orders =  Auth.configuration.tlocation_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif step_update = build_step_update(permitted_params,params)
    update = step_update
    ensure_no_orders =  Auth.configuration.step_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif order_update = build_order_update(permitted_params,params)
    update = order_update
    ensure_no_orders =  Auth.configuration.order_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif sop_update = build_sop_update(permitted_params,params)
    update = sop_update
    ensure_no_orders =  Auth.configuration.sop_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  elsif stage_update = build_stage_update(permitted_params,params)
    update = stage_update
    ensure_no_orders =  Auth.configuration.stage_class.constantize.permitted_params_contain_locked_fields(permitted_params
      )
  else
    ## its an assembly update.
    ## if the permitted params contain any of the assembly redacted fields, then we have to set ensure_no_orders here as well.    
    ensure_no_orders = Auth.configuration.assembly_class.constantize.permitted_params_contain_locked_fields(permitted_params)    

    model.assign_attributes(permitted_params)

    ## if its only the assembly , then it would just be "$set" => model.attributes
    ## if the master is being set to false, then ensure that it is already false.

    update = {
      "$set" => model.attributes.except("doc_version", "_id", "master_assembly_id"),
      "$inc" => {
        "doc_version" => 1
      }
    }

  end

 
  
  ## ensure that there are no orders anywhere inside assembly, in case we are trying to modify locked fields.
  if ensure_no_orders
      query["$and"] << {
        "stages.sops.orders" => {
          "$exists" => false
        }
      }
  end

  ## now do the where.find_one_and_update
  puts "query is:"
  puts JSON.pretty_generate(query)

  puts "update is:"
  puts JSON.pretty_generate(update)
  

  cl = Auth.configuration.assembly_class.constantize.where(query)

  puts "the results of the query are:"
  puts cl.count.to_s

  Auth.configuration.assembly_class.constantize.where(query).find_one_and_update(update,{:return_document => :after})

end