Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.53 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.status, message: err.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. headers: {
  97. Authorization: tokenAuthorization
  98. },
  99. json: true
  100. };
  101. const uri = `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`;
  102.  
  103. try {
  104. const res = await request.post(uri, 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.getDevices = async (applianceOwner) => {
  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. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  159.  
  160. const options = {
  161. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:"${applianceOwner}"`,
  162. headers: {
  163. Authorization: tokenAuthorization
  164. },
  165. json: true
  166. };
  167.  
  168. try {
  169. const appliances = await request.get(options);
  170. if (appliances.length === 0) {
  171. throw { error: { status: '404', message: 'Not Found. The server did not find anything matching the Request-URI.' } };
  172. }
  173. return { status: '200', message: appliances };
  174. }
  175. catch (err) {
  176. if (err.error.status === '304') {
  177. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  178. }
  179. if (err.error.status === '404') {
  180. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  181. }
  182. throw { status: err.error.status, message: err.error.message };
  183. }
  184. };
  185.  
  186. appliancesStorageFacade.getDevice = async (deviceID) => {
  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.  
  197. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  198.  
  199. const options = {
  200. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  201. headers: {
  202. Authorization: tokenAuthorization
  203. },
  204. json: true
  205. };
  206.  
  207.  
  208. try {
  209. const appliances = await request.get(options);
  210. return { status: 200, device: appliances };
  211. }
  212. catch (err) {
  213. if (err.error.status === '304') {
  214. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  215. }
  216. if (err.error.status === '404') {
  217. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  218. }
  219. throw { status: err.error.status, message: err.error.message };
  220.  
  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.checkTenant = async (JSONInfo) => {
  275. const tenantInfo = await appliancesStorageFacade.getTenantInfo(JSONInfo.tenant);
  276.  
  277. if(tenantInfo.tenant.length === 0) return false;
  278.  
  279. if (tenantInfo.tenant[0].tenant === JSONInfo.tenant) {
  280. let token = '';
  281. try {
  282. token = await appliancesStorageFacade.getToken(yaasData);
  283. }
  284. catch (err) {
  285. throw { status: err.error.status, message: err.error.message };
  286. }
  287. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  288. const options = {
  289. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/${tenantInfo.tenant[0].id}`,
  290. body: JSONInfo,
  291. headers: {
  292. Authorization: tokenAuthorization
  293. },
  294. json: true
  295. };
  296. try {
  297. const response = await request.put(options);
  298. if (response.status === '201') {
  299. return { status: response.status, message: 'The request has been fulfilled and a new resource has been created. Returned if upsert==true and the resource did not exist.' };
  300. }
  301. return { status: '200', message: 'Tenant has been updated properly' };
  302. }
  303. catch (err) {
  304. if (err.error.status === '404') {
  305. err.error.message = 'The request is about to change a non-existing resource/object';
  306. }
  307. if (err.error.status === '409') {
  308. err.error.message = 'Data modification failed because of a version conflict';
  309. }
  310. throw { status: err.error.status, message: err.error.message };
  311. }
  312. }
  313. else {
  314. return false;
  315. }
  316.  
  317.  
  318. };
  319.  
  320.  
  321. appliancesStorageFacade.saveTenantInfo = async (JSONInfo) => {
  322.  
  323. let check = '';
  324. try {
  325. check = await appliancesStorageFacade.checkTenant(JSONInfo);
  326. }
  327. catch (error) {
  328. throw error;
  329. }
  330. if (check !== false) {
  331. return check;
  332. }
  333.  
  334. let token = '';
  335. try {
  336. token = await appliancesStorageFacade.getToken(yaasData);
  337. }
  338. catch (err) {
  339. throw { status: err.status, message: err.message };
  340. }
  341.  
  342. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  343.  
  344.  
  345. const options = {
  346. body: JSONInfo,
  347. headers: {
  348. Authorization: tokenAuthorization
  349. },
  350. json: true
  351. };
  352. const uri = `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/`;
  353.  
  354.  
  355. try {
  356. const res = await request.post(uri, options);
  357. return { status: '201', message: 'Tenant info saved properly' };
  358. }
  359. catch (err) {
  360. throw { status: err.error.status, message: err.error.message };
  361. }
  362. };
  363.  
  364.  
  365. appliancesStorageFacade.getTenantInfo = async (tenant) => {
  366.  
  367. let token = '';
  368. try {
  369. token = await appliancesStorageFacade.getToken(yaasData);
  370. }
  371. catch (err) {
  372. throw { status: err.error.status, message: err.error.message };
  373. }
  374.  
  375.  
  376. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  377.  
  378. const options = {
  379. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
  380.  
  381. headers: {
  382. Authorization: tokenAuthorization
  383. },
  384. json: true
  385. };
  386.  
  387.  
  388. try {
  389. const tenantInfo = await request.get(options);
  390. return { status: 200, tenant: tenantInfo };
  391. }
  392. catch (err) {
  393. if (err.error.status === '304') {
  394. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  395. }
  396. if (err.error.status === '404') {
  397. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  398. }
  399. throw { status: err.error.status, message: err.error.message };
  400.  
  401. }
  402. };
  403.  
  404.  
  405.  
  406. appliancesStorageFacade.yaasData = yaasData;
  407. appliancesStorageFacade.yaasConfig = yaasConfig;
  408. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement