Skip to content

Instantly share code, notes, and snippets.

@Talon876
Last active January 2, 2021 22:15
Show Gist options
  • Save Talon876/cf214c2a37a932b43fef98c50efcc4c8 to your computer and use it in GitHub Desktop.
Save Talon876/cf214c2a37a932b43fef98c50efcc4c8 to your computer and use it in GitHub Desktop.
Mint Duplicate Transaction Fixer
/*
* This script is meant to be copy/pasted in to the Chrome javascript console while logged in to Mint.
* It can be used to find all of the transactions that you have checked "This is a duplicate" for.
* Once all of these transactions are found, you can then have the script add a 'Duplicate' tag to
* each of the transactions.
*
* This will then allow you to filter out duplicate transactions after exporting them to CSV as tags are included.
*
* You may need to replace the value of the duplicateTagId variable after this comment.
* To find the Duplicate tag id, edit a transaction and view the list of tags.
* If Duplicate is an available tag, inspect the element and look for <input name="tag1041280" type="hidden">.
* The number is the tag id.
* If the Duplicate tag is not available, select Edit Tags and add it. Then inspect it to find its id.
*/
/*
* Handles adding the duplicate tag to the transaction. It should also preserve the transaction being marked as a duplicate.
* This method is probably the most likely to mess something up so execute with caution
*/
var duplicateTagId = 1041280; //look this up in "Manage Your Tags" and inspecting the Duplicate tag field's value attribute
var addDuplicateTag = function(txnId, tagId) {
console.log('Attempting to add duplicate flag ' + tagId + ' to txn ' + txnId);
var tagParamName = 'tag' + tagId;
var params = {
task: 'txnedit',
txnId: '' + txnId + ':0',
duplicate: 'on',
token: sessionStorage.CSRF,
};
params[tagParamName] = 2; //2 is enabled
jQuery.ajax({
url: '/updateTransaction.xevent',
method: 'POST',
data: params,
success: function(data, status, xhr) {
console.log('Updated txn ' + txnId + ': ' + status);
}
});
}
/*
* Handles adding the duplicate tag to a list of transaction ids. Each server hit will be 1 to 1.2 seconds apart.
*/
var fixDuplications = function(dupeIds) {
dupeIds.forEach(function(id, idx) {
setTimeout(function() {
addDuplicateTag(id, duplicateTagId);
}, (1000 * idx) + Math.floor(Math.random()*200));
});
};
/*
* Handles fetching one page of transactions. The parameter values came from inspecting the requests that Mint makes.
*/
var fetchTxns = function(offset, cb, done) {
var page = (offset / 100) + 1
console.log('Fetching transactions ' + offset + ' to ' + (offset+100) + ' (page ' + page + ')');
jQuery.ajax({
dataType: 'json',
url: '/app/getJsonData.xevent',
data: {
offset: offset,
filterType: 'cash',
comparableType: 8,
acctChanged: 'T',
task: 'transactions',
rnd: Math.floor(Math.random()*10000)
},
success: function(data, status, xhr) {
var txns = data.set[0].data;
if (txns.length > 0) {
cb(txns);
} else {
done();
}
}
});
};
/*
* Called after all transactions have been fetched. Prints out all the transaction ids it found that are marked as duplicates.
*/
var duplicatedTxnIds;
var handleDupes = function(dupes) {
var dupeTxnIds = dupes.map(function(txn) { return txn.id });
duplicatedTxnIds = dupeTxnIds;
console.log('Completed! The following is a list of transaction ids that are marked as duplicates.');
console.log('%c The duplicate tag id is hardcoded to ' + duplicateTagId + '. Make sure this is correct for you before continuing beyond this step.', 'color: f00; font-size: 32px');
console.log(duplicatedTxnIds.join(', '));
console.log('Execute fixDuplications(duplicatedTxnIds) to add the Duplicate tag to them.');
console.log('If you want to try fixing only one transaction to make sure it will work, execute fixDuplications([duplicatedTxnIds[0]])');
};
/*
* Kicks off fetching each page of your transactions and builds up a list of ones that have been marked as a duplicate.
* Each page fetch has between a 1 and 2 second delay in it so we don't send too many requests too quickly.
*/
var curOffset = 0;
var dupeTxns = [];
var fetchEmAll = function() {
fetchTxns(curOffset, function(txns) {
txns.forEach(function(txn) {
if (txn.isDuplicate) {
console.log('DUPE: ' + txn.date + ' ' + txn.merchant + ' ' + txn.category + ' ' + txn.amount);
dupeTxns.push(txn);
}
});
curOffset += 100;
setTimeout(fetchEmAll, Math.floor(Math.random()*1000)+1000);
}, function() {
handleDupes(dupeTxns);
});
}
console.log('%c WAIT! - Before you begin, read this', 'color: #f00; font-size: 38px');
console.log('%c This script has not been well tested. Execute at your own risk.', 'color: #f55; font-size: 28px');
console.log('Before starting, ensure that you do not have duplicate transactions hidden. On the transactions tab, select the gear icon and select "Show duplicate transactions". If it says "Hide duplicate transactions", you are good to go.');
console.log('First call fetchEmAll(). This will iterate over each page of your transactions, looking for ones marked as duplicates using the "Mark as duplicate" checkbox. There is a delay between each page fetch so we do not crush Mints server.');
console.log('Once this is completed, more instructions will be printed out and you will have a chance to review before the transactions are updated to have the duplicate tag');
console.log('Type fetchEmAll() and press enter to continue');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment