|
| 1 | +import { Router } from 'express'; |
| 2 | + |
| 3 | +import type { SubscriptionService } from './service.js'; |
| 4 | +import { |
| 5 | + createNoteSchema, |
| 6 | + createPriceHistorySchema, |
| 7 | + createSubscriptionSchema, |
| 8 | + updateSubscriptionSchema, |
| 9 | +} from './validation.js'; |
| 10 | + |
| 11 | +type JwtManager = { |
| 12 | + verifyAccessToken(token: string): Promise<{ userId: string; email: string }>; |
| 13 | +}; |
| 14 | + |
| 15 | +// ── 인증 미들웨어 (Ledger 패턴 동일) ───────────────────────────── |
| 16 | + |
| 17 | +function makeAuth(jwtManager: JwtManager) { |
| 18 | + return async ( |
| 19 | + req: Parameters<Router['use']>[0] extends (...args: infer A) => unknown ? A[0] : never, |
| 20 | + res: Parameters<Router['use']>[0] extends (...args: infer A) => unknown ? A[1] : never, |
| 21 | + next: Parameters<Router['use']>[0] extends (...args: infer A) => unknown ? A[2] : never, |
| 22 | + ): Promise<void> => { |
| 23 | + const header = (req as { headers: Record<string, string | undefined> }).headers['authorization']; |
| 24 | + if (!header?.startsWith('Bearer ')) { |
| 25 | + (res as { status: (n: number) => { json: (b: unknown) => void } }) |
| 26 | + .status(401) |
| 27 | + .json({ success: false, error: 'Unauthorized' }); |
| 28 | + return; |
| 29 | + } |
| 30 | + try { |
| 31 | + const token = header.slice(7); |
| 32 | + const payload = await jwtManager.verifyAccessToken(token); |
| 33 | + (req as Record<string, unknown>)['userId'] = payload.userId; |
| 34 | + (next as () => void)(); |
| 35 | + } catch { |
| 36 | + (res as { status: (n: number) => { json: (b: unknown) => void } }) |
| 37 | + .status(401) |
| 38 | + .json({ success: false, error: 'Unauthorized' }); |
| 39 | + } |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function createSubscriptionRouter( |
| 44 | + service: SubscriptionService, |
| 45 | + jwtManager: JwtManager, |
| 46 | +): Router { |
| 47 | + const router = Router(); |
| 48 | + const auth = makeAuth(jwtManager); |
| 49 | + |
| 50 | + // ── 구독 목록 / 생성 ────────────────────────────────────────── |
| 51 | + router.get('/services', auth, async (req, res) => { |
| 52 | + const userId = (req as Record<string, string>)['userId']; |
| 53 | + const items = await service.findAll(userId); |
| 54 | + res.json({ success: true, data: items }); |
| 55 | + }); |
| 56 | + |
| 57 | + router.post('/services', auth, async (req, res) => { |
| 58 | + const userId = (req as Record<string, string>)['userId']; |
| 59 | + const parsed = createSubscriptionSchema.safeParse(req.body); |
| 60 | + if (!parsed.success) { |
| 61 | + res.status(400).json({ success: false, error: parsed.error.message }); |
| 62 | + return; |
| 63 | + } |
| 64 | + const sub = await service.create(userId, parsed.data); |
| 65 | + res.status(201).json({ success: true, data: sub }); |
| 66 | + }); |
| 67 | + |
| 68 | + // ── 구독 상세 / 수정 / 삭제 ────────────────────────────────── |
| 69 | + router.get('/services/:id', auth, async (req, res) => { |
| 70 | + const userId = (req as Record<string, string>)['userId']; |
| 71 | + const sub = await service.findById(userId, req.params['id']); |
| 72 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 73 | + res.json({ success: true, data: sub }); |
| 74 | + }); |
| 75 | + |
| 76 | + router.put('/services/:id', auth, async (req, res) => { |
| 77 | + const userId = (req as Record<string, string>)['userId']; |
| 78 | + const parsed = updateSubscriptionSchema.safeParse(req.body); |
| 79 | + if (!parsed.success) { |
| 80 | + res.status(400).json({ success: false, error: parsed.error.message }); |
| 81 | + return; |
| 82 | + } |
| 83 | + const sub = await service.update(userId, req.params['id'], parsed.data); |
| 84 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 85 | + res.json({ success: true, data: sub }); |
| 86 | + }); |
| 87 | + |
| 88 | + router.delete('/services/:id', auth, async (req, res) => { |
| 89 | + const userId = (req as Record<string, string>)['userId']; |
| 90 | + const ok = await service.delete(userId, req.params['id']); |
| 91 | + if (!ok) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 92 | + res.json({ success: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + // ── 요약 통계 ───────────────────────────────────────────────── |
| 96 | + router.get('/summary', auth, async (req, res) => { |
| 97 | + const userId = (req as Record<string, string>)['userId']; |
| 98 | + const summary = await service.getSummary(userId); |
| 99 | + res.json({ success: true, data: summary }); |
| 100 | + }); |
| 101 | + |
| 102 | + // ── 가격 히스토리 ───────────────────────────────────────────── |
| 103 | + router.post('/services/:id/price', auth, async (req, res) => { |
| 104 | + const userId = (req as Record<string, string>)['userId']; |
| 105 | + const sub = await service.findById(userId, req.params['id']); |
| 106 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 107 | + |
| 108 | + const parsed = createPriceHistorySchema.safeParse(req.body); |
| 109 | + if (!parsed.success) { |
| 110 | + res.status(400).json({ success: false, error: parsed.error.message }); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const history = await service.addPriceHistory(req.params['id'], parsed.data); |
| 115 | + res.status(201).json({ success: true, data: history }); |
| 116 | + }); |
| 117 | + |
| 118 | + router.get('/services/:id/history', auth, async (req, res) => { |
| 119 | + const userId = (req as Record<string, string>)['userId']; |
| 120 | + const sub = await service.findById(userId, req.params['id']); |
| 121 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 122 | + |
| 123 | + const history = await service.getPriceHistory(req.params['id']); |
| 124 | + res.json({ success: true, data: history }); |
| 125 | + }); |
| 126 | + |
| 127 | + // ── 메모 ────────────────────────────────────────────────────── |
| 128 | + router.get('/services/:id/notes', auth, async (req, res) => { |
| 129 | + const userId = (req as Record<string, string>)['userId']; |
| 130 | + const sub = await service.findById(userId, req.params['id']); |
| 131 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 132 | + const notes = await service.getNotes(req.params['id']); |
| 133 | + res.json({ success: true, data: notes }); |
| 134 | + }); |
| 135 | + |
| 136 | + router.post('/services/:id/notes', auth, async (req, res) => { |
| 137 | + const userId = (req as Record<string, string>)['userId']; |
| 138 | + const sub = await service.findById(userId, req.params['id']); |
| 139 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 140 | + const parsed = createNoteSchema.safeParse(req.body); |
| 141 | + if (!parsed.success) { |
| 142 | + res.status(400).json({ success: false, error: parsed.error.message }); |
| 143 | + return; |
| 144 | + } |
| 145 | + const note = await service.addNote(req.params['id'], parsed.data); |
| 146 | + res.status(201).json({ success: true, data: note }); |
| 147 | + }); |
| 148 | + |
| 149 | + router.delete('/services/:id/notes/:noteId', auth, async (req, res) => { |
| 150 | + const userId = (req as Record<string, string>)['userId']; |
| 151 | + const sub = await service.findById(userId, req.params['id']); |
| 152 | + if (!sub) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 153 | + const ok = await service.deleteNote(req.params['id'], req.params['noteId']); |
| 154 | + if (!ok) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 155 | + res.json({ success: true }); |
| 156 | + }); |
| 157 | + |
| 158 | + // ── 누적 결제 금액 ──────────────────────────────────────────── |
| 159 | + router.get('/services/:id/cumulative', auth, async (req, res) => { |
| 160 | + const userId = (req as Record<string, string>)['userId']; |
| 161 | + const result = await service.getCumulative(userId, req.params['id']); |
| 162 | + if (!result) { res.status(404).json({ success: false, error: 'Not found' }); return; } |
| 163 | + res.json({ success: true, data: result }); |
| 164 | + }); |
| 165 | + |
| 166 | + return router; |
| 167 | +} |
0 commit comments