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
def champs
Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs|
champs.filter(&:visible?)
end
object.rows.flat_map { _1.filter(&:visible?) }
end
def rows
Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs|
object.association(:champs).target = champs.filter(&:visible?)
object.rows.map { { champs: _1, id: GraphQL::Schema::UniqueWithinType.encode('Row', _1.first.row_id) } }
end
object
.rows
.map do
{
id: GraphQL::Schema::UniqueWithinType.encode('Row', _1.first.row_id),
champs: _1.filter(&:visible?)
}
end
end
end
end

View file

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

View file

@ -36,8 +36,21 @@ class ChampSerializer < ActiveModel::Serializer
object.etablissement&.entreprise
end
class Row < Hashie::Dash
property :index
property :champs
def read_attribute_for_serialization(attribute)
self[attribute]
end
end
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
def include_etablissement?