Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.38 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. let token = '';
  115. try {
  116. token = await appliancesStorageFacade.getToken(yaasData);
  117. }
  118. catch (err) {
  119. throw { status: err.error.status, message: err.error.message };
  120. }
  121.  
  122. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  123.  
  124. const options = {
  125. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  126. headers: {
  127. Authorization: tokenAuthorization
  128. },
  129. json: true
  130. };
  131.  
  132. try {
  133. const appliances = await request.delete(options);
  134. return { status: '204', message: 'The request succeeded' };
  135. }
  136. catch (err) {
  137. if (err.error.status === '400') {
  138. err.error.message = 'Parameter is not valid: Parameter is empty or has special characters.';
  139. }
  140. if (err.error.status === '404') {
  141. err.error.message = 'The request is about to delete a non-existing resource/object.';
  142. }
  143. throw { status: err.error.status, message: err.error.message };
  144. }
  145. };
  146.  
  147. appliancesStorageFacade.getDevices = async (applianceOwner) => {
  148.  
  149. let token = '';
  150. try {
  151. token = await appliancesStorageFacade.getToken(yaasData);
  152. }
  153. catch (err) {
  154. throw { status: err.error.status, message: err.error.message };
  155. }
  156.  
  157. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  158.  
  159. const options = {
  160. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:"${applianceOwner}"`,
  161. headers: {
  162. Authorization: tokenAuthorization
  163. },
  164. json: true
  165. };
  166.  
  167. try {
  168. const appliances = await request.get(options);
  169. if (appliances.length === 0) {
  170. throw { error: { status: '404', message: 'Not Found. The server did not find anything matching the Request-URI.' } };
  171. }
  172. return { status: '200', message: 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. appliancesStorageFacade.getDevice = async (deviceID) => {
  186.  
  187. let token = '';
  188. try {
  189. token = await appliancesStorageFacade.getToken(yaasData);
  190. }
  191. catch (err) {
  192. throw { status: err.error.status, message: err.error.message };
  193. }
  194.  
  195. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  196.  
  197. const options = {
  198. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
  199. headers: {
  200. Authorization: tokenAuthorization
  201. },
  202. json: true
  203. };
  204.  
  205. try {
  206. const appliances = await request.get(options);
  207. return { status: '200', device: appliances };
  208. }
  209. catch (err) {
  210. if (err.error.status === '304') {
  211. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  212. }
  213. if (err.error.status === '404') {
  214. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  215. }
  216. throw { status: err.error.status, message: err.error.message };
  217.  
  218. }
  219. };
  220.  
  221. appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
  222.  
  223. let token = '';
  224. try {
  225. token = await appliancesStorageFacade.getToken(yaasData);
  226. }
  227. catch (err) {
  228. throw { status: err.error.status, message: err.error.message };
  229. }
  230.  
  231. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  232. const options = {
  233. uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  234. body: {
  235. cartID: newCartID
  236. },
  237. headers: {
  238. Authorization: tokenAuthorization
  239. },
  240. json: true
  241. };
  242.  
  243. let cartID = '';
  244. try {
  245. cartID = await appliancesStorageFacade.getCartID(deviceID, token);
  246. }
  247. catch (err) {
  248. throw { status: err.error.status, message: err.error.message };
  249. }
  250.  
  251.  
  252. try {
  253. const appliances = await request.put(options);
  254.  
  255. if (appliances.status === '201') {
  256. 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.' };
  257. }
  258. return { status: '200', cartID: cartID };
  259. }
  260. catch (err) {
  261. if (err.error.status === '404') {
  262. err.error.message = 'The request is about to change a non-existing resource/object';
  263. }
  264. if (err.error.status === '409') {
  265. err.error.message = 'Data modification failed because of a version conflict';
  266. }
  267. throw { status: err.error.status, message: err.error.message };
  268. }
  269. };
  270.  
  271. appliancesStorageFacade.checkTenant = async (JSONInfo, token) => {
  272. const tenantInfo = (await appliancesStorageFacade.getTenantInfo(JSONInfo.tenant)).message;
  273.  
  274. if (tenantInfo.length === 0) return false;
  275.  
  276. if (tenantInfo[0].tenant === JSONInfo.tenant) {
  277. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  278. const options = {
  279. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/${tenantInfo[0].id}`,
  280. body: JSONInfo,
  281. headers: {
  282. Authorization: tokenAuthorization
  283. },
  284. json: true
  285. };
  286.  
  287. try {
  288. const response = await request.put(options);
  289. if (response.status === '201') {
  290. 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.' };
  291. }
  292. return { status: '200', message: 'Tenant has been updated properly' };
  293. }
  294. catch (err) {
  295. if (err.error.status === '404') {
  296. err.error.message = 'The request is about to change a non-existing resource/object';
  297. }
  298. if (err.error.status === '409') {
  299. err.error.message = 'Data modification failed because of a version conflict';
  300. }
  301. throw { status: err.error.status, message: err.error.message };
  302. }
  303. }
  304. else {
  305. return false;
  306. }
  307. };
  308.  
  309. appliancesStorageFacade.saveTenantInfo = async (JSONInfo) => {
  310. let token = '';
  311. try {
  312. token = await appliancesStorageFacade.getToken(yaasData);
  313. }
  314. catch (err) {
  315. throw { status: err.error.status, message: err.error.message };
  316. }
  317.  
  318. let check = '';
  319. try {
  320. check = await appliancesStorageFacade.checkTenant(JSONInfo);
  321. }
  322. catch (error) {
  323. throw error;
  324. }
  325. if (check !== false) {
  326. return check;
  327. }
  328.  
  329. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  330. const options = {
  331. body: JSONInfo,
  332. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/`,
  333. headers: {
  334. Authorization: tokenAuthorization
  335. },
  336. json: true
  337. };
  338.  
  339. try {
  340. const res = await request.post(options);
  341. return { status: '201', message: 'Tenant info saved properly' };
  342. }
  343. catch (err) {
  344. throw { status: err.error.status, message: err.error.message };
  345. }
  346. };
  347.  
  348.  
  349. appliancesStorageFacade.getTenantInfo = async (tenant) => {
  350.  
  351. let token = '';
  352. try {
  353. token = await appliancesStorageFacade.getToken(yaasData);
  354. }
  355. catch (err) {
  356. throw { status: err.error.status, message: err.error.message };
  357. }
  358.  
  359. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  360. const options = {
  361. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
  362.  
  363. headers: {
  364. Authorization: tokenAuthorization
  365. },
  366. json: true
  367. };
  368.  
  369.  
  370. try {
  371. const tenantInfo = await request.get(options);
  372. if (tenantInfo.length === 0) {
  373. throw { error: { status: '404' } };
  374. }
  375.  
  376. return { status: '200', message: tenantInfo };
  377. }
  378. catch (err) {
  379. if (err.error.status === '304') {
  380. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  381. }
  382. if (err.error.status === '404') {
  383. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  384. }
  385. throw { status: err.error.status, message: err.error.message };
  386. }
  387. };
  388.  
  389. appliancesStorageFacade.yaasData = yaasData;
  390. appliancesStorageFacade.yaasConfig = yaasConfig;
  391. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement