Transaction.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Format from "../Format.js";
  2. import Notifier from "../Notifier.js";
  3. export default class Transaction{
  4. constructor(parent, id, iv, date, data){
  5. this._parent = parent;
  6. this._id = id;
  7. this._iv = iv;
  8. this._date = date;
  9. this._category = data.category;
  10. this._categoryId = data.categoryId;
  11. this._amount = data.amount;
  12. this._tags = data.tags;
  13. this._location = data.location;
  14. this._note = data.note;
  15. }
  16. get date(){
  17. return this._date;
  18. }
  19. get category(){
  20. return this._parent.getCategory(this._category, this._categoryId);
  21. }
  22. get location(){
  23. return this._location;
  24. }
  25. get amount(){
  26. return Format.centsToDollars(this._amount);
  27. }
  28. static create(account, date, amount, tags, location, note, category){
  29. let categoryId = null;
  30. if(category !== "discretionary"){
  31. let categorySplit = category.split(":");
  32. category = categorySplit[0];
  33. categoryId = categorySplit[1];
  34. }
  35. return new Transaction(
  36. account,
  37. null,
  38. encryptionHandler.generateIv(),
  39. Format.transactionDate(date),
  40. {
  41. category: category,
  42. categoryId: categoryId,
  43. amount: Format.dollarsToCents(amount),
  44. tags: tags.split(","),
  45. location: location,
  46. note: note
  47. }
  48. );
  49. }
  50. static async fetch(account, from, to){
  51. let response;
  52. try{
  53. response = await fetch("/api/transaction/search", {
  54. method: "POST",
  55. headers: {"Content-Type": "application/json"},
  56. body: JSON.stringify({account, from, to})
  57. });
  58. response = await response.json();
  59. const promises = [];
  60. for(let i = 0; i < response.length; i++){
  61. promises.push(Transaction.decrypt(response[i]));
  62. }
  63. return await Promise.all(promises);
  64. }catch(e){
  65. new Notifier("error", "Something went wrong, try refreshing the page");
  66. }
  67. if(response.error){
  68. new Notifier("error", response.error.message);
  69. return;
  70. }
  71. }
  72. static async decrypt(transaction){
  73. let data = await encryptionHandler.decrypt(transaction.data, transaction.iv);
  74. return new Transaction(
  75. user.account,
  76. transaction.id,
  77. transaction.iv,
  78. transaction.date,
  79. data
  80. );
  81. }
  82. serialize(){
  83. return {
  84. category: this._category,
  85. categoryId: this._categoryId,
  86. amount: this._amount,
  87. tags: this._tags,
  88. location: this._location,
  89. note: this._note
  90. };
  91. }
  92. async save(isNew = false){
  93. const data = await encryptionHandler.encrypt(this.serialize(), this._iv);
  94. let method, body;
  95. if(isNew){
  96. method = "POST";
  97. body = {
  98. account: this._parent.id,
  99. date: this._date,
  100. data: data,
  101. iv: this._iv
  102. };
  103. }
  104. fetch("/api/transaction", {
  105. method: method,
  106. headers: {"Content-Type": "application/json"},
  107. body: JSON.stringify(body)
  108. })
  109. .then(r=>r.json())
  110. .then((response)=>{
  111. if(response.error){
  112. new Notifier("error", response.error.message);
  113. this._parent.removeTransaction(this);
  114. }else{
  115. this._id = response.id;
  116. }
  117. })
  118. .catch((err)=>{
  119. new Notifier("error", "Something went wrong, try refreshing the page");
  120. this._parent.removeTransaction(this);
  121. });
  122. }
  123. }