-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransferWithdrawn.ts
71 lines (68 loc) · 1.57 KB
/
transferWithdrawn.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'
/**
* 振り込みのご確認
*/
const transferWithdrawnSchema = z.object({
type: z.literal('transferWithdrawn'),
/**
* 振込受付日時
*/
withdrawnOn: z.string(),
/**
* 受付番号
*/
number: z.string(),
/**
* 受取人名
*/
recipient: z.string(),
/**
* 振込金額
*/
amount: z.number(),
})
export type TransferWithdrawn = z.infer<typeof transferWithdrawnSchema>
export const transferWithdrawnParser: Parser<TransferWithdrawn> = ({
subject,
text,
}) => {
if (subject !== '振り込みのご確認') {
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 number = extract(text, /受付番号:(?<number>\d+)/)
.map(_ => _.number)
.pop()
const recipient = extract(text, /受取人名:(?<name>.+)/)
.map(_ => _.name)
.pop()
const amount = extract(text, /振込金額:(?<amount>[\d,]+)円/)
.map(_ => parseInt(_.amount.replace(/,/g, '')))
.filter(_ => !Number.isNaN(_))
.pop()
if (
typeof withdrawnOn === 'string' &&
typeof number === 'string' &&
typeof recipient === 'string' &&
typeof amount === 'number'
) {
return {
type: 'transferWithdrawn',
withdrawnOn,
number,
recipient,
amount,
}
}
return
}