-
Notifications
You must be signed in to change notification settings - Fork 0
/
visaWithdrawn.ts
85 lines (82 loc) · 1.91 KB
/
visaWithdrawn.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as z from 'zod'
import { extract } from './extract'
import { Parser } from './parser'
/**
* VISAデビットご利用代金お引き落としのお知らせ
*/
const visaWithdrawnSchema = z.object({
type: z.literal('visaWithdrawn'),
/**
* お引落日時
*/
withdrawnOn: z.string(),
/**
* ご利用日
*/
useOfDate: z.string().optional(),
/**
* お引落金額
*/
amount: z.number(),
/**
* 加盟店名
*/
shop: z.string(),
/**
* 取引明細番号
*/
number: z.string(),
})
export type VisaWithdrawn = z.infer<typeof visaWithdrawnSchema>
export const visaWithdrawnParser: Parser<VisaWithdrawn> = ({
subject,
text,
}) => {
if (
!subject.includes('Visaデビット') ||
!subject.includes('ご利用代金お引き落としのお知らせ')
) {
return
}
const withdrawnOn = 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 useOfDate = extract(
text,
/ご利用日:(?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2})/,
)
.map(_ => `${_.year}-${_.month}-${_.day}`)
.pop()
const number = extract(text, /取引明細番号:(?<number>.+)/)
.map(_ => _.number)
.pop()
if (
typeof withdrawnOn === 'string' &&
typeof amount === 'number' &&
typeof shop === 'string' &&
typeof number === 'string'
) {
return {
type: 'visaWithdrawn',
withdrawnOn,
useOfDate,
amount,
shop,
number,
}
}
return
}