chore: introduced subscription handler and fixed requested review changes
This commit is contained in:
@@ -3,20 +3,24 @@ import { UserEnvironment } from './user-environments.model';
|
|||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import { PubSubService } from '../pubsub/pubsub.service';
|
import { PubSubService } from '../pubsub/pubsub.service';
|
||||||
import * as E from 'fp-ts/Either';
|
import * as E from 'fp-ts/Either';
|
||||||
|
import * as O from 'fp-ts/Option';
|
||||||
import {
|
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_DELETION_FAILED,
|
||||||
USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS,
|
|
||||||
USER_ENVIRONMENT_GLOBAL_ENV_EXISTS,
|
USER_ENVIRONMENT_GLOBAL_ENV_EXISTS,
|
||||||
USER_ENVIRONMENT_IS_NOT_GLOBAL,
|
USER_ENVIRONMENT_IS_NOT_GLOBAL,
|
||||||
USER_ENVIRONMENT_UPDATE_FAILED,
|
USER_ENVIRONMENT_UPDATE_FAILED,
|
||||||
|
USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME,
|
||||||
} from '../errors';
|
} 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 {
|
enum SubscriptionType {
|
||||||
Created = 'created',
|
Created = 'created',
|
||||||
Updated = 'updated',
|
Updated = 'updated',
|
||||||
Deleted = 'deleted',
|
Deleted = 'deleted',
|
||||||
|
DeleteMany = 'delete_many',
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -24,6 +28,7 @@ export class UserEnvironmentsService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly prisma: PrismaService,
|
private readonly prisma: PrismaService,
|
||||||
private readonly pubsub: PubSubService,
|
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
|
* Create a personal or global user environment
|
||||||
* @param uid Users uid
|
* @param uid Users uid
|
||||||
* @param name environments name
|
* @param name environments name, null if the environment is global
|
||||||
* @param variables environment variables
|
* @param variables environment variables
|
||||||
* @param isGlobal flag to indicate type of environment to create
|
* @param isGlobal flag to indicate type of environment to create
|
||||||
* @returns an `UserEnvironment` object
|
* @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
|
// Check for existing global env for a user if exists error out to avoid recreation
|
||||||
if (isGlobal) {
|
if (isGlobal) {
|
||||||
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
||||||
if (E.isRight(globalEnvExists))
|
if (!O.isNone(globalEnvExists))
|
||||||
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS);
|
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({
|
const createdEnvironment = await this.prisma.userEnvironment.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -116,11 +123,11 @@ export class UserEnvironmentsService {
|
|||||||
isGlobal: createdEnvironment.isGlobal,
|
isGlobal: createdEnvironment.isGlobal,
|
||||||
};
|
};
|
||||||
// Publish subscription for environment creation
|
// Publish subscription for environment creation
|
||||||
await this.publishUserEnvironmentSubscription(
|
await this.subscriptionHandler.publish(
|
||||||
userEnvironment,
|
`user_environment/${userEnvironment.userUid}`,
|
||||||
SubscriptionType.Created,
|
SubscriptionType.Created,
|
||||||
|
userEnvironment,
|
||||||
);
|
);
|
||||||
|
|
||||||
return E.right(userEnvironment);
|
return E.right(userEnvironment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,13 +156,14 @@ export class UserEnvironmentsService {
|
|||||||
isGlobal: updatedEnvironment.isGlobal,
|
isGlobal: updatedEnvironment.isGlobal,
|
||||||
};
|
};
|
||||||
// Publish subscription for environment update
|
// Publish subscription for environment update
|
||||||
await this.publishUserEnvironmentSubscription(
|
await this.subscriptionHandler.publish(
|
||||||
updatedUserEnvironment,
|
`user_environment/${updatedUserEnvironment.id}`,
|
||||||
SubscriptionType.Updated,
|
SubscriptionType.Updated,
|
||||||
|
updatedUserEnvironment,
|
||||||
);
|
);
|
||||||
return E.right(updatedUserEnvironment);
|
return E.right(updatedUserEnvironment);
|
||||||
} catch (e) {
|
} 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 {
|
try {
|
||||||
// check if id is of a global environment if it is, don't delete and error out
|
// check if id is of a global environment if it is, don't delete and error out
|
||||||
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
||||||
if (E.isRight(globalEnvExists)) {
|
if (O.isSome(globalEnvExists)) {
|
||||||
const globalEnv = globalEnvExists.right;
|
const globalEnv = globalEnvExists.value;
|
||||||
if (globalEnv.id === id) {
|
if (globalEnv.id === id) {
|
||||||
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED);
|
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED);
|
||||||
}
|
}
|
||||||
@@ -188,14 +196,16 @@ export class UserEnvironmentsService {
|
|||||||
variables: JSON.stringify(deletedEnvironment.variables),
|
variables: JSON.stringify(deletedEnvironment.variables),
|
||||||
isGlobal: deletedEnvironment.isGlobal,
|
isGlobal: deletedEnvironment.isGlobal,
|
||||||
};
|
};
|
||||||
// Publish subscription for environment creation
|
|
||||||
await this.publishUserEnvironmentSubscription(
|
// Publish subscription for environment deletion
|
||||||
deletedUserEnvironment,
|
await this.subscriptionHandler.publish(
|
||||||
|
`user_environment/${deletedUserEnvironment.id}`,
|
||||||
SubscriptionType.Deleted,
|
SubscriptionType.Deleted,
|
||||||
|
deletedUserEnvironment,
|
||||||
);
|
);
|
||||||
return E.right(deletedUserEnvironment);
|
return E.right(true);
|
||||||
} catch (e) {
|
} 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,
|
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 uid users uid
|
||||||
* @param id environment id
|
* @param id environment id
|
||||||
* @returns an `` of environments deleted
|
* @returns an `` of environments deleted
|
||||||
*/
|
*/
|
||||||
async deleteAllVariablesFromUsersGlobalEnvironment(uid: string, id: string) {
|
async clearGlobalEnvironments(uid: string, id: string) {
|
||||||
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
|
||||||
if (E.isRight(globalEnvExists)) {
|
if (O.isNone(globalEnvExists))
|
||||||
const env = globalEnvExists.right;
|
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method to publish subscriptions based on the subscription type of the environment
|
const env = globalEnvExists.value;
|
||||||
async publishUserEnvironmentSubscription(
|
if (env.id === id) {
|
||||||
userEnv: UserEnvironment,
|
try {
|
||||||
subscriptionType: SubscriptionType,
|
const updatedEnvironment = await this.prisma.userEnvironment.update({
|
||||||
) {
|
where: { id: id },
|
||||||
switch (subscriptionType) {
|
data: {
|
||||||
case SubscriptionType.Created:
|
variables: [],
|
||||||
await this.pubsub.publish(
|
},
|
||||||
`user_environment/${userEnv.userUid}/created`,
|
});
|
||||||
userEnv,
|
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;
|
return E.right(updatedUserEnvironment);
|
||||||
case SubscriptionType.Updated:
|
} catch (e) {
|
||||||
await this.pubsub.publish(
|
return E.left(USER_ENVIRONMENT_UPDATE_FAILED);
|
||||||
`user_environment/${userEnv.id}/updated`,
|
}
|
||||||
userEnv,
|
} else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL);
|
||||||
);
|
|
||||||
break;
|
|
||||||
case SubscriptionType.Deleted:
|
|
||||||
await this.pubsub.publish(
|
|
||||||
`user_environment/${userEnv.id}/deleted`,
|
|
||||||
userEnv,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to check for existing global environments for a given user uid
|
// Method to check for existing global environments for a given user uid
|
||||||
@@ -290,9 +278,8 @@ export class UserEnvironmentsService {
|
|||||||
isGlobal: true,
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user