Sha256: c44a23ec0ddeb63c370d1dbe29d06d6f12e10b4f7944bda6d7fcbd6dbbd7f4f0

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

class SlackSmartBot

  def add_vacation(user, type, from, to)
    save_stats(__method__)
    get_vacations()
    from.gsub!('-','/')
    to.gsub!('-','/')
    if type.match?(/sick\s+baby/i) or type.match?(/sick\s+child/i)
      type = 'sick child'
    end

    if from=='today'
      from = Date.today.strftime("%Y/%m/%d")
    elsif from =='tomorrow'
      from = (Date.today+1).strftime("%Y/%m/%d")
    elsif from.match?(/next\s+week/)
      from = Date.today + ((1 - Date.today.wday) % 7)
      to = (from + 6).strftime("%Y/%m/%d")
      from = from.strftime("%Y/%m/%d")
    end

    to = from if to.empty?
    wrong = false
    begin
      from_date = Date.parse(from)
      to_date = Date.parse(to)
    rescue
      wrong = true
      respond "It seems like the date is not in the correct format: YYYY/MM/DD or is a wrong date."      
    end
    unless wrong
      if Date.parse(from).strftime("%Y/%m/%d") != from
        respond "It seems like the date #{from} is not in the correct format: YYYY/MM/DD or is a wrong date."
      elsif Date.parse(to).strftime("%Y/%m/%d") != to
        respond "It seems like the date #{to} is not in the correct format: YYYY/MM/DD or is a wrong date."
      else
        vacations = @vacations.deep_copy
        vacations[user.name] ||= { user_id: user.id, periods: [] }
        if !vacations[user.name].key?(:periods)
          vacations[user.name][:user_id] = user.id
          vacations[user.name][:periods] = []
        end

        if vacations[user.name].periods.empty?
          vacation_id = 1
        else
          vacation_id = vacations[user.name].periods[-1].vacation_id + 1
        end
        vacations[user.name].periods << { vacation_id: vacation_id, type: type.downcase, from: from, to: to }
        update_vacations({user.name => vacations[user.name]})
        respond "Period has been added   ##{vacation_id}"
        check_vacations(date: Date.today, user: user.name, set_status: true, only_first_day: false)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slack-smart-bot-1.14.2 lib/slack/smart-bot/commands/general/add_vacation.rb
slack-smart-bot-1.14.1 lib/slack/smart-bot/commands/general/add_vacation.rb
slack-smart-bot-1.14.0 lib/slack/smart-bot/commands/general/add_vacation.rb
slack-smart-bot-1.13.2 lib/slack/smart-bot/commands/general/add_vacation.rb
slack-smart-bot-1.13.1 lib/slack/smart-bot/commands/general/add_vacation.rb
slack-smart-bot-1.13.0 lib/slack/smart-bot/commands/general/add_vacation.rb