refactor: added fetchUserHistoryByID method and added changes for spread

This commit is contained in:
ankitsridhar16
2023-01-24 12:53:44 +05:30
parent 73ace77305
commit 97eedb568c

View File

@@ -3,6 +3,7 @@ import { PrismaService } from '../prisma/prisma.service';
import { PubSubService } from '../pubsub/pubsub.service'; import { PubSubService } from '../pubsub/pubsub.service';
import { ReqType, UserHistory } from './user-history.model'; import { ReqType, UserHistory } from './user-history.model';
import * as E from 'fp-ts/Either'; import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import { import {
USER_HISTORY_INVALID_REQ_TYPE, USER_HISTORY_INVALID_REQ_TYPE,
USER_HISTORY_NOT_FOUND, USER_HISTORY_NOT_FOUND,
@@ -70,6 +71,7 @@ export class UserHistoryService {
const userHistory = <UserHistory>{ const userHistory = <UserHistory>{
...history, ...history,
reqType: history.reqType,
request: JSON.stringify(history.request), request: JSON.stringify(history.request),
responseMetadata: JSON.stringify(history.responseMetadata), responseMetadata: JSON.stringify(history.responseMetadata),
}; };
@@ -90,22 +92,18 @@ export class UserHistoryService {
* @returns an Either of updated `UserHistory` or Error * @returns an Either of updated `UserHistory` or Error
*/ */
async toggleHistoryStarStatus(uid: string, id: string) { async toggleHistoryStarStatus(uid: string, id: string) {
const userHistory = await this.prisma.userHistory.findFirst({ const userHistory = await this.fetchUserHistoryByID(id);
where: { if (O.isNone(userHistory)) {
id: id,
},
});
if (userHistory == null) {
return E.left(USER_HISTORY_NOT_FOUND); return E.left(USER_HISTORY_NOT_FOUND);
} }
try { try {
const updatedHistory = await this.prisma.userHistory.update({ const updatedHistory = await this.prisma.userHistory.update({
where: { where: {
id: id, id: id,
}, },
data: { data: {
isStarred: !userHistory.isStarred, isStarred: !userHistory.value.isStarred,
}, },
}); });
@@ -182,6 +180,22 @@ export class UserHistoryService {
return E.right(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` * Takes a request type argument as string and validates against `ReqType`
* @param reqType request type to be validated i.e. REST or GraphQL * @param reqType request type to be validated i.e. REST or GraphQL