Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const request = require('request-promise-native');
- const appliancesStorageFacade = [];
- // DURING MILESTONE 2 - CREATE ENVIRONMENTAL VARIABLES //
- const yaasData = {
- client: {
- clientID: process.env.CLIENTID,
- clientSecret: process.env.CLIENTSECRET
- },
- scopes: [process.env.SCOPE1, process.env.SCOPE2, process.env.SCOPE3]
- };
- const yaasConfig = {
- url: process.env.DOC_URL,
- tenant: process.env.DOC_TENANT,
- client: process.env.DOC_CLIENT,
- type: process.env.DOC_TYPE
- };
- const yaasConfigBuilderModule = {
- url: process.env.BUI_URL,
- tenant: process.env.BUI_TENANT,
- client: process.env.BUI_CLIENT,
- type: process.env.BUI_TYPE
- };
- let globalToken = '';
- 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) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.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) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.getCartID(deviceID);
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.registerDevice = async (JSONInfo, cartID) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.access_token}`;
- JSONInfo.cartID = cartID;
- const options = {
- body: JSONInfo,
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- try {
- const res = await request.post(options);
- return { status: '201', message: 'Device registered successfully' };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.registerDevice(JSONInfo, cartID);
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.unregisterDevice = async (deviceID) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.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.delete(options);
- return { status: '204', message: 'The request succeeded' };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.unregisterDevice(deviceID);
- }
- catch (err) {
- if (err.error.status === '400') {
- err.error.message = 'Parameter is not valid: Parameter is empty or has special characters.';
- }
- if (err.error.status === '404') {
- err.error.message = 'The request is about to delete a non-existing resource/object.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '400') {
- err.error.message = 'Parameter is not valid: Parameter is empty or has special characters.';
- }
- if (err.error.status === '404') {
- err.error.message = 'The request is about to delete a non-existing resource/object.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.getDevices = async (applianceOwner) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.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: 'Not Found. The server did not find anything matching the Request-URI.' } };
- }
- return { status: '200', message: appliances };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.getDevices(applianceOwner);
- }
- catch (err) {
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.getDevice = async (deviceID) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.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 { status: '200', device: appliances };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.getDevice(deviceID);
- }
- catch (err) {
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.updateDevice = async (deviceID, newCartID) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.access_token}`;
- const options = {
- uri: `${yaasConfig.url}/${yaasConfig.tenant}/${yaasConfig.client}/data/${yaasConfig.type}/${deviceID}/?&path=true&partial=true`,
- body: {
- cartID: newCartID
- },
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- let cartID = '';
- try {
- cartID = await appliancesStorageFacade.getCartID(deviceID);
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.updateDevice(deviceID, newCartID);
- } catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- }
- throw { status: err.error.status, message: err.error.message };
- }
- try {
- const appliances = await request.put(options);
- if (appliances.status === '201') {
- 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.' };
- }
- return { status: '200', cartID: cartID };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.updateDevice(deviceID, newCartID);
- }
- catch (err) {
- if (err.error.status === '404') {
- err.error.message = 'The request is about to change a non-existing resource/object';
- }
- if (err.error.status === '409') {
- err.error.message = 'Data modification failed because of a version conflict';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '404') {
- err.error.message = 'The request is about to change a non-existing resource/object';
- }
- if (err.error.status === '409') {
- err.error.message = 'Data modification failed because of a version conflict';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.checkTenant = async (JSONInfo) => {
- try {
- const tenantInfo = (await appliancesStorageFacade.getTenantInfo(JSONInfo.tenant)).message;
- if (tenantInfo.length === 0) return false;
- if (tenantInfo[0].tenant === JSONInfo.tenant) {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.access_token}`;
- const options = {
- uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/${tenantInfo[0].id}`,
- body: JSONInfo,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- try {
- const response = await request.put(options);
- if (response.status === '201') {
- 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.' };
- }
- return { status: '200', message: 'Tenant has been updated properly' };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.checkTenant(JSONInfo);
- }
- catch (err) {
- if (err.error.status === '404') {
- err.error.message = 'The request is about to change a non-existing resource/object';
- }
- if (err.error.status === '409') {
- err.error.message = 'Data modification failed because of a version conflict';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '404') {
- err.error.message = 'The request is about to change a non-existing resource/object';
- }
- if (err.error.status === '409') {
- err.error.message = 'Data modification failed because of a version conflict';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- }
- catch (err) {
- throw { status: err.status, message: err.message };
- }
- };
- appliancesStorageFacade.saveTenantInfo = async (tenantInfo) => {
- let check = '';
- try {
- check = await appliancesStorageFacade.checkTenant(tenantInfo);
- if (check.status === '200') {
- return check;
- }
- }
- catch (err) {
- throw { status: err.status, message: err.message };
- }
- try {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.access_token}`;
- const options = {
- body: tenantInfo,
- uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- const res = await request.post(options);
- return { status: '201', message: 'Tenant info saved properly' };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.saveTenantInfo(tenantInfo);
- }
- catch (err) {
- throw { status: err.error.status, message: err.error.message };
- }
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.getTenantInfo = async (tenant) => {
- const tokenAuthorization = `${globalToken.token_type} ${globalToken.access_token}`;
- const options = {
- uri: `${yaasConfigBuilderModule.url}/${yaasConfigBuilderModule.tenant}/${yaasConfigBuilderModule.client}/data/${yaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- try {
- const tenantInfo = await request.get(options);
- return { status: '200', message: tenantInfo };
- }
- catch (err) {
- if (err.error.status === 401) {
- try {
- globalToken = await appliancesStorageFacade.getToken(yaasData);
- return await appliancesStorageFacade.getTenantInfo(tenant);
- }
- catch (err) {
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- }
- if (err.error.status === '304') {
- err.error.message = 'Entity has not been modified and the version cached by the client may be used';
- }
- if (err.error.status === '404') {
- err.error.message = 'Not Found. The server did not find anything matching the Request-URI.';
- }
- throw { status: err.error.status, message: err.error.message };
- }
- };
- appliancesStorageFacade.yaasData = yaasData;
- appliancesStorageFacade.yaasConfig = yaasConfig;
- appliancesStorageFacade.yaasConfigBuilderModule = yaasConfigBuilderModule;
- module.exports = appliancesStorageFacade;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement