refactor: replace individual attribute assignments and save calls with mass update() calls across service classes
This commit is contained in:
parent
b88c2b4701
commit
747836be91
@ -14,9 +14,10 @@ class ProfileService
|
||||
public function update(array $validated, User $user): void
|
||||
{
|
||||
DB::transaction(function () use ($user, $validated): void {
|
||||
$user->email = $validated['email'];
|
||||
$user->username = $validated['username'];
|
||||
$user->save();
|
||||
$user->update([
|
||||
'email' => $validated['email'],
|
||||
'username' => $validated['username'],
|
||||
]);
|
||||
|
||||
$user->profile()->updateOrCreate(
|
||||
['user_id' => $user->id],
|
||||
@ -36,7 +37,8 @@ public function update(array $validated, User $user): void
|
||||
*/
|
||||
public function updatePassword(User $user, string $password): void
|
||||
{
|
||||
$user->password = Hash::make($password);
|
||||
$user->save();
|
||||
$user->update([
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,10 +100,11 @@ public function create(array $validated, User $user): void
|
||||
public function update(EmployeeAdvance $employeeAdvance, array $validated, User $user): void
|
||||
{
|
||||
DB::transaction(function () use ($employeeAdvance, $validated): void {
|
||||
$employeeAdvance->amount = (int) $validated['amount'];
|
||||
$employeeAdvance->description = $validated['description'];
|
||||
$employeeAdvance->due_date = $validated['due_date'];
|
||||
$employeeAdvance->save();
|
||||
$employeeAdvance->update([
|
||||
'amount' => (int) $validated['amount'],
|
||||
'description' => $validated['description'],
|
||||
'due_date' => $validated['due_date'],
|
||||
]);
|
||||
});
|
||||
|
||||
$this->pushNotificationService->sendToRoles(
|
||||
@ -146,11 +147,12 @@ public function approve(EmployeeAdvance $employeeAdvance, User $user): void
|
||||
$user,
|
||||
);
|
||||
|
||||
$employeeAdvance->cash_transaction_id = $cashTransaction->id;
|
||||
$employeeAdvance->status = EmployeeAdvanceStatus::APPROVED;
|
||||
$employeeAdvance->verified_at = now();
|
||||
$employeeAdvance->verified_by_id = $user->id;
|
||||
$employeeAdvance->save();
|
||||
$employeeAdvance->update([
|
||||
'cash_transaction_id' => $cashTransaction->id,
|
||||
'status' => EmployeeAdvanceStatus::APPROVED,
|
||||
'verified_at' => now(),
|
||||
'verified_by_id' => $user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
$employeeAdvance->loadMissing('employee.user');
|
||||
@ -167,10 +169,11 @@ public function approve(EmployeeAdvance $employeeAdvance, User $user): void
|
||||
public function reject(EmployeeAdvance $employeeAdvance, string $reason, User $user): void
|
||||
{
|
||||
DB::transaction(function () use ($employeeAdvance, $user, $reason): void {
|
||||
$employeeAdvance->status = EmployeeAdvanceStatus::REJECTED;
|
||||
$employeeAdvance->verified_at = now();
|
||||
$employeeAdvance->verified_by_id = $user->id;
|
||||
$employeeAdvance->save();
|
||||
$employeeAdvance->update([
|
||||
'status' => EmployeeAdvanceStatus::REJECTED,
|
||||
'verified_at' => now(),
|
||||
'verified_by_id' => $user->id,
|
||||
]);
|
||||
|
||||
$employeeAdvance->rejection()->create([
|
||||
'reason' => $reason,
|
||||
@ -206,11 +209,12 @@ public function pay(EmployeeAdvance $employeeAdvance, User $user): void
|
||||
$user,
|
||||
);
|
||||
|
||||
$employeeAdvance->repayment_cash_transaction_id = $cashTransaction->id;
|
||||
$employeeAdvance->paid_at = now();
|
||||
$employeeAdvance->paid_by_id = $user->id;
|
||||
$employeeAdvance->status = EmployeeAdvanceStatus::PAID;
|
||||
$employeeAdvance->save();
|
||||
$employeeAdvance->update([
|
||||
'repayment_cash_transaction_id' => $cashTransaction->id,
|
||||
'paid_at' => now(),
|
||||
'paid_by_id' => $user->id,
|
||||
'status' => EmployeeAdvanceStatus::PAID,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -72,8 +72,9 @@ public function create(array $validated, User $user): void
|
||||
$user,
|
||||
);
|
||||
|
||||
$expense->cash_transaction_id = $cashTransaction->id;
|
||||
$expense->save();
|
||||
$expense->update([
|
||||
'cash_transaction_id' => $cashTransaction->id,
|
||||
]);
|
||||
|
||||
$this->syncPhotos($expense, $validated);
|
||||
|
||||
@ -97,9 +98,10 @@ public function update(Expense $expense, array $validated): void
|
||||
$amount = (int) $validated['amount'];
|
||||
$description = $validated['description'];
|
||||
|
||||
$expense->amount = $amount;
|
||||
$expense->description = $description;
|
||||
$expense->save();
|
||||
$expense->update([
|
||||
'amount' => $amount,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
if ($expense->cashTransaction) {
|
||||
$this->cashService->updateReferencedTransaction(
|
||||
|
||||
@ -135,11 +135,12 @@ public function checkOut(array $validated, User $user): void
|
||||
$checkOutAt = now();
|
||||
$workDurationMinutes = (int) $attendance->check_in_at->diffInMinutes($checkOutAt);
|
||||
|
||||
$attendance->check_out_at = $checkOutAt;
|
||||
$attendance->check_out_latitude = $validated['latitude'];
|
||||
$attendance->check_out_longitude = $validated['longitude'];
|
||||
$attendance->work_duration_minutes = $workDurationMinutes;
|
||||
$attendance->save();
|
||||
$attendance->update([
|
||||
'check_out_at' => $checkOutAt,
|
||||
'check_out_latitude' => $validated['latitude'],
|
||||
'check_out_longitude' => $validated['longitude'],
|
||||
'work_duration_minutes' => $workDurationMinutes,
|
||||
]);
|
||||
|
||||
$this->mediaService->addBase64Image(
|
||||
$attendance,
|
||||
|
||||
@ -92,9 +92,10 @@ public function update(User $user, array $validated): void
|
||||
$employee = $user->employee;
|
||||
|
||||
DB::transaction(function () use ($validated, $user, $employee): void {
|
||||
$user->email = $validated['email'];
|
||||
$user->username = $validated['username'];
|
||||
$user->save();
|
||||
$user->update([
|
||||
'email' => $validated['email'],
|
||||
'username' => $validated['username'],
|
||||
]);
|
||||
|
||||
$user->profile()->updateOrCreate(
|
||||
['user_id' => $user->id],
|
||||
@ -111,10 +112,11 @@ public function update(User $user, array $validated): void
|
||||
|
||||
if ($hasEmployee) {
|
||||
if ($employee) {
|
||||
$employee->join_date = $validated['join_date'];
|
||||
$employee->employment_status = $validated['employment_status'];
|
||||
$employee->base_salary = $validated['base_salary'];
|
||||
$employee->save();
|
||||
$employee->update([
|
||||
'join_date' => $validated['join_date'],
|
||||
'employment_status' => $validated['employment_status'],
|
||||
'base_salary' => $validated['base_salary'],
|
||||
]);
|
||||
} else {
|
||||
Employee::create([
|
||||
'user_id' => $user->id,
|
||||
@ -135,8 +137,9 @@ public function update(User $user, array $validated): void
|
||||
|
||||
public function toggleStatus(User $user, array $validated): void
|
||||
{
|
||||
$user->is_active = $validated['is_active'];
|
||||
$user->save();
|
||||
$user->update([
|
||||
'is_active' => $validated['is_active'],
|
||||
]);
|
||||
|
||||
if (! $validated['is_active']) {
|
||||
DB::table('sessions')->where('user_id', $user->id)->delete();
|
||||
@ -145,8 +148,9 @@ public function toggleStatus(User $user, array $validated): void
|
||||
|
||||
public function resetPassword(User $user): void
|
||||
{
|
||||
$user->password = config('auth.password_default');
|
||||
$user->save();
|
||||
$user->update([
|
||||
'password' => config('auth.password_default'),
|
||||
]);
|
||||
|
||||
DB::table('sessions')->where('user_id', $user->id)->delete();
|
||||
}
|
||||
|
||||
@ -96,10 +96,11 @@ public function update(LeaveRequest $leaveRequest, array $validated, User $user)
|
||||
|
||||
$this->ensureMinimumLeadTime($startDate);
|
||||
|
||||
$leaveRequest->start_date = $startDate;
|
||||
$leaveRequest->end_date = $endDate;
|
||||
$leaveRequest->total_days = $this->calculateTotalDays($startDate, $endDate);
|
||||
$leaveRequest->save();
|
||||
$leaveRequest->update([
|
||||
'start_date' => $startDate,
|
||||
'end_date' => $endDate,
|
||||
'total_days' => $this->calculateTotalDays($startDate, $endDate),
|
||||
]);
|
||||
|
||||
$this->pushNotificationService->sendToRoles(
|
||||
'✏️ Pengajuan Cuti Diperbarui',
|
||||
@ -127,10 +128,11 @@ public function delete(LeaveRequest $leaveRequest): void
|
||||
public function approve(LeaveRequest $leaveRequest, User $user): void
|
||||
{
|
||||
DB::transaction(function () use ($leaveRequest, $user): void {
|
||||
$leaveRequest->status = LeaveRequestStatus::APPROVED;
|
||||
$leaveRequest->verified_at = Carbon::now();
|
||||
$leaveRequest->verified_by_id = $user->id;
|
||||
$leaveRequest->save();
|
||||
$leaveRequest->update([
|
||||
'status' => LeaveRequestStatus::APPROVED,
|
||||
'verified_at' => Carbon::now(),
|
||||
'verified_by_id' => $user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
$leaveRequest->loadMissing('employee.user');
|
||||
@ -147,10 +149,11 @@ public function approve(LeaveRequest $leaveRequest, User $user): void
|
||||
public function reject(LeaveRequest $leaveRequest, string $reason, User $user): void
|
||||
{
|
||||
DB::transaction(function () use ($leaveRequest, $user, $reason): void {
|
||||
$leaveRequest->status = LeaveRequestStatus::REJECTED;
|
||||
$leaveRequest->verified_at = Carbon::now();
|
||||
$leaveRequest->verified_by_id = $user->id;
|
||||
$leaveRequest->save();
|
||||
$leaveRequest->update([
|
||||
'status' => LeaveRequestStatus::REJECTED,
|
||||
'verified_at' => Carbon::now(),
|
||||
'verified_by_id' => $user->id,
|
||||
]);
|
||||
|
||||
$leaveRequest->rejection()->create([
|
||||
'reason' => $reason,
|
||||
|
||||
@ -237,8 +237,9 @@ public function create(array $validated, User $user): Purchase
|
||||
]);
|
||||
|
||||
foreach ($draftItems as $item) {
|
||||
$item->purchase_id = $purchase->id;
|
||||
$item->save();
|
||||
$item->update([
|
||||
'purchase_id' => $purchase->id,
|
||||
]);
|
||||
$this->incrementStock($item);
|
||||
}
|
||||
|
||||
@ -278,13 +279,14 @@ public function update(Purchase $purchase, array $validated): void
|
||||
$shippingCost = (int) ($validated['shipping_cost'] ?? 0);
|
||||
$total = max($subtotal - $discount + $shippingCost, 0);
|
||||
|
||||
$purchase->supplier_id = $validated['supplier_id'];
|
||||
$purchase->subtotal = $subtotal;
|
||||
$purchase->discount = $discount;
|
||||
$purchase->shipping_cost = $shippingCost;
|
||||
$purchase->total = $total;
|
||||
$purchase->notes = $validated['notes'] ?? null;
|
||||
$purchase->save();
|
||||
$purchase->update([
|
||||
'supplier_id' => $validated['supplier_id'],
|
||||
'subtotal' => $subtotal,
|
||||
'discount' => $discount,
|
||||
'shipping_cost' => $shippingCost,
|
||||
'total' => $total,
|
||||
'notes' => $validated['notes'] ?? null,
|
||||
]);
|
||||
|
||||
foreach ($lineItems as $itemData) {
|
||||
$purchaseItem = $purchase->items()->create($itemData);
|
||||
|
||||
@ -41,7 +41,7 @@ public function create(array $validated): void
|
||||
*/
|
||||
public function update(Category $category, array $validated): void
|
||||
{
|
||||
$category->fill($validated)->save();
|
||||
$category->update($validated);
|
||||
}
|
||||
|
||||
public function delete(Category $category): void
|
||||
|
||||
@ -43,7 +43,7 @@ public function create(array $validated): void
|
||||
*/
|
||||
public function update(Customer $customer, array $validated): void
|
||||
{
|
||||
$customer->fill($validated)->save();
|
||||
$customer->update($validated);
|
||||
}
|
||||
|
||||
public function delete(Customer $customer): void
|
||||
|
||||
@ -94,9 +94,10 @@ public function create(array $validated): void
|
||||
public function update(Product $product, array $validated): void
|
||||
{
|
||||
DB::transaction(function () use ($validated, $product): void {
|
||||
$product->name = $validated['name'];
|
||||
$product->description = $validated['description'] ?? null;
|
||||
$product->save();
|
||||
$product->update([
|
||||
'name' => $validated['name'],
|
||||
'description' => $validated['description'] ?? null,
|
||||
]);
|
||||
|
||||
$product->categories()->sync($validated['category_ids']);
|
||||
|
||||
@ -117,9 +118,10 @@ public function update(Product $product, array $validated): void
|
||||
foreach ($validated['variants'] as $index => $variantData) {
|
||||
if (! empty($variantData['id'])) {
|
||||
$variant = $product->variants()->findOrFail($variantData['id']);
|
||||
$variant->name = $variantData['name'];
|
||||
$variant->stock = $variantData['stock'];
|
||||
$variant->save();
|
||||
$variant->update([
|
||||
'name' => $variantData['name'],
|
||||
'stock' => $variantData['stock'],
|
||||
]);
|
||||
$this->syncVariantImages($variant, $variantData, $index);
|
||||
|
||||
continue;
|
||||
@ -132,8 +134,9 @@ public function update(Product $product, array $validated): void
|
||||
|
||||
public function toggleStatus(Product $product, array $validated): void
|
||||
{
|
||||
$product->is_active = $validated['is_active'];
|
||||
$product->save();
|
||||
$product->update([
|
||||
'is_active' => $validated['is_active'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(Product $product): void
|
||||
|
||||
@ -94,8 +94,9 @@ public function create(array $validated): void
|
||||
public function update(RawMaterial $rawMaterial, array $validated): void
|
||||
{
|
||||
DB::transaction(function () use ($validated, $rawMaterial): void {
|
||||
$rawMaterial->name = $validated['name'];
|
||||
$rawMaterial->save();
|
||||
$rawMaterial->update([
|
||||
'name' => $validated['name'],
|
||||
]);
|
||||
|
||||
$submittedPriceIds = collect($validated['prices'])
|
||||
->pluck('id')
|
||||
@ -114,10 +115,11 @@ public function update(RawMaterial $rawMaterial, array $validated): void
|
||||
foreach ($validated['prices'] as $index => $priceData) {
|
||||
if (! empty($priceData['id'])) {
|
||||
$price = $rawMaterial->prices()->findOrFail($priceData['id']);
|
||||
$price->variant = $priceData['variant'];
|
||||
$price->price = $priceData['price'];
|
||||
$price->stock = $priceData['stock'];
|
||||
$price->save();
|
||||
$price->update([
|
||||
'variant' => $priceData['variant'],
|
||||
'price' => $priceData['price'],
|
||||
'stock' => $priceData['stock'],
|
||||
]);
|
||||
$this->syncPriceImages($price, $priceData, $index);
|
||||
|
||||
continue;
|
||||
@ -130,8 +132,9 @@ public function update(RawMaterial $rawMaterial, array $validated): void
|
||||
|
||||
public function toggleStatus(RawMaterial $rawMaterial, array $validated): void
|
||||
{
|
||||
$rawMaterial->is_active = $validated['is_active'];
|
||||
$rawMaterial->save();
|
||||
$rawMaterial->update([
|
||||
'is_active' => $validated['is_active'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(RawMaterial $rawMaterial): void
|
||||
|
||||
@ -43,7 +43,7 @@ public function create(array $validated): void
|
||||
*/
|
||||
public function update(Supplier $supplier, array $validated): void
|
||||
{
|
||||
$supplier->fill($validated)->save();
|
||||
$supplier->update($validated);
|
||||
}
|
||||
|
||||
public function delete(Supplier $supplier): void
|
||||
|
||||
@ -56,10 +56,11 @@ public function update(Role $role, array $validated): void
|
||||
{
|
||||
DB::transaction(function () use ($role, $validated): void {
|
||||
// Protect developer and owner names from changing
|
||||
$updateData = [];
|
||||
if (! in_array($role->name, [EnumsRole::DEVELOPER->value, EnumsRole::OWNER->value], true)) {
|
||||
$role->name = Str::slug($validated['name']);
|
||||
$updateData['name'] = Str::slug($validated['name']);
|
||||
}
|
||||
$role->save();
|
||||
$role->update($updateData);
|
||||
|
||||
$role->syncPermissions($validated['permissions'] ?? []);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user