liquid_feedback_frontend
view app/main/registration_admin/_action/update_verification.lua @ 1836:1dad272c08eb
Reduce white space
| author | bsw | 
|---|---|
| date | Thu Feb 03 12:44:02 2022 +0100 (2022-02-03) | 
| parents | baa87c3780ad | 
| children | 
 line source
     1 local verification = Verification:by_id(param.get_id())
     3 local function update_data()
     4   local old_verification_data = verification.verification_data or {}
     5   verification.verification_data = json.object()
     7   for i, field in ipairs(config.self_registration.fields) do
     8     local value = param.get(field.name)
     9     if field.name == "fiscal_code" then
    10       value = string.gsub(value, "[^A-Z0-9]", "")
    11     elseif field.name == "mobile_phone" then
    12       value = string.gsub(value, "[^0-9]", "")
    13     elseif field.name == "unit" then
    14       value = string.gsub(value, "[^0-9]", "")
    15       if old_verification_data.unit and old_verification_data.unit ~= "" and old_verification_data.unit ~= value then
    16         local old_unit_privilege = Privilege:by_pk(old_verification_data.unit, verification.requesting_member_id)
    17         if old_unit_privilege then
    18           old_unit_privilege:destroy()
    19         end
    20       end
    21       if value ~= old_verification_data.unit and value ~= "" then
    22         local unit_privilege = Privilege:new()
    23         unit_privilege.member_id = verification.requesting_member_id
    24         unit_privilege.unit_id = tonumber(value)
    25         unit_privilege.voting_right = true
    26         unit_privilege.initiative_right = true
    27         unit_privilege:save()
    28       end
    29     elseif field.name == "sequential_number" then
    30       value = old_verification_data.sequential_number 
    31       if not value then
    32         local last_sequential_number = 0
    33         db:query('LOCK TABLE "verification" IN SHARE ROW EXCLUSIVE MODE')
    34         local record = Verification:new_selector()
    35           :reset_fields()
    36           :add_field("max((verification_data->>'sequential_number')::int8)", "max_sequential_number")
    37           :optional_object_mode()
    38           :exec()
    39         if record and record.max_sequential_number then
    40           last_sequential_number = record.max_sequential_number
    41         end
    42         value = last_sequential_number + 1
    43       end
    44     elseif field.type ~= "image" then
    45       value = string.gsub(value, "^%s+", "")
    46       value = string.gsub(value, "%s+$", "")
    47       value = string.gsub(value, "%s+", " ")
    48     end
    49     verification.verification_data[field.name] = value
    50   end
    51 end
    53 local function check_db_error(db_error)
    54   if db_error then
    55     if db_error:is_kind_of("IntegrityConstraintViolation.UniqueViolation") then
    56       slot.select("error", function()
    57         ui.tag{ content = _"Identification unique violation: This identification is already in use for another member." }
    58       end )
    59       return false
    60     else
    61       error(db_error)
    62     end
    63   end
    64 end
    66 if verification.verified_member_id then
    68   local member = Member:by_id(verification.verified_member_id)
    70   local identification = param.get("identification")
    71   if identification and #identification == 0 then
    72     identification = nil
    73   end
    74   member.identification = identification
    76   member.notify_email = param.get("email")
    78   local success = check_db_error(member:try_save())
    79   if not success then
    80     return false
    81   end
    83   update_data()
    85   verification:save()
    87   if param.get("cancel") then
    88     db:query({ "SELECT delete_member(?)", member.id })
    89     return
    90   end
    92   if param.get("invite") then
    93     member:send_invitation()
    94   end
    96 elseif param.get("drop") then
    98   verification.denied = "now"
    99   verification:save()
   100   return
   102 elseif param.get("accredit") then
   104   local member = Member:by_id(verification.requesting_member_id)
   106   local identification = param.get("identification")
   107   if identification and #identification == 0 then
   108     identification = nil
   109   end
   110   member.identification = identification
   112   member.notify_email = param.get("email")
   114   local success = check_db_error(member:try_save())
   115   if not success then
   116     return false
   117   end
   119   if config.self_registration.manual_invitation then
   120     local function secret_token()
   121       local parts = {}
   122       for i = 1, 5 do
   123         parts[#parts+1] = multirand.string(5, "23456789bcdfghjkmnpqrstvwxyz")
   124       end
   125       return (table.concat(parts, "-"))
   126     end
   127     member.invite_code = secret_token()
   128     member:save()
   129   else
   130     member:send_invitation()
   131   end
   133   for i, unit_id in ipairs(config.self_registration.grant_privileges_for_unit_ids) do
   134     local privilege = Privilege:new()
   135     privilege.member_id = member.id
   136     privilege.unit_id = unit_id
   137     privilege.initiative_right = true
   138     privilege.voting_right = true
   139     privilege:save()
   140   end
   142   update_data()
   144   verification.verified_member_id = verification.requesting_member_id
   145   verification.verifying_member_id = app.session.member_id
   146   verification.verified = "now"
   148   verification:save()
   151 else
   153   update_data()
   154   verification:save()
   156 end
