chore: made review changes for resolvers and introduced stringToJson from user-settings

This commit is contained in:
ankitsridhar16
2023-01-19 16:22:49 +05:30
parent 86aa0251ab
commit 669f8b0431
3 changed files with 29 additions and 32 deletions

View File

@@ -61,11 +61,10 @@ export class UserEnvironmentsResolver {
variables: string, variables: string,
): Promise<UserEnvironment> { ): Promise<UserEnvironment> {
const isGlobal = true; const isGlobal = true;
const globalEnvName: string = null;
const userEnvironment = const userEnvironment =
await this.userEnvironmentsService.createUserEnvironment( await this.userEnvironmentsService.createUserEnvironment(
user.uid, user.uid,
globalEnvName, null,
variables, variables,
isGlobal, isGlobal,
); );
@@ -126,7 +125,7 @@ export class UserEnvironmentsResolver {
} }
@Mutation(() => Number, { @Mutation(() => Number, {
description: 'Deletes users all personal environments', description: 'Deletes all of users personal environments',
}) })
@UseGuards(GqlAuthGuard) @UseGuards(GqlAuthGuard)
async deleteUserEnvironments(@GqlUser() user: User): Promise<void> { async deleteUserEnvironments(@GqlUser() user: User): Promise<void> {
@@ -159,15 +158,8 @@ export class UserEnvironmentsResolver {
resolve: (value) => value, resolve: (value) => value,
}) })
@UseGuards(GqlAuthGuard) @UseGuards(GqlAuthGuard)
userEnvironmentCreated( userEnvironmentCreated(@GqlUser() user: User) {
@Args({ return this.pubsub.asyncIterator(`user_environment/${user.uid}/created`);
name: 'userUid',
description: 'Users uid',
type: () => ID,
})
userUid: string,
) {
return this.pubsub.asyncIterator(`user_environment/${userUid}/created`);
} }
@Subscription(() => UserEnvironment, { @Subscription(() => UserEnvironment, {
@@ -201,4 +193,15 @@ export class UserEnvironmentsResolver {
) { ) {
return this.pubsub.asyncIterator(`user_environment/${id}/deleted`); return this.pubsub.asyncIterator(`user_environment/${id}/deleted`);
} }
@Subscription(() => UserEnvironment, {
description: 'Listen for User Environment DeleteMany',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userEnvironmentDeleteMany(@GqlUser() user: User) {
return this.pubsub.asyncIterator(
`user_environment/${user.uid}/delete_many`,
);
}
} }

View File

@@ -10,6 +10,7 @@ import {
} from '../errors'; } from '../errors';
import { PubSubService } from '../pubsub/pubsub.service'; import { PubSubService } from '../pubsub/pubsub.service';
import { SubscriptionHandler } from '../subscription-handler'; import { SubscriptionHandler } from '../subscription-handler';
import { SubscriptionType } from '../types/subscription-types';
const mockPrisma = mockDeep<PrismaService>(); const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>(); const mockPubSub = mockDeep<PubSubService>();
@@ -23,13 +24,6 @@ const userEnvironmentsService = new UserEnvironmentsService(
mockSubscriptionHandler, mockSubscriptionHandler,
); );
enum SubscriptionType {
Created = 'created',
Updated = 'updated',
Deleted = 'deleted',
DeleteMany = 'delete_many',
}
const userPersonalEnvironments = [ const userPersonalEnvironments = [
{ {
userUiD: 'abc123', userUiD: 'abc123',
@@ -344,7 +338,9 @@ describe('UserEnvironmentsService', () => {
}); });
test('Should resolve left and not update a users environment if env doesnt exist ', async () => { test('Should resolve left and not update a users environment if env doesnt exist ', async () => {
mockPrisma.userEnvironment.update.mockRejectedValueOnce({}); mockPrisma.userEnvironment.update.mockRejectedValueOnce(
'RejectOnNotFound',
);
return expect( return expect(
await userEnvironmentsService.updateUserEnvironment( await userEnvironmentsService.updateUserEnvironment(
@@ -355,7 +351,7 @@ describe('UserEnvironmentsService', () => {
).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
}); });
test('Should resolve right, update a users personal environment and publish an updated subscription ', async () => { test('Should update a users personal environment and publish an updated subscription ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123', userUid: 'abc123',
id: '123', id: '123',
@@ -385,7 +381,7 @@ describe('UserEnvironmentsService', () => {
); );
}); });
test('Should resolve right, update a users global environment and publish an updated subscription ', async () => { test('Should update a users global environment and publish an updated subscription ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123', userUid: 'abc123',
id: '123', id: '123',

View File

@@ -14,14 +14,8 @@ import {
USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME,
} from '../errors'; } from '../errors';
import { SubscriptionHandler } from '../subscription-handler'; import { SubscriptionHandler } from '../subscription-handler';
import { SubscriptionType } from '../types/subscription-types';
// Contains constants for the subscription types we send to subscription handler import { stringToJson } from '../utils';
enum SubscriptionType {
Created = 'created',
Updated = 'updated',
Deleted = 'deleted',
DeleteMany = 'delete_many',
}
@Injectable() @Injectable()
export class UserEnvironmentsService { export class UserEnvironmentsService {
@@ -106,11 +100,13 @@ export class UserEnvironmentsService {
if (name === null && !isGlobal) if (name === null && !isGlobal)
return E.left(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME); return E.left(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME);
const envVariables = stringToJson(variables);
if (E.isLeft(envVariables)) return E.left(envVariables.left);
const createdEnvironment = await this.prisma.userEnvironment.create({ const createdEnvironment = await this.prisma.userEnvironment.create({
data: { data: {
userUid: uid, userUid: uid,
name: name, name: name,
variables: JSON.parse(variables), variables: envVariables.right,
isGlobal: isGlobal, isGlobal: isGlobal,
}, },
}); });
@@ -139,12 +135,14 @@ export class UserEnvironmentsService {
* @returns an Either of `UserEnvironment` or error * @returns an Either of `UserEnvironment` or error
*/ */
async updateUserEnvironment(id: string, name: string, variables: string) { async updateUserEnvironment(id: string, name: string, variables: string) {
const envVariables = stringToJson(variables);
if (E.isLeft(envVariables)) return E.left(envVariables.left);
try { try {
const updatedEnvironment = await this.prisma.userEnvironment.update({ const updatedEnvironment = await this.prisma.userEnvironment.update({
where: { id: id }, where: { id: id },
data: { data: {
name: name, name: name,
variables: JSON.parse(variables), variables: envVariables.right,
}, },
}); });