socialforge/app/services/projects_service.rb

145 lines
4.6 KiB
Ruby

#coding=utf-8
class ProjectsService
include ApplicationHelper
#获取指定用户的项目列表
def user_projects(user)
projects = user.projects.not_deleted.select("projects.*,(SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS updated_at ").order("updated_at desc")
projects
end
#显示项目
def show_project(params,current_user)
project = Project.find(params[:id])
# project.generate_invite_code
# project.generate_qrcode
project
end
def send_wechat_create_project_notice user,project
count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Project' and shield_id=#{project.id}").count
if count == 0
ws = WechatService.new
title = "恭喜您创建项目成功。"
ws.create_project_notice user.id, "create_project_notice", project.id,title, project.name, format_time(project.created_on),"点击查看项目详情。"
end
end
def createNewProject params,user
status = -1
issue_custom_fields = IssueCustomField.sorted.all
trackers = Tracker.sorted.all
project = Project.new
project[:name] = params[:name]
project[:description] = ''
project[:is_public] = '1' #公开
project[:project_type] = 0
project[:project_new_type] = 1
project[:identifier] = Project.next_identifier if Setting.sequential_project_identifiers?
project.organization_id = params[:organization_id]
project.user_id = user.id
# if validate_parent_id && @project.save
if project.save
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
m = Member.new(:user => user, :roles => [r])
# project's score
if ProjectScore.where("project_id=?", project.id).first.nil?
ProjectScore.create(:project_id => project.id, :score => false)
end
# end
project_info = ProjectInfo.new(:user_id => user.id, :project_id => project.id)
user_grades = UserGrade.create(:user_id => user.id, :project_id => project.id)
project_status = ProjectStatus.create(:project_id => project.id, :watchers_count => 0, :changesets_count => 0, :project_type => project.project_type,:grade => 0)
project.members << m
project.project_infos << project_info
status = 0
send_wechat_create_project_notice user,project
end
status
end
#修改项目成员角色
def modify_user_project_role params
status = -1
project = Project.find("#{params[:id]}")
member = project.member_principals.includes(:roles, :principal).where("user_id=?",params[:user_id]).first
if member
member.member_roles[0].role_id = params[:role_id]
role = Role.find(params[:role_id])
if role.allowed_to?(:is_manager)
projectInfo = ProjectInfo.new(:user_id => member.user_id, :project_id => project.id)
projectInfo.save
else
user_admin = ProjectInfo.where("user_id = ? and project_id = ?", member.user_id, project.id)
if user_admin.size > 0
user_admin.each do |user|
user.destroy
end
end
end
if member.member_roles[0].save
status = 0
end
end
status
end
class JoinProjectError < Errors
define_error [
0, '加入成功',
1, '您的邀请码不正确',
2, '您还未登录',
3, '您已经是该项目的管理人员',
4, '您已经是该项目的开发人员',
5, '您已经是该项目的报告人员',
6, '该项目不存在或已被删除啦',
'未知错误,请稍后再试'
]
end
def join_project params,current_user
status = -1
project = project.find_by_invite_code(params[:invite_code]) if params[:invite_code]
if project
if project[:is_delete] == 1
status = 6
else
if current_user.member_of?(project) #如果已经是成员
member = project.member_principals.includes(:roles, :principal).where("user_id=?",current_user.id).first
status = member.member_roles[0].role_id
else
if params[:invite_code].present?
members = []
members << Member.new(:role_ids => [5], :user_id => current_user.id)
project.members << members
projectInfo = ProjectInfo.new(:user_id => current_user.id, :project_id => project.id)
projectInfo.save
status = 0
else
status = 4
end
end
end
else
status = 4
end
status
end
end