refactor(graphql): use new champs methods on API

This commit is contained in:
Paul Chavard 2023-11-28 16:30:23 +00:00
parent f7758d0033
commit 3e296fc75c
3 changed files with 25 additions and 13 deletions

View file

@ -11,16 +11,18 @@ module Types::Champs
field :rows, [Row], null: false field :rows, [Row], null: false
def champs def champs
Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs| object.rows.flat_map { _1.filter(&:visible?) }
champs.filter(&:visible?)
end
end end
def rows def rows
Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs| object
object.association(:champs).target = champs.filter(&:visible?) .rows
object.rows.map { { champs: _1, id: GraphQL::Schema::UniqueWithinType.encode('Row', _1.first.row_id) } } .map do
end {
id: GraphQL::Schema::UniqueWithinType.encode('Row', _1.first.row_id),
champs: _1.filter(&:visible?)
}
end
end end
end end
end end

View file

@ -157,10 +157,7 @@ module Types
.for(object, private: false) .for(object, private: false)
.load(ApplicationRecord.id_from_typed_id(id)) .load(ApplicationRecord.id_from_typed_id(id))
else else
Loaders::Association object.champs_for_revision(:public, true).filter(&:visible?)
.for(object.class, champs_public: :type_de_champ)
.load(object)
.then { |champs| champs.filter(&:visible?) }
end end
end end
@ -170,7 +167,7 @@ module Types
.for(object, private: true) .for(object, private: true)
.load(ApplicationRecord.id_from_typed_id(id)) .load(ApplicationRecord.id_from_typed_id(id))
else else
Loaders::Association.for(object.class, champs_private: :type_de_champ).load(object) object.champs_for_revision(:private, true).filter(&:visible?)
end end
end end

View file

@ -36,8 +36,21 @@ class ChampSerializer < ActiveModel::Serializer
object.etablissement&.entreprise object.etablissement&.entreprise
end end
class Row < Hashie::Dash
property :index
property :champs
def read_attribute_for_serialization(attribute)
self[attribute]
end
end
def rows def rows
object.rows_for_export object.dossier
.champs_for_revision(object.type_de_champ)
.group_by(&:row_id)
.values
.map.with_index(1) { |champs, index| Row.new(index:, champs:) }
end end
def include_etablissement? def include_etablissement?