Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 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.saveTenantInfo = async (JSONInfo) => {
  275.  
  276. let token = '';
  277. try {
  278. token = await appliancesStorageFacade.getToken(yaasData);
  279. }
  280. catch (err) {
  281. throw { status: err.status, message: err.message };
  282. }
  283.  
  284. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  285.  
  286.  
  287. const options = {
  288. body: JSONInfo,
  289. headers: {
  290. Authorization: tokenAuthorization
  291. },
  292. json: true
  293. };
  294. const uri = `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/`;
  295.  
  296. try {
  297. const res = await request.post(uri, options);
  298. return { status: '201', message: 'Tenant info saved properly' };
  299. }
  300. catch (err) {
  301. throw { status: err.error.status, message: err.error.message };
  302. }
  303. };
  304.  
  305.  
  306. appliancesStorageFacade.getTenantInfo = async (tenant) => {
  307.  
  308. let token = '';
  309. try {
  310. token = await appliancesStorageFacade.getToken(yaasData);
  311. }
  312. catch (err) {
  313. throw { status: err.error.status, message: err.error.message };
  314. }
  315.  
  316.  
  317. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  318.  
  319. const options = {
  320. uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
  321. headers: {
  322. Authorization: tokenAuthorization
  323. },
  324. json: true
  325. };
  326.  
  327.  
  328. try {
  329. const tenantInfo = await request.get(options);
  330. return { status: 200, tenant: tenantInfo};
  331. }
  332. catch (err) {
  333. if (err.error.status === '304') {
  334. err.error.message = 'Entity has not been modified and the version cached by the client may be used';
  335. }
  336. if (err.error.status === '404') {
  337. err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
  338. }
  339. throw { status: err.error.status, message: err.error.message };
  340.  
  341. }
  342. };
  343.  
  344.  
  345.  
  346. appliancesStorageFacade.yaasData = yaasData;
  347. appliancesStorageFacade.yaasConfig = yaasConfig;
  348. module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement