Class: Auth::Workflow::Sop
- Inherits:
-
Object
- Object
- Auth::Workflow::Sop
- Includes:
- Concerns::WorkflowConcern
- Defined in:
- app/models/auth/workflow/sop.rb
Constant Summary collapse
- FIELDS_LOCKED_AFTER_ORDER_ADDED =
["applicable"]
Instance Attribute Summary collapse
-
#assembly_doc_version ⇒ Object
Returns the value of attribute assembly_doc_version.
-
#assembly_id ⇒ Object
Returns the value of attribute assembly_id.
-
#sop_index ⇒ Object
Returns the value of attribute sop_index.
-
#stage_doc_version ⇒ Object
Returns the value of attribute stage_doc_version.
-
#stage_id ⇒ Object
Returns the value of attribute stage_id.
-
#stage_index ⇒ Object
Returns the value of attribute stage_index.
Class Method Summary collapse
-
.find_applicable_sops(options = {}) ⇒ Object
@return array of hashes, each with the following structure: it basically returns the stage_index as well as the sop_index alongwith their respective ids.
- .find_self(id, signed_in_resource, options = {}) ⇒ Object
- .permitted_params ⇒ Object
Instance Method Summary collapse
-
#after_create_order(order) ⇒ Object
@return : array of Auth::Transaction::Event Objects.
-
#create_order(arguments = {}) ⇒ Object
defs for events.
- #create_with_conditions(params, permitted_params, model) ⇒ Object
-
#generate_mark_requirement_events(order) ⇒ Object
this was previously in after_create_order.
-
#get_many ⇒ Object
called from #index in authenticated_controller.
-
#get_order_index(order) ⇒ Object
@return the index in the sop’s orders of the given order.
-
#get_sop_requirements(order) ⇒ Object
@return array of Auth::Workflow::Requirement objects.
-
#has_order_with_cart_items(order) ⇒ Object
return true if the sop already has some order with any of the cart_items in this order.
-
#last_time_slot_applicable_to_present_cart_items(cart_items_latest_time) ⇒ Object
iterates the cart_items in the last order of this sop, and sees which of them has the latest start time from the provided hash.
-
#schedule_order(cart_items_latest_time, requirement_query_hash) ⇒ Object
this has to return the cart_items_latest_time, as well as requirement_query_hash both are hashes.
-
#schedule_order_new(cart_items, cart_item_to_preceeding_step_hash) ⇒ Object
@param cart_items : array of cart item objects, basically the cart_items from the order which are applicable to this sop.
-
#update_cart_items_latest_time(last_step, cart_items_latest_time) ⇒ Object
@param last_step : the last step in the sop.
Instance Attribute Details
#assembly_doc_version ⇒ Object
Returns the value of attribute assembly_doc_version.
15 16 17 |
# File 'app/models/auth/workflow/sop.rb', line 15 def assembly_doc_version @assembly_doc_version end |
#assembly_id ⇒ Object
Returns the value of attribute assembly_id.
14 15 16 |
# File 'app/models/auth/workflow/sop.rb', line 14 def assembly_id @assembly_id end |
#sop_index ⇒ Object
Returns the value of attribute sop_index.
19 20 21 |
# File 'app/models/auth/workflow/sop.rb', line 19 def sop_index @sop_index end |
#stage_doc_version ⇒ Object
Returns the value of attribute stage_doc_version.
17 18 19 |
# File 'app/models/auth/workflow/sop.rb', line 17 def stage_doc_version @stage_doc_version end |
#stage_id ⇒ Object
Returns the value of attribute stage_id.
18 19 20 |
# File 'app/models/auth/workflow/sop.rb', line 18 def stage_id @stage_id end |
#stage_index ⇒ Object
Returns the value of attribute stage_index.
16 17 18 |
# File 'app/models/auth/workflow/sop.rb', line 16 def stage_index @stage_index end |
Class Method Details
.find_applicable_sops(options = {}) ⇒ Object
@return array of hashes, each with the following structure: it basically returns the stage_index as well as the sop_index alongwith their respective ids. the matches array contains the product ids to which that sop is applicable, out of the product ids supplied.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'app/models/auth/workflow/sop.rb', line 50 def self.find_applicable_sops(={}) ## instantiate the order object from the serialized hash passed in. order = Auth.configuration.order_class.constantize.new(JSON.parse([:order])) ## we need to get the product ids , given the cart item ids. product_ids = order.cart_item_ids.map{|c| cart_item = Auth.configuration.cart_item_class.constantize.find(c) c = cart_item.product_id }.uniq assembly_id = order.assembly_id res = Auth.configuration.assembly_class.constantize.collection.aggregate([ { "$match" => { "_id" => BSON::ObjectId(assembly_id) } }, { "$unwind" => { "path" => "$stages", "includeArrayIndex" => "stage_index" } }, { "$unwind" => { "path" => "$stages.sops", "includeArrayIndex" => "sop_index" } }, { "$project" => { "common_products" => { "$setIntersection" => ["$stages.sops.applicable_to_product_ids",product_ids] }, "stages" => 1, "sops" => 1, "sop_index" => 1, "stage_index" => 1, "doc_version" => 1, "_id" => 1 } }, { "$addFields" => { "stages.sops.sop_index" => "$sop_index", "stages.sops.stage_index" => "$stage_index", "stages.sops.assembly_doc_version" => "$doc_version", "stages.sops.stage_doc_version" => "$stages.doc_version", "stages.sops.stage_id" => "$stages._id", "stages.sops.doc_version" => "$stages.sops.doc_version", "stages.sops.assembly_id" => "$_id", "stages.sops._id" => "$stages.sops._id", "common_products" => { "$ifNull" => [ "$common_products", []] } } }, { "$project" => { "stages" => { "$cond" => { "if" => { "$gt" => [ {"$size" => "$common_products"}, 0 ] }, "then" => "$stages", "else" => "$$REMOVE" } }, "sop_index" => 1, "stage_index" => 1 } }, { "$group" => { "_id" => nil, "sops" => { "$push" => "$stages.sops" } } } ]) ## so we want to return an array of SOP objects. #puts "initial res is:" #res.each do |result| # puts JSON.pretty_generate(result) #end #puts "res is :#{res}" begin return [] unless res return [] unless res.count > 0 applicable_sops = res.first["sops"].map{|sop_hash| #puts "sop hash is:" #puts JSON.pretty_generate(sop_hash) k = Mongoid::Factory.from_db(Auth.configuration.sop_class.constantize,sop_hash) k.stage_index = sop_hash["stage_index"] k.sop_index = sop_hash["sop_index"] k } ## now emit create_order events. events = [] ## the important thing at this stage will be to add the relevant information on the order. if applicable_sops.size > 0 #puts "THE FIRST STAGE INDEX IS:" #puts applicable_sops.first.stage_index #puts "the first sop index is:" #puts applicable_sops.first.sop_index e = Auth::Transaction::Event.new e.arguments = .merge({:sops => applicable_sops.to_json}) #puts e.arguments.to_s e.object_class = Auth.configuration.assembly_class e.method_to_call = "create_order_in_multiple_sops" e.object_id = order.assembly_id.to_s events << e end events rescue => e puts "rescued" puts e.to_s return nil end end |
.find_self(id, signed_in_resource, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'app/models/auth/workflow/sop.rb', line 33 def self.find_self(id,signed_in_resource,={}) #puts "the id is: #{id}" return nil unless collection = Auth.configuration.assembly_class.constantize.where("stages.sops._id" => BSON::ObjectId(id) ) collection.first end |
.permitted_params ⇒ Object
42 43 44 |
# File 'app/models/auth/workflow/sop.rb', line 42 def self.permitted_params [{:sop => [:name, :applicable, :description,:assembly_id,:assembly_doc_version,:stage_id,:stage_doc_version,:stage_index,:doc_version, :sop_index, {:applicable_to_product_ids => []}]},:id] end |
Instance Method Details
#after_create_order(order) ⇒ Object
@return : array of Auth::Transaction::Event Objects. the event points to the schedule order function. is called from assembly.
357 358 359 360 361 362 363 364 365 366 367 |
# File 'app/models/auth/workflow/sop.rb', line 357 def after_create_order(order) #puts "came to after_create order." e = Auth::Transaction::Event.new e.arguments = {} e.arguments[:sop_index] = order.sop_index e.arguments[:stage_index] = order.stage_index e.method_to_call = "sop_schedule_order" e.object_class = Auth.configuration.assembly_class e.object_id = order.assembly_id.to_s [e] end |
#create_order(arguments = {}) ⇒ Object
defs for events
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'app/models/auth/workflow/sop.rb', line 333 def create_order(arguments={}) #puts "came to create order" #puts JSON.pretty_generate(arguments) return nil if (arguments[:assembly_id].blank? || arguments[:assembly_doc_version].blank? || arguments[:stage_id].blank? || arguments[:stage_index].blank? || arguments[:stage_doc_version].blank? || arguments[:sop_id].blank? || arguments[:sop_index].blank? || arguments[:sop_doc_version].blank?) ## okay, wtf is this? order = Auth.configuration.order_class.constantize.new(:cart_item_ids => arguments[:cart_item_ids],:stage_index => arguments[:stage_index],:stage_id => arguments[:stage_id], :sop_index => arguments[:sop_index], :sop_id => arguments[:sop_id], :assembly_id => arguments[:assembly_id], :assembly_doc_version => arguments[:assembly_doc_version],:sop_doc_version => arguments[:sop_doc_version], :stage_doc_version => arguments[:stage_doc_version], :action => 1) if order_created = order.create_with_conditions(nil,nil,order) return after_create_order(order_created) else #puts "order was not created and checking if any past order already has these cart items?" return nil unless has_order_with_cart_items(order) #puts "it seems it does, so now doing after_create_order." return after_create_order(order) end end |
#create_with_conditions(params, permitted_params, model) ⇒ Object
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/models/auth/workflow/sop.rb', line 203 def create_with_conditions(params,permitted_params,model) ## in this case the model is a stage model. return false unless model.valid? assembly_updated = Auth.configuration.assembly_class.constantize.where({ "$and" => [ { "stages.#{model.stage_index}._id" => BSON::ObjectId(model.stage_id) }, { "stages.#{model.stage_index}.doc_version" => model.stage_doc_version }, { "_id" => BSON::ObjectId(model.assembly_id) }, { "doc_version" => model.assembly_doc_version }, { "stages.sops.orders" => { "$exists" => false } } ] }) .find_one_and_update( { "$push" => { "stages.#{stage_index}.sops" => model.attributes } }, { :return_document => :after } ) #puts "assembly updated is: #{assembly_updated}" return false unless assembly_updated return model end |
#generate_mark_requirement_events(order) ⇒ Object
this was previously in after_create_order.
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'app/models/auth/workflow/sop.rb', line 371 def generate_mark_requirement_events(order) if get_order_index(order) == 0 ## we want to return an array of events. sop_requirements_with_calculated_states = get_sop_requirements(order) sop_requirements_with_calculated_states.each_with_index.map{|requirement,i| e = Auth::Transaction::Event.new e.arguments = {} e.arguments[:sop_index] = order.sop_index e.arguments[:stage_index] = order.stage_index e.arguments[:requirement_index] = i e.arguments[:step_index] = requirement.step_index e.arguments[:requirement] = requirement.attributes if i == (sop_requirements_with_calculated_states.size - 1) e.arguments[:last_requirement] = true e.arguments[:sop_id] = self.id.to_s end e.method_to_call = "mark_sop_requirements" e.object_class = Auth.configuration.assembly_class e.object_id = order.assembly_id.to_s requirement = e } else ## do nothing. end end |
#get_many ⇒ Object
called from #index in authenticated_controller.
254 255 256 |
# File 'app/models/auth/workflow/sop.rb', line 254 def get_many self.class.find_applicable_sops({:product_ids => self.applicable_to_product_ids, :assembly_id => self.assembly_id}) end |
#get_order_index(order) ⇒ Object
@return the index in the sop’s orders of the given order.
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 |
# File 'app/models/auth/workflow/sop.rb', line 277 def get_order_index(order) ## unwind, and match. order_els = Auth.configuration.assembly_class.constantize.collection.aggregate([ { "$match" => { "_id" => BSON::ObjectId(order.assembly_id.to_s) } }, { "$unwind" => { "path" => "$stages", "includeArrayIndex" => "stage_index" } }, { "$unwind" => { "path" => "$stages.sops", "includeArrayIndex" => "sop_index" } }, { "$unwind" => { "path" => "$stages.sops.orders", "includeArrayIndex" => "order_index" } }, { "$match" => { "stages.sops.orders._id" => BSON::ObjectId(order.id.to_s) } } ]) raise "did'nt find the order" unless order_els return order_els.first["order_index"] end |
#get_sop_requirements(order) ⇒ Object
@return array of Auth::Workflow::Requirement objects.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'app/models/auth/workflow/sop.rb', line 403 def get_sop_requirements(order) requirements_with_calculated_states = [] steps.each_with_index {|step,step_index| step.requirements.each do |requirement| requirement.step_index = step_index requirement.calculate_required_states(order) requirements_with_calculated_states << requirement end } #puts "these are the requirements with calculated states." #puts requirements_with_calculated_states.to_s requirements_with_calculated_states end |
#has_order_with_cart_items(order) ⇒ Object
return true if the sop already has some order with any of the cart_items in this order.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'app/models/auth/workflow/sop.rb', line 259 def has_order_with_cart_items(order) results =Auth.configuration.assembly_class.constantize.where({ "$and" => [ { "_id" => BSON::ObjectId(order.assembly_id.to_s) }, { "stages.#{order.stage_index}.sops.#{order.sop_index}.orders.cart_item_ids" => { "$in" => order.cart_item_ids } } ] }) return results && results.size == 1 end |
#last_time_slot_applicable_to_present_cart_items(cart_items_latest_time) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'app/models/auth/workflow/sop.rb', line 433 def last_time_slot_applicable_to_present_cart_items(cart_items_latest_time) greatest_end_time = nil cart_items_latest_time.values.each_with_index{|value,key| greatest_end_time = value unless greatest_end_time if greatest_end_time #puts "value is:" #puts value.to_s #puts "greatest end time is:" #puts greatest_end_time.to_s if value[:end_time_range][1] > greatest_end_time[:end_time_range][1] greatest_end_time = value end end } return greatest_end_time || {} end |
#schedule_order(cart_items_latest_time, requirement_query_hash) ⇒ Object
this has to return the cart_items_latest_time, as well as requirement_query_hash both are hashes. we should just update the results of the query on the step itself. and schedule order should return the last step. and nothing more. secondly, there is only one call. basically it sends the cart_item information to the step so schedule order should receive the cart items array. and nothing else.
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 547 548 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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'app/models/auth/workflow/sop.rb', line 521 def schedule_order(cart_items_latest_time, requirement_query_hash) latest_ending_cart_item = last_time_slot_applicable_to_present_cart_items(cart_items_latest_time) self.steps.each_with_index{|step,key| next unless step.applicable #puts "the self stage index is: #{self.stage_index}, and self sop index is: #{self.sop_index}" step.step_index = key step.stage_index = self.stage_index step.sop_index = self.sop_index ## here we have to check the specifications passed in with the cart items, against the specifications provided inside the step ## and if all is ok, then it will take those and pass them to the query type defined in the step. ## that is the idea. ## so it has to make a seperate query maybe for each of the cart items. ## it will have to have this map somewhere. ## of all the query results till now for each cart_item. step.modify_tlocation_conditions_for_each_product(self.orders.last,self.stage_index,self.sop_index,key) step.calculate_duration ## how to set the default time information. step.time_information ||= {:minimum_time_since_previous_step => 0, :maximum_time_since_previous_step => 1} step.time_information[:minimum_time_since_previous_step] ||= 0 step.time_information[:maximum_time_since_previous_step] ||= 1 ## done. step.resolve_location(key > 0 ? self.steps[key-1].location_information : {}) step.resolve_start_time(key > 0 ? self.steps[key-1].time_information : latest_ending_cart_item) step.resolve_requirements if step.resolve step.calculate_duration step.resolve_end_time step.requirements.each_with_index{|req,req_key| next unless (req.applicable && req.schedulable) req.stage_index = step.stage_index req.sop_index = step.sop_index req.step_index = step.step_index if req.reference_requirement_address == nil requirement_query_hash[req.get_self_address(req_key)] = [{:start_time_range => step.time_information[:start_time_range], :end_time_range => step.time_information[:end_time_range]}] elsif requirement_query_hash[req.reference_requirement_address].nil? requirement_query_hash[req.get_self_address(req_key)] = [{:start_time_range => step.time_information[:start_time_range], :end_time_range => step.time_information[:end_time_range]}] else #puts "the reference requirement address is:" #puts req.reference_requirement_address.to_s #puts "this is the existing requirmenet query hash." #puts requirement_query_hash.to_s #puts "this is the step time information" #puts step.time_information if requirement_query_hash[req.reference_requirement_address].last[:end_time_range] == step.time_information[:start_time_range] #puts "end time range of last step is equal to the start time range of this step" ## now set this as the new hash. requirement_query_hash[req.reference_requirement_address][-1] = {:start_time_range => requirement_query_hash[req.reference_requirement_address].last[:start_time_range], :end_time_range => step.time_information[:end_time_range]} elsif requirement_query_hash[req.reference_requirement_address].last[:end_time_range][1] < step.time_information[:start_time_range][1] #puts "end time range of last step is less than start time range of this step." requirement_query_hash[req.reference_requirement_address] << {:start_time_range => step.time_information[:start_time_range], :end_time_range => step.time_information[:end_time_range]} end end } } cart_items_latest_time = update_cart_items_latest_time(self.steps.last,cart_items_latest_time) return {:cart_items_latest_time => cart_items_latest_time, :requirement_query_hash => requirement_query_hash} end |
#schedule_order_new(cart_items, cart_item_to_preceeding_step_hash) ⇒ Object
@param cart_items : array of cart item objects, basically the cart_items from the order which are applicable to this sop. @param cart_item => last_step hash. this hash contains the last step for each cart item if more than one cart item has been updated in a step, so suppose we have more than one cart item on a particular step, then what ? step_address => [cart_item_ids,step] step_address => [cart_item_ids,step]
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'app/models/auth/workflow/sop.rb', line 490 def schedule_order_new(cart_items,cart_item_to_preceeding_step_hash) ## so to do this, we will have to save the results ordered by minute. ## how to do this with multiple locations. ## if more than one location is present at a minute, for more than one entity? ## in that aggregation we sort by minute. ## and nearness of the location. ## so first of all how to do that, then only we can move forward here. self.steps.each_with_index.map{|step,key| break unless step.merge_cart_item_specifications(cart_items) ## now the next step is to do the query. ## once we have the results from the query ## we can add that to the step ## then we can } cart_item_to_preceeding_step_hash end |
#update_cart_items_latest_time(last_step, cart_items_latest_time) ⇒ Object
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'app/models/auth/workflow/sop.rb', line 461 def update_cart_items_latest_time(last_step,cart_items_latest_time) if cart_items_latest_time.empty? self.orders.last.cart_item_ids.each do |c_id| cart_items_latest_time[c_id] = {} cart_items_latest_time[c_id][:start_time_range] = last_step.time_information[:start_time_range] cart_items_latest_time[c_id][:end_time_range] = last_step.time_information[:end_time_range] end else cart_items_latest_time.keys.each do |c_id| if self.orders.last.cart_item_ids.include? c_id cart_items_latest_time[c_id][:start_time_range] = last_step.time_information[:start_time_range] cart_items_latest_time[c_id][:end_time_range] = last_step.time_information[:end_time_range] end end end cart_items_latest_time end |