chore: resolved all merge conflicts

This commit is contained in:
Balu Babu
2023-01-24 19:35:40 +05:30
31 changed files with 2864 additions and 14 deletions

View File

@@ -112,4 +112,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}

View File

@@ -86,8 +86,13 @@ model User {
isAdmin Boolean @default(false)
refreshToken String?
accounts Account[]
createdOn DateTime @default(now()) @db.Timestamp(3)
PasswordlessVerification PasswordlessVerification[]
settings UserSettings?
UserHistory UserHistory[]
UserEnvironments UserEnvironment[]
currentRESTSession Json?
currentGQLSession Json?
createdOn DateTime @default(now()) @db.Timestamp(3)
}
model Account {
@@ -115,6 +120,39 @@ model PasswordlessVerification {
@@unique(fields: [deviceIdentifier, token], name: "passwordless_deviceIdentifier_tokens")
}
model UserSettings {
id String @id @default(cuid())
userUid String @unique
user User @relation(fields: [userUid], references: [uid], onDelete: Cascade)
properties Json
updatedOn DateTime @updatedAt @db.Timestamp(3)
}
model UserHistory {
id String @id @default(cuid())
userUid String
user User @relation(fields: [userUid], references: [uid], onDelete: Cascade)
reqType ReqType
request Json
responseMetadata Json
isStarred Boolean
executedOn DateTime @default(now()) @db.Timestamp(3)
}
enum ReqType {
REST
GQL
}
model UserEnvironment {
id String @id @default(cuid())
userUid String
user User @relation(fields: [userUid], references: [uid], onDelete: Cascade)
name String?
variables Json
isGlobal Boolean
}
enum TeamMemberRole {
OWNER
VIEWER

View File

@@ -4,6 +4,9 @@ import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { UserModule } from './user/user.module';
import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin';
import { AuthModule } from './auth/auth.module';
import { UserSettingsModule } from './user-settings/user-settings.module';
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
import { UserHistoryModule } from './user-history/user-history.module';
@Module({
imports: [
@@ -50,6 +53,9 @@ import { AuthModule } from './auth/auth.module';
}),
UserModule,
AuthModule,
UserSettingsModule,
UserEnvironmentsModule,
UserHistoryModule,
],
providers: [GQLComplexityPlugin],
})

View File

