1
0

accounts.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. retrieve_accounts : function(request, response) {
  102. Account
  103. .find({user_id: request.user.id})
  104. .sort({name: 1})
  105. .exec(function(errors, accounts) {
  106. if( errors ) {
  107. Handler.errorHandler(errors, 400, response);
  108. }
  109. return response.json(accounts);
  110. });
  111. },
  112. modify : function(request, response) {
  113. return check_account(request, response, function(error, account) {
  114. account.name = request.body.name;
  115. account.reference = request.body.reference;
  116. account.save(function(errors) {
  117. if( errors ) {
  118. return Handler.errorHandler(errors, 400, response);
  119. }
  120. return response.json(account);
  121. });
  122. });
  123. },
  124. delete : function(request, response) {
  125. return check_account(request, response, function(error, account) {
  126. return delete_account(account, function(errors) {
  127. if( errors ) {
  128. return Handler.errorHandler(errors, 500, response);
  129. }
  130. return response.status(204).end();
  131. });
  132. });
  133. },
  134. delete_account : delete_account,
  135. get : function(request, response) {
  136. return check_account(request, response, function(error, account) {
  137. return response.json(account);
  138. });
  139. },
  140. add_entry : function(request, response) {
  141. return check_account(request, response, function(error, account) {
  142. var data = request.body,
  143. entry = new Entry({
  144. account_id: account.id,
  145. category: data.category ? new ObjectId(data.category) : undefined,
  146. sub_category: data.sub_category ? new ObjectId(data.sub_category) : undefined,
  147. label: data.label,
  148. amount: data.amount,
  149. date: new Date(data.date),
  150. type: data.amount >= 0 ? 'DEPOSIT' : 'BILL'
  151. });
  152. entry.save(function(errors) {
  153. if( errors ) {
  154. return Handler.errorHandler(errors, 400, response);
  155. }
  156. list_entries(account.id, entry, function(errors, data) {
  157. if( errors ) {
  158. return Handler.errorHandler(errors, 500, response);
  159. }
  160. return response.status(201).json(data);
  161. });
  162. });
  163. });
  164. },
  165. modify_entry : function(request, response) {
  166. return check_account(request, response, function(error, account) {
  167. Entry.findById(request.params.entry_id, function(errors, entry) {
  168. if( errors ) {
  169. return Handler.errorHandler(errors, 404, response);
  170. }
  171. if( !entry ) {
  172. return response.status(404).end();
  173. }
  174. if( !entry.account_id.equals( account.id ) ) {
  175. return response.status(401).end();
  176. }
  177. var data = request.body;
  178. entry.category = data.category ? new ObjectId(data.category) : undefined;
  179. entry.sub_category = data.sub_category ? new ObjectId(data.sub_category) : undefined;
  180. entry.label = data.label;
  181. entry.amount = data.amount;
  182. entry.date = new Date(data.date);
  183. entry.type = data.amount >= 0 ? 'DEPOSIT' : 'BILL';
  184. entry.save(function(errors) {
  185. if( errors ) {
  186. return Handler.errorHandler(errors, 400, response );
  187. }
  188. list_entries(account.id, entry, function(errors, data) {
  189. if( errors ) {
  190. return Handler.errorHandler(errors, 500, response);
  191. }
  192. return response.status(200).json(data);
  193. });
  194. });
  195. });
  196. });
  197. },
  198. delete_entry : function(request, response) {
  199. return check_account(request, response, function(errors, account) {
  200. Entry.findById(request.params.entry_id, function(errors, entry) {
  201. if( errors ) {
  202. return Handler.errorHandler(errors, 404, response);
  203. }
  204. if( !entry ) {
  205. return response.status(404).end();
  206. }
  207. if( !entry.account_id.equals( account.id ) ) {
  208. return response.status(401).end();
  209. }
  210. entry.remove(function(errors) {
  211. if( errors ) {
  212. return Handler.errorHandler(errors, 500, response);
  213. }
  214. list_entries(account.id, entry, function(errors, data) {
  215. if( errors ) {
  216. return Handler.errorHandler(errors, 500, response);
  217. }
  218. return response.status(200).json(data);
  219. });
  220. });
  221. });
  222. });
  223. },
  224. list_entries : function(request, response) {
  225. return check_account(request, response, function(errors, account) {
  226. list_entries(account.id, null, function(errors, entries) {
  227. if( errors ) {
  228. return Handler.errorHandler(errors, 500, response);
  229. }
  230. return response.json(entries);
  231. });
  232. });
  233. }
  234. }