diff --git a/app/graphql/types/demarche_type.rb b/app/graphql/types/demarche_type.rb index 91745ed25..5f0eafe3f 100644 --- a/app/graphql/types/demarche_type.rb +++ b/app/graphql/types/demarche_type.rb @@ -144,7 +144,19 @@ module Types dossiers = dossiers.hidden_since(deleted_since) end - dossiers.order(hidden_by_user_at: order, hidden_by_administration_at: order) + dossiers_table = Dossier.arel_table + case_statement = dossiers_table[:state] + .when(:en_construction) + .then(dossiers_table[:hidden_by_user_at]) + .else(dossiers_table[:hidden_by_administration_at]) + + dossiers = dossiers.order(case_statement) + + if order == :desc + dossiers = dossiers.reverse_order + end + + dossiers end def champ_descriptors diff --git a/app/graphql/types/groupe_instructeur_with_dossiers_type.rb b/app/graphql/types/groupe_instructeur_with_dossiers_type.rb index f80b38e02..91d0865cb 100644 --- a/app/graphql/types/groupe_instructeur_with_dossiers_type.rb +++ b/app/graphql/types/groupe_instructeur_with_dossiers_type.rb @@ -83,7 +83,19 @@ module Types dossiers = dossiers.hidden_since(deleted_since) end - dossiers.order(hidden_by_user_at: order, hidden_by_administration_at: order) + dossiers_table = Dossier.arel_table + case_statement = dossiers_table[:state] + .when(:en_construction) + .then(dossiers_table[:hidden_by_user_at]) + .else(dossiers_table[:hidden_by_administration_at]) + + dossiers = dossiers.order(case_statement) + + if order == :desc + dossiers = dossiers.reverse_order + end + + dossiers end end end diff --git a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb index 0a473dc19..8da03933a 100644 --- a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb @@ -185,17 +185,20 @@ describe API::V2::GraphqlController do before { dossier.hide_and_keep_track!(dossier.user, DeletedDossier.reasons.fetch(:user_request)) - dossier_accepte.hide_and_keep_track!(instructeur, DeletedDossier.reasons.fetch(:instructeur_request)) + Timecop.travel(3.hours.ago) { + dossier_accepte.hide_and_keep_track!(instructeur, DeletedDossier.reasons.fetch(:instructeur_request)) + } } it { expect(gql_errors).to be_nil expect(gql_data[:demarche][:id]).to eq(procedure.to_typed_id) expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].size).to eq(2) - expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].first[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier.id)) - expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].second[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier_accepte.id)) - expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].first[:dateSupression]).to eq(dossier.hidden_by_user_at.iso8601) - expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].second[:dateSupression]).to eq(dossier_accepte.hidden_by_administration_at.iso8601) + expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].first[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier_accepte.id)) + expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].second[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier.id)) + expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].first[:dateSupression]).to eq(dossier_accepte.hidden_by_administration_at.iso8601) + expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].second[:dateSupression]).to eq(dossier.hidden_by_user_at.iso8601) + expect(gql_data[:demarche][:pendingDeletedDossiers][:nodes].first[:dateSupression] < gql_data[:demarche][:pendingDeletedDossiers][:nodes].second[:dateSupression]) } end end @@ -252,17 +255,20 @@ describe API::V2::GraphqlController do before { dossier.hide_and_keep_track!(dossier.user, DeletedDossier.reasons.fetch(:user_request)) - dossier_accepte.hide_and_keep_track!(instructeur, DeletedDossier.reasons.fetch(:instructeur_request)) + Timecop.travel(3.hours.ago) { + dossier_accepte.hide_and_keep_track!(instructeur, DeletedDossier.reasons.fetch(:instructeur_request)) + } } it { expect(gql_errors).to be_nil expect(gql_data[:groupeInstructeur][:id]).to eq(groupe_instructeur.to_typed_id) expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].size).to eq(2) - expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].first[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier.id)) - expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].second[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier_accepte.id)) - expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].first[:dateSupression]).to eq(dossier.hidden_by_user_at.iso8601) - expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].second[:dateSupression]).to eq(dossier_accepte.hidden_by_administration_at.iso8601) + expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].first[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier_accepte.id)) + expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].second[:id]).to eq(GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', dossier.id)) + expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].first[:dateSupression]).to eq(dossier_accepte.hidden_by_administration_at.iso8601) + expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].second[:dateSupression]).to eq(dossier.hidden_by_user_at.iso8601) + expect(gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].first[:dateSupression] < gql_data[:groupeInstructeur][:pendingDeletedDossiers][:nodes].second[:dateSupression]) } end end