Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 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}`; const options = {
  56. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  57. headers: {
  58. Authorization: tokenAuthorization
  59. },
  60. json: true
  61. };
  62.  
  63. try {
  64. const appliances = await request.get(options);
  65. return appliances.cartID;
  66. }
  67. catch (err) {
  68. throw { status: err.error.status, message: err.error.message };
  69. }
  70. };
  71.  
  72. appliancesStorageFacade.registerDevice = async (JSONInfo, cartID) => {
  73. let token = '';
  74.  
  75. try {
  76. token = await appliancesStorageFacade.getToken(yaasData);
  77. }
  78. catch (err) {
  79. throw { status: err.status, message: err.message };
  80. }
  81.  
  82. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  83.  
  84. JSONInfo.cartID = cartID;
  85.  
  86. const options = {
  87. body: JSONInfo,
  88. headers: {
  89. Authorization: tokenAuthorization
  90. },
  91. json: true
  92. };
  93. const uri = `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`;
  94.  
  95. try {
  96. const res = await request.post(uri, options);
  97. return { status: '200', message: 'Device resgistered properly' };
  98. }
  99. catch (err) {
  100. throw { status: err.error.status, message: err.error.message };
  101. }
  102. };
  103.  
  104.  
  105. appliancesStorageFacade.getDevices = async (applianceOwner) => {
  106.  
  107. let token = '';
  108. try {
  109. token = await appliancesStorageFacade.getToken(yaasData);
  110. }
  111. catch(err) {
  112. throw {status: err.error.status, message: err.error.message};
  113. }
  114.  
  115. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  116.  
  117. const options = {
  118. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:${applianceOwner}`,
  119. headers: {
  120. Authorization: tokenAuthorization
  121. },
  122. json: true
  123. };
  124.  
  125. try {
  126. const appliances = await request.get(options);
  127. if(appliances.length === 0) {
  128.  
  129. throw { error: {status: '404', message: 'Given tenant/applianceOWner is not registered in DOCs REPO' }};
  130. }
  131. return { status: '200', message: appliances };
  132. }
  133. catch (err) {
  134. throw {status: err.error.status, message: err.error.message};
  135. }
  136. };
  137.  
  138. appliancesStorageFacade.getDevice = async (deviceID) => {
  139.  
  140. let token = '';
  141. try {
  142. token = await appliancesStorageFacade.getToken(yaasData);
  143. }
  144. catch(err) {
  145. throw {status: err.error.status, message: err.error.message};
  146. }
  147.  
  148.  
  149. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  150.  
  151. const options = {
  152. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  153. headers: {
  154. Authorization: tokenAuthorization
  155. },
  156. json: true
  157. };
  158.  
  159. try {
  160. const appliances = await request.get(options);
  161. return { status: 200, cartID: appliances.cartID };
  162. }
  163. catch (err) {
  164. throw {status: err.error.status, message: err.error.message};
  165. }
  166. };
  167.  
  168. appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
  169.  
  170. let token = '';
  171. try {
  172. token = await appliancesStorageFacade.getToken(yaasData);
  173. }
  174. catch(err) {
  175. throw {status: err.error.status, message: err.error.message};
  176. }
  177.  
  178. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  179. const options = {
  180. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  181. body: { cartID: Number(newCartID) },
  182. headers: {
  183. Authorization: tokenAuthorization
  184. },
  185. json: true
  186. };
  187.  
  188. let cartID = '';
  189. try {
  190. cartID = await appliancesStorageFacade.getCartID(deviceID, token);
  191. }
  192. catch (err) {
  193. throw {status: err.error.status, message: err.error.message};
  194. }
  195.  
  196.  
  197. try {
  198. const appliances = await request.put(options);
  199. return { status: '200', cartID: cartID };
  200. }
  201. catch (err) {
  202. throw {status: err.error.status, message: err.error.message};
  203. }
  204. };
  205.  
  206.  
  207. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement