Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.34 KB | None | 0 0
  1.  
  2.  
  3. namespace App\Services;
  4.  
  5. use DB,
  6. Auth;
  7. use App\Models\Team;
  8. use App\Models\User;
  9. use App\Services\UserProjectPermissionManager\UserProjectPermissionManager;
  10.  
  11. class TeamService {
  12.  
  13. public static function getStatusTypes() {
  14. return [
  15. Team::STATUS_ACTIVE => 'Active',
  16. Team::STATUS_INACTIVE => 'Inactive',
  17. Team::STATUS_LEFT => 'Left',
  18. 'blocked' => 'Blocked',
  19. ];
  20. }
  21.  
  22. public static function getRoleTypes() {
  23. return [
  24. Team::ROLE_OWNER => 'Owner',
  25. Team::ROLE_PROJECT_MANAGER => 'Project Manager',
  26. Team::ROLE_EXECUTIVE_MANAGER => 'Executive Manager',
  27. Team::ROLE_MEMBER => 'Employee',
  28. Team::ROLE_CLIENT => 'Client',
  29. ];
  30. }
  31.  
  32. public static function getRoleInvitationTypes() {
  33. return [
  34. Team::ROLE_PROJECT_MANAGER => 'Project Manager',
  35. Team::ROLE_EXECUTIVE_MANAGER => 'Executive Manager',
  36. Team::ROLE_MEMBER => 'Employee',
  37. ];
  38. }
  39.  
  40. public static function getTeamList() {
  41. return Team::join('users', function($join) {
  42. $join->on('users.id', '=', 'owner_user_rel.owner_id');
  43. $join->on('users.deleted_at', 'IS', \DB::raw('NULL'));
  44. })
  45. ->where('owner_user_rel.status', Team::STATUS_ACTIVE)
  46. ->where('owner_user_rel.user_id', Auth::user()->id)
  47. ->where('owner_user_rel.is_blocked', false)
  48. ->groupBy('owner_user_rel.owner_id')
  49. ->select([
  50. DB::raw('CONCAT(users.team_name) as name'),
  51. DB::raw('owner_user_rel.owner_id'),
  52. ])->get();
  53. }
  54.  
  55.  
  56.  
  57. public static function getProjectAvailableEmployeeList(Team $userTeamObj, $project_id) {
  58. $userArr = Team::join('project_user_rel', function($join) use($userTeamObj, $project_id) {
  59. $join->on('project_user_rel.user_id', '=', 'owner_user_rel.user_id')
  60. ->on('project_user_rel.project_id', '!=', \DB::raw($project_id))
  61. ->on('project_user_rel.deleted_at', 'IS', \DB::raw('NULL'));
  62. if ($userTeamObj->role == \App\Models\Team::ROLE_PROJECT_MANAGER || $userTeamObj->role == \App\Models\Team::ROLE_CLIENT) {
  63. $user_permitted_projects_ids = $userTeamObj->permissions->lists('project_id')->toArray();
  64. $join->whereIn('project_user_rel.project_id', $user_permitted_projects_ids);
  65. }
  66. })
  67. ->notClient()
  68. ->leftJoin('users', 'users.id', '=', 'owner_user_rel.user_id')
  69. ->where('owner_user_rel.owner_id', $userTeamObj->owner_id)
  70. ->notBlocked()
  71. ->select([
  72. DB::raw('CONCAT(users.firstname, " ", users.lastname) as fullname'),
  73. DB::raw('owner_user_rel.user_id as user_id'),
  74. ]);
  75. $projectObj = \App\Models\Project::where('owner_id', $userTeamObj->owner_id)
  76. ->where('id', $project_id)
  77. ->firstOrFail();
  78. $userArr->whereNotIn('users.id', $projectObj->contracts->lists('user_id')->toArray());
  79. $userArr = $userArr->groupBy('owner_user_rel.user_id')->get();
  80. return $userArr;
  81. }
  82.  
  83. public static function getFilteredMembers(Team $userTeamObj, array $filters = [], array $with = [], $trashed = false) {
  84. $user_projects_ids = [0];
  85. $user_permitted_projects_ids = [0];
  86. $userArr = User::join('owner_user_rel', function($join) use($userTeamObj, $trashed, $filters) {
  87. $join->on('owner_user_rel.user_id', '=', 'users.id');
  88. $join->where('owner_user_rel.owner_id', '=', $userTeamObj->owner_id);
  89. $join->where('owner_user_rel.role', '!=', Team::ROLE_CLIENT);
  90. if (!$trashed) {
  91. $join->on('owner_user_rel.deleted_at', 'IS', \DB::raw('NULL'));
  92. } else {
  93. $join->on('owner_user_rel.deleted_at', 'IS NOT', \DB::raw('NULL'));
  94. }
  95. if (isset($filters['roles']) && count($filters['roles'])) {
  96. $join->whereIn('owner_user_rel.role', $filters['roles']);
  97. }
  98. });
  99. if (isset($filters['statuses']) && count($filters['statuses'])) {
  100. $blocked_index = array_search('blocked', $filters['statuses']);
  101. if ($blocked_index !== false) {
  102. unset($filters['statuses'][$blocked_index]);
  103. $userArr->where('owner_user_rel.is_blocked', true);
  104. if (count($filters['statuses'])) {
  105. $userArr->whereIn('owner_user_rel.status', $filters['statuses']);
  106. }
  107. } else {
  108. $userArr->whereIn('owner_user_rel.status', $filters['statuses']);
  109.  
  110. }
  111. }
  112. if (isset($filters['groups']) && count($filters['groups'])) {
  113. $userArr->join('user_groups', 'user_groups.user_id', '=', 'users.id');
  114. $userArr->whereIn('user_groups.group_id', $filters['groups']);
  115. }
  116. $userArr->leftJoin('project_user_rel', function($join) use($userTeamObj, $filters) {
  117. $join->on('project_user_rel.user_id', '=', 'users.id');
  118. $join->on('project_user_rel.deleted_at', 'IS', \DB::raw('NULL'));
  119. });
  120. if (isset($filters['projects']) && count($filters['projects'])) {
  121. $userArr->whereIn('project_user_rel.project_id', $filters['projects']);
  122. }
  123. if ($userTeamObj->role == \App\Models\Team::ROLE_CLIENT) {
  124. $user_permitted_projects_ids = $userTeamObj->permissions->lists('project_id')->toArray();
  125. $userArr->whereIn('project_user_rel.project_id', $user_permitted_projects_ids);
  126. } elseif ($userTeamObj->role == \App\Models\Team::ROLE_MEMBER || $userTeamObj->role == \App\Models\Team::ROLE_PROJECT_MANAGER) {
  127. $user_projects_ids = ContractService::getUserContractsList($userTeamObj, $userTeamObj->user_id, false, [
  128. 'projects.id' => 'project_id'
  129. ])->lists('project_id')->toArray();
  130. $user_permitted_projects_ids = $userTeamObj->permissions->lists('project_id')->toArray();
  131. $userArr->whereIn('project_user_rel.project_id', array_merge($user_projects_ids, $user_permitted_projects_ids));
  132. }
  133. foreach ($with as $rel) {
  134. if ($rel == 'groups') {
  135. $rels['groups'] = function($query) use($userTeamObj) {
  136. $query->where('groups.owner_id', $userTeamObj->owner_id);
  137. };
  138. } elseif ($rel == 'projects') {
  139. $rels['projects'] = function($query) use($userTeamObj, $user_projects_ids) {
  140. $query->where('projects.owner_id', $userTeamObj->owner_id);
  141. if ($userTeamObj->role == \App\Models\Team::ROLE_PROJECT_MANAGER || $userTeamObj->role == \App\Models\Team::ROLE_CLIENT) {
  142. $user_permitted_projects_ids = $userTeamObj->permissions->lists('project_id')->toArray();
  143. $query->whereIn('project_user_rel.project_id', $user_permitted_projects_ids);
  144. } elseif ($userTeamObj->role == \App\Models\Team::ROLE_MEMBER) {
  145. $query->whereIn('project_user_rel.project_id', $user_projects_ids);
  146. }
  147. };
  148. }
  149. }
  150. if (count($with)) {
  151. $userArr->with($rels);
  152. }
  153. if (isset($filters['users'])) {
  154. $userArr->whereIn('users.id', $filters['users']);
  155. }
  156. $userArr->select([
  157. '*',
  158. DB::raw('users.id as id'),
  159. DB::raw('users.current_owner_id as current_owner_id'),
  160.  
  161. ]);
  162.  
  163. return $userArr->orderBy('owner_user.role', 'ASC')
  164. ->orderBy('owner_user.created_at', 'ASC')
  165. ->groupBy('users.id')->get();
  166. }
  167.  
  168. public static function inviteMember($owner_id, $input_data = []) {
  169. $permissionManager = new UserProjectPermissionManager();
  170. $email = $input_data['email_add_member'];
  171. $firstname = $input_data['firstname_add_member'];
  172. $lastname = $input_data['lastname_add_member'];
  173. $role = $input_data['role_add_member'];
  174. $groups_str = $input_data['groups_add_member'];
  175. $invitedUserObj = User::withTrashed()->where('email', $email)->first();
  176. if (!$invitedUserObj) {
  177. $invitedUserObj = new User();
  178. $invitedUserObj->email = $email;
  179. $invitedUserObj->firstname = $firstname;
  180. $invitedUserObj->lastname = $lastname;
  181. $invitedUserObj->current_owner_id = $owner_id;
  182. $invitedUserObj->current_role = $role;
  183. $invitedUserObj->timezone = \Config::get('global.default_timezone');
  184. $invitedUserObj->team_name = $firstname . "'s team";
  185. $invitedUserObj->status = User::STATUS_INACTIVE;
  186. $invitedUserObj->password = '';
  187. \App\Models\Admin\MailingList::add($email, \App\Models\Admin\MailingList::TYPE_VISITOR);
  188. if (!$invitedUserObj->save()) {
  189. return false;
  190. }
  191. $teamObj = Team::create([
  192. 'owner_id' => $invitedUserObj->id,
  193. 'user_id' => $invitedUserObj->id,
  194. 'role' => Team::ROLE_OWNER,
  195. 'status' => Team::STATUS_INACTIVE,
  196. ]);
  197. }
  198. $token = md5($invitedUserObj->email . strtotime('now'));
  199. $confirmTokenObj = \App\Models\ConfirmToken::create([
  200. 'email' => $invitedUserObj->email,
  201. 'token' => $token,
  202. 'owner_id' => $owner_id,
  203. 'created_at' => date('Y-m-d H:i:s'),
  204. ]);
  205. if (!$confirmTokenObj) {
  206. return false;
  207. }
  208. $teamObj = Team::withTrashed()
  209. ->where('owner_id', $owner_id)
  210. ->where('user_id', $invitedUserObj->id)
  211. ->first();
  212. if ($teamObj) {
  213. if ($teamObj->trashed()) {
  214. $teamObj->update([
  215. 'role' => $role,
  216. 'deleted_at' => null
  217. ]);
  218. }
  219. } else {
  220. $teamObj = Team::create([
  221. 'owner_id' => $owner_id,
  222. 'user_id' => $invitedUserObj->id,
  223. 'role' => $role,
  224. 'status' => Team::STATUS_INACTIVE
  225. ]);
  226. }
  227.  
  228. if ((int) $role === Team::ROLE_EXECUTIVE_MANAGER) {
  229. $permissionManager->setFullPermissionsToAllProjects($teamObj, $invitedUserObj);
  230. }
  231.  
  232. \App\Services\NotificationService::createTeamNotification($teamObj, NotificationService::TYPE_TEAM_INVITED, false, [
  233. 'confirmation_link' => \URL::to('accept-invitation', $token)
  234. ]);
  235. \App\Services\NotificationService::createRawNotification($owner_id, $owner_id, [
  236. 'title' => 'Invitation sent',
  237. 'body' => 'Your invitation has been sent',
  238. ]);
  239. \App\Services\TeamLogService::createTeamNotification($teamObj, TeamLogService::TYPE_TEAM_INVITED);
  240. \App\Services\UserService::syncGroups($invitedUserObj->id, $groups_str);
  241. $initiatorObj = User::find($owner_id);
  242. if ($invitedUserObj->status == User::STATUS_INACTIVE) {
  243. \App\Services\MailQueueService::createMailQueue($invitedUserObj, \App\Services\MailQueueService::TYPE_INVITE_NEW_USER, [
  244. 'confirmation_link' => \URL::to('accept-invitation', $token),
  245. 'fullname' => $initiatorObj->firstname . ' ' . $initiatorObj->lastname,
  246. 'team_name' => $initiatorObj->team_name,
  247. ]);
  248. } else {
  249. \App\Services\MailQueueService::createMailQueue($invitedUserObj, \App\Services\MailQueueService::TYPE_INVITE_EXISTING_USER, [
  250. 'confirmation_link' => \URL::to('accept-invitation', $token),
  251. 'fullname' => $initiatorObj->firstname . ' ' . $initiatorObj->lastname,
  252. 'team_name' => $initiatorObj->team_name,
  253. ]);
  254. }
  255. return true;
  256. }
  257.  
  258. public static function inviteClient(User $ownerObj, $input_data = []) {
  259. $email = $input_data['email_add_client'];
  260. $firstname = $input_data['firstname_add_client'];
  261. $lastname = $input_data['lastname_add_client'];
  262. $invitedUserObj = User::withTrashed()->where('email', $email)->first();
  263. if (!$invitedUserObj) {
  264. $invitedUserObj = new User();
  265. $invitedUserObj->email = $email;
  266. $invitedUserObj->firstname = $firstname;
  267. $invitedUserObj->lastname = $lastname;
  268. $invitedUserObj->current_owner_id = $ownerObj->id;
  269. $invitedUserObj->current_role = Team::ROLE_CLIENT;
  270. $invitedUserObj->timezone = \Config::get('global.default_timezone');
  271. $invitedUserObj->team_name = $firstname . "'s team";
  272. $invitedUserObj->status = User::STATUS_INACTIVE;
  273. $invitedUserObj->password = '';
  274. \App\Models\Admin\MailingList::add($email, \App\Models\Admin\MailingList::TYPE_VISITOR);
  275. if (!$invitedUserObj->save()) {
  276. return false;
  277. }
  278. $teamObj = Team::create([
  279. 'owner_id' => $invitedUserObj->id,
  280. 'user_id' => $invitedUserObj->id,
  281. 'role' => Team::ROLE_OWNER,
  282. 'status' => Team::STATUS_INACTIVE,
  283. ]);
  284. }
  285. $token = md5($invitedUserObj->email . strtotime('now'));
  286. $confirmTokenObj = \App\Models\ConfirmToken::create([
  287. 'email' => $invitedUserObj->email,
  288. 'token' => $token,
  289. 'owner_id' => $ownerObj->id,
  290. 'created_at' => date('Y-m-d H:i:s'),
  291. ]);
  292. if (!$confirmTokenObj) {
  293. return false;
  294. }
  295. $teamObj = Team::withTrashed()
  296. ->where('owner_id', $ownerObj->id)
  297. ->where('user_id', $invitedUserObj->id)
  298. ->first();
  299. if ($teamObj) {
  300. if (!$teamObj->trashed()) {
  301. return false;
  302. }
  303. $teamObj->restore();
  304. $query = 'UPDATE `project_user_rel` INNER JOIN `projects` ON `projects`.`id` = `project_user_rel`.`project_id` AND `projects`.`owner_id` = ? SET `project_user_rel`.`deleted_at` = ?,`project_user_rel`.`updated_at` = ? WHERE `project_user_rel`.`deleted_at` IS NULL AND `user_id` = ?';
  305. \DB::delete($query, [
  306. $ownerObj->id,
  307. date('Y-m-d H:i:s'),
  308. date('Y-m-d H:i:s'),
  309. $invitedUserObj->id
  310. ]);
  311. $teamObj->update([
  312. 'role' => Team::ROLE_CLIENT,
  313. 'status' => Team::STATUS_ACTIVE
  314. ]);
  315. } else {
  316. $teamObj = Team::create([
  317. 'owner_id' => $ownerObj->id,
  318. 'user_id' => $invitedUserObj->id,
  319. 'role' => Team::ROLE_CLIENT,
  320. 'status' => Team::STATUS_INACTIVE,
  321. ]);
  322. }
  323. \App\Services\NotificationService::createTeamNotification($teamObj, NotificationService::TYPE_TEAM_INVITED, false, [
  324. 'confirmation_link' => \URL::to('accept-invitation', $token)
  325. ]);
  326. \App\Services\NotificationService::createRawNotification($ownerObj->id, $ownerObj->id, [
  327. 'title' => 'Invitation sent',
  328. 'body' => 'Your invitation has been sent',
  329. ]);
  330. \App\Services\TeamLogService::createTeamNotification($teamObj, TeamLogService::TYPE_TEAM_INVITED);
  331. if ($invitedUserObj->status == User::STATUS_INACTIVE) {
  332. \App\Services\MailQueueService::createMailQueue($invitedUserObj, \App\Services\MailQueueService::TYPE_INVITE_NEW_CLIENT, [
  333. 'confirmation_link' => \URL::to('accept-invitation', $token),
  334. 'fullname' => $ownerObj->firstname . ' ' . $ownerObj->lastname,
  335. 'team_name' => $ownerObj->team_name,
  336. ]);
  337. } else {
  338. \App\Services\MailQueueService::createMailQueue($invitedUserObj, \App\Services\MailQueueService::TYPE_INVITE_EXISTING_CLIENT, [
  339. 'confirmation_link' => \URL::to('accept-invitation', $token),
  340. 'fullname' => $ownerObj->firstname . ' ' . $ownerObj->lastname,
  341. 'team_name' => $ownerObj->team_name,
  342. ]);
  343. }
  344. return true;
  345. }
  346.  
  347. public static function getTeamDropdownList() {
  348. return Team::active()->where('user_id', Auth::user()->id)->with(['owner'])
  349. ->where('is_blocked', false)
  350. ->get();
  351. }
  352.  
  353. public static function syncNotifications($user_id, $notification_ids) {
  354. $existing_notification_ids = Team::where('user_id', $user_id)
  355. ->where('notifications', true)
  356. ->active()->notBlocked()
  357. ->get()->lists('id')->toArray();
  358. $add = array_diff($notification_ids, $existing_notification_ids);
  359. $remove = array_diff($existing_notification_ids, $notification_ids);
  360. if ($add) {
  361. Team::whereIn('id', $add)->update([
  362. 'notifications' => true
  363. ]);
  364. }
  365. if ($remove) {
  366. Team::whereIn('id', $remove)->update([
  367. 'notifications' => false
  368. ]);
  369. }
  370. return true;
  371. }
  372.  
  373. public static function canAddUser($owner_id) {
  374. $userObj = \App\Models\User::findOrFail($owner_id);
  375. $teamObj = Team::where('owner_id', $owner_id)
  376. ->notBlocked()
  377. ->where('role', '!=', Team::ROLE_CLIENT)
  378. ->whereIn('status', [Team::STATUS_ACTIVE, Team::STATUS_INACTIVE])
  379. ->select([
  380. \DB::raw('COUNT(*) as count')
  381. ])->first();
  382. if (!$userObj->subscription) {
  383. if ($teamObj->count >= \Config::get('global.free_users_limit')) {
  384. return false;
  385. }
  386. } else {
  387. if ($teamObj->count >= $userObj->subscription->max_users) {
  388. return false;
  389. }
  390. }
  391. return true;
  392. }
  393.  
  394. public static function switchToFreePlan($owner_id) {
  395. $availabale_team_ids = Team::where('owner_id', $owner_id)
  396. ->notBlocked()
  397. ->whereIn('status', [Team::STATUS_ACTIVE, Team::STATUS_INACTIVE])
  398. ->orderBy('role', 'ASC')
  399. ->orderBy('status', 'ASC')
  400. ->limit(\Config::get('global.free_users_limit'))
  401. ->lists('id')->toArray();
  402. $teamArr = Team::where('owner_id', $owner_id)
  403. ->whereNotIn('id', $availabale_team_ids)
  404. ->notBlocked()
  405. ->whereIn('status', [Team::STATUS_ACTIVE, Team::STATUS_INACTIVE])
  406. ->with(['employee'])
  407. ->get();
  408. foreach ($teamArr as $teamObj) {
  409. $teamObj->block();
  410. }
  411. }
  412.  
  413. public static function getFilteredClients(Team $userTeamObj, array $filters = [], $trashed = false) {
  414. $clientTeamArr = Team::where('owner_user_rel.owner_id', '=', $userTeamObj->owner_id);
  415. $clientTeamArr->where('owner_user_rel.role', Team::ROLE_CLIENT);
  416. $clientTeamArr->join('users', 'owner_user_rel.user_id', '=', 'users.id');
  417. if ($trashed) {
  418. $clientTeamArr->onlyTrashed();
  419. }
  420.  
  421. if (isset($filters['statuses']) && count($filters['statuses'])) {
  422. $blocked_index = array_search('blocked', $filters['statuses']);
  423. if ($blocked_index !== false) {
  424. unset($filters['statuses'][$blocked_index]);
  425. $clientTeamArr->where('owner_user_rel.is_blocked', true);
  426. if (count($filters['statuses'])) {
  427. $clientTeamArr->whereIn('owner_user_rel.status', $filters['statuses']);
  428. }
  429. } else {
  430. $clientTeamArr->whereIn('owner_user_rel.status', $filters['statuses']);
  431. }
  432. }
  433.  
  434. if (isset($filters['groups']) && count($filters['groups'])) {
  435. $clientTeamArr->join('user_groups', 'user_groups.user_id', '=', 'users.id');
  436. $clientTeamArr->whereIn('user_groups.group_id', $filters['groups']);
  437. }
  438.  
  439. $clientTeamArr->leftJoin('project_permissions', function($join) use($userTeamObj, $filters) {
  440. $join->on('project_permissions.owner_user_rel_id', '=', 'owner_user_rel.id');
  441. });
  442. $clientTeamArr->where('owner_user_rel.owner_id', '=', $userTeamObj->owner_id);
  443. if (isset($filters['projects']) && count($filters['projects'])) {
  444. $clientTeamArr->where(function($query) use($filters) {
  445. $query->whereIn('project_permissions.project_id', $filters['projects']);
  446. });
  447. }
  448.  
  449. $clientTeamArr->with([
  450. 'employee.groups' => function($query) use($userTeamObj) {
  451. $query->where('groups.owner_id', $userTeamObj->owner_id);
  452. },
  453. 'permissions.project' => function($query) use($userTeamObj) {
  454. $query->where('projects.owner_id', $userTeamObj->owner_id);
  455. },
  456. ]);
  457.  
  458. $clientTeamArr->select([
  459. 'owner_user_rel.*',
  460. ]);
  461.  
  462. return $clientTeamArr->groupBy('users.id')->orderBy('owner_user_rel.created_at', 'DESC')->get();
  463. }
  464.  
  465. public static function changeRateStatus($team_id, $rate_status) {
  466. $team = Team::find($team_id);
  467.  
  468. if ($team) {
  469. if ('true' === $rate_status) {
  470. $team->rate_status = Team::SHOW_RATE;
  471. } elseif ('false' === $rate_status) {
  472. $team->rate_status = Team::HIDE_RATE;
  473. }
  474. $team->save();
  475.  
  476. return true;
  477. }
  478.  
  479. return false;
  480. }
  481.  
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement