UNPKG

@robotical/appv2-warranty-service-lib

Version:

A tool that gathers data from the Apps and sends it to the Analytics server

229 lines (228 loc) 10.2 kB
import WarrantyServiceCore from "../core/WarrantyService"; import Logger from "../services/Logger"; import ZohoAPI from "../ZohoIntegration/ZohoAPI"; const SHOW_LOGS = true; const TAG = 'WarrantyServiceFacade'; class WarrantyServiceFacade { static async isSerialNumberRegistered(serialNumber) { const warrantyService = WarrantyServiceCore.getInstance(); try { const isRegistered = await warrantyService.isSerialNumberRegistered(serialNumber); return isRegistered; } catch (e) { Logger.error(SHOW_LOGS, TAG, `isSerialNumberRegistered error: ${e}`); return false; } } static async getSerialNumber(serialNumber) { const warrantyService = WarrantyServiceCore.getInstance(); try { const serialNumberData = await warrantyService.getSerialNumber(serialNumber); return serialNumberData; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getSerialNumber error: ${e}`); return null; } } static async appendSerialNumbersToUser(email, serialNumbers) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.appendSerialNumbersToUser(email, serialNumbers); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `Error appending serial numbers ${serialNumbers} to user with email ${email}: ${e}`); return false; } } static async addRobotNameToSerialNumber(serialNumber, robotName) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.addRobotNameToSerialNumber(serialNumber, robotName); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `addRobotNameToSerialNumber error: ${e}`); return false; } } static async getUser(email) { const warrantyService = WarrantyServiceCore.getInstance(); try { const user = await warrantyService.getUser(email); return user; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getUser error: ${e}`); return null; } } static async registerUser(email, establishment, serialNumbers) { const warrantyService = WarrantyServiceCore.getInstance(); try { const userId = await warrantyService.registerUser(email, establishment, serialNumbers); return userId; } catch (e) { Logger.error(SHOW_LOGS, TAG, `registerUser error: ${e}`); return false; } } static async registerReport(email, serialNumber, title, description, robotName, robotType) { const warrantyService = WarrantyServiceCore.getInstance(); try { const reportId = await warrantyService.registerReport(email, serialNumber, title, description, robotName, robotType); return reportId; } catch (e) { Logger.error(SHOW_LOGS, TAG, `registerReport error: ${e}`); return false; } } static async getZohoTicketBySerialNumber(serialNumber) { try { const ticket = await ZohoAPI.getTicketBySerialNumber(serialNumber); return ticket; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getZohoTicketBySerialNumber error: ${e}`); return null; } } static async getTicketComments(ticketId) { try { const comments = await ZohoAPI.getCommentsForTicket(ticketId); return comments; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getTicketComments error: ${e}`); return null; } } static async getZohoReports(reports) { const warrantyService = WarrantyServiceCore.getInstance(); try { const tickets = await warrantyService.getZohoTicketsFromReports(reports); return tickets; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getZohoReports error: ${e}`); return null; } } static async updateZohoTicketStatus(ticketId, status) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.updateZohoTicketStatus(ticketId, status); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `updateZohoTicketStatus error: ${e}`); return false; } } static async setAutomatedFaultReportingConsent(email, consent) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.setAutomatedFaultReportingConsent(email, consent); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `setAutomatedFaultReportingConsent error: ${e}`); return false; } } static async setAnalyticsConsent(email, consent) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.setAnalyticsConsent(email, consent); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `setAnalyticsConsent error: ${e}`); return false; } } static async addLoginTokenToUser(email) { const warrantyService = WarrantyServiceCore.getInstance(); try { const result = await warrantyService.addLoginTokenToUser(email); return result; } catch (e) { Logger.error(SHOW_LOGS, TAG, `addLoginTokenToUser error: ${e}`); return false; } } static async getLoginToken(email) { const warrantyService = WarrantyServiceCore.getInstance(); try { const token = await warrantyService.getLoginToken(email); return token; } catch (e) { Logger.error(SHOW_LOGS, TAG, `getLoginToken error: ${e}`); return undefined; } } static async addAlternativeEmailToUser(userPrimaryEmail, alternativeEmail) { Logger.info(SHOW_LOGS, TAG, `addAlternativeEmailToUser: userPrimaryEmail: ${userPrimaryEmail}, alternativeEmail: ${alternativeEmail}`); const warrantyService = WarrantyServiceCore.getInstance(); const wasAdded = await warrantyService.addAlternativeEmailToRegisteredUser(userPrimaryEmail, alternativeEmail); if (!wasAdded) { Logger.warn(SHOW_LOGS, TAG, `addAlternativeEmailToUser: Could not add alternative email to user ${userPrimaryEmail}`); return false; } const wasAddedToAlternativeToPrimary = await warrantyService.addAlternativeToPrimaryEntry(userPrimaryEmail, alternativeEmail); if (!wasAddedToAlternativeToPrimary) { Logger.warn(SHOW_LOGS, TAG, `addAlternativeEmailToUser: Could not add primary email to alternative email map`); return false; } const wasAddedToPrimaryToAlternative = await warrantyService.addAlternativeEmailToPrimaryEmail(userPrimaryEmail, alternativeEmail); if (!wasAddedToPrimaryToAlternative) { Logger.warn(SHOW_LOGS, TAG, `addAlternativeEmailToUser: Could not add alternative email to primary email map`); return false; } Logger.info(SHOW_LOGS, TAG, `addAlternativeEmailToUser: Successfully added alternative email to user ${userPrimaryEmail}`); return true; } static async removeAlternativeEmailFromUser(userPrimaryEmail, alternativeEmail) { Logger.info(SHOW_LOGS, TAG, `removeAlternativeEmailFromUser: userPrimaryEmail: ${userPrimaryEmail}, alternativeEmail: ${alternativeEmail}`); const warrantyService = WarrantyServiceCore.getInstance(); const wasRemoved = await warrantyService.removeAlternativeEmailFromRegisteredUser(userPrimaryEmail, alternativeEmail); if (!wasRemoved) { Logger.warn(SHOW_LOGS, TAG, `removeAlternativeEmailFromUser: Could not remove alternative email from user ${userPrimaryEmail}`); return false; } const wasRemovedFromAlternativeToPrimary = await warrantyService.removeAlternativeToPrimaryEntry(alternativeEmail); if (!wasRemovedFromAlternativeToPrimary) { Logger.warn(SHOW_LOGS, TAG, `removeAlternativeEmailFromUser: Could not remove alternative email from primary email map`); return false; } const wasRemovedFromPrimaryToAlternative = await warrantyService.removeAlternativeEmailFromPrimaryToAlternativeMap(userPrimaryEmail, alternativeEmail); if (!wasRemovedFromPrimaryToAlternative) { Logger.warn(SHOW_LOGS, TAG, `removeAlternativeEmailFromUser: Could not remove primary email from alternative email map`); return false; } Logger.info(SHOW_LOGS, TAG, `removeAlternativeEmailFromUser: Successfully removed alternative email from user ${userPrimaryEmail}`); return true; } static async swapPrimaryAndAlternativeEmails(userPrimaryEmail, alternativeEmail) { Logger.info(SHOW_LOGS, TAG, `swapPrimaryAndAlternativeEmails: userPrimaryEmail: ${userPrimaryEmail}, alternativeEmail: ${alternativeEmail}`); const warrantyService = WarrantyServiceCore.getInstance(); const wasSwapped = await warrantyService.swapPrimaryAndAlternativeEmailsFromMaps(userPrimaryEmail, alternativeEmail); if (!wasSwapped) { console.warn('swapPrimaryAndAlternativeEmails:', "Could not swap primary and alternative emails"); return false; } const wasChanged = await warrantyService.changePrimaryEmailOfRegisteredUser(userPrimaryEmail, alternativeEmail); if (!wasChanged) { console.warn('swapPrimaryAndAlternativeEmails:', "Could not change primary email of registered user"); return false; } return true; } } export default WarrantyServiceFacade;