-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (109 loc) · 4.16 KB
/
index.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
const axios = require("axios");
require("dotenv").config();
const FormData = require("form-data");
// =======================================
// START OF DATA TO BE ADDED BY THE USER
// =======================================
// The SILVERFIN_TOKEN is a valid access token for the Silverfin API that's created through another application like Postman before running the script
// The SILVERFIN_FIRM_ID is the id of the firm that can be found in the URL of the Silverfin web application (e.g. https://live.getsilverfin.com/f/13827/)
// RECONCILIATION_HANDLES is an comma delimited string of reconciliation handles that should be starred
const { SILVERFIN_TOKEN, SILVERFIN_FIRM_ID, RECONCILIATION_HANDLES } =
process.env;
const reconciliationHandles = RECONCILIATION_HANDLES.split(",");
// =======================================
// END OF DATA TO BE ADDED BY THE USER
// =======================================
// Create an axios instance with the base URL and the authorization header that should be used for each Silverfin API request
const defaultHeaders = {
Authorization: `Bearer ${SILVERFIN_TOKEN}`,
Accept: "application/json",
};
const axiosInstance = axios.create({
baseURL: "https://live.getsilverfin.com/api/v4",
Accept: "application/json",
headers: {
...defaultHeaders,
},
});
const starReconciliation = async () => {
try {
// Loop through each company
let page = 1;
let perPage = 10;
let companies = [];
let countCompanies = 0;
do {
companies = await axiosInstance.get(`/f/${SILVERFIN_FIRM_ID}/companies`, {
params: {
page,
per_page: perPage,
},
});
for (let company of companies.data) {
console.log("====================================");
console.log(`Processing company ${company.name}`);
countCompanies++;
// Run through each period of the company
const periods = await axiosInstance.get(
`/f/${SILVERFIN_FIRM_ID}/companies/${company.id}/periods`
);
for (let period of periods.data) {
console.log(`Processing period ${period.end_date}`);
let reconsPage = 1;
let reconsPerPage = 30;
let reconciliations = [];
const reconsToStar = [];
// Search through the reconciliations for each period to find the reconciliation with a specific handle
do {
reconciliations = await axiosInstance.get(
`/f/${SILVERFIN_FIRM_ID}/companies/${company.id}/periods/${period.id}/reconciliations`,
{
params: {
page: reconsPage,
per_page: reconsPerPage,
},
}
);
reconciliations.data.map((recon) => {
if (reconciliationHandles.includes(recon.handle)) {
// console.log(recon);
reconsToStar.push(recon);
}
});
reconsPage++;
} while (reconciliations.data.length === reconsPerPage);
// Star the reconciliation in non-locked ledgers
if (reconsToStar.length > 0) {
for (let starredRecon of reconsToStar) {
let data = new FormData();
data.append("starred", "true");
const response = await axiosInstance.post(
`/f/${SILVERFIN_FIRM_ID}/companies/${company.id}/periods/${period.id}/reconciliations/${starredRecon.id}`,
data,
{
headers: {
"Content-Type": "multipart/form-data",
...defaultHeaders,
},
}
);
console.log(
`Set starred status of reconciliation '${starredRecon.handle}' to ${response.data.starred}`
);
}
}
console.log("");
}
}
page++;
} while (companies.data.length === perPage);
console.log(
`====================================\n${countCompanies} companies processed`
);
} catch (error) {
console.log("====================================");
console.log(error);
console.log("An error occured");
}
};
starReconciliation();