diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index b09d9a5e3..7bf8c7ae1 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -3,20 +3,24 @@ import { UserEnvironment } from './user-environments.model'; import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; import * as E from 'fp-ts/Either'; +import * as O from 'fp-ts/Option'; import { - USER_ENVIRONMENT_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS, + USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS, USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED, - USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS, USER_ENVIRONMENT_GLOBAL_ENV_EXISTS, USER_ENVIRONMENT_IS_NOT_GLOBAL, USER_ENVIRONMENT_UPDATE_FAILED, + USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; +import { SubscriptionHandler } from '../subscription-handler'; -// Contains constants for the subscription types we send to pubsub service +// Contains constants for the subscription types we send to subscription handler enum SubscriptionType { Created = 'created', Updated = 'updated', Deleted = 'deleted', + DeleteMany = 'delete_many', } @Injectable() @@ -24,6 +28,7 @@ export class UserEnvironmentsService { constructor( private readonly prisma: PrismaService, private readonly pubsub: PubSubService, + private readonly subscriptionHandler: SubscriptionHandler, ) {} /** @@ -75,13 +80,13 @@ export class UserEnvironmentsService { }); } - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } /** * Create a personal or global user environment * @param uid Users uid - * @param name environments name + * @param name environments name, null if the environment is global * @param variables environment variables * @param isGlobal flag to indicate type of environment to create * @returns an `UserEnvironment` object @@ -95,9 +100,11 @@ export class UserEnvironmentsService { // Check for existing global env for a user if exists error out to avoid recreation if (isGlobal) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) + if (!O.isNone(globalEnvExists)) return E.left(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); } + if (name === null && !isGlobal) + return E.left(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME); const createdEnvironment = await this.prisma.userEnvironment.create({ data: { @@ -116,11 +123,11 @@ export class UserEnvironmentsService { isGlobal: createdEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - userEnvironment, + await this.subscriptionHandler.publish( + `user_environment/${userEnvironment.userUid}`, SubscriptionType.Created, + userEnvironment, ); - return E.right(userEnvironment); } @@ -149,13 +156,14 @@ export class UserEnvironmentsService { isGlobal: updatedEnvironment.isGlobal, }; // Publish subscription for environment update - await this.publishUserEnvironmentSubscription( - updatedUserEnvironment, + await this.subscriptionHandler.publish( + `user_environment/${updatedUserEnvironment.id}`, SubscriptionType.Updated, + updatedUserEnvironment, ); return E.right(updatedUserEnvironment); } catch (e) { - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } } @@ -169,8 +177,8 @@ export class UserEnvironmentsService { try { // check if id is of a global environment if it is, don't delete and error out const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) { - const globalEnv = globalEnvExists.right; + if (O.isSome(globalEnvExists)) { + const globalEnv = globalEnvExists.value; if (globalEnv.id === id) { return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED); } @@ -188,14 +196,16 @@ export class UserEnvironmentsService { variables: JSON.stringify(deletedEnvironment.variables), isGlobal: deletedEnvironment.isGlobal, }; - // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - deletedUserEnvironment, + + // Publish subscription for environment deletion + await this.subscriptionHandler.publish( + `user_environment/${deletedUserEnvironment.id}`, SubscriptionType.Deleted, + deletedUserEnvironment, ); - return E.right(deletedUserEnvironment); + return E.right(true); } catch (e) { - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } } @@ -211,75 +221,53 @@ export class UserEnvironmentsService { isGlobal: false, }, }); - return deletedEnvironments.count; + + await this.subscriptionHandler.publish( + `user_environment/${uid}`, + SubscriptionType.DeleteMany, + deletedEnvironments.count, + ); } /** - * Deletes all existing variables in a users global environment + * Removes all existing variables in a users global environment * @param uid users uid * @param id environment id * @returns an `` of environments deleted */ - async deleteAllVariablesFromUsersGlobalEnvironment(uid: string, id: string) { + async clearGlobalEnvironments(uid: string, id: string) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) { - const env = globalEnvExists.right; - if (env.id === id) { - try { - const updatedEnvironment = await this.prisma.userEnvironment.update({ - where: { id: id }, - data: { - variables: [], - }, - }); - const updatedUserEnvironment: UserEnvironment = { - userUid: updatedEnvironment.userUid, - id: updatedEnvironment.id, - name: updatedEnvironment.name, - variables: JSON.stringify(updatedEnvironment.variables), - isGlobal: updatedEnvironment.isGlobal, - }; - // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - updatedUserEnvironment, - SubscriptionType.Updated, - ); - return E.right(updatedUserEnvironment); - } catch (e) { - return E.left(USER_ENVIRONMENT_UPDATE_FAILED); - } - } else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL); - } - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); - } + if (O.isNone(globalEnvExists)) + return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS); - // Method to publish subscriptions based on the subscription type of the environment - async publishUserEnvironmentSubscription( - userEnv: UserEnvironment, - subscriptionType: SubscriptionType, - ) { - switch (subscriptionType) { - case SubscriptionType.Created: - await this.pubsub.publish( - `user_environment/${userEnv.userUid}/created`, - userEnv, + const env = globalEnvExists.value; + if (env.id === id) { + try { + const updatedEnvironment = await this.prisma.userEnvironment.update({ + where: { id: id }, + data: { + variables: [], + }, + }); + const updatedUserEnvironment: UserEnvironment = { + userUid: updatedEnvironment.userUid, + id: updatedEnvironment.id, + name: updatedEnvironment.name, + variables: JSON.stringify(updatedEnvironment.variables), + isGlobal: updatedEnvironment.isGlobal, + }; + + // Publish subscription for environment update + await this.subscriptionHandler.publish( + `user_environment/${updatedUserEnvironment.id}`, + SubscriptionType.Updated, + updatedUserEnvironment, ); - break; - case SubscriptionType.Updated: - await this.pubsub.publish( - `user_environment/${userEnv.id}/updated`, - userEnv, - ); - break; - case SubscriptionType.Deleted: - await this.pubsub.publish( - `user_environment/${userEnv.id}/deleted`, - userEnv, - ); - break; - default: - break; - } + return E.right(updatedUserEnvironment); + } catch (e) { + return E.left(USER_ENVIRONMENT_UPDATE_FAILED); + } + } else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL); } // Method to check for existing global environments for a given user uid @@ -290,9 +278,8 @@ export class UserEnvironmentsService { isGlobal: true, }, }); - if (globalEnv === null) - return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS); - return E.right(globalEnv); + if (globalEnv == null) return O.none; + return O.some(globalEnv); } }