1
0

accounts.js 18 KB

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