accounts.js 9.5 KB

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