Skip to content

Commit a484b8b

Browse files
committed
Incomplete Error Path Testing
1 parent 865ee00 commit a484b8b

10 files changed

Lines changed: 4478 additions & 0 deletions

app/backend/ERROR_HANDLING_GUIDE.md

Lines changed: 435 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# State Machine - Quick Reference Card
2+
3+
## 🎯 At a Glance
4+
5+
```
6+
States: DRAFT → PUBLISHED → COMPLETED/CANCELLED
7+
Terminal States: CANCELLED, COMPLETED (no exit!)
8+
Initial State: DRAFT
9+
```
10+
11+
---
12+
13+
## ✅ Valid Transitions Cheat Sheet
14+
15+
```typescript
16+
// ✓ ALLOWED
17+
DRAFTPUBLISHED // Publish event
18+
DRAFTCANCELLED // Cancel before publishing
19+
PUBLISHEDCANCELLED // Cancel live event
20+
PUBLISHEDCOMPLETED // Event ended successfully
21+
22+
// ✗ FORBIDDEN
23+
DRAFTCOMPLETED // Must publish first!
24+
PUBLISHEDDRAFT // Can't go backwards!
25+
CANCELLEDANY // Terminal state!
26+
COMPLETEDANY // Terminal state!
27+
```
28+
29+
---
30+
31+
## 🔑 Key Methods
32+
33+
```typescript
34+
// Inject service
35+
constructor(
36+
private stateMachine: EventStateMachineService
37+
) {}
38+
39+
// Publish
40+
await stateMachine.publishEvent(eventId, user);
41+
42+
// Cancel
43+
await stateMachine.cancelEvent(eventId, user, reason);
44+
45+
// Complete
46+
await stateMachine.completeEvent(eventId, user);
47+
48+
// Check what's possible
49+
const info = stateMachine.getStateMachineInfo(event);
50+
// { currentState: 'draft', validTransitions: ['published', 'cancelled'] }
51+
```
52+
53+
---
54+
55+
## ⚠️ Common Errors
56+
57+
```typescript
58+
InvalidStateTransitionError
59+
// "Invalid state transition from DRAFT to COMPLETED.
60+
// Valid transitions from DRAFT: PUBLISHED, CANCELLED"
61+
62+
TerminalStateError
63+
// "Cannot transition from terminal state COMPLETED"
64+
65+
ForbiddenException
66+
// "Only event organizer or admin can publish events"
67+
68+
BadRequestException
69+
// "Event must have valid start and end dates"
70+
// "End date must be after start date"
71+
// "Event capacity must be greater than 0"
72+
```
73+
74+
---
75+
76+
## 🧪 Testing Quick Start
77+
78+
```typescript
79+
import { EventStatus } from '../entities/event-write.entity';
80+
81+
// Test valid transition
82+
it('should allow DRAFT → PUBLISHED', async () => {
83+
const draftEvent = createMockEvent({ status: EventStatus.DRAFT });
84+
const published = { ...draftEvent, status: EventStatus.PUBLISHED };
85+
86+
const result = await repo.save(published);
87+
expect(result.status).toBe(EventStatus.PUBLISHED);
88+
});
89+
90+
// Test invalid transition
91+
it('should reject DRAFT → COMPLETED', async () => {
92+
const draftEvent = createMockEvent({ status: EventStatus.DRAFT });
93+
const completed = { ...draftEvent, status: EventStatus.COMPLETED };
94+
95+
await expect(repo.save(completed)).rejects.toThrow();
96+
});
97+
```
98+
99+
---
100+
101+
## 📋 Validation Checklist
102+
103+
Before any transition:
104+
- [ ] User is authenticated
105+
- [ ] User has ORGANIZER or ADMIN role
106+
- [ ] Event exists
107+
- [ ] Current state allows target transition
108+
- [ ] Not exiting terminal state
109+
- [ ] All required data present
110+
- [ ] Temporal constraints satisfied
111+
112+
---
113+
114+
## 🎭 State Characteristics
115+
116+
| State | Editable | Visible | Bookable | Terminal |
117+
|-----------|----------|---------|----------|----------|
118+
| DRAFT | ✅ Full | ❌ No | ❌ No | ❌ No |
119+
| PUBLISHED | ⚠️ Limited | ✅ Yes | ✅ Yes | ❌ No |
120+
| CANCELLED | ❌ No | ✅ Org | ❌ No | ✅ Yes |
121+
| COMPLETED | ❌ No | ✅ Yes | ❌ No | ✅ Yes |
122+
123+
---
124+
125+
## 🔐 Permission Quick Ref
126+
127+
| Action | Organizer | Admin | Regular User |
128+
|-----------------|-----------|-------|--------------|
129+
| Create ||||
130+
| Publish ||||
131+
| Cancel ||||
132+
| Complete ||||
133+
| View (any) |||* |
134+
135+
*Published/Completed only for regular users
136+
137+
---
138+
139+
## ⏰ Time-Based Rules
140+
141+
```
142+
Publish: endDate > startDate > now
143+
Complete: now > endDate
144+
Cancel: Anytime before event starts (recommended)
145+
146+
Example:
147+
Start: 2024-01-15 10:00
148+
End: 2024-01-15 18:00
149+
150+
Can Publish: Before 2024-01-15 10:00
151+
Can Complete: After 2024-01-15 18:00
152+
Can Cancel: Before 2024-01-15 10:00 (best practice)
153+
```
154+
155+
---
156+
157+
## 💡 Pro Tips
158+
159+
1. **Always check current state** before attempting transition
160+
2. **Use the service methods** instead of direct entity updates
161+
3. **Handle errors gracefully** with try-catch blocks
162+
4. **Log transitions** for audit trail
163+
5. **Notify affected users** on state changes
164+
6. **Test edge cases** with property-based testing
165+
166+
---
167+
168+
## 📚 Full Documentation
169+
170+
- **Detailed Guide**: `STATE_MACHINE_DOCUMENTATION.md`
171+
- **Visual Guide**: `STATE_MACHINE_VISUAL_GUIDE.md`
172+
- **Implementation Summary**: `TASK1_STATE_TRANSITION_SUMMARY.md`
173+
- **Test File**: `test/state-machine.transition.spec.ts`
174+
- **Service**: `src/events/services/event-state-machine.service.ts`
175+
176+
---
177+
178+
## 🆘 Need Help?
179+
180+
Common issues and solutions:
181+
182+
**Problem**: "Invalid state transition" error
183+
**Solution**: Check current state with `getStateMachineInfo()`
184+
185+
**Problem**: Can't complete event
186+
**Solution**: Verify `endTime < now`
187+
188+
**Problem**: Can't cancel event
189+
**Solution**: Check if already in terminal state
190+
191+
**Problem**: Authorization error
192+
**Solution**: Ensure user has ORGANIZER or ADMIN role
193+
194+
---
195+
196+
Keep this card handy when working with event states! 🚀

0 commit comments

Comments
 (0)