Init
This commit is contained in:
184
app/Temporal/OrderFulfillment/OrderActivity.php
Normal file
184
app/Temporal/OrderFulfillment/OrderActivity.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Temporal\OrderFulfillment;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Temporal\Shared\FaultSimulator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Temporal\Activity;
|
||||
|
||||
class OrderActivity implements OrderActivityInterface
|
||||
{
|
||||
public function validateOrder(int $orderId, array $simulationConfig = []): bool
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'validateOrder');
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
if ($order->status !== 'pending') {
|
||||
throw new \RuntimeException("Order #{$orderId} is not in 'pending' status. Current status: {$order->status}");
|
||||
}
|
||||
|
||||
Log::info("Order #{$orderId} validated successfully.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function checkInventory(int $orderId, array $simulationConfig = []): bool
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'checkInventory');
|
||||
|
||||
$order = Order::with('items.product')->find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
foreach ($order->items as $item) {
|
||||
if ($item->product->stock_quantity < $item->quantity) {
|
||||
throw new \RuntimeException(
|
||||
"Insufficient stock for product '{$item->product->name}'. "
|
||||
. "Available: {$item->product->stock_quantity}, Requested: {$item->quantity}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Log::info("Inventory check passed for order #{$orderId}.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function processPayment(int $orderId, array $simulationConfig = []): string
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'processPayment');
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
$paymentId = 'PAY-' . strtoupper(substr(md5(uniqid((string) $orderId, true)), 0, 12));
|
||||
|
||||
$order->update([
|
||||
'status' => 'processing',
|
||||
'payment_id' => $paymentId,
|
||||
]);
|
||||
|
||||
Log::info("Payment processed for order #{$orderId}. Payment ID: {$paymentId}");
|
||||
|
||||
return $paymentId;
|
||||
}
|
||||
|
||||
public function refundPayment(int $orderId, string $paymentId): bool
|
||||
{
|
||||
usleep(300000);
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if ($order) {
|
||||
$order->update([
|
||||
'status' => 'refunded',
|
||||
'payment_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
Log::warning("Payment refunded for order #{$orderId}. Payment ID: {$paymentId}");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateInventory(int $orderId, array $simulationConfig = []): bool
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'updateInventory');
|
||||
|
||||
$order = Order::with('items.product')->find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
foreach ($order->items as $item) {
|
||||
$item->product->decrement('stock_quantity', $item->quantity);
|
||||
}
|
||||
|
||||
Log::info("Inventory updated (decremented) for order #{$orderId}.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function restoreInventory(int $orderId): bool
|
||||
{
|
||||
usleep(200000);
|
||||
|
||||
$order = Order::with('items.product')->find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
foreach ($order->items as $item) {
|
||||
$item->product->increment('stock_quantity', $item->quantity);
|
||||
}
|
||||
|
||||
Log::info("Inventory restored (incremented) for order #{$orderId}.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function notifyWarehouse(int $orderId, array $simulationConfig = []): bool
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'notifyWarehouse');
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
$order->update(['status' => 'warehouse_notified']);
|
||||
|
||||
Log::info("Warehouse notified for order #{$orderId}.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function cancelWarehouseNotification(int $orderId): bool
|
||||
{
|
||||
usleep(100000);
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if ($order) {
|
||||
$order->update(['status' => 'warehouse_cancelled']);
|
||||
}
|
||||
|
||||
Log::warning("Warehouse notification cancelled for order #{$orderId}.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendTrackingInfo(int $orderId, string $trackingNumber, array $simulationConfig = []): bool
|
||||
{
|
||||
FaultSimulator::maybeApply($simulationConfig, 'sendTrackingInfo');
|
||||
|
||||
$order = Order::find($orderId);
|
||||
|
||||
if (!$order) {
|
||||
throw new \RuntimeException("Order #{$orderId} not found.");
|
||||
}
|
||||
|
||||
$order->update([
|
||||
'tracking_number' => $trackingNumber,
|
||||
'status' => 'shipped',
|
||||
]);
|
||||
|
||||
Log::info("Tracking info sent for order #{$orderId}. Tracking: {$trackingNumber}");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user