accounts.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 {get} /accounts/:account_id/entries List account entries
  293. * @apiVersion 1.0.0
  294. * @apiName List entries
  295. * @apiGroup Entries
  296. *
  297. * @apiParam {String} account_id The account id to retrieve
  298. *
  299. * @apiHeader {String} Content-Type application/json
  300. *
  301. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  302. * @apiHeaderExample {string} Authorization header example:
  303. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  304. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  305. * @apiErrorExample AuthenticationFailed:
  306. * HTTP/1.1 401 Not Found
  307. * {
  308. * "message": "Authentication failed"
  309. * }
  310. *
  311. * @apiSuccess (200) {json} entries List of all account entries.
  312. * @apiSuccessExample Success-Response:
  313. * HTTP/1.1 200 OK
  314. * [
  315. * {
  316. * _id: '',
  317. * account_id: '1000',
  318. * type: 'DEPOSIT'
  319. * amount: 1000,
  320. * date: 2015-09-03T10:04:11.481Z
  321. * }
  322. * ]
  323. *
  324. * @apiError (404) {json} AccountNotFound The account can't be found.
  325. * @apiErrorExample AccountNotFound:
  326. * HTTP/1.1 404 Not Found
  327. * {
  328. * "message": "Unknown account"
  329. * }
  330. */
  331. app.get('/api/accounts/:account_id/entries', passport.jwt, AccountController.list_entries);
  332. /**
  333. * @api {post} /accounts/:account_id/entries Create entry
  334. * @apiVersion 1.0.0
  335. * @apiName Create entry
  336. * @apiGroup Entries
  337. *
  338. * @apiParam {String} account_id The account id to populate
  339. * @apiParam {String} amount Amount of the entry
  340. * @apiParam {String} date Date of the bill/deposit
  341. * @apiParam {String} [category] Category id of the bill/deposit
  342. * @apiParam {String} [sub_category] Sub category id of the bill/deposit
  343. * @apiParam {String} [label] A label for the entry
  344. * @apiParamExample {json} Request-Example:
  345. * {
  346. * amount: 1000,
  347. * date: 2015-09-03T10:04:11.481Z
  348. * }
  349. *
  350. * @apiHeader {String} Content-Type application/json
  351. *
  352. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  353. * @apiHeaderExample {string} Authorization header example:
  354. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  355. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  356. * @apiErrorExample AuthenticationFailed:
  357. * HTTP/1.1 401 Not Found
  358. * {
  359. * "message": "Authentication failed"
  360. * }
  361. *
  362. * @apiSuccess (201) {Object} entry The created entry.
  363. * @apiSuccess (201) {Object[]} entries All account's entries
  364. * @apiSuccess (201) {Number} balance The account's total balance
  365. * @apiSuccessExample Success-Response:
  366. * HTTP/1.1 201 Created
  367. * {
  368. * entry: {
  369. * _id: '',
  370. * account_id: '1000',
  371. * type: 'DEPOSIT'
  372. * amount: 1000,
  373. * date: 2015-09-03T10:04:11.481Z
  374. * },
  375. * entries: [
  376. * {
  377. * _id: '',
  378. * account_id: '1000',
  379. * type: 'DEPOSIT'
  380. * amount: 1000,
  381. * date: 2015-09-03T10:04:11.481Z
  382. * }
  383. * ],
  384. * balance: 1000
  385. * }
  386. *
  387. * @apiError (400) {json} BadRequest The user can't be found.
  388. * @apiErrorExample BadRequest:
  389. * HTTP/1.1 400 Bad Request
  390. * [
  391. * {
  392. * "field": "amount",
  393. * "rule": "required",
  394. * "message": "Path `amount` is required."
  395. * }
  396. * ]
  397. *
  398. * @apiError (404) {json} AccountNotFound The account can't be found.
  399. * @apiErrorExample AccountNotFound:
  400. * HTTP/1.1 404 Not Found
  401. * {
  402. * "message": "Unknown account"
  403. * }
  404. */
  405. app.post('/api/accounts/:account_id/entries', passport.jwt, AccountController.add_entry);
  406. /**
  407. * @api {post} /accounts/:account_id/entries/:entry_id Modify entry
  408. * @apiVersion 1.0.0
  409. * @apiName Modify entry
  410. * @apiGroup Entries
  411. *
  412. * @apiParam {String} account_id The owner account
  413. * @apiParam {String} entry_id The entry to modify
  414. * @apiParam {String} amount Amount of the entry
  415. * @apiParam {String} date Date of the bill/deposit
  416. * @apiParam {String} [category] Category id of the bill/deposit
  417. * @apiParam {String} [sub_category] Sub category id of the bill/deposit
  418. * @apiParam {String} [label] A label for the entry
  419. * @apiParamExample {json} Request-Example:
  420. * {
  421. * amount: 1000,
  422. * date: 2015-09-03T10:04:11.481Z
  423. * }
  424. *
  425. * @apiHeader {String} Content-Type application/json
  426. *
  427. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  428. * @apiHeaderExample {string} Authorization header example:
  429. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  430. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  431. * @apiErrorExample AuthenticationFailed:
  432. * HTTP/1.1 401 Not Found
  433. * {
  434. * "message": "Authentication failed"
  435. * }
  436. *
  437. * @apiSuccess (200) {Object} entry The created entry.
  438. * @apiSuccess (200) {Object[]} entries All account's entries
  439. * @apiSuccess (200) {Number} balance The account's total balance
  440. * @apiSuccessExample Success-Response:
  441. * HTTP/1.1 200 OK
  442. * {
  443. * entry: {
  444. * _id: '',
  445. * account_id: '1000',
  446. * type: 'DEPOSIT'
  447. * amount: 1000,
  448. * date: 2015-09-03T10:04:11.481Z
  449. * },
  450. * entries: [
  451. * {
  452. * _id: '',
  453. * account_id: '1000',
  454. * type: 'DEPOSIT'
  455. * amount: 1000,
  456. * date: 2015-09-03T10:04:11.481Z
  457. * }
  458. * ],
  459. * balance: 1000
  460. * }
  461. *
  462. * @apiError (400) {json} BadRequest The user can't be found.
  463. * @apiErrorExample BadRequest:
  464. * HTTP/1.1 400 Bad Request
  465. * [
  466. * {
  467. * "field": "amount",
  468. * "rule": "required",
  469. * "message": "Path `amount` is required."
  470. * }
  471. * ]
  472. *
  473. * @apiError (404) {json} AccountNotFound The account can't be found.
  474. * @apiErrorExample AccountNotFound:
  475. * HTTP/1.1 404 Not Found
  476. * {
  477. * "message": "Unknown account"
  478. * }
  479. * @apiError (404) {json} EntryNotFound The entry can't be found.
  480. * @apiErrorExample AccountNotFound:
  481. * HTTP/1.1 404 Not Found
  482. */
  483. app.put('/api/accounts/:account_id/entries/:entry_id', passport.jwt, AccountController.modify_entry);
  484. /**
  485. * @api {delete} /accounts/:account_id/entries/:entry_id Delete entry
  486. * @apiVersion 1.0.0
  487. * @apiName Delete entry
  488. * @apiGroup Entries
  489. *
  490. * @apiParam {String} account_id The owner account
  491. * @apiParam {String} entry_id The entry to delete
  492. *
  493. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  494. * @apiHeaderExample {string} Authorization header example:
  495. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  496. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  497. * @apiErrorExample AuthenticationFailed:
  498. * HTTP/1.1 401 Not Found
  499. * {
  500. * "message": "Authentication failed"
  501. * }
  502. *
  503. * @apiSuccess (204) -
  504. * @apiSuccessExample Success-Response:
  505. * HTTP/1.1 204 No Content
  506. *
  507. * @apiError (404) {json} AccountNotFound The account can't be found.
  508. * @apiErrorExample AccountNotFound:
  509. * HTTP/1.1 404 Not Found
  510. * {
  511. * "message": "Unknown account"
  512. * }
  513. * @apiError (404) {json} EntryNotFound The entry can't be found.
  514. * @apiErrorExample AccountNotFound:
  515. * HTTP/1.1 404 Not Found
  516. */
  517. app.delete('/api/accounts/:account_id/entries/:entry_id', passport.jwt, AccountController.delete_entry);
  518. app.get('/api/accounts/:account_id/entries', passport.jwt, AccountController.list_entries);
  519. };