module Mobile
  module Entities
    class Project < Grape::Entity
      expose :name
      expose :id
      expose :is_public
      expose :user_id
      expose :invite_code
      expose :qrcode
      expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options|
        current_user = options[:user]

        my_member = instance.member_principals.where("users.id=#{current_user.id}").first
        can_setting = false
        if my_member && my_member.roles[0] && my_member.roles[0].id == 3
          can_setting = true
        end
        can_setting
      end

      expose :is_creator, if: lambda { |instance, options| options[:user] } do |instance, options|
        current_user = options[:user]

        current_user.id == instance.user_id
      end

      expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options|
        current_user = options[:user]
        if instance[:project]
          project = instance[:project]
        else
          project = instance
        end
        current_user.member_of?(project)
      end


      expose :member_count, if: lambda { |instance, options| options[:user] } do |instance, options|
        instance.members.count
      end
    end
  end
end
