2020-11-13 14:34:53 +01:00
|
|
|
class Cron::FindDubiousProceduresJob < Cron::CronJob
|
2020-04-01 15:26:46 +02:00
|
|
|
self.schedule_expression = "every day at midnight"
|
2018-01-05 11:16:50 +01:00
|
|
|
|
2018-01-15 14:42:48 +01:00
|
|
|
FORBIDDEN_KEYWORDS = [
|
2018-05-23 13:34:13 +02:00
|
|
|
'NIR', 'NIRPP', 'race', 'religion',
|
2018-05-31 11:28:31 +02:00
|
|
|
'carte bancaire', 'carte bleue', 'sécurité sociale',
|
2020-08-25 11:28:43 +02:00
|
|
|
'agdref', 'syndicat', 'syndical',
|
|
|
|
'parti politique', 'opinion politique', 'bord politique', 'courant politique',
|
|
|
|
'médical', 'handicap', 'maladie', 'allergie', 'hospitalisé', 'RQTH', 'vaccin'
|
2018-01-15 14:42:48 +01:00
|
|
|
]
|
2018-01-05 11:16:50 +01:00
|
|
|
|
2022-04-25 14:28:52 +02:00
|
|
|
# \\y is a word boundary
|
|
|
|
def self.forbidden_regexp
|
|
|
|
FORBIDDEN_KEYWORDS.map { |keyword| "\\y#{keyword}\\y" }
|
2018-01-05 11:16:50 +01:00
|
|
|
.join('|')
|
2022-04-25 14:28:52 +02:00
|
|
|
end
|
2018-01-05 11:16:50 +01:00
|
|
|
|
2022-04-25 14:28:52 +02:00
|
|
|
def perform(*args)
|
2018-01-05 11:16:50 +01:00
|
|
|
# ~* -> case insensitive regexp match
|
|
|
|
# https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
|
|
|
|
forbidden_tdcs = TypeDeChamp
|
|
|
|
.joins(:procedure)
|
2022-04-25 14:28:52 +02:00
|
|
|
.where("unaccent(types_de_champ.libelle) ~* unaccent(?)", Cron::FindDubiousProceduresJob.forbidden_regexp)
|
2018-08-28 16:22:34 +02:00
|
|
|
.where(type_champ: [TypeDeChamp.type_champs.fetch(:text), TypeDeChamp.type_champs.fetch(:textarea)])
|
2019-11-14 09:43:45 +01:00
|
|
|
.where(procedures: { closed_at: nil, whitelisted_at: nil })
|
2018-01-05 11:16:50 +01:00
|
|
|
|
|
|
|
dubious_procedures_and_tdcs = forbidden_tdcs
|
2020-08-27 20:14:23 +02:00
|
|
|
.group_by { |type_de_champ| type_de_champ.procedure.id }
|
2018-01-05 11:16:50 +01:00
|
|
|
.map { |_procedure_id, tdcs| [tdcs[0].procedure, tdcs] }
|
|
|
|
|
2018-05-25 18:05:28 +02:00
|
|
|
AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_later
|
2018-01-05 11:16:50 +01:00
|
|
|
end
|
|
|
|
end
|