import { createUser, loadUser, isUserLogged, updateUser, deleteUSer, getUserHTTP, } from "./api/user-api-service"; import { changeRevision, deleteService, deployService, redeployService, requestRevisionData, restartInstance, restartService, updateService, updateServiceLinks, } from "./api/service-api-service"; import { deployMarketplaceItem, getMarketplaceItems, getMarketplaceSchema, } from "./api/marketplace-api-service"; import { clearAccount, createAccount, deleteAccount, updateAccount, } from "./api/account-api-service"; import { acceptInvite, createRegistry, createTenant, createTenantHTTP, createToken, deleteRegistry, deleteTenant, deleteToken, inviteUser, rejectInvite, removeUser, updateRegistry, updateTenant, updateTenantHTTP, updateUserRole, } from "./api/tenant-api-service"; import { clearEnvironment, createEnvironment, deleteEnvironment, scaleEnvironment, updateEnvironment, } from "./api/environment-api-service"; import EventHelper, { RegistryUpdatePayload } from "./event-helper"; import { environment } from "./environment"; import { fetchAndStoreMarketplaceSchema, initializeGlobalWebSocketClient, updateUserComplete, } from "./websocket-manager"; import { createResource, deleteResource, updateResource, } from "./api/resources-api-service"; import { getPlanProviders } from "./api/planProvider-api-service"; import { Account, Environment, errors, Link, MarketplaceItem, MarketplaceService, Notification, Organization, Resource, Service, Tenant, tenantRole, User, UserData, } from "@kumori/aurora-interfaces"; export let eventHelper: EventHelper; const token = ""; export class BackendHandler { private unsubscribeFunctions: (() => void)[] = []; constructor( route: string, globalEventHandler: any, baseUrl: string, apiVersion: string, ) { if ( !globalEventHandler || typeof globalEventHandler.subscribe !== "function" ) { throw new Error( "globalEventHandler inválido. Asegúrate de pasar una instancia correcta de EventHandler.", ); } eventHelper = new EventHelper(globalEventHandler); environment.apiServer.baseUrl = baseUrl; environment.apiServer.apiVersion = apiVersion; getUserHTTP().then(() => { eventHelper.user.subscribe.load((data: UserData) => { initializeGlobalWebSocketClient(token, "", "", data); }); const loadedCb = (data: UserData) => { updateUserComplete(data); }; eventHelper.user.subscribe.loaded(loadedCb); this.changeRoute(route); eventHelper.user.publish.load({} as UserData); }) } /** * Function to change the route and subscribe to the new route events * @param newRoute string with the new route */ public changeRoute(newRoute: string): void { this.unsubscribeRouteEvents(); this.subscribeForRoute(newRoute); } /** * Function to subscribe to the events of the route * @param route string with the route */ public subscribeForRoute(route: string): void { // const userRoutes = ["user-information", "home", "register"]; // const accountRoutes = ["accounts", "create-account"]; // const environmentRoutes = ["environments", "create-environment"]; // const serviceRoutes = ["deploy-service", "service-detail", "deploy-service","runtime"]; // const marketplaceRoutes = ["deploy-marketplace", "marketplace","deploy-service"]; // const resourceRoutes = ["resources", "create-resource", "deploy-service","deploy-marketplace"]; // const organizationRoutes = ["plans"]; // const tenantRoutes = ["tenants", "create-tenant", ...organizationRoutes, ...userRoutes]; // if (tenantRoutes.includes(route)) { // } // if (userRoutes.includes(route)) { // } // if (accountRoutes.includes(route)) { // } // if (environmentRoutes.includes(route)) { // } // if (serviceRoutes.includes(route)) { // } // if (marketplaceRoutes.includes(route)) { // } // if (resourceRoutes.includes(route)) { // } // if (organizationRoutes.includes(route)) { // } this.subscribeTenantEvents(); this.subscribeUserEvents(); this.subscribeAccountEvents(); this.subscribePlanEvents(); this.subscribeEnvironmentEvents(); this.subscribeServiceEvents(); this.subscribeMarketplaceEvents(); this.subscribeResourceEvents(); this.subscribeOrganizationEvents(); } public async isUserLoggedIn(): Promise { const loggedIn = await isUserLogged(token); return loggedIn === true; } /** * Function to unsubscribe all the events */ private unsubscribeRouteEvents(): void { this.unsubscribeFunctions.forEach((fn) => fn()); this.unsubscribeFunctions = []; } /** * Function to subscribe to the tenant events */ private subscribeTenantEvents(): void { const creationCb = (data: Tenant) => { createTenant(data, token); }; eventHelper.tenant.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.creation(creationCb), ); const createdCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.created(createdCb), ); const creationErrorCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: Tenant) => { updateTenant(data, token); }; eventHelper.tenant.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.update(updateCb), ); const updatedCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Tenant) => { deleteTenant(data, token); //loadUserData(); }; eventHelper.tenant.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Tenant) => {}; eventHelper.tenant.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.deletionError(deletionErrorCb), ); const createRegistryCb = (payload: RegistryUpdatePayload) => { createRegistry(payload.tenant, token, payload.registry); }; eventHelper.tenant.subscribe.createRegistry(createRegistryCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.createRegistry(createRegistryCb), ); const registryCreatedCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryCreated(registryCreatedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryCreated(registryCreatedCb), ); const registryCreationErrorCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryCreationError(registryCreationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryCreationError( registryCreationErrorCb, ), ); const updateRegistryCb = (payload: RegistryUpdatePayload) => { updateRegistry(payload.tenant, payload.registry, token); }; eventHelper.tenant.subscribe.updateRegistry(updateRegistryCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.updateRegistry(updateRegistryCb), ); const registryUpdatedCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryUpdated(registryUpdatedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryUpdated(registryUpdatedCb), ); const registryUpdateErrorCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryUpdateError(registryUpdateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryUpdateError(registryUpdateErrorCb), ); const deleteRegistryCb = (payload: RegistryUpdatePayload) => { deleteRegistry(payload.tenant, payload.registry, token); }; eventHelper.tenant.subscribe.deleteRegistry(deleteRegistryCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.deleteRegistry(deleteRegistryCb), ); const registryDeletedCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryDeleted(registryDeletedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryDeleted(registryDeletedCb), ); const registryDeletionErrorCb = (payload: RegistryUpdatePayload) => {}; eventHelper.tenant.subscribe.registryDeletionError(registryDeletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.registryDeletionError( registryDeletionErrorCb, ), ); const inviteUserCb = (payload: { user: string; tenant: string; role: tenantRole; }) => { inviteUser(payload.tenant, payload.user, payload.role, token); }; eventHelper.tenant.subscribe.inviteUser(inviteUserCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteUser(inviteUserCb), ); const userInvitedCb = (payload: { user: string; tenant: string; role: tenantRole; }) => { updateUserRole(payload.user, payload.tenant, payload.role, token); }; eventHelper.tenant.subscribe.userInvited(userInvitedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.userInvited(userInvitedCb), ); const userInviteErrorCb = (payload: { user: string; tenant: string; role: tenantRole; }) => {}; eventHelper.tenant.subscribe.inviteError(userInviteErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteError(userInviteErrorCb), ); const removeUserFromTenantCb = (payload: { user: string; tenant: string; }) => { removeUser(payload.tenant, payload.user, token); }; eventHelper.tenant.subscribe.removeUser(removeUserFromTenantCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.removeUser(removeUserFromTenantCb), ); const userRemovedFromTenantCb = (payload: { user: string; tenant: string; }) => {}; eventHelper.tenant.subscribe.userRemoved(userRemovedFromTenantCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.userRemoved(userRemovedFromTenantCb), ); const userRemoveErrorCb = (payload: { user: string; tenant: string }) => {}; eventHelper.tenant.subscribe.removeUserError(userRemoveErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.removeUserError(userRemoveErrorCb), ); const updateInviteCb = (payload: { user: string; role: tenantRole; tenant: string; }) => { updateUserRole(payload.tenant, payload.user, payload.role, token); }; eventHelper.tenant.subscribe.updateInvite(updateInviteCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.updateInvite(updateInviteCb), ); const inviteUpdatedCb = (payload: { user: string; role: string; tenant: string; }) => {}; eventHelper.tenant.subscribe.inviteUpdated(inviteUpdatedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteUpdated(inviteUpdatedCb), ); const inviteUpdateErrorCb = (payload: { user: string; role: string; tenant: string; }) => {}; eventHelper.tenant.subscribe.inviteUpdateError(inviteUpdateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteUpdateError(inviteUpdateErrorCb), ); const acceptInviteCb = (payload: { tenant: string }) => { acceptInvite(payload.tenant, token); }; eventHelper.tenant.subscribe.acceptInvite(acceptInviteCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.acceptInvite(acceptInviteCb), ); const inviteAcceptedCb = (payload: { tenant: string }) => {}; eventHelper.tenant.subscribe.inviteAccepted(inviteAcceptedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteAccepted(inviteAcceptedCb), ); const inviteAcceptErrorCb = (payload: { tenant: string }) => {}; eventHelper.tenant.subscribe.acceptInviteError(inviteAcceptErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.acceptInviteError(inviteAcceptErrorCb), ); const rejectInviteCb = (payload: { tenant: string; leave: boolean }) => { rejectInvite(payload.tenant, token, payload.leave); }; eventHelper.tenant.subscribe.rejectInvite(rejectInviteCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.rejectInvite(rejectInviteCb), ); const inviteRejectedCb = (payload: { user: string; tenant: string }) => {}; eventHelper.tenant.subscribe.inviteRejected(inviteRejectedCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteRejected(inviteRejectedCb), ); const inviteRejectErrorCb = (payload: { tenant: string }) => {}; eventHelper.tenant.subscribe.inviteRejectError(inviteRejectErrorCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.inviteRejectError(inviteRejectErrorCb), ); const getPlanProvidersCb = () => { getPlanProviders(token); }; eventHelper.planProviders.subscribe.loadPlans(getPlanProvidersCb); this.unsubscribeFunctions.push(() => eventHelper.planProviders.unsubscribe.loadPlans(getPlanProvidersCb), ); const crateTokenCb = (payload: { tenant: string; expiration: string; description: string; }) => { createToken( payload.tenant, payload.description, payload.expiration, token, ); }; eventHelper.tenant.subscribe.createToken(crateTokenCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.createToken(crateTokenCb), ); const deleteTokenCb = (payload: { tenant: string; token: string }) => { deleteToken(payload.tenant, payload.token, token); }; eventHelper.tenant.subscribe.deleteToken(deleteTokenCb); this.unsubscribeFunctions.push(() => eventHelper.tenant.unsubscribe.deleteToken(deleteTokenCb), ); } /** * Function to subscribe to the user events */ private subscribeUserEvents(): void { const creationCb = (data: UserData) => { createUser(data); }; eventHelper.user.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.creation(creationCb), ); const createdCb = (data: UserData) => {}; eventHelper.user.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.created(createdCb), ); const creationErrorCb = (data: UserData) => {}; eventHelper.user.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: UserData) => { updateUser(data); }; eventHelper.user.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.update(updateCb), ); const updatedCb = (data: UserData) => {}; eventHelper.user.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: UserData) => {}; eventHelper.user.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.updateError(updateErrorCb), ); const loadCb = (data: UserData) => { loadUser(token, data); }; eventHelper.user.subscribe.load(loadCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.load(loadCb), ); // const loadedCb = (data: UserData) => { // //updateUserData(data); // }; // eventHelper.user.subscribe.loaded(loadedCb); // this.unsubscribeFunctions.push(() => // eventHelper.user.unsubscribe.loaded(loadedCb) // ); const loadErrorCb = (data: UserData) => {}; eventHelper.user.subscribe.loadError(loadErrorCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.loadError(loadErrorCb), ); const deleteCb = (data: UserData) => { deleteUSer(data, token); }; eventHelper.user.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.delete(deleteCb), ); const deletedCb = (data: UserData) => {}; eventHelper.user.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: UserData) => {}; eventHelper.user.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.user.unsubscribe.deletionError(deletionErrorCb), ); } /** * Function to subscribe to the account events */ private subscribeAccountEvents(): void { const creationCb = (data: Account) => { createAccount(data, token); }; eventHelper.account.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.creation(creationCb), ); const createdCb = (data: Account) => {}; eventHelper.account.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.created(createdCb), ); const creationErrorCb = (data: Account) => {}; eventHelper.account.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: Account) => { updateAccount(data, token); }; eventHelper.account.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.update(updateCb), ); const updatedCb = (data: Account) => {}; eventHelper.account.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Account) => {}; eventHelper.account.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Account) => { deleteAccount(data, token); }; eventHelper.account.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Account) => {}; eventHelper.account.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Account) => {}; eventHelper.account.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.deletionError(deletionErrorCb), ); const cleanCb = (data: Account) => { clearAccount(data, token); }; eventHelper.account.subscribe.clean(cleanCb); this.unsubscribeFunctions.push(() => eventHelper.account.unsubscribe.clean(cleanCb), ); } /** * Function to subscribe to the plan events */ private subscribePlanEvents(): void { const updateCb = (data: string) => {}; eventHelper.plan.subscribe.upgrade(updateCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.upgrade(updateCb), ); const updatedCb = (data: string) => { const upgradeNotification: Notification = { type: "success", subtype: errors.user.planUpgraded.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { plan: data, }, }; eventHelper.notification.publish.creation(upgradeNotification); }; eventHelper.plan.subscribe.upgraded(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.upgraded(updatedCb), ); const updateErrorCb = (data: string) => {}; eventHelper.plan.subscribe.upgradeError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.upgradeError(updateErrorCb), ); const downgradeCb = (data: string) => {}; eventHelper.plan.subscribe.downgrade(downgradeCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.downgrade(downgradeCb), ); const downgradedCb = (data: string) => { const downgradeNotification: Notification = { type: "success", subtype: errors.user.planDowngraded.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { plan: data, }, }; eventHelper.notification.publish.creation(downgradeNotification); }; eventHelper.plan.subscribe.downgraded(downgradedCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.downgraded(downgradedCb), ); const downgradeErrorCb = (data: string) => {}; eventHelper.plan.subscribe.downgradeError(downgradeErrorCb); this.unsubscribeFunctions.push(() => eventHelper.plan.unsubscribe.downgradeError(downgradeErrorCb), ); } /** * Function to subscribe to the environment events */ private subscribeEnvironmentEvents(): void { const creationCb = (data: Environment) => { createEnvironment(data, token); }; eventHelper.environment.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.creation(creationCb), ); const createdCb = (data: Environment) => { const envNotification: Notification = { type: "success", subtype: errors.environment.created.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { environment: data.name, account: data.account, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(envNotification); }; eventHelper.environment.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.created(createdCb), ); const creationErrorCb = (data: Environment) => {}; eventHelper.environment.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: Environment) => { updateEnvironment(data, token); }; eventHelper.environment.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.update(updateCb), ); const updatedCb = (data: Environment) => {}; eventHelper.environment.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Environment) => {}; eventHelper.environment.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Environment) => { deleteEnvironment(data, token); }; eventHelper.environment.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Environment) => {}; eventHelper.environment.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Environment) => {}; eventHelper.environment.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.deletionError(deletionErrorCb), ); const cleanCb = (data: Environment) => { clearEnvironment(data, token); }; eventHelper.environment.subscribe.clean(cleanCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.clean(cleanCb), ); const scaleCb = (data: Environment) => { scaleEnvironment(data, token); }; eventHelper.environment.subscribe.scale(scaleCb); this.unsubscribeFunctions.push(() => eventHelper.environment.unsubscribe.scale(scaleCb), ); } /** * Function to subscribe to the service events */ private subscribeServiceEvents(): void { const deployedCb = (data: Service) => { const deploymentSuccessNotification: Notification = { type: "success", subtype: errors.deployment.success.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(deploymentSuccessNotification); }; eventHelper.service.subscribe.deployed(deployedCb); const deploymentErrorCb = (data: Service) => { const deploymentErrorNotification: Notification = { type: "error", subtype: errors.deployment.error.subtype, date: Date.now().toString(), status: "unread", info_content: { code: data.error?.code || "", message: data.error?.message || "", timestamp: data.error?.timestamp || "", }, callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(deploymentErrorNotification); }; eventHelper.service.subscribe.deploymentError(deploymentErrorCb); const deployCb = async (data: Service) => { deployService(data, token); const deploymentSuccessNotification: Notification = { type: "info", subtype: errors.deployment.inProgress.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; data.download ? "" : eventHelper.notification.publish.creation( deploymentSuccessNotification, ); }; eventHelper.service.subscribe.deploy(deployCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.deploy(deployCb), ); const updateCb = (data: Service) => { updateService(data, token); }; eventHelper.service.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.update(updateCb), ); const updatedCb = (data: Service) => { const deploymentNotification: Notification = { type: "success", subtype: errors.deployment.updated.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(deploymentNotification); }; eventHelper.service.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Service) => {}; eventHelper.service.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Service) => { const deploymentNotification: Notification = { type: "info", subtype: errors.deployment.deleting.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(deploymentNotification); deleteService(data, token); }; eventHelper.service.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Service) => { const deploymentNotification: Notification = { type: "success", subtype: errors.deployment.deleted.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { service: data.name, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(deploymentNotification); }; eventHelper.service.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Service) => {}; eventHelper.service.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.deletionError(deletionErrorCb), ); const requestLogsCb = (data: Service) => {}; eventHelper.service.subscribe.requestLogs(requestLogsCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.requestLogs(requestLogsCb), ); const restartCb = (data: Service) => { restartService(data, token); }; eventHelper.service.subscribe.restart(restartCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.restart(restartCb), ); const requestRevisionCb = (data: Service) => { requestRevisionData(data, token); }; eventHelper.service.subscribe.requestRevisionData(requestRevisionCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.requestRevisionData(requestRevisionCb), ); const updateServiceLinksCb = (data: Link) => { updateServiceLinks(data, token); }; eventHelper.service.subscribe.updateServiceLinks(updateServiceLinksCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.updateServiceLinks(updateServiceLinksCb), ); const changeRevisionCb = (data: Service) => { changeRevision(data, token); }; eventHelper.service.subscribe.changeRevision(changeRevisionCb); this.unsubscribeFunctions.push(() => { eventHelper.service.unsubscribe.changeRevision(changeRevisionCb); }); const restartInstanceCb = (payload: { service: Service; roleId: string; instanceId: string; }) => { restartInstance( payload.service, payload.roleId, payload.instanceId, token, ); }; eventHelper.service.subscribe.restartInstance(restartInstanceCb); this.unsubscribeFunctions.push(() => eventHelper.service.unsubscribe.restartInstance(restartInstanceCb), ); } /** * Function to subscribe to the marketplace events */ private subscribeMarketplaceEvents(): void { const deployItemCb = (data: MarketplaceService) => { deployMarketplaceItem(data); }; eventHelper.marketplace.subscribe.deployItem(deployItemCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.deployItem(deployItemCb), ); const itemDeployedCb = (data: Service) => {}; eventHelper.marketplace.subscribe.itemDeployed(itemDeployedCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.itemDeployed(itemDeployedCb), ); const deploymentErrorCb = (data: Service) => {}; eventHelper.marketplace.subscribe.deploymentError(deploymentErrorCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.deploymentError(deploymentErrorCb), ); const updateItemCb = (data: Service) => {}; eventHelper.marketplace.subscribe.updateItem(updateItemCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.updateItem(updateItemCb), ); const itemUpdatedCb = (data: Service) => {}; eventHelper.marketplace.subscribe.itemUpdated(itemUpdatedCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.itemUpdated(itemUpdatedCb), ); const updateErrorCb = (data: Service) => {}; eventHelper.marketplace.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.updateError(updateErrorCb), ); const deleteItemCb = (data: Service) => {}; eventHelper.marketplace.subscribe.deleteItem(deleteItemCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.deleteItem(deleteItemCb), ); const itemDeletedCb = (data: Service) => {}; eventHelper.marketplace.subscribe.itemDeleted(itemDeletedCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.itemDeleted(itemDeletedCb), ); const deletionErrorCb = (data: Service) => {}; eventHelper.marketplace.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.deletionError(deletionErrorCb), ); const loadItemsCb = (data: string[]) => { // getMarketplaceItems(data, token) // .then((result) => { // eventHelper.marketplace.publish.itemsLoaded(result.items); // }) // .catch((err) => { // console.error("Error al cargar marketplace items:", err); // }); }; eventHelper.marketplace.subscribe.loadItems(loadItemsCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.loadItems(loadItemsCb), ); const loadSchemaCb = async (payload: string) => { try { const { item } = JSON.parse(payload) as { item: MarketplaceItem }; await fetchAndStoreMarketplaceSchema(item); eventHelper.marketplace.publish.schemaLoaded(item); } catch (error) { console.error(`Error loading schema for marketplace item:`, error); eventHelper.marketplace.publish.schemaLoadError(payload); } }; eventHelper.marketplace.subscribe.loadSchema(loadSchemaCb); this.unsubscribeFunctions.push(() => eventHelper.marketplace.unsubscribe.loadSchema(loadSchemaCb), ); } /** * Function to subscribe to the resource events */ private subscribeResourceEvents(): void { const creationCb = (data: Resource) => { createResource(data.tenant ?? "", data, token); }; eventHelper.resource.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.creation(creationCb), ); const createdCb = (data: Resource) => {}; eventHelper.resource.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.created(createdCb), ); const creationErrorCb = (data: Resource) => {}; eventHelper.resource.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: Resource) => { updateResource(data.tenant ?? "", data, token); }; eventHelper.resource.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.update(updateCb), ); const updatedCb = (data: Resource) => {}; eventHelper.resource.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Resource) => {}; eventHelper.resource.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Resource) => { deleteResource(data.tenant ?? "", data, token); }; eventHelper.resource.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Resource) => { const resourceNotification: Notification = { type: "success", subtype: errors.resource.deleted.subtype, date: Date.now().toString(), status: "unread", callToAction: false, data: { resource: data.name, type: data.type, tenant: data.tenant, }, }; eventHelper.notification.publish.creation(resourceNotification); }; eventHelper.resource.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Resource) => {}; eventHelper.resource.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.resource.unsubscribe.deletionError(deletionErrorCb), ); } /** * Function to subscribe to the organization events */ private subscribeOrganizationEvents(): void { const creationCb = (data: Organization) => {}; eventHelper.organization.subscribe.creation(creationCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.creation(creationCb), ); const createdCb = (data: Organization) => {}; eventHelper.organization.subscribe.created(createdCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.created(createdCb), ); const creationErrorCb = (data: Organization) => {}; eventHelper.organization.subscribe.creationError(creationErrorCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.creationError(creationErrorCb), ); const updateCb = (data: Organization) => {}; eventHelper.organization.subscribe.update(updateCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.update(updateCb), ); const updatedCb = (data: Organization) => {}; eventHelper.organization.subscribe.updated(updatedCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.updated(updatedCb), ); const updateErrorCb = (data: Organization) => {}; eventHelper.organization.subscribe.updateError(updateErrorCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.updateError(updateErrorCb), ); const deleteCb = (data: Organization) => {}; eventHelper.organization.subscribe.delete(deleteCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.delete(deleteCb), ); const deletedCb = (data: Organization) => {}; eventHelper.organization.subscribe.deleted(deletedCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.deleted(deletedCb), ); const deletionErrorCb = (data: Organization) => {}; eventHelper.organization.subscribe.deletionError(deletionErrorCb); this.unsubscribeFunctions.push(() => eventHelper.organization.unsubscribe.deletionError(deletionErrorCb), ); } }