forked from jy617lee/functional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.ts
65 lines (56 loc) · 1.46 KB
/
3.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 데이터
interface Subscriber {
email: string,
rec_count: number
}
interface Coupon {
code: string,
rank: COUPON_RANK
}
enum COUPON_RANK {
BAD = 'BAD',
GOOD = 'GOOD',
BEST = 'BEST'
}
interface Email {
from: string,
to: string,
subject: string
body: string
}
const EMAIL_CONTENTS: {[key in COUPON_RANK]?: { subject: string, body: string }} = {
GOOD: {
subject: 'good subject',
body: 'good body'
},
BEST: {
subject: 'best subject',
body: 'best body'
}
}
const COMPANY_EMAIL = '[email protected]'
// 계산
const getCouponRank = (recCount: number): COUPON_RANK => {
if (recCount >= 10) return COUPON_RANK.BEST
if (recCount > 0) return COUPON_RANK.GOOD
return COUPON_RANK.BAD
}
const getCouponByRank = (coupons: Coupon[], rank: COUPON_RANK) => {
return coupons.filter(coupon => coupon.rank === rank)
}
const getEmailForSubscriber = (subscriber: Subscriber, coupons: Coupon[]): Email => {
const rank = getCouponRank(subscriber.rec_count)
const couponsForSubscriber = getCouponByRank(coupons, rank)
return {
from: COMPANY_EMAIL,
to: subscriber.email,
subject: `Your weekly ${rank} coupon`,
body: `Here are ${rank} coupons: ${couponsForSubscriber.join(', ')}`
}
}
// 액션
const sendEmail = async () => {
const coupons: Coupon[] = fetchCouponsFromDb()
const subscribers: Subscriber[] = fetchSubscribersFromDb()
subscribers.forEach(subscriber => sendEmail(getEmailForSubscriber(subscriber, coupons)))
}