@@ -8,6 +8,12 @@ export const EMAIL_FAILED = 'email/failed' as const;
*/
export const AUTH_FAIL = 'auth/fail';
/**
* Invalid JSON
* (Utils)
*/
export const JSON_INVALID = 'json_invalid';
/**
* Tried to delete an user data document from fb firestore but failed.
* (FirebaseService)
@@ -20,6 +26,12 @@ export const USER_FB_DOCUMENT_DELETION_FAILED =
*/
export const USER_NOT_FOUND = 'user/not_found' as const;
/**
* User update failure
* (UserService)
*/
export const USER_UPDATE_FAILED = 'user/update_failed' as const;
/**
* User deletion failure
* (UserService)
@@ -155,6 +167,94 @@ export const TEAM_ENVIRONMMENT_NOT_FOUND =
export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER =
'team_environment/not_team_member' as const;
/**
* User setting not found for a user
* (UserSettingsService)
*/
export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const;
/**
* User setting already exists for a user
* (UserSettingsService)
*/
export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_exists' as const;
/**
* User setting invalid (null) settings
* (UserSettingsService)
*/
export const USER_SETTINGS_NULL_SETTINGS = 'user_settings/null_settings' as const;
/*
* Global environment doesnt exists for the user
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS =
'user_environment/global_env_does_not_exists' as const;
/**
* Global environment already exists for the user
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_GLOBAL_ENV_EXISTS =
'user_environment/global_env_already_exists' as const;
/*
/**
* User environment doesn't exist for the user
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS =
'user_environment/user_env_does_not_exists' as const;
/*
/**
* Cannot delete the global user environment
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED =
'user_environment/user_env_global_env_deletion_failed' as const;
/*
/**
* User environment is not a global environment
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_IS_NOT_GLOBAL =
'user_environment/user_env_is_not_global' as const;
/*
/**
* User environment update failed
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_UPDATE_FAILED =
'user_environment/user_env_update_failed' as const;
/*
/**
* User environment invalid environment name
* (UserEnvironmentsService)
*/
export const USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME =
'user_environment/user_env_invalid_env_name' as const;
/*
/**
* User history not found
* (UserHistoryService)
*/
export const USER_HISTORY_NOT_FOUND = 'user_history/history_not_found' as const;
/*
/**
* Invalid Request Type in History
* (UserHistoryService)
*/
export const USER_HISTORY_INVALID_REQ_TYPE =
'user_history/req_type_invalid' as const;
/*
|------------------------------------|

View File

@@ -16,4 +16,4 @@ export class PrismaService
async onModuleDestroy() {
await this.$disconnect();
}
}
}

View File

@@ -4,6 +4,7 @@ import { default as Redis, RedisOptions } from 'ioredis';
import { RedisPubSub } from 'graphql-redis-subscriptions';
import { PubSub as LocalPubSub } from 'graphql-subscriptions';
import { TopicDef } from './topicsDefs';
/**
* RedisPubSub uses JSON parsing for back and forth conversion, which loses Date objects, hence this reviver brings them back
@@ -70,7 +71,7 @@ export class PubSubService implements OnModuleInit {
return this.pubsub.asyncIterator(topic, options);
}
async publish(topic: string, payload: any) {
async publish<T extends keyof TopicDef>(topic: T, payload: TopicDef[T]) {
await this.pubsub.publish(topic, payload);
}
}

View File

@@ -0,0 +1,19 @@
import { User } from 'src/user/user.model';
import { UserSettings } from 'src/user-settings/user-settings.model';
import { UserEnvironment } from '../user-environment/user-environments.model';
import { UserHistory } from '../user-history/user-history.model';
// A custom message type that defines the topic and the corresponding payload.
// For every module that publishes a subscription add its type def and the possible subscription type.
export type TopicDef = {
[topic: `user/${string}/${'updated'}`]: User;
[topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings;
[
topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}`
]: UserEnvironment;
[topic: `user_environment/${string}/deleted_many`]: number;
[
topic: `user_history/${string}/${'created' | 'updated' | 'deleted'}`
]: UserHistory;
[topic: `user_history/${string}/deleted_many`]: number;
};

View File

@@ -0,0 +1,30 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class UserEnvironment {
@Field(() => ID, {
description: 'ID of the User Environment',
})
id: string;
@Field(() => ID, {
description: 'ID of the user this environment belongs to',
})
userUid: string;
@Field(() => String, {
nullable: true,
description: 'Name of the environment',
})
name: string | null | undefined; // types have a union to avoid TS warnings and field is nullable when it is global env
@Field({
description: 'All variables present in the environment',
})
variables: string; // JSON string of the variables object (format:[{ key: "bla", value: "bla_val" }, ...] ) which will be received from the client
@Field({
description: 'Flag to indicate the environment is global or not',
})
isGlobal: boolean;
}

View File

@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { PubSubModule } from '../pubsub/pubsub.module';
import { UserModule } from '../user/user.module';
import { UserEnvsUserResolver } from './user.resolver';
import { UserEnvironmentsResolver } from './user-environments.resolver';
import { UserEnvironmentsService } from './user-environments.service';
@Module({
imports: [PrismaModule, PubSubModule, UserModule],
providers: [
UserEnvironmentsResolver,
UserEnvironmentsService,
UserEnvsUserResolver,
],
exports: [UserEnvironmentsService],
})
export class UserEnvironmentsModule {}

View File

@@ -0,0 +1,207 @@
import { Args, ID, Mutation, Resolver, Subscription } from '@nestjs/graphql';
import { PubSubService } from '../pubsub/pubsub.service';
import { UserEnvironment } from './user-environments.model';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '../guards/gql-auth.guard';
import { GqlUser } from '../decorators/gql-user.decorator';
import { User } from '../user/user.model';
import { UserEnvironmentsService } from './user-environments.service';
import * as E from 'fp-ts/Either';
import { throwErr } from 'src/utils';
@Resolver()
export class UserEnvironmentsResolver {
constructor(
private readonly userEnvironmentsService: UserEnvironmentsService,
private readonly pubsub: PubSubService,
) {}
/* Mutations */
@Mutation(() => UserEnvironment, {
description: 'Create a new personal user environment for given user uid',
})
@UseGuards(GqlAuthGuard)
async createUserEnvironment(
@GqlUser() user: User,
@Args({
name: 'name',
description:
'Name of the User Environment, if global send an empty string',
})
name: string,
@Args({
name: 'variables',
description: 'JSON string of the variables object',
})
variables: string,
): Promise<UserEnvironment> {
const isGlobal = false;
const userEnvironment =
await this.userEnvironmentsService.createUserEnvironment(
user.uid,
name,
variables,
isGlobal,
);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
@Mutation(() => UserEnvironment, {
description: 'Create a new global user environment for given user uid',
})
@UseGuards(GqlAuthGuard)
async createUserGlobalEnvironment(
@GqlUser() user: User,
@Args({
name: 'variables',
description: 'JSON string of the variables object',
})
variables: string,
): Promise<UserEnvironment> {
const isGlobal = true;
const userEnvironment =
await this.userEnvironmentsService.createUserEnvironment(
user.uid,
null,
variables,
isGlobal,
);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
@Mutation(() => UserEnvironment, {
description: 'Updates a users personal or global environment',
})
@UseGuards(GqlAuthGuard)
async updateUserEnvironment(
@Args({
name: 'id',
description: 'ID of the user environment',
type: () => ID,
})
id: string,
@Args({
name: 'name',
description:
'Name of the User Environment, if global send an empty string',
})
name: string,
@Args({
name: 'variables',
description: 'JSON string of the variables object',
})
variables: string,
): Promise<UserEnvironment> {
const userEnvironment =
await this.userEnvironmentsService.updateUserEnvironment(
id,
name,
variables,
);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
@Mutation(() => Boolean, {
description: 'Deletes a users personal environment',
})
@UseGuards(GqlAuthGuard)
async deleteUserEnvironment(
@GqlUser() user: User,
@Args({
name: 'id',
description: 'ID of the user environment',
type: () => ID,
})
id: string,
): Promise<boolean> {
const userEnvironment =
await this.userEnvironmentsService.deleteUserEnvironment(user.uid, id);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
@Mutation(() => Number, {
description: 'Deletes all of users personal environments',
})
@UseGuards(GqlAuthGuard)
async deleteUserEnvironments(@GqlUser() user: User): Promise<number> {
return await this.userEnvironmentsService.deleteUserEnvironments(user.uid);
}
@Mutation(() => UserEnvironment, {
description: 'Deletes all variables inside a users global environment',
})
@UseGuards(GqlAuthGuard)
async clearGlobalEnvironments(
@GqlUser() user: User,
@Args({
name: 'id',
description: 'ID of the users global environment',
type: () => ID,
})
id: string,
): Promise<UserEnvironment> {
const userEnvironment =
await this.userEnvironmentsService.clearGlobalEnvironments(user.uid, id);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
/* Subscriptions */
@Subscription(() => UserEnvironment, {
description: 'Listen for User Environment Creation',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userEnvironmentCreated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_environment/${user.uid}/created`);
}
@Subscription(() => UserEnvironment, {
description: 'Listen for User Environment updates',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userEnvironmentUpdated(
@Args({
name: 'id',
description: 'Environment id',
type: () => ID,
})
id: string,
) {
return this.pubsub.asyncIterator(`user_environment/${id}/updated`);
}
@Subscription(() => UserEnvironment, {
description: 'Listen for User Environment deletion',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userEnvironmentDeleted(
@Args({
name: 'id',
description: 'Environment id',
type: () => ID,
})
id: string,
) {
return this.pubsub.asyncIterator(`user_environment/${id}/deleted`);
}
@Subscription(() => Number, {
description: 'Listen for User Environment DeleteMany',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userEnvironmentDeleteMany(@GqlUser() user: User) {
return this.pubsub.asyncIterator(
`user_environment/${user.uid}/deleted_many`,
);
}
}

View File

@@ -0,0 +1,565 @@
import { UserEnvironment } from './user-environments.model';
import { mockDeep, mockReset } from 'jest-mock-extended';
import { PrismaService } from '../prisma/prisma.service';
import { UserEnvironmentsService } from './user-environments.service';
import {
USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS,
USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED,
USER_ENVIRONMENT_GLOBAL_ENV_EXISTS,
USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME,
} from '../errors';
import { PubSubService } from '../pubsub/pubsub.service';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userEnvironmentsService = new UserEnvironmentsService(
mockPrisma,
mockPubSub as any,
);
const userPersonalEnvironments = [
{
userUiD: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
},
{
userUiD: 'abc123',
id: '1234',
name: 'test2',
variables: [{}],
isGlobal: false,
},
];
beforeEach(() => {
mockReset(mockPrisma);
mockPubSub.publish.mockClear();
});
describe('UserEnvironmentsService', () => {
describe('fetchUserEnvironments', () => {
test('Should return a list of users personal environments', async () => {
mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([
{
userUid: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
},
{
userUid: 'abc123',
id: '1234',
name: 'test2',
variables: [{}],
isGlobal: false,
},
]);
const userEnvironments: UserEnvironment[] = [
{
userUid: userPersonalEnvironments[0].userUiD,
id: userPersonalEnvironments[0].id,
name: userPersonalEnvironments[0].name,
variables: JSON.stringify(userPersonalEnvironments[0].variables),
isGlobal: userPersonalEnvironments[0].isGlobal,
},
{
userUid: userPersonalEnvironments[1].userUiD,
id: userPersonalEnvironments[1].id,
name: userPersonalEnvironments[1].name,
variables: JSON.stringify(userPersonalEnvironments[1].variables),
isGlobal: userPersonalEnvironments[1].isGlobal,
},
];
return expect(
await userEnvironmentsService.fetchUserEnvironments('abc123'),
).toEqual(userEnvironments);
});
test('Should return an empty list of users personal environments', async () => {
mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([]);
return expect(
await userEnvironmentsService.fetchUserEnvironments('testuser'),
).toEqual([]);
});
test('Should return an empty list of users personal environments if user uid is invalid', async () => {
mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([]);
return expect(
await userEnvironmentsService.fetchUserEnvironments('invaliduid'),
).toEqual([]);
});
});
describe('fetchUserGlobalEnvironment', () => {
test('Should resolve right and return a Global Environment for the uid', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
id: 'genv1',
userUid: 'abc',
name: '',
variables: [{}],
isGlobal: true,
});
expect(
await userEnvironmentsService.fetchUserGlobalEnvironment('abc'),
).toEqualRight(<UserEnvironment>{
id: 'genv1',
userUid: 'abc',
name: '',
variables: JSON.stringify([{}]),
isGlobal: true,
});
});
test('Should resolve left and return an error if global env it doesnt exists', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
expect(
await userEnvironmentsService.fetchUserGlobalEnvironment('abc'),
).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
});
describe('createUserEnvironment', () => {
test('Should resolve right and create a users personal environment and return a `UserEnvironment` object ', async () => {
mockPrisma.userEnvironment.create.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: 'test',
variables: JSON.stringify([{}]),
isGlobal: false,
};
return expect(
await userEnvironmentsService.createUserEnvironment(
'abc123',
'test',
'[{}]',
false,
),
).toEqualRight(result);
});
test('Should resolve right and create a new users global environment and return a `UserEnvironment` object ', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
mockPrisma.userEnvironment.create.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: null,
variables: [{}],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: null,
variables: JSON.stringify([{}]),
isGlobal: true,
};
return expect(
await userEnvironmentsService.createUserEnvironment(
'abc123',
null,
'[{}]',
true,
),
).toEqualRight(result);
});
test('Should resolve left and not create a new users global environment if existing global env exists ', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: null,
variables: [{}],
isGlobal: true,
});
return expect(
await userEnvironmentsService.createUserEnvironment(
'abc123',
null,
'[{}]',
true,
),
).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS);
});
test('Should resolve left when an invalid personal environment name has been passed', async () => {
return expect(
await userEnvironmentsService.createUserEnvironment(
'abc123',
null,
'[{}]',
false,
),
).toEqualLeft(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME);
});
test('Should create a users personal environment and publish a created subscription', async () => {
mockPrisma.userEnvironment.create.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: 'test',
variables: JSON.stringify([{}]),
isGlobal: false,
};
await userEnvironmentsService.createUserEnvironment(
'abc123',
'test',
'[{}]',
false,
);
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.userUid}/created`,
result,
);
});
test('Should create a users global environment and publish a created subscription', async () => {
mockPrisma.userEnvironment.create.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: '',
variables: [{}],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: '',
variables: JSON.stringify([{}]),
isGlobal: true,
};
await userEnvironmentsService.createUserEnvironment(
'abc123',
'',
'[{}]',
true,
);
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.userUid}/created`,
result,
);
});
});
describe('UpdateUserEnvironment', () => {
test('Should resolve right and update a users personal or environment and return a `UserEnvironment` object ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: 'test',
variables: JSON.stringify([{}]),
isGlobal: false,
};
return expect(
await userEnvironmentsService.updateUserEnvironment(
'abc123',
'test',
'[{}]',
),
).toEqualRight(result);
});
test('Should resolve right and update a users global environment and return a `UserEnvironment` object ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: null,
variables: [{}],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: null,
variables: JSON.stringify([{}]),
isGlobal: true,
};
return expect(
await userEnvironmentsService.updateUserEnvironment(
'abc123',
null,
'[{}]',
),
).toEqualRight(result);
});
test('Should resolve left and not update a users environment if env doesnt exist ', async () => {
mockPrisma.userEnvironment.update.mockRejectedValueOnce(
'RejectOnNotFound',
);
return expect(
await userEnvironmentsService.updateUserEnvironment(
'abc123',
'test',
'[{}]',
),
).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
test('Should update a users personal environment and publish an updated subscription ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: 'test',
variables: [{}],
isGlobal: false,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: 'test',
variables: JSON.stringify([{}]),
isGlobal: false,
};
await userEnvironmentsService.updateUserEnvironment(
'abc123',
'test',
'[{}]',
);
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.id}/updated`,
result,
);
});
test('Should update a users global environment and publish an updated subscription ', async () => {
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: '123',
name: null,
variables: [{}],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: '123',
name: null,
variables: JSON.stringify([{}]),
isGlobal: true,
};
await userEnvironmentsService.updateUserEnvironment(
'abc123',
null,
'[{}]',
);
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.id}/updated`,
result,
);
});
});
describe('deleteUserEnvironment', () => {
test('Should resolve right and delete a users personal environment and return a `UserEnvironment` object ', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
mockPrisma.userEnvironment.delete.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [{}],
isGlobal: false,
});
return expect(
await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'),
).toEqualRight(true);
});
test('Should resolve left and return an error when deleting a global user environment', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: 'genv1',
name: 'en1',
variables: [{}],
isGlobal: true,
});
return expect(
await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'),
).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED);
});
test('Should resolve left and return an error when deleting an invalid user environment', async () => {
mockPrisma.userEnvironment.delete.mockResolvedValueOnce(null);
return expect(
await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'),
).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
test('Should resolve right, delete a users personal environment and publish a deleted subscription', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
mockPrisma.userEnvironment.delete.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [{}],
isGlobal: false,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: JSON.stringify([{}]),
isGlobal: false,
};
await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.id}/deleted`,
result,
);
});
});
describe('deleteUserEnvironments', () => {
test('Should publish a subscription with a count of deleted environments', async () => {
mockPrisma.userEnvironment.deleteMany.mockResolvedValueOnce({
count: 1,
});
await userEnvironmentsService.deleteUserEnvironments('abc123');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${'abc123'}/deleted_many`,
1,
);
});
});
describe('clearGlobalEnvironments', () => {
test('Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [{}],
isGlobal: true,
});
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: JSON.stringify([]),
isGlobal: true,
};
return expect(
await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1'),
).toEqualRight(result);
});
test('Should resolve left and return an error if global environment id and passed id dont match', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: 'genv2',
name: 'en1',
variables: [{}],
isGlobal: true,
});
return expect(
await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'),
).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
test('Should resolve right,delete all variables inside users global environment and publish an updated subscription', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [{}],
isGlobal: true,
});
mockPrisma.userEnvironment.update.mockResolvedValueOnce({
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: [],
isGlobal: true,
});
const result: UserEnvironment = {
userUid: 'abc123',
id: 'env1',
name: 'en1',
variables: JSON.stringify([]),
isGlobal: true,
};
await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_environment/${result.id}/updated`,
result,
);
});
});
});

View File

@@ -0,0 +1,278 @@
import { Injectable } from '@nestjs/common';
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_DOES_NOT_EXISTS,
USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS,
USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED,
USER_ENVIRONMENT_GLOBAL_ENV_EXISTS,
USER_ENVIRONMENT_IS_NOT_GLOBAL,
USER_ENVIRONMENT_UPDATE_FAILED,
USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME,
} from '../errors';
import { stringToJson } from '../utils';
@Injectable()
export class UserEnvironmentsService {
constructor(
private readonly prisma: PrismaService,
private readonly pubsub: PubSubService,
) {}
/**
* Fetch personal user environments
* @param uid Users uid
* @returns array of users personal environments
*/
async fetchUserEnvironments(uid: string) {
const environments = await this.prisma.userEnvironment.findMany({
where: {
userUid: uid,
isGlobal: false,
},
});
const userEnvironments: UserEnvironment[] = [];
environments.forEach((environment) => {
userEnvironments.push(<UserEnvironment>{
userUid: environment.userUid,
id: environment.id,
name: environment.name,
variables: JSON.stringify(environment.variables),
isGlobal: environment.isGlobal,
});
});
return userEnvironments;
}
/**
* Fetch users global environment
* @param uid Users uid
* @returns an `UserEnvironment` object
*/
async fetchUserGlobalEnvironment(uid: string) {
const globalEnvironment = await this.prisma.userEnvironment.findFirst({
where: {
userUid: uid,
isGlobal: true,
},
});
if (globalEnvironment != null) {
return E.right(<UserEnvironment>{
userUid: globalEnvironment.userUid,
id: globalEnvironment.id,
name: globalEnvironment.name,
variables: JSON.stringify(globalEnvironment.variables),
isGlobal: globalEnvironment.isGlobal,
});
}
return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
}
/**
* Create a personal or global user environment
* @param uid Users uid
* @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
*/
async createUserEnvironment(
uid: string,
name: string,
variables: string,
isGlobal: boolean,
) {
// Check for existing global env for a user if exists error out to avoid recreation
if (isGlobal) {
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
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 envVariables = stringToJson(variables);
if (E.isLeft(envVariables)) return E.left(envVariables.left);
const createdEnvironment = await this.prisma.userEnvironment.create({
data: {
userUid: uid,
name: name,
variables: envVariables.right,
isGlobal: isGlobal,
},
});
const userEnvironment: UserEnvironment = {
userUid: createdEnvironment.userUid,
id: createdEnvironment.id,
name: createdEnvironment.name,
variables: JSON.stringify(createdEnvironment.variables),
isGlobal: createdEnvironment.isGlobal,
};
// Publish subscription for environment creation
await this.pubsub.publish(
`user_environment/${userEnvironment.userUid}/created`,
userEnvironment,
);
return E.right(userEnvironment);
}
/**
* Update an existing personal or global user environment
* @param id environment id
* @param name environments name
* @param variables environment variables
* @returns an Either of `UserEnvironment` or error
*/
async updateUserEnvironment(id: string, name: string, variables: string) {
const envVariables = stringToJson(variables);
if (E.isLeft(envVariables)) return E.left(envVariables.left);
try {
const updatedEnvironment = await this.prisma.userEnvironment.update({
where: { id: id },
data: {
name: name,
variables: envVariables.right,
},
});
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.pubsub.publish(
`user_environment/${updatedUserEnvironment.id}/updated`,
updatedUserEnvironment,
);
return E.right(updatedUserEnvironment);
} catch (e) {
return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
}
}
/**
* Delete an existing personal user environment based on environment id
* @param uid users uid
* @param id environment id
* @returns an Either of deleted `UserEnvironment` or error
*/
async deleteUserEnvironment(uid: string, id: string) {
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 (O.isSome(globalEnvExists)) {
const globalEnv = globalEnvExists.value;
if (globalEnv.id === id) {
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED);
}
}
const deletedEnvironment = await this.prisma.userEnvironment.delete({
where: {
id: id,
},
});
const deletedUserEnvironment: UserEnvironment = {
userUid: deletedEnvironment.userUid,
id: deletedEnvironment.id,
name: deletedEnvironment.name,
variables: JSON.stringify(deletedEnvironment.variables),
isGlobal: deletedEnvironment.isGlobal,
};
// Publish subscription for environment deletion
await this.pubsub.publish(
`user_environment/${deletedUserEnvironment.id}/deleted`,
deletedUserEnvironment,
);
return E.right(true);
} catch (e) {
return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
}
}
/**
* Deletes all existing personal user environments
* @param uid user uid
* @returns a count of environments deleted
*/
async deleteUserEnvironments(uid: string) {
const deletedEnvironments = await this.prisma.userEnvironment.deleteMany({
where: {
userUid: uid,
isGlobal: false,
},
});
// Publish subscription for multiple environment deletions
await this.pubsub.publish(
`user_environment/${uid}/deleted_many`,
deletedEnvironments.count,
);
return deletedEnvironments.count;
}
/**
* Removes all existing variables in a users global environment
* @param uid users uid
* @param id environment id
* @returns an `` of environments deleted
*/
async clearGlobalEnvironments(uid: string, id: string) {
const globalEnvExists = await this.checkForExistingGlobalEnv(uid);
if (O.isNone(globalEnvExists))
return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS);
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.pubsub.publish(
`user_environment/${updatedUserEnvironment.id}/updated`,
updatedUserEnvironment,
);
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
private async checkForExistingGlobalEnv(uid: string) {
const globalEnv = await this.prisma.userEnvironment.findFirst({
where: {
userUid: uid,
isGlobal: true,
},
});
if (globalEnv == null) return O.none;
return O.some(globalEnv);
}
}

