module ServiceObjects
  class UpdateLegacyEmailTable < Base
    DB = Sequel.connect(Settings.ws.db.to_hash)

    def insert(email)
      DB[:email].insert(idnumber: change.biola_id, email: email)
    end

    def insert_and_update(biola_id, old_email, new_email)
      DB[:email].where(idnumber: biola_id, email: old_email).update(primary: 0)
      DB[:email].insert(idnumber: biola_id, email: new_email)
    end

    def ignore?
      !(change.affiliation_added? && !change.university_email_exists?)
    end
  end
end

module ServiceObjects
  class UpdateLegacyEmailTable < Base
    DB = Sequel.connect(Settings.ws.db.to_hash)

    def insert(email)
      DB[:email].insert(idnumber: change.biola_id, email: email)
    end

    def insert_and_update(biola_id, old_email, new_email)
      DB[:email].where(idnumber: biola_id, email: old_email).update(primary: 0)
      if DB[:email].where(idnumber: biola_id, email: new_email).count > 0
        DB[:email].where(idnumber: biola_id, email: new_email).update(primary: 1, expiration_date: "0000-00-00 00:00:00")
      else
        DB[:email].insert(idnumber: biola_id, email: new_email)
      end
    end

    def ignore?
      !(change.affiliation_added? && !change.university_email_exists?)
    end
  end
end

Update email table if entry exists
Update email table if entry exists

If the email table in WS already has the exact email assigned to the
exact person (per their idnumber), then we should be updating that
record instead of creating a duplicate record with one as primary and
one not.
