-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
96 lines (83 loc) · 2.49 KB
/
sync.js
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
86
87
88
89
90
91
92
93
94
95
96
require("dotenv").config();
const { Client } = require("@notionhq/client");
const csv = require("csvtojson/v2");
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
const listFund = async () => {
const resp = await notion.databases.query({
database_id: process.env.FUND_DB_ID,
});
return resp.results
.map((item) => ({
[item.properties["名称"]["title"][0]["plain_text"]]: item.id,
}))
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
};
const toInt = (value) => parseInt(value, 10);
const parseBuyDate = (transactionDate) => {
var date = transactionDate.split(" ")[0];
var parts = date.split("/");
return new Date(
toInt(parts[2]),
toInt(parts[0]) - 1,
toInt(parts[1])
).toISOString();
};
const parseBuyDetails = async () => {
const details = await csv().fromFile(process.env.FILE_PATH);
return details
.filter((item) => item["商品名称"].endsWith("买入"))
.map((item) => ({
fund: item["商品名称"].split("-")[1],
amount: parseFloat(item["金额(元)"]),
date: parseBuyDate(item["交易创建时间"]),
}));
};
const syncToNotion = async (buyDetails, funds) => {
let success = 0;
let fail = 0;
buyDetails.forEach(async (buy) => {
try {
await notion.pages.create({
parent: {
type: "database_id",
database_id: process.env.FUND_BUY_DB_ID,
},
properties: {
基金: { relation: [{ id: funds[buy.fund] }] },
金额: { number: buy.amount },
类型: { select: { name: "定投" } },
日期: { date: { start: buy.date, end: null, time_zone: null } },
},
});
success++;
} catch (error) {
fail++;
console.log(`Failed to process ${buy.date} ${buy.fund} ${buy.amount}`);
}
});
console.log(
`Total record count: ${buyDetails.length}, success: ${success}, failed: ${fail}`
);
};
const printSummary = (buyDetails) => {
const total = buyDetails
.map((item) => item.amount)
.reduce((acc, curr) => acc + curr, 0);
const totalByDate = buyDetails.reduce((acc, curr) => {
acc[curr.date] = curr.amount + (acc[curr.date] || 0);
return acc;
}, {});
console.log("Summary:");
Object.entries(totalByDate).forEach(([date, amount]) =>
console.log(` ${date}: ¥${amount}`)
);
console.log(` Total: ${total}`);
};
(async () => {
const funds = await listFund();
const buyDetails = await parseBuyDetails();
syncToNotion(buyDetails, funds);
printSummary(buyDetails);
})();