factories.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. angular.module('odoo.factories', ['xml-rpc'])
  2. .factory('odoo', function (xmlrpc, methodCallManager) {
  3. return {
  4. auth: function (config, success, error) {
  5. xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {
  6. if (!response || response['faultCode']) {
  7. error(response);
  8. return;
  9. }
  10. success({ id: response, username: config.username, password: config.password });
  11. });
  12. },
  13. fetch: function (model, domain, config, success, error) {
  14. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
  15. if (!response || response['faultCode']) {
  16. error(response);
  17. return;
  18. }
  19. success(response);
  20. }, function (err) {
  21. error(err);
  22. });
  23. }
  24. }
  25. })
  26. .factory('methodCallManager', function(xmlrpc) {
  27. return {
  28. call: function(methodName, configuration) {
  29. var hostName = configuration.host + ':' + configuration.port;
  30. if(!hostName.startsWith('http://')) {
  31. hostName = 'http://' + hostName;
  32. }
  33. if(methodName == 'authenticate') {
  34. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  35. } else {
  36. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  37. }
  38. return methodName;
  39. }
  40. }
  41. })
  42. .factory('storage', function () {
  43. return {
  44. // Customer
  45. saveCustomer(data, success, error) {
  46. var sql = 'INSERT INTO customer(remote_id, name, phone) VALUES(?, ?, ?)';
  47. db.executeSql(sql, data, function(result) {
  48. success(result.insertId);
  49. }, function(err) {
  50. error(err);
  51. });
  52. },
  53. // Users
  54. saveUser: function(data, success, error) {
  55. var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)';
  56. db.executeSql(sql, data, function (result) {
  57. success(result.insertId);
  58. }, function (err) {
  59. error(err);
  60. });
  61. },
  62. // Utils
  63. get: function(tableName, success, error) {
  64. var sql = 'SELECT * FROM ' + tableName;
  65. db.executeSql(sql, [], function(result) {
  66. success(result.rows);
  67. }, function(err) {
  68. error(err);
  69. });
  70. },
  71. count: function(tableName, success, error) {
  72. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  73. db.executeSql(sql, [], function(result) {
  74. success(result.rows.item(0).total);
  75. }, function(err) {
  76. error(err);
  77. });
  78. }
  79. }
  80. });