|  |  | @ -37,30 +37,45 @@ module Submissions | 
			
		
	
		
		
			
				
					
					|  |  |  |       submissions.where(created_by_user_id: user&.id || -1) |  |  |  |       submissions.where(created_by_user_id: user&.id || -1) | 
			
		
	
		
		
			
				
					
					|  |  |  |     end |  |  |  |     end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     # rubocop:disable Metrics/MethodLength | 
			
		
	
		
		
			
				
					
					|  |  |  |     def filter_by_status(submissions, filters) |  |  |  |     def filter_by_status(submissions, filters) | 
			
		
	
		
		
			
				
					
					|  |  |  |       submissions = submissions.pending if filters[:status] == 'pending' |  |  |  |       case filters[:status] | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       submissions = submissions.completed if filters[:status] == 'completed' |  |  |  |       when 'pending' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       submissions = submissions.declined if filters[:status] == 'declined' |  |  |  |         submissions.pending | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       submissions = submissions.expired if filters[:status] == 'expired' |  |  |  |       when 'completed' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |         submissions.completed | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       if filters[:status] == 'partially_completed' |  |  |  |       when 'declined' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         submissions = |  |  |  |         submissions.declined | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |           submissions.joins(:submitters) |  |  |  |       when 'expired' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                      .group(:id) |  |  |  |         submissions.expired | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                      .having(Arel::Nodes::NamedFunction.new( |  |  |  |       when 'sent' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                        'COUNT', [Arel::Nodes::NamedFunction.new('NULLIF', |  |  |  |         submissions.joins(:submitters) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                                                 [Submitter.arel_table[:completed_at].eq(nil), |  |  |  |                    .where(submitters: { opened_at: nil, completed_at: nil, declined_at: nil }) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                                                  Arel::Nodes.build_quoted(false)])] |  |  |  |                    .where.not(submitters: { sent_at: nil }) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                      ).gt(0)) |  |  |  |                    .group(:id) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                      .having(Arel::Nodes::NamedFunction.new( |  |  |  |       when 'opened' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                        'COUNT', [Arel::Nodes::NamedFunction.new('NULLIF', |  |  |  |         submissions.joins(:submitters) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                                                 [Submitter.arel_table[:completed_at].not_eq(nil), |  |  |  |                    .where(submitters: { completed_at: nil, declined_at: nil }) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                                                  Arel::Nodes.build_quoted(false)])] |  |  |  |                    .where.not(submitters: { opened_at: nil }) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                      ).gt(0)) |  |  |  |                    .group(:id) | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       when 'partially_completed' | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         submissions.joins(:submitters) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                    .group(:id) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                    .having(Arel::Nodes::NamedFunction.new( | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                      'COUNT', [Arel::Nodes::NamedFunction.new('NULLIF', | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                                                               [Submitter.arel_table[:completed_at].eq(nil), | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                                                                Arel::Nodes.build_quoted(false)])] | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                    ).gt(0)) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                    .having(Arel::Nodes::NamedFunction.new( | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                      'COUNT', [Arel::Nodes::NamedFunction.new('NULLIF', | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                                                               [Submitter.arel_table[:completed_at].not_eq(nil), | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                                                                Arel::Nodes.build_quoted(false)])] | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                    ).gt(0)) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       else | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         submissions | 
			
		
	
		
		
			
				
					
					|  |  |  |       end |  |  |  |       end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       submissions |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     end |  |  |  |     end | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     # rubocop:enable Metrics/MethodLength | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     def filter_by_created_at(submissions, filters) |  |  |  |     def filter_by_created_at(submissions, filters) | 
			
		
	
		
		
			
				
					
					|  |  |  |       submissions = submissions.where(created_at: filters[:created_at_from]..) if filters[:created_at_from].present? |  |  |  |       submissions = submissions.where(created_at: filters[:created_at_from]..) if filters[:created_at_from].present? | 
			
		
	
	
		
		
			
				
					|  |  | 
 |