Created
May 27, 2022 22:32
-
-
Save framp/e940c0400179c6bc24191ace9235dc61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update all your invoices on FreeAgent to reverse charge, even if they're already reconciled with your bank account | |
// Follow https://dev.freeagent.com/docs/quick_start and use the Google OAuth 2.0 Playground to login into your company account. | |
// Remove .sandbox from links | |
// Get a fresh token and save it here | |
const TOKEN = '' | |
// Manually ignore some invoices by reference | |
const ignoreList = [].map(String) | |
const superagent = require("superagent") | |
// Download a list of invoices and explanation from the API | |
const { bank_transaction_explanations } = require("./explanations.json") | |
const { invoices } = require("./invoices.json") | |
let c = 0 | |
const makeCall = async (method, url, body) => { | |
console.log(method, url, body) | |
let client = superagent[method.toLowerCase()](url) | |
if (body) { | |
client = client.send(body) | |
} | |
client.set("user-agent", "Framp") | |
client.set("accept", "application/json") | |
client.set("content-type", "application/json") | |
client.set( | |
"authorization", | |
`Bearer ${TOKEN}` | |
) | |
return client | |
} | |
const main = async () => { | |
for (const i of invoices) { | |
console.log( | |
i.reference, | |
i.contact_name, | |
i.dated_on, | |
i.total_value, | |
i.ec_status, | |
i.status | |
) | |
if (ignoreList.includes(i.reference)) { | |
console.log("skip\n") | |
continue | |
} | |
if (i.ec_status === "EC Services" || i.ec_status === "EC VAT MOSS") { | |
console.log("skip\n") | |
continue | |
} | |
c++ | |
let explanation | |
if (i.status === "Paid") { | |
explanation = bank_transaction_explanations.find( | |
({ paid_invoice }) => paid_invoice === i.url | |
) | |
await makeCall("DELETE", explanation.url) | |
} | |
await makeCall("PUT", i.url + "/transitions/mark_as_draft") | |
await makeCall("PUT", i.url, { invoice: { ec_status: "EC Services" } }) | |
await makeCall("PUT", i.url + "/transitions/mark_as_sent") | |
if (i.status === "Paid") { | |
explanation.ec_status = "EC Services" | |
await makeCall( | |
"POST", | |
"https://api.freeagent.com/v2/bank_transaction_explanations", | |
{ bank_transaction_explanation: explanation } | |
) | |
} | |
} | |
console.log(c) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment