accounts.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. var passport = require('../security/passport'),
  2. AccountController = require('../controllers/accounts');
  3. module.exports = function(app) {
  4. /**
  5. * @api {get} /accounts List accounts
  6. * @apiVersion 1.0.0
  7. * @apiName Retrieve accounts
  8. * @apiGroup Accounts
  9. *
  10. * @apiHeader {String} Content-Type application/json
  11. *
  12. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  13. * @apiHeaderExample {string} Authorization header example:
  14. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  15. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  16. * @apiErrorExample AuthenticationFailed:
  17. * HTTP/1.1 401 Not Found
  18. * {
  19. * "message": "Authentication failed"
  20. * }
  21. *
  22. * @apiSuccess (200) {json} accounts List of all accounts and their (sub)categories.
  23. * @apiSuccessExample Success-Response:
  24. * HTTP/1.1 200 OK
  25. * [{
  26. * "name": "Home",
  27. * "reference": "1234567890",
  28. * "user_id": "55e6e4e005230f49271c7078",
  29. * "_id": "55e8218912c65a1730c34858",
  30. * "created_at": "2015-09-03T10:31:37.889Z",
  31. * "categories": [
  32. * {
  33. * "key": "alimony_payments",
  34. * "label": "Alimony Payments",
  35. * "_id": "55e8218912c65a1730c34859",
  36. * "sub_categories": []
  37. * },
  38. * {
  39. * "key": "automobile_expenses",
  40. * "label": "Automobile Expenses",
  41. * "_id": "55e8218912c65a1730c3485a",
  42. * "sub_categories": [
  43. * {
  44. * "label": "Car Payment",
  45. * "key": "car_payment",
  46. * "_id": "55e8218912c65a1730c3485d"
  47. * }
  48. * ]
  49. * }
  50. * ]
  51. * }]
  52. */
  53. app.get('/api/accounts', passport.jwt, AccountController.retrieve_accounts);
  54. /**
  55. * @api {post} /accounts Create account
  56. * @apiVersion 1.0.0
  57. * @apiName Create account
  58. * @apiGroup Accounts
  59. *
  60. * @apiParam {String} name Name for the new account
  61. * @apiParam {String} reference A reference (bank account number) for the new account
  62. * @apiParamExample {json} Request-Example:
  63. * {
  64. * name: 'Home',
  65. * reference: '1234567890'
  66. * }
  67. *
  68. * @apiHeader {String} Content-Type application/json
  69. *
  70. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  71. * @apiHeaderExample {string} Authorization header example:
  72. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  73. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  74. * @apiErrorExample AuthenticationFailed:
  75. * HTTP/1.1 401 Not Found
  76. * {
  77. * "message": "Authentication failed"
  78. * }
  79. *
  80. * @apiSuccess (201) {Object} account The new account with its (sub)categories.
  81. * @apiSuccessExample Success-Response:
  82. * HTTP/1.1 201 Created
  83. * {
  84. * "name": "Home",
  85. * "reference": "1234567890",
  86. * "user_id": "55e6e4e005230f49271c7078",
  87. * "_id": "55e8218912c65a1730c34858",
  88. * "created_at": "2015-09-03T10:31:37.889Z",
  89. * "categories": [
  90. * {
  91. * "key": "alimony_payments",
  92. * "label": "Alimony Payments",
  93. * "_id": "55e8218912c65a1730c34859",
  94. * "sub_categories": []
  95. * },
  96. * {
  97. * "key": "automobile_expenses",
  98. * "label": "Automobile Expenses",
  99. * "_id": "55e8218912c65a1730c3485a",
  100. * "sub_categories": [
  101. * {
  102. * "label": "Car Payment",
  103. * "key": "car_payment",
  104. * "_id": "55e8218912c65a1730c3485d"
  105. * }
  106. * ]
  107. * }
  108. * ]
  109. * }
  110. *
  111. * @apiError (400) {json} BadRequest The user can't be found.
  112. *
  113. * @apiErrorExample BadRequest:
  114. * HTTP/1.1 400 Bad Request
  115. * [
  116. * {
  117. * "field": "name",
  118. * "rule": "required",
  119. * "message": "Path `name` is required."
  120. * }
  121. * ]
  122. *
  123. */
  124. app.post('/api/accounts', passport.jwt, AccountController.create);
  125. /**
  126. * @api {delete} /accounts/:account_id Delete account
  127. * @apiVersion 1.0.0
  128. * @apiName Delete account
  129. * @apiGroup Accounts
  130. *
  131. * @apiParam {String} account_id The account to delete
  132. *
  133. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  134. * @apiHeaderExample {string} Authorization header example:
  135. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  136. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  137. * @apiErrorExample AuthenticationFailed:
  138. * HTTP/1.1 401 Not Found
  139. * {
  140. * "message": "Authentication failed"
  141. * }
  142. *
  143. * @apiSuccess (204) -
  144. * @apiSuccessExample Success-Response:
  145. * HTTP/1.1 204 No Content
  146. *
  147. * @apiError (404) {json} AccountNotFound The account can't be found.
  148. * @apiErrorExample AccountNotFound:
  149. * HTTP/1.1 404 Not Found
  150. * {
  151. * "message": "Unknown account"
  152. * }
  153. */
  154. app.delete('/api/accounts/:account_id', passport.jwt, AccountController.delete);
  155. /**
  156. * @api {get} /accounts/:account_id Get account
  157. * @apiVersion 1.0.0
  158. * @apiName Get account
  159. * @apiGroup Accounts
  160. *
  161. * @apiParam {String} account_id The given account
  162. *
  163. * @apiHeader {String} Content-Type application/json
  164. *
  165. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  166. * @apiHeaderExample {string} Authorization header example:
  167. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  168. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  169. * @apiErrorExample AuthenticationFailed:
  170. * HTTP/1.1 401 Not Found
  171. * {
  172. * "message": "Authentication failed"
  173. * }
  174. *
  175. * @apiSuccess (200) {Object} account The account with its (sub)categories.
  176. * @apiSuccessExample Success-Response:
  177. * HTTP/1.1 200 OK
  178. * {
  179. * "name": "Home",
  180. * "reference": "1234567890",
  181. * "user_id": "55e6e4e005230f49271c7078",
  182. * "_id": "55e8218912c65a1730c34858",
  183. * "created_at": "2015-09-03T10:31:37.889Z",
  184. * "categories": [
  185. * {
  186. * "key": "alimony_payments",
  187. * "label": "Alimony Payments",
  188. * "_id": "55e8218912c65a1730c34859",
  189. * "sub_categories": []
  190. * },
  191. * {
  192. * "key": "automobile_expenses",
  193. * "label": "Automobile Expenses",
  194. * "_id": "55e8218912c65a1730c3485a",
  195. * "sub_categories": [
  196. * {
  197. * "label": "Car Payment",
  198. * "key": "car_payment",
  199. * "_id": "55e8218912c65a1730c3485d"
  200. * }
  201. * ]
  202. * }
  203. * ]
  204. * }
  205. *
  206. * @apiError (404) {json} AccountNotFound The account can't be found.
  207. * @apiErrorExample AccountNotFound:
  208. * HTTP/1.1 404 Not Found
  209. * {
  210. * "message": "Unknown account"
  211. * }
  212. */
  213. app.get('/api/accounts/:account_id', passport.jwt, AccountController.get);
  214. /**
  215. * @api {put} /accounts/:account_id Modify account
  216. * @apiVersion 1.0.0
  217. * @apiName Modify account
  218. * @apiGroup Accounts
  219. *
  220. * @apiParam {String} account_id The account id to modify
  221. * @apiParam {String} name Name for the new account
  222. * @apiParam {String} reference A reference (bank account number) for the new account
  223. * @apiParamExample {json} Request-Example:
  224. * {
  225. * name: 'Home',
  226. * reference: '1234567890'
  227. * }
  228. *
  229. * @apiHeader {String} Content-Type application/json
  230. *
  231. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  232. * @apiHeaderExample {string} Authorization header example:
  233. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  234. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  235. * @apiErrorExample AuthenticationFailed:
  236. * HTTP/1.1 401 Not Found
  237. * {
  238. * "message": "Authentication failed"
  239. * }
  240. *
  241. * @apiSuccess {String} username Username of the User.
  242. * @apiSuccess {String} token The JWT valid token.
  243. * @apiSuccessExample Success-Response:
  244. * HTTP/1.1 200 OK
  245. * {
  246. * "name": "Home",
  247. * "reference": "1234567890",
  248. * "user_id": "55e6e4e005230f49271c7078",
  249. * "_id": "55e8218912c65a1730c34858",
  250. * "created_at": "2015-09-03T10:31:37.889Z",
  251. * "categories": [
  252. * {
  253. * "key": "alimony_payments",
  254. * "label": "Alimony Payments",
  255. * "_id": "55e8218912c65a1730c34859",
  256. * "sub_categories": []
  257. * },
  258. * {
  259. * "key": "automobile_expenses",
  260. * "label": "Automobile Expenses",
  261. * "_id": "55e8218912c65a1730c3485a",
  262. * "sub_categories": [
  263. * {
  264. * "label": "Car Payment",
  265. * "key": "car_payment",
  266. * "_id": "55e8218912c65a1730c3485d"
  267. * }
  268. * ]
  269. * }
  270. * ]
  271. * }
  272. *
  273. * @apiError (400) {json} BadRequest The user can't be found.
  274. * @apiErrorExample BadRequest:
  275. * HTTP/1.1 400 Bad Request
  276. * [
  277. * {
  278. * "field": "name",
  279. * "rule": "required",
  280. * "message": "Path `name` is required."
  281. * }
  282. * ]
  283. * @apiError (404) {json} AccountNotFound The account can't be found.
  284. * @apiErrorExample AccountNotFound:
  285. * HTTP/1.1 404 Not Found
  286. * {
  287. * "message": "Unknown account"
  288. * }
  289. */
  290. app.put('/api/accounts/:account_id', passport.jwt, AccountController.modify);
  291. /**
  292. * @api {post} /accounts/:account_id/entries Create entry
  293. * @apiVersion 1.0.0
  294. * @apiName Create entry
  295. * @apiGroup Entries
  296. *
  297. * @apiParam {String} account_id The account id to populate
  298. * @apiParam {String} amount Amount of the entry
  299. * @apiParam {String} date Date of the bill/deposit
  300. * @apiParam {String} [category] Category id of the bill/deposit
  301. * @apiParam {String} [sub_category] Sub category id of the bill/deposit
  302. * @apiParam {String} [label] A label for the entry
  303. * @apiParamExample {json} Request-Example:
  304. * {
  305. * amount: 1000,
  306. * date: 2015-09-03T10:04:11.481Z
  307. * }
  308. *
  309. * @apiHeader {String} Content-Type application/json
  310. *
  311. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  312. * @apiHeaderExample {string} Authorization header example:
  313. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  314. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  315. * @apiErrorExample AuthenticationFailed:
  316. * HTTP/1.1 401 Not Found
  317. * {
  318. * "message": "Authentication failed"
  319. * }
  320. *
  321. * @apiSuccess (201) {Object} entry The created entry.
  322. * @apiSuccess (201) {Object[]} entries All account's entries
  323. * @apiSuccess (201) {Number} balance The account's total balance
  324. * @apiSuccessExample Success-Response:
  325. * HTTP/1.1 201 Created
  326. * {
  327. * entry: {
  328. * _id: '',
  329. * account_id: '1000',
  330. * type: 'DEPOSIT'
  331. * amount: 1000,
  332. * date: 2015-09-03T10:04:11.481Z
  333. * },
  334. * entries: [
  335. * {
  336. * _id: '',
  337. * account_id: '1000',
  338. * type: 'DEPOSIT'
  339. * amount: 1000,
  340. * date: 2015-09-03T10:04:11.481Z
  341. * }
  342. * ],
  343. * balance: 1000
  344. * }
  345. *
  346. * @apiError (400) {json} BadRequest The user can't be found.
  347. * @apiErrorExample BadRequest:
  348. * HTTP/1.1 400 Bad Request
  349. * [
  350. * {
  351. * "field": "amount",
  352. * "rule": "required",
  353. * "message": "Path `amount` is required."
  354. * }
  355. * ]
  356. *
  357. * @apiError (404) {json} AccountNotFound The account can't be found.
  358. * @apiErrorExample AccountNotFound:
  359. * HTTP/1.1 404 Not Found
  360. * {
  361. * "message": "Unknown account"
  362. * }
  363. */
  364. app.post('/api/accounts/:account_id/entries', passport.jwt, AccountController.add_entry);
  365. /**
  366. * @api {post} /accounts/:account_id/entries/:entry_id Modify entry
  367. * @apiVersion 1.0.0
  368. * @apiName Modify entry
  369. * @apiGroup Entries
  370. *
  371. * @apiParam {String} account_id The owner account
  372. * @apiParam {String} entry_id The entry to modify
  373. * @apiParam {String} amount Amount of the entry
  374. * @apiParam {String} date Date of the bill/deposit
  375. * @apiParam {String} [category] Category id of the bill/deposit
  376. * @apiParam {String} [sub_category] Sub category id of the bill/deposit
  377. * @apiParam {String} [label] A label for the entry
  378. * @apiParamExample {json} Request-Example:
  379. * {
  380. * amount: 1000,
  381. * date: 2015-09-03T10:04:11.481Z
  382. * }
  383. *
  384. * @apiHeader {String} Content-Type application/json
  385. *
  386. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  387. * @apiHeaderExample {string} Authorization header example:
  388. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  389. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  390. * @apiErrorExample AuthenticationFailed:
  391. * HTTP/1.1 401 Not Found
  392. * {
  393. * "message": "Authentication failed"
  394. * }
  395. *
  396. * @apiSuccess (200) {Object} entry The created entry.
  397. * @apiSuccess (200) {Object[]} entries All account's entries
  398. * @apiSuccess (200) {Number} balance The account's total balance
  399. * @apiSuccessExample Success-Response:
  400. * HTTP/1.1 200 OK
  401. * {
  402. * entry: {
  403. * _id: '',
  404. * account_id: '1000',
  405. * type: 'DEPOSIT'
  406. * amount: 1000,
  407. * date: 2015-09-03T10:04:11.481Z
  408. * },
  409. * entries: [
  410. * {
  411. * _id: '',
  412. * account_id: '1000',
  413. * type: 'DEPOSIT'
  414. * amount: 1000,
  415. * date: 2015-09-03T10:04:11.481Z
  416. * }
  417. * ],
  418. * balance: 1000
  419. * }
  420. *
  421. * @apiError (400) {json} BadRequest The user can't be found.
  422. * @apiErrorExample BadRequest:
  423. * HTTP/1.1 400 Bad Request
  424. * [
  425. * {
  426. * "field": "amount",
  427. * "rule": "required",
  428. * "message": "Path `amount` is required."
  429. * }
  430. * ]
  431. *
  432. * @apiError (404) {json} AccountNotFound The account can't be found.
  433. * @apiErrorExample AccountNotFound:
  434. * HTTP/1.1 404 Not Found
  435. * {
  436. * "message": "Unknown account"
  437. * }
  438. * @apiError (404) {json} EntryNotFound The entry can't be found.
  439. * @apiErrorExample AccountNotFound:
  440. * HTTP/1.1 404 Not Found
  441. */
  442. app.put('/api/accounts/:account_id/entries/:entry_id', passport.jwt, AccountController.modify_entry);
  443. /**
  444. * @api {delete} /accounts/:account_id/entries/:entry_id Delete entry
  445. * @apiVersion 1.0.0
  446. * @apiName Delete entry
  447. * @apiGroup Entries
  448. *
  449. * @apiParam {String} account_id The owner account
  450. * @apiParam {String} entry_id The entry to delete
  451. *
  452. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  453. * @apiHeaderExample {string} Authorization header example:
  454. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  455. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  456. * @apiErrorExample AuthenticationFailed:
  457. * HTTP/1.1 401 Not Found
  458. * {
  459. * "message": "Authentication failed"
  460. * }
  461. *
  462. * @apiSuccess (204) -
  463. * @apiSuccessExample Success-Response:
  464. * HTTP/1.1 204 No Content
  465. *
  466. * @apiError (404) {json} AccountNotFound The account can't be found.
  467. * @apiErrorExample AccountNotFound:
  468. * HTTP/1.1 404 Not Found
  469. * {
  470. * "message": "Unknown account"
  471. * }
  472. * @apiError (404) {json} EntryNotFound The entry can't be found.
  473. * @apiErrorExample AccountNotFound:
  474. * HTTP/1.1 404 Not Found
  475. */
  476. app.delete('/api/accounts/:account_id/entries/:entry_id', passport.jwt, AccountController.delete_entry);
  477. app.get('/api/accounts/:account_id/entries', passport.jwt, AccountController.list_entries);
  478. };