Merge pull request #7 from hoppscotch/feat/user-history
feat: introduce user history in self hosted
This commit is contained in:
@@ -1,129 +0,0 @@
|
|||||||
-- CreateEnum
|
|
||||||
CREATE TYPE "TeamMemberRole" AS ENUM ('OWNER', 'VIEWER', 'EDITOR');
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "Team" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"name" TEXT NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "Team_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "TeamMember" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"role" "TeamMemberRole" NOT NULL,
|
|
||||||
"userUid" TEXT NOT NULL,
|
|
||||||
"teamID" TEXT NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "TeamMember_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "TeamInvitation" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"teamID" TEXT NOT NULL,
|
|
||||||
"creatorUid" TEXT NOT NULL,
|
|
||||||
"inviteeEmail" TEXT NOT NULL,
|
|
||||||
"inviteeRole" "TeamMemberRole" NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "TeamInvitation_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "TeamCollection" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"parentID" TEXT,
|
|
||||||
"teamID" TEXT NOT NULL,
|
|
||||||
"title" TEXT NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "TeamCollection_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "TeamRequest" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"collectionID" TEXT NOT NULL,
|
|
||||||
"teamID" TEXT NOT NULL,
|
|
||||||
"title" TEXT NOT NULL,
|
|
||||||
"request" JSONB NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "TeamRequest_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "Shortcode" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"request" JSONB NOT NULL,
|
|
||||||
"creatorUid" TEXT,
|
|
||||||
"createdOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
|
|
||||||
CONSTRAINT "Shortcode_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "TeamEnvironment" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"teamID" TEXT NOT NULL,
|
|
||||||
"name" TEXT NOT NULL,
|
|
||||||
"variables" JSONB NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "TeamEnvironment_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "User" (
|
|
||||||
"uid" TEXT NOT NULL,
|
|
||||||
"displayName" TEXT,
|
|
||||||
"email" TEXT,
|
|
||||||
"photoURL" TEXT,
|
|
||||||
|
|
||||||
CONSTRAINT "User_pkey" PRIMARY KEY ("uid")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "UserEnvironment" (
|
|
||||||
"id" TEXT NOT NULL,
|
|
||||||
"userUid" TEXT NOT NULL,
|
|
||||||
"name" TEXT,
|
|
||||||
"variables" JSONB NOT NULL,
|
|
||||||
"isGlobal" BOOLEAN NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "UserEnvironment_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE UNIQUE INDEX "TeamMember_teamID_userUid_key" ON "TeamMember"("teamID", "userUid");
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE INDEX "TeamInvitation_teamID_idx" ON "TeamInvitation"("teamID");
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE UNIQUE INDEX "TeamInvitation_teamID_inviteeEmail_key" ON "TeamInvitation"("teamID", "inviteeEmail");
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE UNIQUE INDEX "Shortcode_id_creatorUid_key" ON "Shortcode"("id", "creatorUid");
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamMember" ADD CONSTRAINT "TeamMember_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamInvitation" ADD CONSTRAINT "TeamInvitation_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_parentID_fkey" FOREIGN KEY ("parentID") REFERENCES "TeamCollection"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_collectionID_fkey" FOREIGN KEY ("collectionID") REFERENCES "TeamCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "TeamEnvironment" ADD CONSTRAINT "TeamEnvironment_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "UserEnvironment" ADD CONSTRAINT "UserEnvironment_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
@@ -84,6 +84,8 @@ model User {
|
|||||||
email String?
|
email String?
|
||||||
photoURL String?
|
photoURL String?
|
||||||
settings UserSettings?
|
settings UserSettings?
|
||||||
|
|
||||||
|
UserHistory UserHistory[]
|
||||||
UserEnvironments UserEnvironment[]
|
UserEnvironments UserEnvironment[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +97,22 @@ model UserSettings {
|
|||||||
updatedOn DateTime @updatedAt @db.Timestamp(3)
|
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 {
|
model UserEnvironment {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
userUid String
|
userUid String
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { UserModule } from './user/user.module';
|
|||||||
import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin';
|
import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin';
|
||||||
import { UserSettingsModule } from './user-settings/user-settings.module';
|
import { UserSettingsModule } from './user-settings/user-settings.module';
|
||||||
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
import { UserEnvironmentsModule } from './user-environment/user-environments.module';
|
||||||
|
import { UserHistoryModule } from './user-history/user-history.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -48,6 +49,7 @@ import { UserEnvironmentsModule } from './user-environment/user-environments.mod
|
|||||||
UserModule,
|
UserModule,
|
||||||
UserSettingsModule,
|
UserSettingsModule,
|
||||||
UserEnvironmentsModule,
|
UserEnvironmentsModule,
|
||||||
|
UserHistoryModule,
|
||||||
],
|
],
|
||||||
providers: [GQLComplexityPlugin],
|
providers: [GQLComplexityPlugin],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -234,6 +234,23 @@ export const USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME =
|
|||||||
'user_environment/user_env_invalid_env_name' as const;
|
'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|
|
|Server errors that are actually bugs|
|
||||||
|------------------------------------|
|
|------------------------------------|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { UserSettings } from 'src/user-settings/user-settings.model';
|
import { UserSettings } from 'src/user-settings/user-settings.model';
|
||||||
import { UserEnvironment } from '../user-environment/user-environments.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.
|
// 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.
|
// For every module that publishes a subscription add its type def and the possible subscription type.
|
||||||
@@ -9,4 +10,8 @@ export type TopicDef = {
|
|||||||
]: UserEnvironment;
|
]: UserEnvironment;
|
||||||
[topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings;
|
[topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings;
|
||||||
[topic: `user_environment/${string}/deleted_many`]: number;
|
[topic: `user_environment/${string}/deleted_many`]: number;
|
||||||
|
[
|
||||||
|
topic: `user_history/${string}/${'created' | 'updated' | 'deleted'}`
|
||||||
|
]: UserHistory;
|
||||||
|
[topic: `user_history/${string}/deleted_many`]: number;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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',
|
||||||
|
});
|
||||||
@@ -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 {}
|
||||||
@@ -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`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user