accounts.js 19 KB

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