findOrFail($projectId); return TeamMemberResource::collection($project->teamMembers); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { $teamMember = TeamMember::query()->findOrFail($id); if ($teamMember->project?->company_id != Auth::user()->company_id) { return $this->forbidden('No permission to delete'); } $teamMember->delete(); return $this->noContent(); } public function manageMembers(Request $request, string $projectId) { $project = Project::query()->findOrFail($projectId); foreach ($request->all() as $item) { if (! isset($item['user_id'])) { continue; } $user = User::query()->where("company_id", Auth::user()->company_id)->find($item['user_id']); if (! $user) { return $this->badRequest("Illegal parameters"); } } foreach ($request->all() as $item) { $teamMember = TeamMember::query()->where([ 'project_id' => $project->id, 'user_id' => $item['user_id'], ])->first(); if ($teamMember) { $teamMember->fill([ 'role' => $item['role'] ?? "", 'limited' => data_get($item, "limited", 1) == 1 ? 1 : 0, ]); $teamMember->save(); continue; } TeamMember::query()->create([ 'project_id' => $project->id, 'user_id' => $item['user_id'], 'role' => $item['role'] ?? "", 'limited' => data_get($item, "limited", 1) == 1 ? 1 : 0, 'join_at' => Carbon::now()->toDateString(), 'created_by' => Auth::id(), ]); } return $this->created(); } }