accounts.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. var mongoose = require('mongoose'),
  2. ObjectId = mongoose.Types.ObjectId,
  3. Account = mongoose.model('Account'),
  4. Entry = mongoose.model('Entry'),
  5. Handler = require('../helpers/handler'),
  6. categories = require('../models/categories');
  7. var load_categories = function(language, callback) {
  8. var catz = {};
  9. categories.forEach(function(category) {
  10. var selected_category = category[language],
  11. key = category.en.categorygroup.toLowerCase().replace(/ /g, '_'),
  12. cat = catz[key] || {key: key, label: selected_category.categorygroup, sub_categories: []};
  13. if( !selected_category.category ) {
  14. cat.sub_categories.push({label: selected_category.subcategory, key: selected_category.subcategory.toLowerCase().replace(/ /g, '_')});
  15. }
  16. catz[key] = cat;
  17. });
  18. callback(null, catz);
  19. };
  20. var check_account = function(request, response, callback) {
  21. Account.findById(request.params.account_id, function(errors, account) {
  22. if( errors ) {
  23. return Handler.errorHandler(errors, 404, response);
  24. }
  25. if( !account ) {
  26. return response.status(404).json({message: 'Unknown account'});
  27. }
  28. if( !account.user_id.equals(request.user.id) ) {
  29. return response.status(401).end();
  30. }
  31. return callback(null, account);
  32. });
  33. };
  34. var delete_account = function(account, callback) {
  35. Entry.find({account_id: account.id}).remove(function(errors) {
  36. if( errors ) {
  37. if( callback ) {
  38. return callback(errors);
  39. }
  40. return;
  41. }
  42. account.remove(function(errors) {
  43. if( errors ) {
  44. if( callback ) {
  45. return callback(errors);
  46. }
  47. return;
  48. }
  49. if( callback ) {
  50. return callback();
  51. }
  52. });
  53. });
  54. };
  55. var get_balance = function(account_id, callback) {
  56. Entry.aggregate()
  57. .match({account_id: new ObjectId(account_id)})
  58. .group({_id : null, balance: { $sum: '$amount' }})
  59. .exec(callback);
  60. };
  61. var list_entries = function(account_id, entry, callback ) {
  62. get_balance(account_id, function(errors, data) {
  63. if( errors ) {
  64. return callback(errors);
  65. }
  66. Entry
  67. .find({account_id: account_id})
  68. .sort({date: -1})
  69. .exec(function(errors, entries) {
  70. if( errors ) {
  71. return callback(errors);
  72. }
  73. return callback( null, {
  74. entry: entry,
  75. entries: entries,
  76. balance: data[0].balance
  77. });
  78. });
  79. });
  80. }
  81. module.exports = {
  82. create : function(request, response) {
  83. var user = request.user,
  84. account = new Account({
  85. name: request.body.name,
  86. reference: request.body.reference,
  87. user_id: user.id
  88. });
  89. load_categories(user.language, function(error, result) {
  90. for( var key in result ) {
  91. account.categories.push( result[key] );
  92. }
  93. account.save(function(errors) {
  94. if( errors ) {
  95. return Handler.errorHandler(errors, 400, response);
  96. }
  97. return response.status(201).json(account);
  98. });
  99. });
  100. },
  101. modify : function(request, response) {
  102. return check_account(request, response, function(error, account) {
  103. account.name = request.body.name;
  104. account.reference = request.body.reference;
  105. account.save(function(errors) {
  106. if( errors ) {
  107. return Handler.errorHandler(errors, 400, response);
  108. }
  109. return response.json(account);
  110. });
  111. });
  112. },
  113. delete : function(request, response) {
  114. return check_account(request, response, function(error, account) {
  115. return delete_account(account, function(errors) {
  116. if( errors ) {
  117. return Handler.errorHandler(errors, 500, response);
  118. }
  119. return response.status(204).end();
  120. });
  121. });
  122. },
  123. delete_account : delete_account,
  124. get : function(request, response) {
  125. return check_account(request, response, function(error, account) {
  126. return response.json(account);
  127. });
  128. },
  129. add_entry : function(request, response) {
  130. return check_account(request, response, function(error, account) {
  131. var data = request.body,
  132. entry = new Entry({
  133. account_id: account.id,
  134. category: data.category ? new ObjectId(data.category) : undefined,
  135. sub_category: data.sub_category ? new ObjectId(data.sub_category) : undefined,
  136. label: data.label,
  137. amount: data.amount,
  138. date: new Date(data.date),
  139. type: data.amount >= 0 ? 'DEPOSIT' : 'BILL'
  140. });
  141. entry.save(function(errors) {
  142. if( errors ) {
  143. return Handler.errorHandler(errors, 400, response);
  144. }
  145. list_entries(account.id, entry, function(errors, data) {
  146. if( errors ) {
  147. return Handler.errorHandler(errors, 500, response);
  148. }
  149. return response.status(201).json(data);
  150. });
  151. });
  152. });
  153. },
  154. modify_entry : function(request, response) {
  155. return check_account(request, response, function(error, account) {
  156. Entry.findById(request.params.entry_id, function(errors, entry) {
  157. if( errors ) {
  158. return Handler.errorHandler(errors, 404, response);
  159. }
  160. if( !entry ) {
  161. return response.status(404).end();
  162. }
  163. if( !entry.account_id.equals( account.id ) ) {
  164. return response.status(401).end();
  165. }
  166. var data = request.body;
  167. entry.category = data.category ? new ObjectId(data.category) : undefined;
  168. entry.sub_category = data.sub_category ? new ObjectId(data.sub_category) : undefined;
  169. entry.label = data.label;
  170. entry.amount = data.amount;
  171. entry.date = new Date(data.date);
  172. entry.type = data.amount >= 0 ? 'DEPOSIT' : 'BILL';
  173. entry.save(function(errors) {
  174. if( errors ) {
  175. return Handler.errorHandler(errors, 400, response );
  176. }
  177. list_entries(account.id, entry, function(errors, data) {
  178. if( errors ) {
  179. return Handler.errorHandler(errors, 500, response);
  180. }
  181. return response.status(200).json(data);
  182. });
  183. });
  184. });
  185. });
  186. },
  187. delete_entry : function(request, response) {
  188. return check_account(request, response, function(errors, account) {
  189. Entry.findById(request.params.entry_id, function(errors, entry) {
  190. if( errors ) {
  191. return Handler.errorHandler(errors, 404, response);
  192. }
  193. if( !entry ) {
  194. return response.status(404).end();
  195. }
  196. if( !entry.account_id.equals( account.id ) ) {
  197. return response.status(401).end();
  198. }
  199. entry.remove(function(errors) {
  200. if( errors ) {
  201. return Handler.errorHandler(errors, 500, response);
  202. }
  203. list_entries(account.id, entry, function(errors, data) {
  204. if( errors ) {
  205. return Handler.errorHandler(errors, 500, response);
  206. }
  207. return response.status(200).json(data);
  208. });
  209. });
  210. });
  211. });
  212. },
  213. list_entries : function(request, response) {
  214. return check_account(request, response, function(errors, account) {
  215. list_entries(account.id, null, function(errors, entries) {
  216. if( errors ) {
  217. return Handler.errorHandler(errors, 500, response);
  218. }
  219. return response.json(entries);
  220. });
  221. });
  222. }
  223. }