-
Notifications
You must be signed in to change notification settings - Fork 0
/
visaRefunded.ts
71 lines (68 loc) · 1.63 KB
/
visaRefunded.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
66
67
68
69
70
71
import * as z from 'zod'
import { extract } from './extract'
import { Parser } from './parser'
/**
* VISAデビットご利用代金ご返金のお知らせ
*/
const visaRefundedSchema = z.object({
type: z.literal('visaRefunded'),
/**
* ご返金日時
*/
refundedOn: z.string(),
/**
* ご返金額
*/
amount: z.number(),
/**
* 加盟店名
*/
shop: z.string(),
/**
* 取引明細番号
*/
number: z.string(),
})
export type VisaRefunded = z.infer<typeof visaRefundedSchema>
export const visaRefundedParser: Parser<VisaRefunded> = ({ subject, text }) => {
if (
!subject.includes('Visaデビット') ||
!subject.includes('ご利用代金ご返金のお知らせ')
) {
return
}
const refundedOn = extract(
text,
/ご返金日時:(?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2})\s(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})/,
)
.map(
_ =>
`${_.year}-${_.month}-${_.day}T${_.hour}:${_.minute}:${_.second}+09:00`,
)
.pop()
const amount = extract(text, /ご返金額:(?<amount>[\d,]+)円/)
.map(_ => parseInt(_.amount.replace(/,/g, '')))
.filter(_ => !Number.isNaN(_))
.pop()
const shop = extract(text, /加盟店名:(?<shop>.+)/)
.map(_ => _.shop)
.pop()
const number = extract(text, /取引明細番号:(?<number>.+)/)
.map(_ => _.number)
.pop()
if (
typeof refundedOn === 'string' &&
typeof amount === 'number' &&
typeof shop === 'string' &&
typeof number === 'string'
) {
return {
type: 'visaRefunded',
refundedOn,
amount,
shop,
number,
}
}
return
}