1
0

db.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var mongoose = require('mongoose'),
  2. jwt = require('jsonwebtoken'),
  3. security = require('../config/security');
  4. var USER_ID = '55c9e2e3d300cc798928cc87',
  5. HACKER_ID = '55c9e2e4d300cc798928cc88',
  6. ACCOUNT_ID = '55c9e2fcd300cc798928cc8b';
  7. var DATA = {
  8. User: [
  9. {
  10. _id: USER_ID,
  11. username: 'test',
  12. password: 's3cr3t'
  13. },
  14. {
  15. _id: HACKER_ID,
  16. username: 'hacker',
  17. password: 'bl4ckh4t'
  18. }
  19. ],
  20. Account: [
  21. {
  22. _id: ACCOUNT_ID,
  23. name: 'Default',
  24. reference: '1234567890',
  25. user_id: USER_ID,
  26. categories: [{key: 'test', label: 'Test', sub_categories: []}]
  27. }
  28. ],
  29. Entry: [
  30. {
  31. account_id: ACCOUNT_ID,
  32. label: 'Test bill',
  33. type: 'BILL',
  34. amount: -100,
  35. date: '2015-08-13',
  36. }
  37. ]
  38. },
  39. process_collection = function(collection, data, done) {
  40. mongoose.connection.base.models[collection].find({}).remove(function(err) {
  41. if (err) {
  42. console.log('Can\'t delete collection ' + collection, err );
  43. }
  44. var res = [];
  45. for( var item in data ) {
  46. mongoose.connection.base.models[collection].create(data[item], function(err, newItem) {
  47. res.push(err);
  48. if( err ) {
  49. console.log('Can\'t insert document', err);
  50. }
  51. if (res.length === data.length) {
  52. return done();
  53. }
  54. newItem.save(function(error) {
  55. res.push(err);
  56. if (res.length === data.length) {
  57. return done();
  58. }
  59. });
  60. });
  61. }
  62. });
  63. },
  64. drop_collection = function(collection, data, done) {
  65. mongoose.connection.base.models[collection].find({}).remove(function(err) {
  66. if (err) {
  67. console.log('Can\'t delete collection ' + collection, err );
  68. }
  69. return done();
  70. });
  71. };
  72. module.exports = {
  73. USER_ID: USER_ID,
  74. HACKER_ID: HACKER_ID,
  75. ACCOUNT_ID: ACCOUNT_ID,
  76. init : function(done) {
  77. var collections_to_process = Object.keys(DATA).length,
  78. collectionsDone = 0;
  79. for( var collection in DATA ) {
  80. process_collection(collection, DATA[collection], function() {
  81. collectionsDone++;
  82. if( collectionsDone === collections_to_process ) {
  83. done();
  84. }
  85. })
  86. }
  87. },
  88. drop : function(done) {
  89. var collections_to_process = Object.keys(DATA).length,
  90. collectionsDone = 0;
  91. for( var collection in DATA ) {
  92. drop_collection(collection, DATA[collection], function() {
  93. collectionsDone++;
  94. if( collectionsDone === collections_to_process ) {
  95. done();
  96. }
  97. })
  98. }
  99. },
  100. get_user_token: function() {
  101. return jwt.sign( { user_id: USER_ID}, security.jwt.secretOrKey);
  102. },
  103. get_hacker_token: function() {
  104. return jwt.sign( { user_id: HACKER_ID}, security.jwt.secretOrKey);
  105. }
  106. }