accounts.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var mongoose = require('mongoose'),
  2. ObjectId = mongoose.Schema.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. module.exports = {
  56. create : function(request, response) {
  57. var user = request.user,
  58. account = new Account({
  59. name: request.body.name,
  60. reference: request.body.reference,
  61. user_id: user.id
  62. });
  63. load_categories(user.language, function(error, result) {
  64. for( var key in result ) {
  65. account.categories.push( result[key] );
  66. }
  67. account.save(function(errors) {
  68. if( errors ) {
  69. return Handler.errorHandler(errors, 400, response);
  70. }
  71. return response.status(201).json(account);
  72. });
  73. });
  74. },
  75. modify : function(request, response) {
  76. return check_account(request, response, function(error, account) {
  77. account.name = request.body.name;
  78. account.reference = request.body.reference;
  79. account.save(function(errors) {
  80. if( errors ) {
  81. return Handler.errorHandler(errors, 400, response);
  82. }
  83. return response.json(account);
  84. });
  85. });
  86. },
  87. delete : function(request, response) {
  88. return check_account(request, response, function(error, account) {
  89. return delete_account(account, function(errors) {
  90. if( errors ) {
  91. return Handler.errorHandler(errors, 500, response);
  92. }
  93. return response.status(204).end();
  94. });
  95. });
  96. },
  97. delete_account : delete_account,
  98. get : function(request, response) {
  99. return check_account(request, response, function(error, account) {
  100. return response.json(account);
  101. });
  102. },
  103. add_entry : function(request, response) {
  104. return check_account(request, response, function(error, account) {
  105. var data = request.body,
  106. entry = new Entry({
  107. account_id: account.id,
  108. category: data.category ? new ObjectId(data.category) : undefined,
  109. sub_category: data.sub_category ? new ObjectId(data.sub_category) : undefined,
  110. label: data.label,
  111. amount: data.amount,
  112. date: new Date(data.date),
  113. type: data.amount >= 0 ? 'DEPOSIT' : 'BILL'
  114. });
  115. entry.save(function(errors) {
  116. if( errors ) {
  117. return Handler.errorHandler(errors, 400, response);
  118. }
  119. Entry
  120. .find({account_id: account.id})
  121. .sort({date: -1})
  122. .exec(function(errors, entries) {
  123. if( errors ) {
  124. return Handler.errorHandler(errors, 500, response);
  125. }
  126. response.status(201).json({
  127. entry: entry,
  128. entries: entries
  129. });
  130. });
  131. });
  132. });
  133. },
  134. modify_entry : function(request, response) {
  135. return check_account(request, response, function(error, account) {
  136. Entry.findById(request.params.entry_id, function(errors, entry) {
  137. if( errors ) {
  138. return Handler.errorHandler(errors, 404, response);
  139. }
  140. if( !entry ) {
  141. return response.status(404).end();
  142. }
  143. if( !entry.account_id.equals( account.id ) ) {
  144. return response.status(401).end();
  145. }
  146. var data = request.body;
  147. entry.category = data.category ? new ObjectId(data.category) : undefined;
  148. entry.sub_category = data.sub_category ? new ObjectId(data.sub_category) : undefined;
  149. entry.label = data.label;
  150. entry.amount = data.amount;
  151. entry.date = new Date(data.date);
  152. entry.type = data.amount >= 0 ? 'DEPOSIT' : 'BILL';
  153. entry.save(function(errors) {
  154. if( errors ) {
  155. return Handler.errorHandler(errors, 400, response );
  156. }
  157. Entry
  158. .find({account_id: account.id})
  159. .sort({date: -1})
  160. .exec(function(errors, entries) {
  161. if( errors ) {
  162. return Handler.errorHandler(errors, 500, response);
  163. }
  164. response.status(200).json({
  165. entry: entry,
  166. entries: entries
  167. });
  168. });
  169. });
  170. });
  171. });
  172. },
  173. delete_entry : function(request, response) {
  174. return check_account(request, response, function(errors, account) {
  175. Entry.findById(request.params.entry_id, function(errors, entry) {
  176. if( errors ) {
  177. return Handler.errorHandler(errors, 404, response);
  178. }
  179. if( !entry ) {
  180. return response.status(404).end();
  181. }
  182. if( !entry.account_id.equals( account.id ) ) {
  183. return response.status(401).end();
  184. }
  185. entry.remove(function(errors) {
  186. if( errors ) {
  187. return Handler.errorHandler(errors, 500, response);
  188. }
  189. Entry
  190. .find({account_id: account.id})
  191. .sort({date: -1})
  192. .exec(function(errors, entries) {
  193. if( errors ) {
  194. return Handler.errorHandler(errors, 500, response);
  195. }
  196. response.status(204).json({
  197. entry: entry,
  198. entries: entries
  199. });
  200. });
  201. });
  202. });
  203. });
  204. },
  205. list_entries : function(request, response) {
  206. return check_account(request, response, function(errors, account) {
  207. Entry.find({
  208. account_id: account.id
  209. })
  210. .sort('-date')
  211. .exec(function(errors, entries) {
  212. if( errors ) {
  213. return Handler.errorHandler(errors, 500, response);
  214. }
  215. return response.json(entries);
  216. });
  217. });
  218. }
  219. }