demarches-normaliennes/app/serializers/dossier_serializer.rb

108 lines
2.3 KiB
Ruby
Raw Normal View History

2016-01-20 15:48:46 +01:00
class DossierSerializer < ActiveModel::Serializer
2018-11-06 18:45:29 +01:00
include DossierHelper
2016-01-20 15:48:46 +01:00
attributes :id,
2017-06-12 13:49:51 +02:00
:created_at,
:updated_at,
:archived,
2017-08-01 14:19:58 +02:00
:email,
2017-06-12 13:49:51 +02:00
:state,
:simplified_state,
:initiated_at,
:received_at,
:processed_at,
2017-06-15 17:36:30 +02:00
:motivation,
2018-10-10 09:06:32 +02:00
:instructeurs
has_one :individual
has_one :entreprise
has_one :etablissement
has_many :cerfa
2016-03-14 10:34:46 +01:00
has_many :commentaires
has_many :champs_private
has_many :pieces_justificatives
2016-03-17 14:50:10 +01:00
has_many :types_de_piece_justificative
has_one :justificatif_motivation
2019-06-27 14:38:57 +02:00
has_many :avis
has_many :champs, serializer: ChampSerializer
2018-10-31 13:32:17 +01:00
def champs
champs = object.champs.reject { |c| c.type_de_champ.old_pj.present? }
2018-10-31 13:32:17 +01:00
2018-11-27 15:55:14 +01:00
if object.expose_legacy_carto_api?
champ_carte = champs.find do |champ|
champ.type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:carte)
end
if champ_carte.present?
carto_champs = champ_carte.geo_areas.to_a
carto_champs << champ_carte.user_geo_area
champs += carto_champs.compact
end
end
2018-10-31 13:32:17 +01:00
champs
end
2018-04-24 16:02:36 +02:00
def cerfa
[]
end
def pieces_justificatives
ActiveModelSerializers::SerializableResource.new(object.pieces_justificatives).serializable_hash +
PiecesJustificativesService.serialize_champs_as_pjs(object)
end
def justificatif_motivation
if object.justificatif_motivation.attached?
Rails.application.routes.url_helpers.url_for(object.justificatif_motivation)
end
end
def types_de_piece_justificative
ActiveModelSerializers::SerializableResource.new(object.types_de_piece_justificative).serializable_hash +
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object)
end
2017-08-01 14:19:58 +02:00
def email
2018-05-30 18:45:46 +02:00
object.user&.email
2017-08-01 14:19:58 +02:00
end
def entreprise
object.etablissement&.entreprise
end
def state
2018-11-06 18:46:17 +01:00
dossier_legacy_state(object)
end
def simplified_state
2018-11-06 18:45:29 +01:00
dossier_display_state(object)
end
2017-04-14 18:10:39 +02:00
def initiated_at
object.en_construction_at&.in_time_zone('UTC')
end
def received_at
object.en_instruction_at&.in_time_zone('UTC')
end
2018-08-29 22:11:38 +02:00
def instructeurs
2017-04-14 18:10:39 +02:00
object.followers_gestionnaires.pluck(:email)
end
2017-04-14 18:20:14 +02:00
def created_at
object.created_at&.in_time_zone('UTC')
end
def updated_at
object.updated_at&.in_time_zone('UTC')
end
def processed_at
object.processed_at&.in_time_zone('UTC')
end
end