chore: pulled from main

This commit is contained in:
Mir Arif Hasan
2023-01-24 15:24:11 +06:00
parent d7b02da719
commit 6627514e88
17 changed files with 1381 additions and 3 deletions

View File

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

View File

@@ -85,9 +85,35 @@ model User {
photoURL String?
currentRESTSession Json?
currentGQLSession Json?
settings UserSettings?
UserHistory UserHistory[]
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 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

View File

@@ -3,7 +3,9 @@ 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';
import { UserHistoryModule } from './user-history/user-history.module';
@Module({
imports: [
@@ -45,7 +47,9 @@ import { UserEnvironmentsModule } from './user-environment/user-environments.mod
driver: ApolloDriver,
}),
UserModule,
UserSettingsModule,
UserEnvironmentsModule,
UserHistoryModule,
],
providers: [GQLComplexityPlugin],
})

View File

@@ -168,12 +168,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
@@ -223,6 +240,23 @@ 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;
/*
|------------------------------------|
|Server errors that are actually bugs|
|------------------------------------|

View File

@@ -1,12 +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/${string}/${'updated'}`]: User;
[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,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;
}
}