Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const request = require('request-promise-native');
- const appliancesStorageFacade = [];
- const yaasData = {
- client: {
- clientID: 'ecxEHXeKZJdo4xhVvEvZPX9uyccRMqCl',
- clientSecret: 'rIITEf40Hh8lIbXK'
- },
- scopes: ['hybris.document_manage', 'hybris.document_view', 'thack.infinitecart_manage']
- };
- const yaasConfig = {
- url: 'https://api.us.yaas.io/hybris/document/v1',
- tenant: 'inficart',
- client: 'inficart.appliancedoc',
- type: 'savedAppliances'
- };
- appliancesStorageFacade.getToken = async (yaasData) => {
- const { client, tenant, scopes } = yaasData;
- let requestedScopes = [];
- if (tenant) {
- requestedScopes.push(`hybris.tenant=${tenant}`);
- }
- if (scopes) {
- requestedScopes = requestedScopes.concat(scopes);
- }
- const requestOptions = {
- auth: {
- username: client.clientID,
- password: client.clientSecret
- },
- form: {
- grant_type: 'client_credentials',
- scope: requestedScopes.join(' ')
- }
- };
- const url = 'https://api.us.yaas.io/hybris/oauth2/v1/token';
- try {
- const token = await request.post(url, requestOptions);
- return JSON.parse(token);
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.getCartID = async (deviceID, token) => {
- const tokenAuthorization = `${token.token_type} ${token.access_token}`; const options = {
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- try {
- const appliances = await request.get(options);
- return appliances.cartID;
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.registerDevice = async (JSONInfo, cartID) => {
- let token = '';
- try {
- token = await appliancesStorageFacade.getToken(yaasData);
- }
- catch (err) {
- throw { status: err.status, message: err.message };
- }
- const tokenAuthorization = `${token.token_type} ${token.access_token}`;
- JSONInfo.cartID = cartID;
- const options = {
- body: JSONInfo,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- const uri = `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`;
- try {
- const res = await request.post(uri, options);
- return { status: '200', message: 'Device resgistered properly' };
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.getDevices = async (applianceOwner) => {
- let token = '';
- try {
- token = await appliancesStorageFacade.getToken(yaasData);
- }
- catch(err) {
- throw {status: err.error.status, message: err.error.message};
- }
- const tokenAuthorization = `${token.token_type} ${token.access_token}`;
- const options = {
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/?fetchAll=true&q=applianceOwner:${applianceOwner}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- try {
- const appliances = await request.get(options);
- if(appliances.length === 0) {
- throw { error: {status: '404', message: 'Given tenant/applianceOWner is not registered in DOCs REPO' }};
- }
- return { status: '200', message: appliances };
- }
- catch (err) {
- throw {status: err.error.status, message: err.error.message};
- }
- };
- appliancesStorageFacade.getDevice = async (deviceID) => {
- let token = '';
- try {
- token = await appliancesStorageFacade.getToken(yaasData);
- }
- catch(err) {
- throw {status: err.error.status, message: err.error.message};
- }
- const tokenAuthorization = `${token.token_type} ${token.access_token}`;
- const options = {
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- const checkChar = function (id) {
- return /^[0-9a-zA-Z]+$/.test(id);
- };
- try {
- if (checkChar(deviceID)) {
- const appliances = await request.get(options);
- return { status: 200, cartID: appliances.cartID };
- }
- throw { error: {status: 404, message: 'ID is not proper' }};
- }
- catch (err) {
- throw {status: err.error.status, message: err.error.message};
- }
- };
- appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
- let token = '';
- try {
- token = await appliancesStorageFacade.getToken(yaasData);
- }
- catch(err) {
- throw {status: err.error.status, message: err.error.message};
- }
- const tokenAuthorization = `${token.token_type} ${token.access_token}`;
- const options = {
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
- body: { cartID: Number(newCartID) },
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- let cartID = '';
- try {
- cartID = await appliancesStorageFacade.getCartID(deviceID, token);
- }
- catch (err) {
- throw {status: err.error.status, message: err.error.message};
- }
- try {
- const appliances = await request.put(options);
- return { status: '200', cartID: cartID };
- }
- catch (err) {
- throw {status: err.error.status, message: err.error.message};
- }
- };
- module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement