Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 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.  
  132. return { status: '200', message: appliances };
  133. }
  134. catch (err) {
  135. throw {status: err.error.status, message: err.error.message};
  136. }
  137. };
  138.  
  139. appliancesStorageFacade.getDevice = async (deviceID) => {
  140.  
  141. let token = '';
  142. try {
  143. token = await appliancesStorageFacade.getToken(yaasData);
  144. }
  145. catch(err) {
  146. throw {status: err.error.status, message: err.error.message};
  147. }
  148.  
  149.  
  150. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  151.  
  152. const options = {
  153. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  154. headers: {
  155. Authorization: tokenAuthorization
  156. },
  157. json: true
  158. };
  159.  
  160. const checkChar = function (id) {
  161. return /^[0-9a-zA-Z]+$/.test(id);
  162. };
  163.  
  164. try {
  165. if (checkChar(deviceID)) {
  166. const appliances = await request.get(options);
  167. return { status: 200, cartID: appliances.cartID };
  168. }
  169. throw { error: {status: 404, message: 'ID is not proper' }};
  170. }
  171. catch (err) {
  172. throw {status: err.error.status, message: err.error.message};
  173. }
  174. };
  175.  
  176. appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
  177.  
  178. let token = '';
  179. try {
  180. token = await appliancesStorageFacade.getToken(yaasData);
  181. }
  182. catch(err) {
  183. throw {status: err.error.status, message: err.error.message};
  184. }
  185.  
  186. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  187. const options = {
  188. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  189. body: { cartID: Number(newCartID) },
  190. headers: {
  191. Authorization: tokenAuthorization
  192. },
  193. json: true
  194. };
  195.  
  196. let cartID = '';
  197. try {
  198. cartID = await appliancesStorageFacade.getCartID(deviceID, token);
  199. }
  200. catch (err) {
  201. throw {status: err.error.status, message: err.error.message};
  202. }
  203.  
  204.  
  205. try {
  206. const appliances = await request.put(options);
  207. return { status: '200', cartID: cartID };
  208. }
  209. catch (err) {
  210. throw {status: err.error.status, message: err.error.message};
  211. }
  212. };
  213.  
  214.  
  215. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement