Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. const request = require('request-promise-native');
  2.  
  3. const appliancesStorageFacade = [];
  4.  
  5. const yaasData = {
  6. client: {
  7. clientID: 'ecxEHXeKZJdo4xhVvEvZPX9uyccRMqCl',
  8. clientSecret: 'rIITEf40Hh8lIbXK'
  9. },
  10. scopes: ['hybris.document_manage', 'hybris.document_view', 'thack.infinitecart_manage']
  11. };
  12.  
  13. const yaasConfig = {
  14. url: 'https://api.us.yaas.io/hybris/document/v1',
  15. tenant: 'inficart',
  16. client: 'inficart.appliancedoc',
  17. type: 'savedAppliances'
  18. };
  19.  
  20. appliancesStorageFacade.getToken = async (yaasData) => {
  21. const { client, tenant, scopes } = yaasData;
  22.  
  23. let requestedScopes = [];
  24.  
  25. if (tenant) {
  26. requestedScopes.push(`hybris.tenant=${tenant}`);
  27. }
  28.  
  29. if (scopes) {
  30. requestedScopes = requestedScopes.concat(scopes);
  31. }
  32. const requestOptions = {
  33. auth: {
  34. username: client.clientID,
  35. password: client.clientSecret
  36. },
  37. form: {
  38. grant_type: 'client_credentials',
  39. scope: requestedScopes.join(' ')
  40. }
  41. };
  42.  
  43. const url = 'https://api.us.yaas.io/hybris/oauth2/v1/token';
  44.  
  45. try {
  46. const token = await request.post(url, requestOptions);
  47. return JSON.parse(token);
  48. }
  49. catch (err) {
  50. throw { status: err.error.status, message: err.error.message };
  51. }
  52. };
  53.  
  54. appliancesStorageFacade.getCartID = async (deviceID, token) => {
  55. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  56. const options = {
  57. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  58. headers: {
  59. Authorization: tokenAuthorization
  60. },
  61. json: true
  62. };
  63.  
  64. try {
  65. const appliances = await request.get(options);
  66. return appliances.cartID;
  67. }
  68. catch (err) {
  69. throw { status: err.error.status, message: err.error.message };
  70. }
  71. };
  72.  
  73. appliancesStorageFacade.registerDevice = async (JSONInfo, cartID) => {
  74. let token = '';
  75.  
  76. try {
  77. token = await appliancesStorageFacade.getToken(yaasData);
  78. }
  79. catch (err) {
  80. throw { status: err.status, message: err.message };
  81. }
  82.  
  83. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  84.  
  85. JSONInfo.cartID = cartID;
  86.  
  87. const options = {
  88. body: JSONInfo,
  89. headers: {
  90. Authorization: tokenAuthorization
  91. },
  92. json: true
  93. };
  94. const uri = `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`;
  95.  
  96. try {
  97. const res = await request.post(uri, options);
  98. return { status: '201', message: 'Device registered successfully' };
  99. }
  100. catch (err) {
  101.  
  102. throw { status: err.error.status, message: err.error.message };
  103.  
  104. }
  105. };
  106.  
  107.  
  108. appliancesStorageFacade.getDevices = async (applianceOwner) => {
  109.  
  110. let token = '';
  111. try {
  112. token = await appliancesStorageFacade.getToken(yaasData);
  113. }
  114. catch(err) {
  115. throw {status: err.error.status, message: err.error.message};
  116. }
  117.  
  118. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  119.  
  120. const options = {
  121. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:${applianceOwner}`,
  122. headers: {
  123. Authorization: tokenAuthorization
  124. },
  125. json: true
  126. };
  127.  
  128. try {
  129. const appliances = await request.get(options);
  130. if(appliances.length === 0) {
  131.  
  132. throw { error: {status: '404', message: 'Given tenant/applianceOWner is not registered in DOCs REPO' }};
  133. }
  134.  
  135. return { status: '200', message: appliances };
  136. }
  137. catch (err) {
  138. if (err.error.status === '304') {
  139. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  140. }
  141. if (err.error.status === '404') {
  142. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  143. }
  144. throw {status: err.error.status, message: err.error.message};
  145. }
  146. };
  147.  
  148. appliancesStorageFacade.getDevice = async (deviceID) => {
  149.  
  150. let token = '';
  151. try {
  152. token = await appliancesStorageFacade.getToken(yaasData);
  153. }
  154. catch(err) {
  155. throw {status: err.error.status, message: err.error.message};
  156. }
  157.  
  158.  
  159. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  160.  
  161. const options = {
  162. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  163. headers: {
  164. Authorization: tokenAuthorization
  165. },
  166. json: true
  167. };
  168.  
  169.  
  170. try {
  171. const appliances = await request.get(options);
  172. return { status: 200, cartID: appliances.cartID };
  173. }
  174. catch (err) {
  175. if (err.error.status === '304') {
  176. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  177. }
  178. if (err.error.status === '404') {
  179. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  180. }
  181. throw { status: err.error.status, message: err.error.message };
  182.  
  183. }
  184. };
  185.  
  186. appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
  187.  
  188. let token = '';
  189. try {
  190. token = await appliancesStorageFacade.getToken(yaasData);
  191. }
  192. catch(err) {
  193. throw {status: err.error.status, message: err.error.message};
  194. }
  195.  
  196. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  197. const options = {
  198. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  199. body: {
  200. cartID: newCartID
  201. },
  202. headers: {
  203. Authorization: tokenAuthorization
  204. },
  205. json: true
  206. };
  207.  
  208. let cartID = '';
  209. try {
  210. cartID = await appliancesStorageFacade.getCartID(deviceID, token);
  211. }
  212. catch (err) {
  213. throw {status: err.error.status, message: err.error.message};
  214. }
  215.  
  216.  
  217. try {
  218. const appliances = await request.put(options);
  219. return { status: appliances.status, cartID: cartID };
  220. }
  221. catch (err) {
  222. if (err.error.status === '404') {
  223. err.error.message = 'The request is about to change a non-existing resource/object';
  224. }
  225. if (err.error.status === '409') {
  226. err.error.message = 'Data modification failed because of a version conflict';
  227. }
  228. throw {status: err.error.status, message: err.error.message};
  229. }
  230. };
  231.  
  232.  
  233. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement