accountConfig.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const db = require('../database.js');
  2. const TABLE_NAME = "accountConfig";
  3. function AccountConfigManager() {
  4. }
  5. AccountConfigManager.prototype.defaultForAccount = function(id, cb) {
  6. var self = this;
  7. db.database.queryFirst(TABLE_NAME, { accountId: id, serviceId: null, deviceId: null }, (err, result) => {
  8. cb(result ? self.fromDb(result) : null);
  9. });
  10. };
  11. AccountConfigManager.prototype.fromAccountId = function(id, cb) {
  12. var self = this;
  13. db.database.query(TABLE_NAME, { accountId: id }, (err, result) => {
  14. if (!result)
  15. cb(null);
  16. let accConfigs = [];
  17. result.forEach((res) => {
  18. accConfigs.push(self.fromDb(res));
  19. });
  20. cb(accConfigs);
  21. });
  22. };
  23. AccountConfigManager.prototype.fromAccountIdAndVersion = function(id, v, cb) {
  24. var self = this;
  25. db.database.query(TABLE_NAME, { accountId: id }, (err, result) => {
  26. if (!result)
  27. cb(null);
  28. for (var i =0, nbConfig = result.length; i < nbConfig; i++) {
  29. if (result[i].modified > v) {
  30. var configs = [];
  31. for (i =0; i < nbConfig; i++) {
  32. configs.push(self.fromDb(result[i]));
  33. }
  34. cb(configs);
  35. return;
  36. }
  37. }
  38. cb(null);
  39. });
  40. };
  41. AccountConfigManager.prototype.newConfigFor = function(account) {
  42. var conf = new AccountConfigWrapper();
  43. conf.dirty = true;
  44. conf.accountId = account.id;
  45. return conf;
  46. };
  47. AccountConfig.prototype.addService = function(serviceType, serviceName, serviceId) {
  48. if (!this.services[serviceType])
  49. this.services[serviceType] = {};
  50. this.services[serviceType][serviceId] = serviceName;
  51. };
  52. AccountConfigManager.prototype.fromId = function(id, cb) {
  53. var self = this;
  54. db.database.queryFirst(TABLE_NAME, { id: id }, (err, result) => {
  55. cb(self.fromDb(result));
  56. });
  57. };
  58. AccountConfigManager.prototype.fromDb = function(dbResult) {
  59. if (!dbResult)
  60. return null;
  61. return new AccountConfigWrapper(dbResult);
  62. };
  63. function AccountConfigWrapper(dbResult) {
  64. this.id;
  65. this.accountId;
  66. this.serviceId; // Can be null, or a serviceId
  67. this.deviceId; // Can be null, or a deviceId
  68. this.config = new AccountConfig();
  69. this.modified; // modified ts
  70. if (dbResult) {
  71. this.id = dbResult.id;
  72. this.accountId = dbResult.accountId;
  73. this.serviceId = dbResult.serviceId;
  74. this.deviceId = dbResult.deviceId;
  75. this.config.fromDb(dbResult.config);
  76. this.modified = dbResult.modified;
  77. this.dirty = false;
  78. } else {
  79. this.id = null;
  80. this.accountId = null;
  81. this.serviceId = null;
  82. this.deviceId = null;
  83. this.modified = Date.now();
  84. this.dirty = true;
  85. }
  86. }
  87. AccountConfigWrapper.prototype.toDb = function() {
  88. return {
  89. accountId: this.accountId,
  90. serviceId: this.serviceId,
  91. deviceId: this.deviceId,
  92. config: this.config.toDb(),
  93. modified: this.modified
  94. };
  95. };
  96. AccountConfigManager.prototype.save = function(account, cb) {
  97. if (!account.id) {
  98. // insert and save id
  99. var data = account.toDb();
  100. data.id = null;
  101. db.database.insert(TABLE_NAME, data, (newId) => {
  102. account.id = newId;
  103. account.dirty = false;
  104. if (cb) cb(account);
  105. });
  106. } else if (account.dirty) {
  107. // update
  108. db.database.update(TABLE_NAME, account.id, account.toDb(), () => {
  109. account.dirty = false;
  110. if (cb) cb(account);
  111. });
  112. } else {
  113. // not changed
  114. if (cb) cb(account);
  115. }
  116. };
  117. AccountConfigWrapper.prototype.expose = function() {
  118. return {
  119. service: this.serviceId,
  120. device: this.deviceId,
  121. config: this.config.toDb()
  122. };
  123. };
  124. AccountConfigWrapper.prototype.edit = function() {
  125. this.dirty = true;
  126. return this;
  127. };
  128. function AccountConfig() {
  129. this.services = {};
  130. }
  131. AccountConfig.prototype.toDb = function() {
  132. return JSON.stringify({
  133. services: this.services
  134. });
  135. };
  136. AccountConfig.prototype.fromDb = function(dbObj) {
  137. dbObj = JSON.parse(dbObj);
  138. this.services = dbObj.services || [];
  139. };
  140. module.exports.accountConfigManager = new AccountConfigManager();
  141. module.exports.createTable = function(dbObject, cb) {
  142. console.info("Creating table " +TABLE_NAME);
  143. dbObject.run('CREATE TABLE IF NOT EXISTS `' +TABLE_NAME +'` ('
  144. +"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
  145. +"`accountId` STRING NOT NULL,"
  146. +"`serviceId` STRING,"
  147. +"`deviceId` STRING,"
  148. +"`config` STRING NOT NULL,"
  149. +"`modified` INT NOT NULL"
  150. +');CREATE INDEX accconfigbyaccount ON ' +TABLE_NAME +'(accountId); CREATE UNIQUE INDEX accconfigUniq ON ' +TABLE_NAME +'(accountId, serviceId, deviceId)', cb);
  151. //TODO permanent login token array
  152. };