with(['company'])->filter($request->all())->paginate(); return NamingRuleResource::collection($namingRules); } /** * Store a newly created resource in storage. */ public function store(CreateOrUpdateRequest $request) { $formData = $request->only(['name', 'status', 'company_id', 'global']); if (! Auth::user()->super_admin) { $formData['company_id'] = Auth::user()->company_id; unset($formData['global']); } NamingRule::create($formData); return $this->created(); } /** * Display the specified resource. */ public function show(string $id) { $namingRule = NamingRule::query()->allowed()->findOrFail($id); return new NamingRuleResource($namingRule); } /** * Update the specified resource in storage. */ public function update(CreateOrUpdateRequest $request, string $id) { $namingRule = NamingRule::query()->allowed()->findOrFail($id); $namingRule->fill($request->only([ 'name', 'status', 'global' ])); $namingRule->save(); return $this->noContent(); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { DB::transaction(function () use ($id) { $namerule = NamingRule::query()->allowed()->findOrFail($id); $namerule->delete(); $customfield = CustomField::query()->where('group', $id); if (!empty($customfield)) { $customfield->delete(); } }); return $this->noContent(); } public function enabled() { $namingRules = NamingRule::where('status', 1)->where(function ($query) { return $query->where("company_id", Auth::user()->company_id)->orWhere([ 'company_id' => 0, 'global' => 1 ]); })->select(['id', 'name'])->get(); return $this->success([ 'data' => $namingRules ]); } public function combinationSetting(CombinationSettingRequest $request, string $id) { $namingRule = NamingRule::query()->allowed()->findOrFail($id); $namingRule->combination_rules = $request->all(); $namingRule->save(); return $this->noContent(); } public function combinationShow(string $id){ $nameRule=NamingRule::query()->allowed()->findOrFail($id); return new NamingRuleCombinationResource($nameRule); } public function autoname(AutonameRequest $request, string $id) { $namingRule = NamingRule::query()->allowed()->findOrFail($id); if (! $namingRule->combination_rules) { return $this->badRequest("Please set naming combination rules"); } $code = match ($request->code_type) { "project","container_project"=> Project::query()->find($request->code_id)?->code, "container_asset" => Asset::query()->find($request->code_id)?->code, default => "" }; if (! $code) { return $this->badRequest("The supported type or selected item does not exist"); } $name = $code; foreach ($namingRule->combination_rules as $rule) { if (! isset($request->fields[$rule['field']]) || !$request->fields[$rule['field']]) { return $this->badRequest(sprintf("%s field is required", $rule['field'])); } $name .= $rule['link'] . $request->fields[$rule['field']]; } return $this->success([ 'data' => $name ]); } }