Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 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. const yaasConfigBuilderModule = {
  21. url: 'https://api.us.yaas.io/hybris/document/v1',
  22. tenant: 'inficart',
  23. client: 'inficart.appliancedoc',
  24. type: 'builderModule'
  25. };
  26.  
  27. appliancesStorageFacade.getToken = async (yaasData) => {
  28. const { client, tenant, scopes } = yaasData;
  29.  
  30. let requestedScopes = [];
  31.  
  32. if (tenant) {
  33. requestedScopes.push(`hybris.tenant=${tenant}`);
  34. }
  35.  
  36. if (scopes) {
  37. requestedScopes = requestedScopes.concat(scopes);
  38. }
  39. const requestOptions = {
  40. auth: {
  41. username: client.clientID,
  42. password: client.clientSecret
  43. },
  44. form: {
  45. grant_type: 'client_credentials',
  46. scope: requestedScopes.join(' ')
  47. }
  48. };
  49.  
  50. const url = 'https://api.us.yaas.io/hybris/oauth2/v1/token';
  51.  
  52. try {
  53. const token = await request.post(url, requestOptions);
  54. return JSON.parse(token);
  55. }
  56. catch (err) {
  57. throw { status: err.error.status, message: err.error.message };
  58. }
  59. };
  60.  
  61. appliancesStorageFacade.getCartID = async (deviceID, token) => {
  62. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  63. const options = {
  64. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  65. headers: {
  66. Authorization: tokenAuthorization
  67. },
  68. json: true
  69. };
  70.  
  71. try {
  72. const appliances = await request.get(options);
  73. return appliances.cartID;
  74. }
  75. catch (err) {
  76. throw { status: err.error.status, message: err.error.message };
  77. }
  78. };
  79.  
  80. appliancesStorageFacade.registerDevice = async (JSONInfo, cartID) => {
  81.  
  82. let token = '';
  83. try {
  84. token = await appliancesStorageFacade.getToken(yaasData);
  85. }
  86. catch (err) {
  87. throw { status: err.error.status, message: err.error.message };
  88. }
  89.  
  90. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  91.  
  92. JSONInfo.cartID = cartID;
  93.  
  94. const options = {
  95. body: JSONInfo,
  96. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`,
  97. headers: {
  98. Authorization: tokenAuthorization
  99. },
  100. json: true
  101. };
  102.  
  103. try {
  104. const res = await request.post(options);
  105. return { status: '201', message: 'Device registered successfully' };
  106. }
  107. catch (err) {
  108. throw { status: err.error.status, message: err.error.message };
  109. }
  110. };
  111.  
  112.  
  113. appliancesStorageFacade.unregisterDevice = async (deviceID) => {
  114.  
  115. let token = '';
  116. try {
  117. token = await appliancesStorageFacade.getToken(yaasData);
  118. }
  119. catch (err) {
  120. throw { status: err.error.status, message: err.error.message };
  121. }
  122.  
  123. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  124.  
  125. const options = {
  126. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  127. headers: {
  128. Authorization: tokenAuthorization
  129. },
  130. json: true
  131. };
  132.  
  133. try {
  134. const appliances = await request.delete(options);
  135. return { status: '204', message: 'The request succeeded' };
  136. }
  137. catch (err) {
  138. if (err.error.status === '400') {
  139. err.error.message = 'Parameter is not valid: Parameter is empty or has special characters.';
  140. }
  141. if (err.error.status === '404') {
  142. err.error.message = 'The request is about to delete a non-existing resource/object.';
  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, device: appliances };
  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.getDevices = async (applianceOwner) => {
  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.  
  198. const options = {
  199. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:"${applianceOwner}"`,
  200. headers: {
  201. Authorization: tokenAuthorization
  202. },
  203. json: true
  204. };
  205.  
  206. try {
  207. const appliances = await request.get(options);
  208. if (appliances.length === 0) {
  209. throw { error: { status: '404', message: 'Not Found. The server did not find anything matching the Request-URI.' } };
  210. }
  211. return { status: '200', message: appliances };
  212. }
  213. catch (err) {
  214. if (err.error.status === '304') {
  215. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  216. }
  217. if (err.error.status === '404') {
  218. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  219. }
  220. throw { status: err.error.status, message: err.error.message };
  221. }
  222. };
  223.  
  224. appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
  225.  
  226. let token = '';
  227. try {
  228. token = await appliancesStorageFacade.getToken(yaasData);
  229. }
  230. catch (err) {
  231. throw { status: err.error.status, message: err.error.message };
  232. }
  233.  
  234. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  235. const options = {
  236. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  237. body: {
  238. cartID: newCartID
  239. },
  240. headers: {
  241. Authorization: tokenAuthorization
  242. },
  243. json: true
  244. };
  245.  
  246. let cartID = '';
  247. try {
  248. cartID = await appliancesStorageFacade.getCartID(deviceID, token);
  249. }
  250. catch (err) {
  251. throw { status: err.error.status, message: err.error.message };
  252. }
  253.  
  254.  
  255. try {
  256. const appliances = await request.put(options);
  257.  
  258. if (appliances.status === '201') {
  259. return { status: appliances.status, message: 'The request has been fulfilled and a new resource has been created. Returned if upsert==true and the resource did not exist.' };
  260. }
  261. return { status: '200', cartID: cartID };
  262. }
  263. catch (err) {
  264. if (err.error.status === '404') {
  265. err.error.message = 'The request is about to change a non-existing resource/object';
  266. }
  267. if (err.error.status === '409') {
  268. err.error.message = 'Data modification failed because of a version conflict';
  269. }
  270. throw { status: err.error.status, message: err.error.message };
  271. }
  272. };
  273.  
  274. appliancesStorageFacade.saveTenantInfo = async (JSONInfo) => {
  275.  
  276. let token = '';
  277. try {
  278. token = await appliancesStorageFacade.getToken(yaasData);
  279. }
  280. catch (err) {
  281. throw { status: err.error.status, message: err.error.message };
  282. }
  283.  
  284. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  285.  
  286. const options = {
  287. body: JSONInfo,
  288. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/`,
  289. headers: {
  290. Authorization: tokenAuthorization
  291. },
  292. json: true
  293. };
  294.  
  295. try {
  296. const res = await request.post(options);
  297. return { status: '201', message: 'Tenant info saved properly' };
  298. }
  299. catch (err) {
  300. throw { status: err.error.status, message: err.error.message };
  301. }
  302. };
  303.  
  304.  
  305. appliancesStorageFacade.getTenantInfo = async (tenant) => {
  306.  
  307. let token = '';
  308. try {
  309. token = await appliancesStorageFacade.getToken(yaasData);
  310. }
  311. catch (err) {
  312. throw { status: err.error.status, message: err.error.message };
  313. }
  314.  
  315.  
  316. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  317.  
  318. const options = {
  319.         uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
  320.         headers: {
  321.             Authorization: tokenAuthorization
  322.         },
  323.         json: true
  324.     };
  325.  
  326.  
  327. try {
  328. const tenantInfo = await request.get(options);
  329. return { status: '200', tenant: tenantInfo};
  330. }
  331. catch (err) {
  332. if (err.error.status === '304') {
  333. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  334. }
  335. if (err.error.status === '404') {
  336. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  337. }
  338. throw { status: err.error.status, message: err.error.message };
  339.  
  340. }
  341. };
  342.  
  343.  
  344. appliancesStorageFacade.yaasData = yaasData;
  345. appliancesStorageFacade.yaasConfig = yaasConfig;
  346. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement