@anhth2/spec-driven-dev-plugin 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +243 -0
- package/bin/build.js +230 -0
- package/bin/index.js +311 -0
- package/commands/debug.md +374 -0
- package/commands/debug.tmpl +77 -0
- package/commands/define-product.md +451 -0
- package/commands/define-product.tmpl +154 -0
- package/commands/fix-bug.md +379 -0
- package/commands/fix-bug.tmpl +82 -0
- package/commands/generate-bdd.md +591 -0
- package/commands/generate-bdd.tmpl +294 -0
- package/commands/generate-code.md +395 -0
- package/commands/generate-code.tmpl +98 -0
- package/commands/generate-prd.md +488 -0
- package/commands/generate-prd.tmpl +191 -0
- package/commands/generate-tech-docs.md +362 -0
- package/commands/generate-tech-docs.tmpl +65 -0
- package/commands/generate-tests.md +377 -0
- package/commands/generate-tests.tmpl +80 -0
- package/commands/refine-prd.md +408 -0
- package/commands/refine-prd.tmpl +111 -0
- package/commands/review-code.md +354 -0
- package/commands/review-code.tmpl +57 -0
- package/commands/review-context.md +646 -0
- package/commands/review-context.tmpl +349 -0
- package/commands/review-tech-docs.md +518 -0
- package/commands/review-tech-docs.tmpl +221 -0
- package/commands/run-tests.md +343 -0
- package/commands/run-tests.tmpl +46 -0
- package/commands/setup-ai-first.md +278 -0
- package/commands/setup-ai-first.tmpl +197 -0
- package/commands/smoke-test.md +366 -0
- package/commands/smoke-test.tmpl +69 -0
- package/commands/validate-traces.md +529 -0
- package/commands/validate-traces.tmpl +232 -0
- package/core/FRAMEWORK_VERSION +1 -0
- package/core/commands/debug.md +374 -0
- package/core/commands/define-product.md +451 -0
- package/core/commands/fix-bug.md +379 -0
- package/core/commands/generate-bdd.md +591 -0
- package/core/commands/generate-code.md +395 -0
- package/core/commands/generate-prd.md +488 -0
- package/core/commands/generate-tech-docs.md +362 -0
- package/core/commands/generate-tests.md +377 -0
- package/core/commands/refine-prd.md +408 -0
- package/core/commands/review-code.md +354 -0
- package/core/commands/review-context.md +646 -0
- package/core/commands/review-tech-docs.md +518 -0
- package/core/commands/run-tests.md +343 -0
- package/core/commands/setup-ai-first.md +278 -0
- package/core/commands/smoke-test.md +366 -0
- package/core/commands/validate-traces.md +529 -0
- package/core/hooks/data-guard.js +141 -0
- package/core/hooks/settings.json +18 -0
- package/core/modules/angular/architecture-snippets/component-patterns.md +187 -0
- package/core/modules/angular/module.yaml +6 -0
- package/core/modules/angular/stack-profile.yaml +38 -0
- package/core/modules/context-engineering/architecture-snippets/context-design.md +119 -0
- package/core/modules/context-engineering/module.yaml +9 -0
- package/core/modules/context-engineering/stack-profile.yaml +61 -0
- package/core/modules/dotnet/architecture-snippets/clean-arch.md +160 -0
- package/core/modules/dotnet/module.yaml +6 -0
- package/core/modules/dotnet/stack-profile.yaml +50 -0
- package/core/modules/golang/architecture-snippets/domain-layout.md +283 -0
- package/core/modules/golang/module.yaml +6 -0
- package/core/modules/golang/stack-profile.yaml +40 -0
- package/core/modules/java-spring/architecture-snippets/layered-arch.md +201 -0
- package/core/modules/java-spring/module.yaml +15 -0
- package/core/modules/java-spring/stack-profile.yaml +28 -0
- package/core/modules/nextjs/architecture-snippets/app-router-patterns.md +269 -0
- package/core/modules/nextjs/module.yaml +14 -0
- package/core/modules/nextjs/stack-profile.yaml +74 -0
- package/core/modules/php-laravel/architecture-snippets/service-repository.md +302 -0
- package/core/modules/php-laravel/module.yaml +15 -0
- package/core/modules/php-laravel/stack-profile.yaml +56 -0
- package/core/modules/react/architecture-snippets/hooks-query-patterns.md +254 -0
- package/core/modules/react/module.yaml +14 -0
- package/core/modules/react/stack-profile.yaml +63 -0
- package/core/rules/data-protection.md +80 -0
- package/core/rules/workflow.md +44 -0
- package/core/skills/code/SKILL.md +526 -0
- package/core/skills/debug/SKILL.md +584 -0
- package/core/skills/discovery/SKILL.md +363 -0
- package/core/skills/prd/SKILL.md +456 -0
- package/core/skills/setup-ai-first/SKILL.md +160 -0
- package/core/skills/spec/SKILL.md +361 -0
- package/core/skills/test/SKILL.md +862 -0
- package/core/steps/context-loader.md +163 -0
- package/core/steps/gate.md +81 -0
- package/core/steps/report-footer.md +53 -0
- package/core/steps/spawn-agent.md +123 -0
- package/core/templates/architecture.template.md +113 -0
- package/core/templates/feature.template +259 -0
- package/core/templates/platform-guide.template.md +145 -0
- package/core/templates/prd.template.md +312 -0
- package/core/templates/product-definition.template.md +168 -0
- package/core/templates/project-context.yaml +78 -0
- package/hooks/data-guard.js +141 -0
- package/hooks/settings.json +18 -0
- package/modules/angular/architecture-snippets/component-patterns.md +187 -0
- package/modules/angular/module.yaml +6 -0
- package/modules/angular/stack-profile.yaml +38 -0
- package/modules/context-engineering/architecture-snippets/context-design.md +119 -0
- package/modules/context-engineering/module.yaml +9 -0
- package/modules/context-engineering/stack-profile.yaml +61 -0
- package/modules/dotnet/architecture-snippets/clean-arch.md +160 -0
- package/modules/dotnet/module.yaml +6 -0
- package/modules/dotnet/stack-profile.yaml +50 -0
- package/modules/golang/architecture-snippets/domain-layout.md +283 -0
- package/modules/golang/module.yaml +6 -0
- package/modules/golang/stack-profile.yaml +40 -0
- package/modules/java-spring/architecture-snippets/layered-arch.md +201 -0
- package/modules/java-spring/module.yaml +15 -0
- package/modules/java-spring/stack-profile.yaml +28 -0
- package/modules/nextjs/architecture-snippets/app-router-patterns.md +269 -0
- package/modules/nextjs/module.yaml +14 -0
- package/modules/nextjs/stack-profile.yaml +74 -0
- package/modules/php-laravel/architecture-snippets/service-repository.md +302 -0
- package/modules/php-laravel/module.yaml +15 -0
- package/modules/php-laravel/stack-profile.yaml +56 -0
- package/modules/react/architecture-snippets/hooks-query-patterns.md +254 -0
- package/modules/react/module.yaml +14 -0
- package/modules/react/stack-profile.yaml +63 -0
- package/package.json +42 -0
- package/rules/data-protection.md +80 -0
- package/rules/workflow.md +44 -0
- package/scripts/init.sh +49 -0
- package/scripts/upgrade.sh +94 -0
- package/skills/code/SKILL.md +526 -0
- package/skills/code/SKILL.tmpl +176 -0
- package/skills/debug/SKILL.md +584 -0
- package/skills/debug/SKILL.tmpl +262 -0
- package/skills/discovery/SKILL.md +363 -0
- package/skills/discovery/SKILL.tmpl +147 -0
- package/skills/prd/SKILL.md +456 -0
- package/skills/prd/SKILL.tmpl +188 -0
- package/skills/setup-ai-first/SKILL.md +160 -0
- package/skills/setup-ai-first/SKILL.tmpl +107 -0
- package/skills/spec/SKILL.md +361 -0
- package/skills/spec/SKILL.tmpl +174 -0
- package/skills/test/SKILL.md +862 -0
- package/skills/test/SKILL.tmpl +296 -0
- package/steps/context-loader.md +163 -0
- package/steps/gate.md +81 -0
- package/steps/report-footer.md +53 -0
- package/steps/spawn-agent.md +123 -0
- package/templates/architecture.template.md +113 -0
- package/templates/feature.template +259 -0
- package/templates/platform-guide.template.md +145 -0
- package/templates/prd.template.md +312 -0
- package/templates/product-definition.template.md +168 -0
- package/templates/project-context.yaml +78 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Java Spring Boot — Layered Architecture Code Patterns
|
|
2
|
+
|
|
3
|
+
## Controller Pattern
|
|
4
|
+
|
|
5
|
+
```java
|
|
6
|
+
@RestController
|
|
7
|
+
@RequestMapping("/v1/orders")
|
|
8
|
+
@RequiredArgsConstructor
|
|
9
|
+
// @trace.implements=ORDER-UC1-SC1
|
|
10
|
+
// @trace.source=specs/bdd/order/ORDER-UC1-create-order.feature
|
|
11
|
+
public class OrderController {
|
|
12
|
+
|
|
13
|
+
private final OrderFacade orderFacade;
|
|
14
|
+
|
|
15
|
+
@PostMapping
|
|
16
|
+
public ResponseEntity<ApiResponse<OrderResponse>> createOrder(
|
|
17
|
+
@Valid @RequestBody CreateOrderRequest request) {
|
|
18
|
+
OrderResponse result = orderFacade.createOrder(request);
|
|
19
|
+
return ResponseEntity.status(HttpStatus.CREATED)
|
|
20
|
+
.body(ApiResponse.success(result));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@GetMapping("/{id}")
|
|
24
|
+
// @trace.implements=ORDER-UC1-SC2
|
|
25
|
+
public ResponseEntity<ApiResponse<OrderResponse>> getOrder(@PathVariable Long id) {
|
|
26
|
+
OrderResponse result = orderFacade.getOrder(id);
|
|
27
|
+
return ResponseEntity.ok(ApiResponse.success(result));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Service Interface Pattern
|
|
33
|
+
|
|
34
|
+
```java
|
|
35
|
+
public interface OrderService {
|
|
36
|
+
OrderResponse createOrder(CreateOrderRequest request);
|
|
37
|
+
OrderResponse getOrderById(Long id);
|
|
38
|
+
void cancelOrder(Long id);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Service Implementation Pattern
|
|
43
|
+
|
|
44
|
+
```java
|
|
45
|
+
@Service
|
|
46
|
+
@RequiredArgsConstructor
|
|
47
|
+
@Transactional(readOnly = true)
|
|
48
|
+
public class OrderServiceImpl implements OrderService {
|
|
49
|
+
|
|
50
|
+
private final OrderRepository orderRepository;
|
|
51
|
+
private final OrderMapper orderMapper;
|
|
52
|
+
|
|
53
|
+
@Override
|
|
54
|
+
@Transactional
|
|
55
|
+
public OrderResponse createOrder(CreateOrderRequest request) {
|
|
56
|
+
Order order = orderMapper.toEntity(request);
|
|
57
|
+
order.setStatus(OrderStatus.PENDING);
|
|
58
|
+
Order saved = orderRepository.save(order);
|
|
59
|
+
return orderMapper.toResponse(saved);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@Override
|
|
63
|
+
public OrderResponse getOrderById(Long id) {
|
|
64
|
+
Order order = orderRepository.findById(id)
|
|
65
|
+
.orElseThrow(() -> new ResourceNotFoundException("Order", id));
|
|
66
|
+
return orderMapper.toResponse(order);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Override
|
|
70
|
+
@Transactional
|
|
71
|
+
public void cancelOrder(Long id) {
|
|
72
|
+
Order order = orderRepository.findById(id)
|
|
73
|
+
.orElseThrow(() -> new ResourceNotFoundException("Order", id));
|
|
74
|
+
order.setStatus(OrderStatus.CANCELLED);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Repository Interface Pattern
|
|
80
|
+
|
|
81
|
+
```java
|
|
82
|
+
@Repository
|
|
83
|
+
public interface OrderRepository extends JpaRepository<Order, Long> {
|
|
84
|
+
|
|
85
|
+
List<Order> findByCustomerIdAndStatus(Long customerId, OrderStatus status);
|
|
86
|
+
|
|
87
|
+
@Query("SELECT o FROM Order o JOIN FETCH o.items WHERE o.id = :id")
|
|
88
|
+
Optional<Order> findByIdWithItems(@Param("id") Long id);
|
|
89
|
+
|
|
90
|
+
Page<Order> findByCreatedAtBetween(
|
|
91
|
+
LocalDateTime from, LocalDateTime to, Pageable pageable);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## DTO Pattern
|
|
96
|
+
|
|
97
|
+
```java
|
|
98
|
+
// Request DTO
|
|
99
|
+
@Data
|
|
100
|
+
@NoArgsConstructor
|
|
101
|
+
@AllArgsConstructor
|
|
102
|
+
public class CreateOrderRequest {
|
|
103
|
+
@NotNull(message = "customerId is required")
|
|
104
|
+
private Long customerId;
|
|
105
|
+
|
|
106
|
+
@NotEmpty(message = "items must not be empty")
|
|
107
|
+
private List<OrderItemRequest> items;
|
|
108
|
+
|
|
109
|
+
@NotBlank(message = "shippingAddress is required")
|
|
110
|
+
private String shippingAddress;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Response DTO
|
|
114
|
+
@Data
|
|
115
|
+
@Builder
|
|
116
|
+
@NoArgsConstructor
|
|
117
|
+
@AllArgsConstructor
|
|
118
|
+
public class OrderResponse {
|
|
119
|
+
private Long id;
|
|
120
|
+
private Long customerId;
|
|
121
|
+
private OrderStatus status;
|
|
122
|
+
private List<OrderItemResponse> items;
|
|
123
|
+
private BigDecimal totalAmount;
|
|
124
|
+
private LocalDateTime createdAt;
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## MapStruct Mapper Pattern
|
|
129
|
+
|
|
130
|
+
```java
|
|
131
|
+
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.ERROR)
|
|
132
|
+
public interface OrderMapper {
|
|
133
|
+
|
|
134
|
+
@Mapping(target = "id", ignore = true)
|
|
135
|
+
@Mapping(target = "status", ignore = true)
|
|
136
|
+
@Mapping(target = "createdAt", ignore = true)
|
|
137
|
+
Order toEntity(CreateOrderRequest request);
|
|
138
|
+
|
|
139
|
+
OrderResponse toResponse(Order order);
|
|
140
|
+
|
|
141
|
+
List<OrderResponse> toResponseList(List<Order> orders);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## ApiResponse Wrapper Pattern
|
|
146
|
+
|
|
147
|
+
```java
|
|
148
|
+
@Data
|
|
149
|
+
@Builder
|
|
150
|
+
@NoArgsConstructor
|
|
151
|
+
@AllArgsConstructor
|
|
152
|
+
public class ApiResponse<T> {
|
|
153
|
+
private boolean success;
|
|
154
|
+
private T data;
|
|
155
|
+
private String message;
|
|
156
|
+
private String errorCode;
|
|
157
|
+
private LocalDateTime timestamp;
|
|
158
|
+
|
|
159
|
+
public static <T> ApiResponse<T> success(T data) {
|
|
160
|
+
return ApiResponse.<T>builder()
|
|
161
|
+
.success(true)
|
|
162
|
+
.data(data)
|
|
163
|
+
.timestamp(LocalDateTime.now())
|
|
164
|
+
.build();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public static <T> ApiResponse<T> error(String errorCode, String message) {
|
|
168
|
+
return ApiResponse.<T>builder()
|
|
169
|
+
.success(false)
|
|
170
|
+
.errorCode(errorCode)
|
|
171
|
+
.message(message)
|
|
172
|
+
.timestamp(LocalDateTime.now())
|
|
173
|
+
.build();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Facade Pattern (optional orchestration layer)
|
|
179
|
+
|
|
180
|
+
```java
|
|
181
|
+
@Service
|
|
182
|
+
@RequiredArgsConstructor
|
|
183
|
+
public class OrderFacade {
|
|
184
|
+
|
|
185
|
+
private final OrderService orderService;
|
|
186
|
+
private final InventoryService inventoryService;
|
|
187
|
+
private final NotificationService notificationService;
|
|
188
|
+
|
|
189
|
+
public OrderResponse createOrder(CreateOrderRequest request) {
|
|
190
|
+
// 1. Validate inventory
|
|
191
|
+
inventoryService.validateAvailability(request.getItems());
|
|
192
|
+
// 2. Create order
|
|
193
|
+
OrderResponse order = orderService.createOrder(request);
|
|
194
|
+
// 3. Reserve inventory
|
|
195
|
+
inventoryService.reserveItems(order.getId(), request.getItems());
|
|
196
|
+
// 4. Send confirmation
|
|
197
|
+
notificationService.sendOrderConfirmation(order);
|
|
198
|
+
return order;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
name: "Java Spring Boot"
|
|
2
|
+
version: "1.0.0"
|
|
3
|
+
description: "Spring Boot 3.x with layered architecture"
|
|
4
|
+
language: "Java"
|
|
5
|
+
framework: "Spring Boot"
|
|
6
|
+
stack_type: "backend"
|
|
7
|
+
default_layer_order:
|
|
8
|
+
- DTO
|
|
9
|
+
- Entity/Model
|
|
10
|
+
- Repository
|
|
11
|
+
- Service interface
|
|
12
|
+
- Service impl
|
|
13
|
+
- Facade (optional)
|
|
14
|
+
- Controller
|
|
15
|
+
test_framework: "JUnit 5 + Mockito"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
build:
|
|
2
|
+
compile: "mvn clean install -DskipTests"
|
|
3
|
+
test: "mvn test"
|
|
4
|
+
run: "mvn spring-boot:run"
|
|
5
|
+
|
|
6
|
+
architecture:
|
|
7
|
+
style: "Layered (Controller → Facade → Service → Repository)"
|
|
8
|
+
key_rules:
|
|
9
|
+
- "Controllers must not contain business logic"
|
|
10
|
+
- "Services own transaction boundaries (@Transactional)"
|
|
11
|
+
- "Repositories use Spring Data JPA"
|
|
12
|
+
- "Facades orchestrate multiple services (optional layer)"
|
|
13
|
+
|
|
14
|
+
coding_standards:
|
|
15
|
+
naming:
|
|
16
|
+
classes: "PascalCase"
|
|
17
|
+
methods: "camelCase"
|
|
18
|
+
packages: "lowercase.snake_case"
|
|
19
|
+
patterns:
|
|
20
|
+
response_wrapper: "ApiResponse<T>"
|
|
21
|
+
mapping: "MapStruct"
|
|
22
|
+
exception_base: "ResourceNotFoundException"
|
|
23
|
+
|
|
24
|
+
trace_tags:
|
|
25
|
+
implements: "@trace.implements={UC-ID}-SC{N}"
|
|
26
|
+
source: "@trace.source=specs/bdd/{domain}/{UC-ID}.feature"
|
|
27
|
+
verifies: "@trace.verifies={UC-ID}"
|
|
28
|
+
test_type: "@trace.test_type=unit|integration"
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# Next.js App Router — Server Components, Server Actions & DAL Patterns
|
|
2
|
+
|
|
3
|
+
## Data Access Layer (server-only)
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
// lib/dal/orders.dal.ts
|
|
7
|
+
// @trace.source=specs/bdd/order/ORD-UC1.feature
|
|
8
|
+
import 'server-only'; // prevents accidental import on client
|
|
9
|
+
import { db } from '@/lib/db';
|
|
10
|
+
import type { Order } from '@/types/order';
|
|
11
|
+
|
|
12
|
+
export async function getOrderById(id: number): Promise<Order | null> {
|
|
13
|
+
return db.order.findUnique({
|
|
14
|
+
where: { id },
|
|
15
|
+
include: { items: { include: { product: true } }, customer: true },
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function listOrdersByCustomer(customerId: number): Promise<Order[]> {
|
|
20
|
+
return db.order.findMany({
|
|
21
|
+
where: { customerId },
|
|
22
|
+
include: { items: true },
|
|
23
|
+
orderBy: { createdAt: 'desc' },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Server Component (Data Fetching + Layout)
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
// app/(dashboard)/orders/page.tsx
|
|
32
|
+
// @trace.implements=ORD-UC1-SC1
|
|
33
|
+
// @trace.source=specs/bdd/order/ORD-UC1.feature
|
|
34
|
+
import { listOrdersByCustomer } from '@/lib/dal/orders.dal';
|
|
35
|
+
import { getCurrentUser } from '@/lib/auth';
|
|
36
|
+
import { OrderCard } from './_components/OrderCard';
|
|
37
|
+
import { EmptyState } from '@/components/ui/EmptyState';
|
|
38
|
+
|
|
39
|
+
export default async function OrdersPage() {
|
|
40
|
+
const user = await getCurrentUser();
|
|
41
|
+
const orders = await listOrdersByCustomer(user.id);
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<section>
|
|
45
|
+
<h1>My Orders</h1>
|
|
46
|
+
{orders.length === 0 ? (
|
|
47
|
+
<EmptyState message="No orders yet." />
|
|
48
|
+
) : (
|
|
49
|
+
<ul>
|
|
50
|
+
{orders.map(order => (
|
|
51
|
+
<OrderCard key={order.id} order={order} />
|
|
52
|
+
))}
|
|
53
|
+
</ul>
|
|
54
|
+
)}
|
|
55
|
+
</section>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Zod Schema (shared server + client)
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// lib/validations/order.schema.ts
|
|
64
|
+
import { z } from 'zod';
|
|
65
|
+
|
|
66
|
+
export const createOrderSchema = z.object({
|
|
67
|
+
customerId: z.number().positive(),
|
|
68
|
+
items: z.array(z.object({
|
|
69
|
+
productId: z.number().positive(),
|
|
70
|
+
quantity: z.number().int().min(1, 'Quantity must be at least 1'),
|
|
71
|
+
})).min(1, 'At least one item is required'),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export type CreateOrderInput = z.infer<typeof createOrderSchema>;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Server Action (Mutation)
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// lib/actions/order.actions.ts
|
|
81
|
+
'use server';
|
|
82
|
+
// @trace.implements=ORD-UC2-SC1
|
|
83
|
+
// @trace.source=specs/bdd/order/ORD-UC2.feature
|
|
84
|
+
|
|
85
|
+
import { revalidatePath } from 'next/cache';
|
|
86
|
+
import { redirect } from 'next/navigation';
|
|
87
|
+
import { createOrderSchema } from '@/lib/validations/order.schema';
|
|
88
|
+
import { db } from '@/lib/db';
|
|
89
|
+
import { getCurrentUser } from '@/lib/auth';
|
|
90
|
+
|
|
91
|
+
// Returns { error } on failure, redirects on success
|
|
92
|
+
export async function createOrder(formData: FormData) {
|
|
93
|
+
const user = await getCurrentUser();
|
|
94
|
+
|
|
95
|
+
const parsed = createOrderSchema.safeParse({
|
|
96
|
+
customerId: user.id,
|
|
97
|
+
items: JSON.parse(formData.get('items') as string),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
if (!parsed.success) {
|
|
101
|
+
return { error: parsed.error.flatten().fieldErrors };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
await db.order.create({
|
|
106
|
+
data: {
|
|
107
|
+
customerId: parsed.data.customerId,
|
|
108
|
+
status: 'pending',
|
|
109
|
+
items: {
|
|
110
|
+
create: parsed.data.items.map(item => ({
|
|
111
|
+
productId: item.productId,
|
|
112
|
+
quantity: item.quantity,
|
|
113
|
+
})),
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
} catch {
|
|
118
|
+
return { error: { _form: ['Failed to create order. Please try again.'] } };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
revalidatePath('/orders');
|
|
122
|
+
redirect('/orders');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function cancelOrder(orderId: number) {
|
|
126
|
+
const user = await getCurrentUser();
|
|
127
|
+
|
|
128
|
+
const order = await db.order.findFirst({
|
|
129
|
+
where: { id: orderId, customerId: user.id },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (!order) return { error: 'Order not found.' };
|
|
133
|
+
if (order.status !== 'pending') return { error: 'Only pending orders can be cancelled.' };
|
|
134
|
+
|
|
135
|
+
await db.order.update({ where: { id: orderId }, data: { status: 'cancelled' } });
|
|
136
|
+
|
|
137
|
+
revalidatePath('/orders');
|
|
138
|
+
return { success: true };
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Client Component (Interactive UI)
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
// app/(dashboard)/orders/_components/CancelOrderButton.tsx
|
|
146
|
+
'use client';
|
|
147
|
+
// @trace.implements=ORD-UC3-SC1
|
|
148
|
+
// @trace.source=specs/bdd/order/ORD-UC3.feature
|
|
149
|
+
import { useTransition } from 'react';
|
|
150
|
+
import { cancelOrder } from '@/lib/actions/order.actions';
|
|
151
|
+
|
|
152
|
+
interface Props {
|
|
153
|
+
orderId: number;
|
|
154
|
+
disabled?: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function CancelOrderButton({ orderId, disabled }: Props) {
|
|
158
|
+
const [isPending, startTransition] = useTransition();
|
|
159
|
+
|
|
160
|
+
const handleCancel = () => {
|
|
161
|
+
startTransition(async () => {
|
|
162
|
+
const result = await cancelOrder(orderId);
|
|
163
|
+
if (result?.error) {
|
|
164
|
+
alert(result.error); // replace with toast in real app
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<button
|
|
171
|
+
onClick={handleCancel}
|
|
172
|
+
disabled={disabled || isPending}
|
|
173
|
+
className="btn-danger"
|
|
174
|
+
>
|
|
175
|
+
{isPending ? 'Cancelling...' : 'Cancel Order'}
|
|
176
|
+
</button>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## API Route (for webhooks / external consumers)
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
// app/api/orders/webhook/route.ts
|
|
185
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
186
|
+
import { verifyWebhookSignature } from '@/lib/webhook';
|
|
187
|
+
import { db } from '@/lib/db';
|
|
188
|
+
|
|
189
|
+
export async function POST(request: NextRequest) {
|
|
190
|
+
const body = await request.json();
|
|
191
|
+
|
|
192
|
+
if (!verifyWebhookSignature(request, body)) {
|
|
193
|
+
return NextResponse.json({ error: 'Invalid signature' }, { status: 401 });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (body.event === 'payment.completed') {
|
|
197
|
+
await db.order.update({
|
|
198
|
+
where: { id: body.orderId },
|
|
199
|
+
data: { status: 'paid', paidAt: new Date() },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return NextResponse.json({ received: true });
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Loading + Error Boundaries
|
|
208
|
+
|
|
209
|
+
```tsx
|
|
210
|
+
// app/(dashboard)/orders/loading.tsx
|
|
211
|
+
export default function OrdersLoading() {
|
|
212
|
+
return (
|
|
213
|
+
<div className="order-skeleton">
|
|
214
|
+
{Array.from({ length: 3 }).map((_, i) => (
|
|
215
|
+
<div key={i} className="skeleton-card animate-pulse" />
|
|
216
|
+
))}
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
```tsx
|
|
223
|
+
// app/(dashboard)/orders/error.tsx
|
|
224
|
+
'use client';
|
|
225
|
+
export default function OrdersError({ error, reset }: {
|
|
226
|
+
error: Error & { digest?: string };
|
|
227
|
+
reset: () => void;
|
|
228
|
+
}) {
|
|
229
|
+
return (
|
|
230
|
+
<div className="error-state">
|
|
231
|
+
<p>Failed to load orders: {error.message}</p>
|
|
232
|
+
<button onClick={reset}>Try again</button>
|
|
233
|
+
</div>
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Playwright E2E Test
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
// e2e/orders/create-order.spec.ts
|
|
242
|
+
// @trace.verifies=ORD-UC2
|
|
243
|
+
// @trace.test_type=e2e
|
|
244
|
+
import { test, expect } from '@playwright/test';
|
|
245
|
+
import { loginAs } from '../fixtures/auth';
|
|
246
|
+
|
|
247
|
+
test.describe('Create Order', () => {
|
|
248
|
+
test.use({ storageState: 'e2e/.auth/customer.json' }); // reuse auth state
|
|
249
|
+
|
|
250
|
+
test('customer creates an order successfully', async ({ page }) => {
|
|
251
|
+
await page.goto('/orders/new');
|
|
252
|
+
|
|
253
|
+
await page.getByLabel('Product').selectOption({ label: 'Wireless Headphones' });
|
|
254
|
+
await page.getByLabel('Quantity').fill('2');
|
|
255
|
+
await page.getByRole('button', { name: 'Add to Cart' }).click();
|
|
256
|
+
await page.getByRole('button', { name: 'Place Order' }).click();
|
|
257
|
+
|
|
258
|
+
await expect(page).toHaveURL(/\/orders\/\d+/);
|
|
259
|
+
await expect(page.getByText('Order placed successfully')).toBeVisible();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('shows validation error when cart is empty', async ({ page }) => {
|
|
263
|
+
await page.goto('/orders/new');
|
|
264
|
+
await page.getByRole('button', { name: 'Place Order' }).click();
|
|
265
|
+
|
|
266
|
+
await expect(page.getByText('At least one item is required')).toBeVisible();
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
name: "Next.js"
|
|
2
|
+
version: "1.0.0"
|
|
3
|
+
description: "Next.js 14+ App Router with Server Components, Server Actions, and React Query"
|
|
4
|
+
language: "TypeScript"
|
|
5
|
+
framework: "Next.js"
|
|
6
|
+
stack_type: "fullstack"
|
|
7
|
+
default_layer_order:
|
|
8
|
+
- Types / interfaces
|
|
9
|
+
- Server Actions (mutations)
|
|
10
|
+
- Data access layer (DAL) — server-side DB/API calls
|
|
11
|
+
- Server Components (data fetching + layout)
|
|
12
|
+
- Client Components (interactive UI)
|
|
13
|
+
- API Routes (for external consumers / webhooks)
|
|
14
|
+
test_framework: "Vitest + React Testing Library + Playwright (E2E)"
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
build:
|
|
2
|
+
compile: "npm run build"
|
|
3
|
+
test: "npm test"
|
|
4
|
+
run: "npm run dev"
|
|
5
|
+
lint: "npm run lint"
|
|
6
|
+
e2e: "npx playwright test"
|
|
7
|
+
|
|
8
|
+
architecture:
|
|
9
|
+
style: "App Router — Server Components + Server Actions + Client Components"
|
|
10
|
+
key_rules:
|
|
11
|
+
- "Default to Server Components — only add 'use client' when interactivity is needed"
|
|
12
|
+
- "Data fetching happens in Server Components or Server Actions — never in useEffect"
|
|
13
|
+
- "Server Actions handle all mutations (form submits, data writes)"
|
|
14
|
+
- "Data Access Layer (DAL) abstracts all DB/external API calls — used only on server"
|
|
15
|
+
- "Client Components are 'leaf nodes' — keep them small and focused on interactivity"
|
|
16
|
+
- "API Routes (/api) used only for external consumers or webhooks, not for own UI"
|
|
17
|
+
- "Use Next.js cache() and unstable_cache for data memoization"
|
|
18
|
+
folder_structure: |
|
|
19
|
+
app/
|
|
20
|
+
├── (auth)/ ← route group: auth pages
|
|
21
|
+
├── (dashboard)/ ← route group: authenticated pages
|
|
22
|
+
│ └── orders/
|
|
23
|
+
│ ├── page.tsx ← Server Component (list page)
|
|
24
|
+
│ ├── [id]/
|
|
25
|
+
│ │ └── page.tsx ← Server Component (detail page)
|
|
26
|
+
│ └── _components/ ← feature-scoped Client Components
|
|
27
|
+
├── api/ ← API Routes (webhooks, external consumers)
|
|
28
|
+
└── layout.tsx
|
|
29
|
+
lib/
|
|
30
|
+
├── dal/ ← Data Access Layer (server-only DB/API calls)
|
|
31
|
+
│ └── orders.dal.ts
|
|
32
|
+
├── actions/ ← Server Actions
|
|
33
|
+
│ └── order.actions.ts
|
|
34
|
+
└── validations/ ← Zod schemas (shared server+client)
|
|
35
|
+
└── order.schema.ts
|
|
36
|
+
components/
|
|
37
|
+
└── ui/ ← shared primitive Client Components
|
|
38
|
+
|
|
39
|
+
coding_standards:
|
|
40
|
+
naming:
|
|
41
|
+
server_components: "PascalCase, no suffix — co-locate in app/ route folder"
|
|
42
|
+
client_components: "PascalCase + 'use client' at top — in _components/ folders"
|
|
43
|
+
server_actions: "camelCase verb + noun (e.g., createOrder, cancelOrder) in lib/actions/"
|
|
44
|
+
dal_functions: "camelCase (e.g., getOrderById, listOrdersByCustomer) in lib/dal/"
|
|
45
|
+
files:
|
|
46
|
+
page: "app/{route}/page.tsx"
|
|
47
|
+
layout: "app/{route}/layout.tsx"
|
|
48
|
+
server_action: "lib/actions/{domain}.actions.ts"
|
|
49
|
+
dal: "lib/dal/{domain}.dal.ts"
|
|
50
|
+
schema: "lib/validations/{domain}.schema.ts"
|
|
51
|
+
patterns:
|
|
52
|
+
mutations: "Server Actions with useFormState / useTransition"
|
|
53
|
+
data_fetching: "Server Component async/await + fetch with revalidate options"
|
|
54
|
+
client_state: "Zustand or Context API (avoid for server-derived data)"
|
|
55
|
+
forms: "React Hook Form (client) OR native <form> with Server Actions"
|
|
56
|
+
error_handling: "error.tsx (route-level) + try/catch in Server Actions returning {error}"
|
|
57
|
+
loading: "loading.tsx (Suspense boundary) per route segment"
|
|
58
|
+
auth: "next-auth v5 (Auth.js) or middleware-based session check"
|
|
59
|
+
|
|
60
|
+
testing:
|
|
61
|
+
unit: "Vitest + React Testing Library for Client Components"
|
|
62
|
+
integration: "Vitest + MSW for Server Actions"
|
|
63
|
+
e2e: "Playwright — full browser tests"
|
|
64
|
+
patterns:
|
|
65
|
+
- "Test Server Components by rendering with async render utilities"
|
|
66
|
+
- "Mock fetch/DB calls for Server Action tests"
|
|
67
|
+
- "Playwright tests cover full user flows (auth → action → result)"
|
|
68
|
+
- "Use @playwright/test fixtures for auth state reuse"
|
|
69
|
+
|
|
70
|
+
trace_tags:
|
|
71
|
+
implements: "// @trace.implements={UC-ID}-SC{N}"
|
|
72
|
+
source: "// @trace.source=specs/bdd/{domain}/{UC-ID}.feature"
|
|
73
|
+
verifies: "// @trace.verifies={UC-ID}"
|
|
74
|
+
test_type: "// @trace.test_type=unit|e2e"
|