Merge pull request #5 from hoppscotch/feat/user-settings
feat: introducing user-settings in self-hosted (backend)
This commit is contained in:
@@ -88,4 +88,4 @@
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,9 +83,18 @@ model User {
|
||||
displayName String?
|
||||
email String?
|
||||
photoURL String?
|
||||
settings UserSettings?
|
||||
UserEnvironments UserEnvironment[]
|
||||
}
|
||||
|
||||
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 UserEnvironment {
|
||||
id String @id @default(cuid())
|
||||
userUid String
|
||||
|
||||
@@ -3,6 +3,7 @@ import { GraphQLModule } from '@nestjs/graphql';
|
||||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
|
||||
import { UserModule } from './user/user.module';
|
||||
import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin';
|
||||
import { UserSettingsModule } from './user-settings/user-settings.module';
|
||||
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
||||
|
||||
@Module({
|
||||
@@ -45,6 +46,7 @@ import { UserEnvironmentsModule } from './user-environment/user-environments.mod
|
||||
driver: ApolloDriver,
|
||||
}),
|
||||
UserModule,
|
||||
UserSettingsModule,
|
||||
UserEnvironmentsModule,
|
||||
],
|
||||
providers: [GQLComplexityPlugin],
|
||||
|
||||
@@ -162,12 +162,29 @@ 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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { UserSettings } from 'src/user-settings/user-settings.model';
|
||||
import { UserEnvironment } from '../user-environment/user-environments.model';
|
||||
|
||||
// A custom message type that defines the topic and the corresponding payload.
|
||||
@@ -6,5 +7,6 @@ export type TopicDef = {
|
||||
[
|
||||
topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}`
|
||||
]: UserEnvironment;
|
||||
[topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings;
|
||||
[topic: `user_environment/${string}/deleted_many`]: number;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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`);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user