View File

@@ -0,0 +1,29 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { User } from 'src/user/user.model';
import { UserEnvironment } from './user-environments.model';
import { UserEnvironmentsService } from './user-environments.service';
import * as E from 'fp-ts/Either';
import { throwErr } from '../utils';
@Resolver(() => User)
export class UserEnvsUserResolver {
constructor(private userEnvironmentsService: UserEnvironmentsService) {}
@ResolveField(() => [UserEnvironment], {
description: 'Returns a list of users personal environments',
})
async environments(@Parent() user: User): Promise<UserEnvironment[]> {
return await this.userEnvironmentsService.fetchUserEnvironments(user.uid);
}
@ResolveField(() => UserEnvironment, {
description: 'Returns the users global environments',
})
async globalEnvironments(
@Parent() user: User,
): Promise<UserEnvironment | string> {
const userEnvironment =
await this.userEnvironmentsService.fetchUserGlobalEnvironment(user.uid);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
return userEnvironment.right;
}
}

View File

@@ -0,0 +1,49 @@
import { Field, ID, ObjectType, registerEnumType } from '@nestjs/graphql';
@ObjectType()
export class UserHistory {
@Field(() => ID, {
description: 'ID of the user request in history',
})
id: string;
@Field(() => ID, {
description: 'ID of the user this history belongs to',
})
userUid: string;
@Field(() => ReqType, {
description: 'Type of the request in the history',
})
reqType: ReqType;
@Field({
description: 'JSON string representing the request data',
})
request: string;
@Field({
description: 'JSON string representing the response meta-data',
})
responseMetadata: string;
@Field({
description: 'If the request in the history starred',
})
isStarred: boolean;
@Field({
description:
'Timestamp of when the request was executed or history was created',
})
executedOn: Date;
}
export enum ReqType {
REST = 'REST',
GQL = 'GQL',
}
registerEnumType(ReqType, {
name: 'ReqType',
});

View File

@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { PubSubModule } from '../pubsub/pubsub.module';
import { UserModule } from '../user/user.module';
import { UserHistoryUserResolver } from './user.resolver';
import { UserHistoryResolver } from './user-history.resolver';
import { UserHistoryService } from './user-history.service';
@Module({
imports: [PrismaModule, PubSubModule, UserModule],
providers: [UserHistoryResolver, UserHistoryService, UserHistoryUserResolver],
exports: [UserHistoryService],
})
export class UserHistoryModule {}

View File

@@ -0,0 +1,147 @@
import { Args, Mutation, Resolver, Subscription } from '@nestjs/graphql';
import { UserHistoryService } from './user-history.service';
import { PubSubService } from '../pubsub/pubsub.service';
import { UserHistory } from './user-history.model';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '../guards/gql-auth.guard';
import { GqlUser } from '../decorators/gql-user.decorator';
import { User } from '../user/user.model';
import { throwErr } from '../utils';
import * as E from 'fp-ts/Either';
@Resolver()
export class UserHistoryResolver {
constructor(
private readonly userHistoryService: UserHistoryService,
private readonly pubsub: PubSubService,
) {}
/* Mutations */
@Mutation(() => UserHistory, {
description: 'Adds a new REST/GQL request to user history',
})
@UseGuards(GqlAuthGuard)
async createUserHistory(
@GqlUser() user: User,
@Args({
name: 'reqData',
description: 'JSON string of the request data',
})
reqData: string,
@Args({
name: 'resMetadata',
description: 'JSON string of the response metadata',
})
resMetadata: string,
@Args({
name: 'reqType',
description: 'Request type, REST or GQL',
})
reqType: string,
): Promise<UserHistory> {
const createdHistory = await this.userHistoryService.createUserHistory(
user.uid,
reqData,
resMetadata,
reqType,
);
if (E.isLeft(createdHistory)) throwErr(createdHistory.left);
return createdHistory.right;
}
@Mutation(() => UserHistory, {
description: 'Stars/Unstars a REST/GQL request in user history',
})
@UseGuards(GqlAuthGuard)
async toggleHistoryStarStatus(
@GqlUser() user: User,
@Args({
name: 'id',
description: 'ID of User History',
})
id: string,
): Promise<UserHistory> {
const updatedHistory =
await this.userHistoryService.toggleHistoryStarStatus(user.uid, id);
if (E.isLeft(updatedHistory)) throwErr(updatedHistory.left);
return updatedHistory.right;
}
@Mutation(() => UserHistory, {
description: 'Removes a REST/GQL request from user history',
})
@UseGuards(GqlAuthGuard)
async removeRequestFromHistory(
@GqlUser() user: User,
@Args({
name: 'id',
description: 'ID of User History',
})
id: string,
): Promise<UserHistory> {
const deletedHistory =
await this.userHistoryService.removeRequestFromHistory(user.uid, id);
if (E.isLeft(deletedHistory)) throwErr(deletedHistory.left);
return deletedHistory.right;
}
@Mutation(() => Number, {
description:
'Deletes all REST/GQL history for a user based on Request type',
})
@UseGuards(GqlAuthGuard)
async deleteAllUserHistory(
@GqlUser() user: User,
@Args({
name: 'reqType',
description: 'Request type, REST or GQL',
})
reqType: string,
): Promise<number> {
const deletedHistory = await this.userHistoryService.deleteAllUserHistory(
user.uid,
reqType,
);
if (E.isLeft(deletedHistory)) throwErr(deletedHistory.left);
return deletedHistory.right;
}
/* Subscriptions */
@Subscription(() => UserHistory, {
description: 'Listen for User History Creation',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userHistoryCreated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_history/${user.uid}/created`);
}
@Subscription(() => UserHistory, {
description: 'Listen for User History update',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userHistoryUpdated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_history/${user.uid}/updated`);
}
@Subscription(() => UserHistory, {
description: 'Listen for User History deletion',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userHistoryDeleted(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_history/${user.uid}/deleted`);
}
@Subscription(() => Number, {
description: 'Listen for User History deleted many',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userHistoryDeletedMany(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_history/${user.uid}/deleted_many`);
}
}

View File

@@ -0,0 +1,486 @@
import { UserHistoryService } from './user-history.service';
import { PrismaService } from '../prisma/prisma.service';
import { PubSubService } from '../pubsub/pubsub.service';
import { mockDeep, mockReset } from 'jest-mock-extended';
import { ReqType, UserHistory } from './user-history.model';
import {
USER_HISTORY_INVALID_REQ_TYPE,
USER_HISTORY_NOT_FOUND,
} from '../errors';
import { ReqType as DBReqType } from '@prisma/client';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userHistoryService = new UserHistoryService(
mockPrisma,
mockPubSub as any,
);
beforeEach(() => {
mockReset(mockPrisma);
mockPubSub.publish.mockClear();
});
describe('UserHistoryService', () => {
describe('fetchUserHistory', () => {
test('Should return a list of users REST history if exists', async () => {
const executedOn = new Date();
mockPrisma.userHistory.findMany.mockResolvedValueOnce([
{
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: executedOn,
isStarred: false,
},
{
userUid: 'abc',
id: '2',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: executedOn,
isStarred: true,
},
]);
const userHistory: UserHistory[] = [
{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: executedOn,
isStarred: false,
},
{
userUid: 'abc',
id: '2',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: executedOn,
isStarred: true,
},
];
return expect(
await userHistoryService.fetchUserHistory('abc', ReqType.REST),
).toEqual(userHistory);
});
test('Should return a list of users GQL history if exists', async () => {
const executedOn = new Date();
mockPrisma.userHistory.findMany.mockResolvedValueOnce([
{
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.GQL,
executedOn: executedOn,
isStarred: false,
},
{
userUid: 'abc',
id: '2',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.GQL,
executedOn: executedOn,
isStarred: true,
},
]);
const userHistory: UserHistory[] = [
{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.GQL,
executedOn: executedOn,
isStarred: false,
},
{
userUid: 'abc',
id: '2',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.GQL,
executedOn: executedOn,
isStarred: true,
},
];
return expect(
await userHistoryService.fetchUserHistory('abc', ReqType.GQL),
).toEqual(userHistory);
});
test('Should return an empty list of users REST history if doesnt exists', async () => {
mockPrisma.userHistory.findMany.mockResolvedValueOnce([]);
const userHistory: UserHistory[] = [];
return expect(
await userHistoryService.fetchUserHistory('abc', ReqType.REST),
).toEqual(userHistory);
});
test('Should return an empty list of users GQL history if doesnt exists', async () => {
mockPrisma.userHistory.findMany.mockResolvedValueOnce([]);
const userHistory: UserHistory[] = [];
return expect(
await userHistoryService.fetchUserHistory('abc', ReqType.GQL),
).toEqual(userHistory);
});
});
describe('createUserHistory', () => {
test('Should resolve right and create a REST request to users history and return a `UserHistory` object', async () => {
mockPrisma.userHistory.create.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
};
return expect(
await userHistoryService.createUserHistory(
'abc',
JSON.stringify([{}]),
JSON.stringify([{}]),
'REST',
),
).toEqualRight(userHistory);
});
test('Should resolve right and create a GQL request to users history and return a `UserHistory` object', async () => {
mockPrisma.userHistory.create.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.GQL,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.GQL,
executedOn: new Date(),
isStarred: false,
};
return expect(
await userHistoryService.createUserHistory(
'abc',
JSON.stringify([{}]),
JSON.stringify([{}]),
'GQL',
),
).toEqualRight(userHistory);
});
test('Should resolve left when invalid ReqType is passed', async () => {
return expect(
await userHistoryService.createUserHistory(
'abc',
JSON.stringify([{}]),
JSON.stringify([{}]),
'INVALID',
),
).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE);
});
test('Should create a GQL request to users history and publish a created subscription', async () => {
mockPrisma.userHistory.create.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.GQL,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.GQL,
executedOn: new Date(),
isStarred: false,
};
await userHistoryService.createUserHistory(
'abc',
JSON.stringify([{}]),
JSON.stringify([{}]),
'GQL',
);
return expect(await mockPubSub.publish).toHaveBeenCalledWith(
`user_history/${userHistory.userUid}/created`,
userHistory,
);
});
test('Should create a REST request to users history and publish a created subscription', async () => {
mockPrisma.userHistory.create.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
};
await userHistoryService.createUserHistory(
'abc',
JSON.stringify([{}]),
JSON.stringify([{}]),
'REST',
);
return expect(await mockPubSub.publish).toHaveBeenCalledWith(
`user_history/${userHistory.userUid}/created`,
userHistory,
);
});
});
describe('toggleHistoryStarStatus', () => {
test('Should resolve right and star/unstar a request in the history', async () => {
mockPrisma.userHistory.findFirst.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
mockPrisma.userHistory.update.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: true,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: true,
};
return expect(
await userHistoryService.toggleHistoryStarStatus('abc', '1'),
).toEqualRight(userHistory);
});
test('Should resolve left and error out due to invalid user history request ID', async () => {
mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null);
return expect(
await userHistoryService.toggleHistoryStarStatus('abc', '1'),
).toEqualLeft(USER_HISTORY_NOT_FOUND);
});
test('Should star/unstar a request in the history and publish a updated subscription', async () => {
mockPrisma.userHistory.findFirst.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
mockPrisma.userHistory.update.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: true,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: true,
};
await userHistoryService.toggleHistoryStarStatus('abc', '1');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_history/${userHistory.userUid}/updated`,
userHistory,
);
});
});
describe('removeRequestFromHistory', () => {
test('Should resolve right and delete request from users history', async () => {
mockPrisma.userHistory.delete.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
};
return expect(
await userHistoryService.removeRequestFromHistory('abc', '1'),
).toEqualRight(userHistory);
});
test('Should resolve left and error out when req id is invalid ', async () => {
mockPrisma.userHistory.delete.mockResolvedValueOnce(null);
return expect(
await userHistoryService.removeRequestFromHistory('abc', '1'),
).toEqualLeft(USER_HISTORY_NOT_FOUND);
});
test('Should delete request from users history and publish deleted subscription', async () => {
mockPrisma.userHistory.delete.mockResolvedValueOnce({
userUid: 'abc',
id: '1',
request: [{}],
responseMetadata: [{}],
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
});
const userHistory: UserHistory = <UserHistory>{
userUid: 'abc',
id: '1',
request: JSON.stringify([{}]),
responseMetadata: JSON.stringify([{}]),
reqType: ReqType.REST,
executedOn: new Date(),
isStarred: false,
};
await userHistoryService.removeRequestFromHistory('abc', '1');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_history/${userHistory.userUid}/deleted`,
userHistory,
);
});
});
describe('deleteAllUserHistory', () => {
test('Should resolve right and delete all user REST history for a request type', async () => {
mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({
count: 2,
});
return expect(
await userHistoryService.deleteAllUserHistory('abc', 'REST'),
).toEqualRight(2);
});
test('Should resolve right and delete all user GQL history for a request type', async () => {
mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({
count: 2,
});
return expect(
await userHistoryService.deleteAllUserHistory('abc', 'GQL'),
).toEqualRight(2);
});
test('Should resolve left and error when ReqType is invalid', async () => {
return expect(
await userHistoryService.deleteAllUserHistory('abc', 'INVALID'),
).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE);
});
test('Should delete all user REST history for a request type and publish deleted many subscription', async () => {
mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({
count: 2,
});
await userHistoryService.deleteAllUserHistory('abc', 'REST');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_history/abc/deleted_many`,
2,
);
});
test('Should delete all user GQL history for a request type and publish deleted many subscription', async () => {
mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({
count: 2,
});
await userHistoryService.deleteAllUserHistory('abc', 'GQL');
return expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_history/abc/deleted_many`,
2,
);
});
});
describe('validateReqType', () => {
test('Should resolve right when a valid REST ReqType is provided', async () => {
return expect(userHistoryService.validateReqType('REST')).toEqualRight(
ReqType.REST,
);
});
test('Should resolve right when a valid GQL ReqType is provided', async () => {
return expect(userHistoryService.validateReqType('GQL')).toEqualRight(
ReqType.GQL,
);
});
test('Should resolve left and error out when a invalid ReqType is provided', async () => {
return expect(userHistoryService.validateReqType('INVALID')).toEqualLeft(
USER_HISTORY_INVALID_REQ_TYPE,
);
});
});
});

View File

@@ -0,0 +1,209 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { PubSubService } from '../pubsub/pubsub.service';
import { ReqType, UserHistory } from './user-history.model';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import {
USER_HISTORY_INVALID_REQ_TYPE,
USER_HISTORY_NOT_FOUND,
} from '../errors';
@Injectable()
export class UserHistoryService {
constructor(
private readonly prisma: PrismaService,
private readonly pubsub: PubSubService,
) {}
/**
* Fetch users REST or GraphQL history based on ReqType param.
* @param uid Users uid
* @param reqType request Type to fetch i.e. GraphQL or REST
* @returns an array of user history
*/
async fetchUserHistory(uid: string, reqType: ReqType) {
const userHistory = await this.prisma.userHistory.findMany({
where: {
userUid: uid,
reqType: reqType,
},
});
const userHistoryColl: UserHistory[] = userHistory.map(
(history) =>
<UserHistory>{
...history,
request: JSON.stringify(history.request),
responseMetadata: JSON.stringify(history.responseMetadata),
},
);
return userHistoryColl;
}
/**
* Creates a user history.
* @param uid Users uid
* @param reqData the request data
* @param resMetadata the response metadata
* @param reqType request Type to fetch i.e. GraphQL or REST
* @returns a `UserHistory` object
*/
async createUserHistory(
uid: string,
reqData: string,
resMetadata: string,
reqType: string,
) {
const requestType = this.validateReqType(reqType);
if (E.isLeft(requestType)) return E.left(requestType.left);
const history = await this.prisma.userHistory.create({
data: {
userUid: uid,
request: JSON.parse(reqData),
responseMetadata: JSON.parse(resMetadata),
reqType: requestType.right,
isStarred: false,
},
});
const userHistory = <UserHistory>{
...history,
reqType: history.reqType,
request: JSON.stringify(history.request),
responseMetadata: JSON.stringify(history.responseMetadata),
};
// Publish created user history subscription
await this.pubsub.publish(
`user_history/${userHistory.userUid}/created`,
userHistory,
);
return E.right(userHistory);
}
/**
* Toggles star status of a user history
* @param uid Users uid
* @param id id of the request in the history
* @returns an Either of updated `UserHistory` or Error
*/
async toggleHistoryStarStatus(uid: string, id: string) {
const userHistory = await this.fetchUserHistoryByID(id);
if (O.isNone(userHistory)) {
return E.left(USER_HISTORY_NOT_FOUND);
}
try {
const updatedHistory = await this.prisma.userHistory.update({
where: {
id: id,
},
data: {
isStarred: !userHistory.value.isStarred,
},
});
const updatedUserHistory = <UserHistory>{
...updatedHistory,
request: JSON.stringify(updatedHistory.request),
responseMetadata: JSON.stringify(updatedHistory.responseMetadata),
};
// Publish updated user history subscription
await this.pubsub.publish(
`user_history/${updatedUserHistory.userUid}/updated`,
updatedUserHistory,
);
return E.right(updatedUserHistory);
} catch (e) {
return E.left(USER_HISTORY_NOT_FOUND);
}
}
/**
* Removes a REST/GraphQL request from the history
* @param uid Users uid
* @param id id of the request
* @returns an Either of deleted `UserHistory` or Error
*/
async removeRequestFromHistory(uid: string, id: string) {
try {
const delUserHistory = await this.prisma.userHistory.delete({
where: {
id: id,
},
});
const deletedUserHistory = <UserHistory>{
...delUserHistory,
request: JSON.stringify(delUserHistory.request),
responseMetadata: JSON.stringify(delUserHistory.responseMetadata),
};
// Publish deleted user history subscription
await this.pubsub.publish(
`user_history/${deletedUserHistory.userUid}/deleted`,
deletedUserHistory,
);
return E.right(deletedUserHistory);
} catch (e) {
return E.left(USER_HISTORY_NOT_FOUND);
}
}
/**
* Delete all REST/GraphQl user history based on ReqType
* @param uid Users uid
* @param reqType request type to be deleted i.e. REST or GraphQL
* @returns a count of deleted history
*/
async deleteAllUserHistory(uid: string, reqType: string) {
const requestType = this.validateReqType(reqType);
if (E.isLeft(requestType)) return E.left(requestType.left);
const deletedCount = await this.prisma.userHistory.deleteMany({
where: {
userUid: uid,
reqType: requestType.right,
},
});
// Publish multiple user history deleted subscription
await this.pubsub.publish(
`user_history/${uid}/deleted_many`,
deletedCount.count,
);
return E.right(deletedCount.count);
}
/**
* Fetch a user history based on history ID.
* @param id User History ID
* @returns an `UserHistory` object
*/
async fetchUserHistoryByID(id: string) {
const userHistory = await this.prisma.userHistory.findFirst({
where: {
id: id,
},
});
if (userHistory == null) return O.none;
return O.some(userHistory);
}
/**
* Takes a request type argument as string and validates against `ReqType`
* @param reqType request type to be validated i.e. REST or GraphQL
* @returns an either of `ReqType` or error
*/
validateReqType(reqType: string) {
if (reqType == ReqType.REST) return E.right(ReqType.REST);
else if (reqType == ReqType.GQL) return E.right(ReqType.GQL);
return E.left(USER_HISTORY_INVALID_REQ_TYPE);
}
}

View File

@@ -0,0 +1,27 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { User } from '../user/user.model';
import { UserHistoryService } from './user-history.service';
import { ReqType, UserHistory } from './user-history.model';
@Resolver(() => User)
export class UserHistoryUserResolver {
constructor(private userHistoryService: UserHistoryService) {}
@ResolveField(() => [UserHistory], {
description: 'Returns a users REST history',
})
async RESTHistory(@Parent() user: User): Promise<UserHistory[]> {
return await this.userHistoryService.fetchUserHistory(
user.uid,
ReqType.REST,
);
}
@ResolveField(() => [UserHistory], {
description: 'Returns a users GraphQL history',
})
async GraphQLHistory(@Parent() user: User): Promise<UserHistory[]> {
return await this.userHistoryService.fetchUserHistory(
user.uid,
ReqType.GQL,
);
}
}

View File

@@ -0,0 +1,24 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class UserSettings {
@Field(() => ID, {
description: 'ID of the User Setting',
})
id: string;
@Field(() => ID, {
description: 'ID of the user this setting belongs to',
})
userUid: string;
@Field({
description: 'Stringified JSON settings object',
})
properties: string; // JSON string of the userSettings object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client
@Field({
description: 'Last updated on',
})
updatedOn: Date;
}

View File

@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from 'src/prisma/prisma.module';
import { PubSubModule } from 'src/pubsub/pubsub.module';
import { UserModule } from 'src/user/user.module';
import { UserSettingsResolver } from './user-settings.resolver';
import { UserSettingsService } from './user-settings.service';
import { UserSettingsUserResolver } from './user.resolver';
@Module({
imports: [PrismaModule, PubSubModule, UserModule],
providers: [
UserSettingsResolver,
UserSettingsService,
UserSettingsUserResolver,
],
})
export class UserSettingsModule {}

View File

@@ -0,0 +1,78 @@
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver, Subscription } from '@nestjs/graphql';
import { GqlUser } from 'src/decorators/gql-user.decorator';
import { GqlAuthGuard } from 'src/guards/gql-auth.guard';
import { User } from 'src/user/user.model';
import * as E from 'fp-ts/Either';
import { throwErr } from 'src/utils';
import { UserSettings } from './user-settings.model';
import { UserSettingsService } from './user-settings.service';
import { PubSubService } from 'src/pubsub/pubsub.service';
@Resolver()
export class UserSettingsResolver {
constructor(
private readonly userSettingsService: UserSettingsService,
private readonly pubsub: PubSubService,
) {}
/* Mutations */
@Mutation(() => UserSettings, {
description: 'Creates a new user setting',
})
@UseGuards(GqlAuthGuard)
async createUserSettings(
@GqlUser() user: User,
@Args({
name: 'properties',
description: 'Stringified JSON settings object',
})
properties: string,
) {
const createdUserSettings =
await this.userSettingsService.createUserSettings(user, properties);
if (E.isLeft(createdUserSettings)) throwErr(createdUserSettings.left);
return createdUserSettings.right;
}
@Mutation(() => UserSettings, {
description: 'Update user setting for a given user',
})
@UseGuards(GqlAuthGuard)
async updateUserSettings(
@GqlUser() user: User,
@Args({
name: 'properties',
description: 'Stringified JSON settings object',
})
properties: string,
) {
const updatedUserSettings =
await this.userSettingsService.updateUserSettings(user, properties);
if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left);
return updatedUserSettings.right;
}
/* Subscriptions */
@Subscription(() => UserSettings, {
description: 'Listen for user setting creation',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userSettingsCreated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_settings/${user.uid}/created`);
}
@Subscription(() => UserSettings, {
description: 'Listen for user setting updates',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userSettingsUpdated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user_settings/${user.uid}/updated`);
}
}

View File

@@ -0,0 +1,125 @@
import { mockDeep, mockReset } from 'jest-mock-extended';
import { PrismaService } from 'src/prisma/prisma.service';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { UserSettingsService } from './user-settings.service';
import { JSON_INVALID, USER_SETTINGS_NULL_SETTINGS } from 'src/errors';
import { UserSettings } from './user-settings.model';
import { User } from 'src/user/user.model';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userSettingsService = new UserSettingsService(
mockPrisma,
mockPubSub as any,
);
const user: User = {
uid: 'aabb22ccdd',
displayName: 'user-display-name',
email: 'user-email',
photoURL: 'user-photo-url',
};
const settings: UserSettings = {
id: '1',
userUid: user.uid,
properties: JSON.stringify({ key: 'k', value: 'v' }),
updatedOn: new Date('2022-12-19T12:43:18.635Z'),
};
beforeEach(() => {
mockReset(mockPrisma);
mockPubSub.publish.mockClear();
});
describe('UserSettingsService', () => {
describe('createUserSettings', () => {
test('Should resolve right and create an user setting with valid user and properties', async () => {
mockPrisma.userSettings.create.mockResolvedValue({
...settings,
properties: JSON.parse(settings.properties),
});
const result = await userSettingsService.createUserSettings(
user,
settings.properties,
);
expect(result).toEqualRight(settings);
});
test('Should reject user settings creation for invalid properties', async () => {
const result = await userSettingsService.createUserSettings(
user,
'invalid-settings',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should reject user settings creation for null properties', async () => {
const result = await userSettingsService.createUserSettings(
user,
null as any,
);
expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS);
});
test('Should publish pubsub message on successful user settings create', async () => {
mockPrisma.userSettings.create.mockResolvedValue({
...settings,
properties: JSON.parse(settings.properties),
});
await userSettingsService.createUserSettings(user, settings.properties);
expect(mockPubSub.publish).toBeCalledWith(
`user_settings/${user.uid}/created`,
settings,
);
});
});
describe('updateUserSettings', () => {
test('Should update a user setting for valid user and settings', async () => {
mockPrisma.userSettings.update.mockResolvedValue({
...settings,
properties: JSON.parse(settings.properties),
});
const result = await userSettingsService.updateUserSettings(
user,
settings.properties,
);
expect(result).toEqualRight(settings);
});
test('Should reject user settings updation for invalid stringified JSON settings', async () => {
const result = await userSettingsService.updateUserSettings(
user,
'invalid-settings',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should reject user settings updation for null properties', async () => {
const result = await userSettingsService.updateUserSettings(
user,
null as any,
);
expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS);
});
test('Should publish pubsub message on successful user settings update', async () => {
mockPrisma.userSettings.update.mockResolvedValue({
...settings,
properties: JSON.parse(settings.properties),
});
await userSettingsService.updateUserSettings(user, settings.properties);
expect(mockPubSub.publish).toBeCalledWith(
`user_settings/${user.uid}/updated`,
settings,
);
});
});
});

View File

@@ -0,0 +1,110 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { User } from 'src/user/user.model';
import * as E from 'fp-ts/Either';
import { stringToJson } from 'src/utils';
import { UserSettings } from './user-settings.model';
import {
USER_SETTINGS_ALREADY_EXISTS,
USER_SETTINGS_NULL_SETTINGS,
USER_SETTINGS_NOT_FOUND,
} from 'src/errors';
@Injectable()
export class UserSettingsService {
constructor(
private readonly prisma: PrismaService,
private readonly pubsub: PubSubService,
) {}
/**
* Fetch user settings for a given user
* @param user User object
* @returns Promise of an Either of `UserSettings` or error
*/
async fetchUserSettings(user: User) {
try {
const userSettings = await this.prisma.userSettings.findUniqueOrThrow({
where: { userUid: user.uid },
});
const settings: UserSettings = {
...userSettings,
properties: JSON.stringify(userSettings.properties),
};
return E.right(settings);
} catch (e) {
return E.left(USER_SETTINGS_NOT_FOUND);
}
}
/**
* Create user setting for a given user
* @param user User object
* @param properties stringified user settings properties
* @returns an Either of `UserSettings` or error
*/
async createUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS);
const jsonProperties = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left);
try {
const userSettings = await this.prisma.userSettings.create({
data: {
properties: jsonProperties.right,
userUid: user.uid,
},
});
const settings: UserSettings = {
...userSettings,
properties: JSON.stringify(userSettings.properties),
};
// Publish subscription for user settings creation
await this.pubsub.publish(`user_settings/${user.uid}/created`, settings);
return E.right(settings);
} catch (e) {
return E.left(USER_SETTINGS_ALREADY_EXISTS);
}
}
/**
* Update user setting for a given user
* @param user User object
* @param properties stringified user settings
* @returns Promise of an Either of `UserSettings` or error
*/
async updateUserSettings(user: User, properties: string) {
if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS);
const jsonProperties = stringToJson(properties);
if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left);
try {
const updatedUserSettings = await this.prisma.userSettings.update({
where: { userUid: user.uid },
data: {
properties: jsonProperties.right,
},
});
const settings: UserSettings = {
...updatedUserSettings,
properties: JSON.stringify(updatedUserSettings.properties),
};
// Publish subscription for user settings update
await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);
return E.right(settings);
} catch (e) {
return E.left(USER_SETTINGS_NOT_FOUND);
}
}
}

View File

@@ -0,0 +1,21 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { User } from 'src/user/user.model';
import { UserSettings } from './user-settings.model';
import { UserSettingsService } from './user-settings.service';
import * as E from 'fp-ts/Either';
import { throwErr } from 'src/utils';
@Resolver(() => User)
export class UserSettingsUserResolver {
constructor(private readonly userSettingsService: UserSettingsService) {}
@ResolveField(() => UserSettings, {
description: 'Returns user settings',
})
async settings(@Parent() user: User) {
const userSettings = await this.userSettingsService.fetchUserSettings(user);
if (E.isLeft(userSettings)) throwErr(userSettings.left);
return userSettings.right;
}
}

View File

@@ -1,9 +1,15 @@
import { ObjectType, ID, Field } from '@nestjs/graphql';
import {
ObjectType,
ID,
Field,
InputType,
registerEnumType,
} from '@nestjs/graphql';
@ObjectType()
export class User {
@Field(() => ID, {
description: 'ID of the user',
description: 'UID of the user',
})
uid: string;
@@ -15,7 +21,7 @@ export class User {
@Field({
nullable: true,
description: 'Email of the user (if fetched)',
description: 'Email of the user',
})
email?: string;
@@ -34,4 +40,25 @@ export class User {
description: 'Date when the user account was created',
})
createdOn: Date;
@Field({
nullable: true,
description: 'Stringified current REST session for logged-in User',
})
currentRESTSession?: string;
@Field({
nullable: true,
description: 'Stringified current GraphQL session for logged-in User',
})
currentGQLSession?: string;
}
export enum SessionType {
REST = 'REST',
GQL = 'GQL',
}
registerEnumType(SessionType, {
name: 'SessionType',
});

View File

@@ -1,12 +1,19 @@
import { Resolver, Query } from '@nestjs/graphql';
import { User } from './user.model';
import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql';
import { SessionType, User } from './user.model';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '../guards/gql-auth.guard';
import { GqlUser } from '../decorators/gql-user.decorator';
import { UserService } from './user.service';
import { throwErr } from 'src/utils';
import * as E from 'fp-ts/lib/Either';
import { PubSubService } from 'src/pubsub/pubsub.service';
@Resolver(() => User)
export class UserResolver {
constructor() {}
constructor(
private readonly userService: UserService,
private readonly pubsub: PubSubService,
) {}
@Query(() => User, {
description:
@@ -16,4 +23,43 @@ export class UserResolver {
me(@GqlUser() user) {
return user;
}
/* Mutations */
@Mutation(() => User, {
description: 'Update user sessions',
})
@UseGuards(GqlAuthGuard)
async updateUserSessions(
@GqlUser() user: User,
@Args({
name: 'currentSession',
description: 'JSON string of the saved REST/GQL session',
})
currentSession: string,
@Args({
name: 'sessionType',
description: 'Type of the session',
})
sessionType: SessionType,
): Promise<User> {
const updatedUser = await this.userService.updateUserSessions(
user,
currentSession,
sessionType,
);
if (E.isLeft(updatedUser)) throwErr(updatedUser.left);
return updatedUser.right;
}
/* Subscriptions */
@Subscription(() => User, {
description: 'Listen for user updates',
resolve: (value) => value,
})
@UseGuards(GqlAuthGuard)
userUpdated(@GqlUser() user: User) {
return this.pubsub.asyncIterator(`user/${user.uid}/updated`);
}
}

View File

@@ -1,14 +1,17 @@
import { JSON_INVALID } from 'src/errors';
import { mockDeep, mockReset } from 'jest-mock-extended';
import { PrismaService } from 'src/prisma/prisma.service';
import { AuthUser } from 'src/types/AuthUser';
import { User } from './user.model';
import { UserService } from './user.service';
import { PubSubService } from 'src/pubsub/pubsub.service';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userService = new UserService(mockPrisma);
const userService = new UserService(mockPrisma, mockPubSub as any);
const currentTime = new Date();
@@ -18,6 +21,8 @@ const user: AuthUser = {
displayName: 'Dwight Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: false,
currentRESTSession: JSON.stringify({}),
currentGQLSession: JSON.stringify({}),
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
};
@@ -30,6 +35,11 @@ const exampleSSOProfileData = {
photos: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
};
beforeEach(() => {
mockReset(mockPrisma);
mockPubSub.publish.mockClear();
});
describe('findUserByEmail', () => {
test('should successfully return a valid user given a valid email', async () => {
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
@@ -213,3 +223,84 @@ describe('createProviderAccount', () => {
});
});
});
describe('updateUserSessions', () => {
test('Should resolve right and update users GQL session', async () => {
const sessionData = user.currentGQLSession;
mockPrisma.user.update.mockResolvedValue({
...user,
currentGQLSession: JSON.parse(sessionData),
currentRESTSession: null,
});
const result = await userService.updateUserSessions(
user,
sessionData,
'GQL',
);
expect(result).toEqualRight({
...user,
currentGQLSession: sessionData,
currentRESTSession: null,
});
});
test('Should resolve right and update users REST session', async () => {
const sessionData = user.currentGQLSession;
mockPrisma.user.update.mockResolvedValue({
...user,
currentGQLSession: null,
currentRESTSession: JSON.parse(sessionData),
});
const result = await userService.updateUserSessions(
user,
sessionData,
'REST',
);
expect(result).toEqualRight({
...user,
currentGQLSession: null,
currentRESTSession: sessionData,
});
});
test('Should reject left and update user for invalid GQL session', async () => {
const sessionData = 'invalid json';
const result = await userService.updateUserSessions(
user,
sessionData,
'GQL',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should reject left and update user for invalid REST session', async () => {
const sessionData = 'invalid json';
const result = await userService.updateUserSessions(
user,
sessionData,
'REST',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should publish pubsub message on user update sessions', async () => {
mockPrisma.user.update.mockResolvedValue({
...user,
currentGQLSession: JSON.parse(user.currentGQLSession),
currentRESTSession: JSON.parse(user.currentRESTSession),
});
await userService.updateUserSessions(user, user.currentGQLSession, 'GQL');
expect(mockPubSub.publish).toHaveBeenCalledTimes(1);
expect(mockPubSub.publish).toHaveBeenCalledWith(
`user/${user.uid}/updated`,
user,
);
});
});

View File

@@ -4,10 +4,17 @@ import * as O from 'fp-ts/Option';
import * as E from 'fp-ts/Either';
import { AuthUser } from 'src/types/AuthUser';
import { USER_NOT_FOUND } from 'src/errors';
import { SessionType, User } from './user.model';
import { USER_UPDATE_FAILED } from 'src/errors';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { stringToJson } from 'src/utils';
@Injectable()
export class UserService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private readonly pubsub: PubSubService,
) {}
/**
* Find User with given email id
@@ -152,4 +159,68 @@ export class UserService {
return E.left(USER_NOT_FOUND);
}
}
/**
* Update a user's sessions
* @param user User object
* @param currentRESTSession user's current REST session
* @param currentGQLSession user's current GQL session
* @returns a Either of User or error
*/
async updateUserSessions(
user: User,
currentSession: string,
sessionType: string,
): Promise<E.Right<User> | E.Left<string>> {
const validatedSession = await this.validateSession(currentSession);
if (E.isLeft(validatedSession)) return E.left(validatedSession.left);
try {
const sessionObj = {};
switch (sessionType) {
case SessionType.GQL:
sessionObj['currentGQLSession'] = validatedSession.right;
break;
case SessionType.REST:
sessionObj['currentRESTSession'] = validatedSession.right;
break;
default:
return E.left(USER_UPDATE_FAILED);
}
const dbUpdatedUser = await this.prisma.user.update({
where: { uid: user.uid },
data: sessionObj,
});
const updatedUser: User = {
...dbUpdatedUser,
currentGQLSession: dbUpdatedUser.currentGQLSession
? JSON.stringify(dbUpdatedUser.currentGQLSession)
: null,
currentRESTSession: dbUpdatedUser.currentRESTSession
? JSON.stringify(dbUpdatedUser.currentRESTSession)
: null,
};
// Publish subscription for user updates
await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser);
return E.right(updatedUser);
} catch (e) {
return E.left(USER_UPDATE_FAILED);
}
}
/**
* Validate and parse currentRESTSession and currentGQLSession
* @param sessionData string of the session
* @returns a Either of JSON object or error
*/
async validateSession(sessionData: string) {
const jsonSession = stringToJson(sessionData);
if (E.isLeft(jsonSession)) return E.left(jsonSession.left);
return E.right(jsonSession.right);
}
}

View File

@@ -4,13 +4,14 @@ import { pipe } from 'fp-ts/lib/function';
import * as O from 'fp-ts/Option';
import * as TE from 'fp-ts/TaskEither';
import * as T from 'fp-ts/Task';
import * as E from 'fp-ts/Either';
import { User } from './user/user.model';
import * as A from 'fp-ts/Array';
import * as E from 'fp-ts/Either';
import { AuthErrorHandler } from './types/AuthErrorHandler';
import { AuthTokens } from './types/AuthTokens';
import { Response } from 'express';
import { DateTime } from 'luxon';
import { JSON_INVALID } from './errors';
/**
* A workaround to throw an exception in an expression.
@@ -174,3 +175,18 @@ export const authCookieHandler = (
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
} else res.status(HttpStatus.OK).send();
};
/*
* String to JSON parser
* @param {str} str The string to parse
* @returns {E.Right<T> | E.Left<"json_invalid">} An Either of the parsed JSON
*/
export function stringToJson<T>(
str: string,
): E.Right<T | any> | E.Left<string> {
try {
return E.right(JSON.parse(str));
} catch (err) {
return E.left(JSON_INVALID);
}
}

View File

@@ -12,10 +12,11 @@
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strict": false,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"noFallthroughCasesInSwitch": true,
}
}