-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeds.rb
295 lines (241 loc) · 10.6 KB
/
seeds.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
class Seeds
def perform
create_entities
create_all_verified_emails
create_authorization_requests_for_clamart
create_authorization_requests_for_dinum
end
def flushdb
raise 'Not in production!' if production?
ActiveRecord::Base.connection.tables.each do |table|
# rubocop:disable Performance/CollectionLiteralInLoop
next if %w[schema_migrations ar_internal_metadata].include?(table)
# rubocop:enable Performance/CollectionLiteralInLoop
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table} CASCADE;")
end
end
private
# rubocop:disable Metrics/AbcSize
def create_entities
clamart_organization.users << demandeur
clamart_organization.users << another_demandeur
dinum_organization.users << api_entreprise_instructor
dinum_organization.users << api_entreprise_reporter
dinum_organization.users << foreign_demandeur
dinum_organization.users << data_pass_admin
end
# rubocop:enable Metrics/AbcSize
def create_authorization_requests_for_clamart
create_validated_authorization_request(:api_entreprise, attributes: { intitule: "Portail des appels d'offres", applicant: demandeur })
authorization_request = create_request_changes_authorization_request(:api_entreprise, attributes: { intitule: 'Portail des aides publiques', applicant: another_demandeur })
send_message_to_instructors(authorization_request, body: 'Bonjour, je ne suis pas sûr du cadre légal de cette demande, pouvez-vous m\'aider ?')
send_message_to_applicant(authorization_request, body: 'Bonjour, il faut que vous demandiez à votre DPO de vous fournir le document inférent à votre demande.')
authorization_request = create_request_changes_authorization_request(:api_entreprise, attributes: { intitule: 'Portail des aides dans le secteur du bâtiment' })
send_message_to_instructors(authorization_request, body: 'Bonjour, dois-je inclure les aides pour les particuliers ?')
send_message_to_applicant(authorization_request, body: 'Bonjour, non il s\'agit uniquement des aides pour les entreprises.')
create_refused_authorization_request(:api_entreprise, attributes: { intitule: 'Statistiques sur les effectifs', applicant: demandeur })
create_reopened_authorization_request(:api_entreprise_mgdis, attributes: { applicant: demandeur })
authorization_request = create_submitted_authorization_request(:api_entreprise, attributes: { intitule: 'Place des entreprises', applicant: another_demandeur })
send_message_to_instructors(authorization_request, body: "Je ne suis pas sûr du cadre de cette demande, pouvez-vous m'aider ?")
end
def create_authorization_requests_for_dinum
create_validated_authorization_request(:api_entreprise, attributes: { intitule: 'Démarches simplifiées', applicant: foreign_demandeur, contact_metier_email: demandeur.email })
create_validated_authorization_request(:api_particulier, attributes: { intitule: 'Cantine à 1€', applicant: demandeur, scopes: AuthorizationDefinition.find('api_particulier').scopes.map(&:value).sample(3) + ['dgfip_annee_impot'] })
end
def demandeur
@demandeur ||= User.create!(
given_name: 'Jean',
family_name: 'Dupont',
email: '[email protected]',
external_id: '1',
job_title: 'Responsable des affaires générales',
phone_number: '0123456789',
current_organization: clamart_organization,
)
end
def another_demandeur
@another_demandeur ||= User.create!(
given_name: 'Jacques',
family_name: 'Dupont',
email: '[email protected]',
external_id: '10',
job_title: 'Responsable des affaires juridiques',
phone_number: '0823456789',
current_organization: clamart_organization,
)
end
def foreign_demandeur
@foreign_demandeur ||= User.create!(
given_name: 'Pierre',
family_name: 'Dupont',
email: '[email protected]',
external_id: '11',
job_title: 'Responsable des affaires étrangères',
phone_number: '0323456789',
current_organization: dinum_organization,
)
end
def api_entreprise_instructor
@api_entreprise_instructor ||= User.create!(
given_name: 'Paul',
family_name: 'Dupont',
email: '[email protected]',
external_id: '4',
job_title: 'Responsable des instructions',
phone_number: '0423456789',
current_organization: dinum_organization,
roles: ['api_entreprise:instructor']
)
end
def api_entreprise_reporter
@api_entreprise_reporter ||= User.create!(
given_name: 'Marc',
family_name: 'Dupont',
email: '[email protected]',
external_id: '12',
job_title: 'Responsable des reporteurs',
phone_number: '0423456789',
current_organization: dinum_organization,
roles: ['api_entreprise:reporter']
)
end
def data_pass_admin
@data_pass_admin ||= User.create!(
email: '[email protected]',
roles: ['admin'] + all_authorization_definition_instructor_roles,
current_organization: dinum_organization,
)
end
def all_authorization_definition_instructor_roles
AuthorizationDefinition.all.map { |definition| "#{definition.id}:instructor" }
end
def clamart_organization
@clamart_organization ||= create_organization(siret: '21920023500014', name: 'Ville de Clamart')
end
def dinum_organization
@dinum_organization ||= create_organization(siret: '13002526500013', name: 'DINUM')
end
def create_organization(siret:, name:)
Organization.create!(
siret:,
last_mon_compte_pro_updated_at: DateTime.now,
mon_compte_pro_payload: {
label: name
},
insee_payload: JSON.parse(Rails.root.join('spec', 'fixtures', 'insee', "#{siret}.json").read),
last_insee_payload_updated_at: DateTime.now,
)
end
def create_draft_authorization_request(kind, attributes: {})
create_authorization_request_model(
kind,
attributes: attributes.merge(
fill_all_attributes: true,
description: random_description,
)
)
end
def create_submitted_authorization_request(kind, attributes: {})
user = extract_applicant(attributes)
authorization_request = create_draft_authorization_request(kind, attributes:)
SubmitAuthorizationRequest.call(authorization_request:, user:).perform
authorization_request
end
def create_validated_authorization_request(kind, attributes: {})
authorization_request = create_submitted_authorization_request(kind, attributes:)
ApproveAuthorizationRequest.call(authorization_request:, user: api_entreprise_instructor).perform
authorization_request
end
def create_refused_authorization_request(kind, attributes: {})
authorization_request = create_submitted_authorization_request(kind, attributes:)
denial_of_authorization_params = {
reason: 'Cette demande ne correspond pas à nos critères',
}.merge(attributes[:denial_of_authorization_params] || {})
RefuseAuthorizationRequest.call(authorization_request:, user: api_entreprise_instructor, denial_of_authorization_params:).perform
authorization_request
end
def create_request_changes_authorization_request(kind, attributes: {})
authorization_request = create_submitted_authorization_request(kind, attributes:)
instructor_modification_request_params = {
reason: "Le cadre juridique n'est pas suffisamment précis, merci de le compléter",
}.merge(attributes[:instructor_modification_request_params] || {})
RequestChangesOnAuthorizationRequest.call(authorization_request:, user: api_entreprise_instructor, instructor_modification_request_params:).perform
authorization_request
end
def create_reopened_authorization_request(kind, attributes: {})
authorization_request = create_validated_authorization_request(kind, attributes:)
ReopenAuthorization.call(authorization: authorization_request.latest_authorization, user: authorization_request.applicant).perform
authorization_request
end
def create_authorization_request_model(kind, attributes: {})
traits = [:draft]
traits << kind
applicant = extract_applicant(attributes)
FactoryBot.create(
:authorization_request,
*traits,
{
applicant:,
organization: applicant.current_organization,
}.merge(attributes.except(:applicant))
)
end
def extract_applicant(attributes)
attributes[:applicant] || demandeur
end
def create_all_verified_emails
User.find_each do |user|
create_verified_email(user.email)
end
AuthorizationRequest.find_each do |authorization_request|
authorization_request.class.contact_types.each do |contact_type|
create_verified_email(authorization_request.send(:"#{contact_type}_email"))
end
end
end
def create_verified_email(email)
return if email.blank?
return if VerifiedEmail.exists?(email:)
VerifiedEmail.create!(
email:,
status: 'deliverable',
verified_at: Time.zone.now,
)
end
def send_message_to_instructors(authorization_request, message_params)
SendMessageToInstructors.call(
authorization_request:,
user: authorization_request.applicant,
message_params:,
)
authorization_request.mark_messages_as_read_by_applicant!
end
def send_message_to_applicant(authorization_request, message_params)
SendMessageToApplicant.call(
authorization_request:,
user: api_entreprise_instructor,
message_params:,
)
authorization_request.mark_messages_as_read_by_instructors!
end
def random_description
[
"Demande d'accès sécurisé aux données fiscales pour analyse économique.",
"Requête d'habilitation pour accéder aux dossiers de santé publique.",
"Solicitation d'accès aux registres d'état civil pour recherche démographique.",
'Demande de permission pour consulter les données de permis de conduire pour étude de mobilité.',
"Application pour accéder aux données cadastrales pour projet d'urbanisme.",
"Requête pour l'utilisation des données de sécurité sociale dans le cadre d'une étude sur le vieillissement.",
"Demande d'habilitation pour étudier les tendances de l'emploi avec accès aux données du ministère du Travail.",
"Solicitation d'accès à la base de données électorales pour analyse politique.",
"Demande d'autorisation pour utiliser les données de consommation énergétique pour recherche environnementale.",
"Requête pour accéder aux archives judiciaires dans le but d'une étude sur la justice pénale."
].sample
end
def load_all_models!
Rails.root.glob('app/models/**/*.rb').each { |f| require f }
end
def production?
Rails.env.production? && ENV['CAN_FLUSH_DB'].blank?
end
end