-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
150 lines (137 loc) · 4.94 KB
/
importer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use strict";
const block_id = "_spearmint-importer-block";
function insert() {
if (null == document.body) {
return;
}
var current = document.getElementById(block_id);
if (current != null) {
current.remove();
current = null;
}
const importerdiv = document.createElement("div");
importerdiv.id = block_id;
importerdiv.classList.add("spearmint-box");
document.body.appendChild(importerdiv);
const csvtext = document.createElement("textarea");
csvtext.classList.add("spearmint-payload");
csvtext.cols = 64;
csvtext.rows = 16;
csvtext.spellcheck = false;
csvtext.wrap = "off";
importerdiv.appendChild(csvtext);
const csvload = document.createElement("input");
csvload.type = "button";
csvload.value = "Load CSV";
importerdiv.appendChild(csvload);
const csvfile = document.createElement("input");
csvfile.type = "file";
csvfile.accept = ".csv,text/csv";
csvload.addEventListener("click", async function(event) {
csvtext.textContent = await csvfile.files[0].text();
});
importerdiv.appendChild(csvfile);
const launch = document.createElement("input");
launch.type = "button";
launch.value = "Import CSV";
launch.addEventListener("click", do_import);
importerdiv.appendChild(launch);
}
async function do_import(event) {
const intuit_apikey = window.__shellInternal.appExperience.appApiKey;
var categories;
fetch("/pfm/v1/categories", {
credentials: "include",
headers: {
"Accept": "application/json, text/plain, */*",
// "intuit_tid": crypto.randomUUID(),
"Authorization": `Intuit_APIKey intuit_apikey=${intuit_apikey},intuit_apikey_version=1.0`
},
referrer: `${window.origin}/transactions`,
// "method": "GET",
mode: "cors"
}).then(
(response) => response.ok
? response.json()
: Promise.reject(`categories failed to download: ${response.statusText} (${response.status})`)
).then(
(json) => {
categories = json.Category; // array
console.log("categories", json.metadata);
/* example
{
"type": "Category",
"name": "Entertainment",
"depth": 1,
"categoryType": "EXPENSE",
"isBusiness": false,
"isCustom": false,
"isUnassignable": false,
"isUnbudgetable": false,
"isUntrendable": false,
"isIgnored": false,
"isEditable": false,
"isDeleted": false,
"discretionaryType": "DISCRETIONARY",
"metaData": {
"lastUpdatedDate": "2020-11-18T07:31:47Z",
"link": [
{
"otherAttributes": {},
"href": "/v1/categories/90767559_1",
"rel": "self"
}
]
},
"id": "90767559_1"
}
*/
const data = {
"date": "2022-12-14",
"description": "Some words",
"category": {
"id": "90767559_709",
"name": null
},
"accountId": null,
"amount": -12.34,
"parentId": null,
"type": "CashAndCreditTransaction",
"id": null,
"isExpense": true,
"isPending": false,
"isDuplicate": false,
"tagData": null,
"splitData": null,
"manualTransactionType": "CASH",
"checkNumber": null,
"isLinkedToRule": false,
"shouldPullFromAtmWithdrawals": false,
"notes": "Notable words"
};
return fetch("/pfm/v1/transactions", {
credentials: "include",
headers: {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
//"intuit_tid": "47554052-3188-41f0-8c2d-ca1f2ea7434f",
"Authorization": `Intuit_APIKey intuit_apikey=${intuit_apikey},intuit_apikey_version=1.0`
},
referrer: `${window.origin}/transactions`,
body: window.JSON.stringify(data),
method: "POST",
mode: "cors"
});
}
).then(
(response) => response.ok
? response.text()
: Promise.reject(`submission failed: ${response.statusText} (${response.status})`)
).then(
(text) => {
window.alert(`Success\nCSV submitted. Please check transactions page\n${text}`);
return Promise.resolve(text);
}
);
}
insert();