Skip to content

Commit 94fe898

Browse files
DangDang
authored andcommitted
feat: Add Cancelled status for Order and ServiceOrder, return type as number
- Add Cancelled status to OrderStatus enum (4=Cancelled) - Add Cancel() method to Order entity with proper validation - Update Order UpdateStatus method to handle Cancelled status - Add IsCancelled computed property to Order entity - Update ServiceOrder DTOs to return Type as number instead of string - Update GetServiceOrdersQueryHandler to map Type as (int)so.Type - Update OrdersController status mapping to include Cancelled (4) - Update Order status handler business logic: * Order Cancelled -> Vehicle Available (release reserved vehicle) - Update ServiceOrder status handler to handle Cancelled status - ServiceOrder Type now returns: 1=PreDelivery, 2=Maintenance, 3=Repair - ServiceOrder Status now returns: 1=Scheduled, 2=InProgress, 3=Completed, 4=Cancelled
1 parent 457da2e commit 94fe898

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

VehicleShowroomManagement/src/Application/Features/Orders/Commands/UpdateOrderStatus/UpdateOrderStatusCommandHandler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public async Task<Unit> Handle(UpdateOrderStatusCommand request, CancellationTok
4040
vehicle.Sell();
4141
await vehicleRepository.UpdateAsync(vehicle, cancellationToken);
4242
}
43+
else if (request.Status == OrderStatus.Cancelled)
44+
{
45+
// When order is cancelled, make vehicle available again
46+
vehicle.UpdateStatus(VehicleStatus.Available);
47+
await vehicleRepository.UpdateAsync(vehicle, cancellationToken);
48+
}
4349
}
4450
}
4551

VehicleShowroomManagement/src/Application/Features/ServiceOrders/Commands/UpdateStatus/UpdateServiceOrderStatusCommandHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public async Task<UpdateServiceOrderStatusResult> Handle(UpdateServiceOrderStatu
2929
Message = "Service order status updated successfully"
3030
};
3131

32-
// Business Logic: Only PreDelivery service orders affect Order/Vehicle status
33-
if (request.Status == ServiceOrderStatus.Completed && serviceOrder.Type == ServiceType.PreDelivery)
32+
// Business Logic: Handle different status updates
33+
if (request.Status == ServiceOrderStatus.Cancelled)
34+
{
35+
// For cancelled service orders, no impact on Order/Vehicle status
36+
result.Message = "Service order cancelled";
37+
}
38+
else if (request.Status == ServiceOrderStatus.Completed && serviceOrder.Type == ServiceType.PreDelivery)
3439
{
3540
// Get related order
3641
var order = await orderRepository.GetByIdAsync(serviceOrder.OrderId, cancellationToken) ?? throw new ArgumentException("Related order not found");

VehicleShowroomManagement/src/Application/Features/ServiceOrders/Queries/GetServiceOrders/GetServiceOrdersQueryHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<ServiceOrdersResponse> Handle(GetServiceOrdersQuery request, C
5050
AppointmentDate = so.AppointmentDate,
5151
Description = so.Description,
5252
Cost = so.Cost,
53-
Type = so.Type.ToString(),
53+
Type = (int)so.Type,
5454
Status = (int)so.Status
5555
}).ToList();
5656

VehicleShowroomManagement/src/Application/Features/ServiceOrders/Queries/GetServiceOrders/ServiceOrderDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ServiceOrderDto
1313
public DateTime? AppointmentDate { get; set; }
1414
public string? Description { get; set; }
1515
public decimal Cost { get; set; }
16-
public string Type { get; set; } = string.Empty;
16+
public int Type { get; set; }
1717
public int Status { get; set; }
1818
}
1919

VehicleShowroomManagement/src/Domain/Entities/Order.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ public void Complete()
131131
UpdatedAt = DateTime.UtcNow;
132132
}
133133

134+
public void Cancel()
135+
{
136+
if (Status == OrderStatus.Completed)
137+
throw new InvalidOperationException("Cannot cancel a completed order");
138+
139+
if (Status == OrderStatus.Cancelled)
140+
throw new InvalidOperationException("Order is already cancelled");
141+
142+
Status = OrderStatus.Cancelled;
143+
UpdatedAt = DateTime.UtcNow;
144+
}
145+
134146
public void UpdateStatus(OrderStatus status)
135147
{
136148
switch (status)
@@ -148,6 +160,9 @@ public void UpdateStatus(OrderStatus status)
148160
case OrderStatus.Completed:
149161
Complete();
150162
break;
163+
case OrderStatus.Cancelled:
164+
Cancel();
165+
break;
151166
default:
152167
throw new InvalidOperationException("Unsupported status transition");
153168
}
@@ -175,6 +190,7 @@ public void SetReservationPeriod(DateTime from, DateTime to)
175190
public bool IsPending => Status == OrderStatus.Pending;
176191
public bool IsConfirmed => Status == OrderStatus.Confirmed;
177192
public bool IsCompleted => Status == OrderStatus.Completed;
193+
public bool IsCancelled => Status == OrderStatus.Cancelled;
178194
public bool HasVehicle => !string.IsNullOrEmpty(VehicleId);
179195
}
180196
}

VehicleShowroomManagement/src/Domain/Enums/OrderStatus.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public enum OrderStatus
1818
/// <summary>
1919
/// Order has been completed
2020
/// </summary>
21-
Completed = 3
21+
Completed = 3,
22+
23+
/// <summary>
24+
/// Order has been cancelled
25+
/// </summary>
26+
Cancelled = 4
2227
}
2328
}

VehicleShowroomManagement/src/WebAPI/Controllers/OrdersController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public async Task<IActionResult> GetOrders(
3636
OrderStatus? statusEnum = null;
3737
if (status.HasValue)
3838
{
39-
// 1=Pending, 2=Confirmed, 3=Completed
39+
// 1=Pending, 2=Confirmed, 3=Completed, 4=Cancelled
4040
statusEnum = status.Value switch
4141
{
4242
1 => OrderStatus.Pending,
4343
2 => OrderStatus.Confirmed,
4444
3 => OrderStatus.Completed,
45+
4 => OrderStatus.Cancelled,
4546
_ => null
4647
};
4748
}

0 commit comments

Comments
 (0)