refactor: updated pubsub publishing to leverage new topic defs and rewrote map logic
This commit is contained in:
@@ -8,13 +8,6 @@ import {
|
|||||||
USER_HISTORY_NOT_FOUND,
|
USER_HISTORY_NOT_FOUND,
|
||||||
} from '../errors';
|
} from '../errors';
|
||||||
|
|
||||||
// Contains constants for the subscription types we send to pubsub service
|
|
||||||
enum SubscriptionType {
|
|
||||||
Created = 'created',
|
|
||||||
Updated = 'updated',
|
|
||||||
Deleted = 'deleted',
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserHistoryService {
|
export class UserHistoryService {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -36,18 +29,18 @@ export class UserHistoryService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const userHistoryColl: UserHistory[] = [];
|
const userHistoryColl: UserHistory[] = userHistory.map(
|
||||||
userHistory.forEach((history) => {
|
(history) =>
|
||||||
userHistoryColl.push(<UserHistory>{
|
<UserHistory>{
|
||||||
id: history.id,
|
id: history.id,
|
||||||
userUid: history.userUid,
|
userUid: history.userUid,
|
||||||
reqType: history.type,
|
reqType: history.type,
|
||||||
request: JSON.stringify(history.request),
|
request: JSON.stringify(history.request),
|
||||||
responseMetadata: JSON.stringify(history.responseMetadata),
|
responseMetadata: JSON.stringify(history.responseMetadata),
|
||||||
isStarred: history.isStarred,
|
isStarred: history.isStarred,
|
||||||
executedOn: history.executedOn,
|
executedOn: history.executedOn,
|
||||||
});
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
return userHistoryColl;
|
return userHistoryColl;
|
||||||
}
|
}
|
||||||
@@ -89,9 +82,10 @@ export class UserHistoryService {
|
|||||||
reqType: history.type,
|
reqType: history.type,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.publishUserHistorySubscription(
|
// Publish created user history subscription
|
||||||
|
await this.pubsub.publish(
|
||||||
|
`user_history/${userHistory.userUid}/created`,
|
||||||
userHistory,
|
userHistory,
|
||||||
SubscriptionType.Created,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return E.right(userHistory);
|
return E.right(userHistory);
|
||||||
@@ -103,7 +97,7 @@ export class UserHistoryService {
|
|||||||
* @param id id of the request in the history
|
* @param id id of the request in the history
|
||||||
* @returns an Either of updated `UserHistory` or Error
|
* @returns an Either of updated `UserHistory` or Error
|
||||||
*/
|
*/
|
||||||
async starUnstarRequestInHistory(uid: string, id: string) {
|
async toggleHistoryStarStatus(uid: string, id: string) {
|
||||||
const userHistory = await this.prisma.userHistory.findFirst({
|
const userHistory = await this.prisma.userHistory.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
@@ -133,9 +127,10 @@ export class UserHistoryService {
|
|||||||
reqType: updatedHistory.type,
|
reqType: updatedHistory.type,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.publishUserHistorySubscription(
|
// Publish updated user history subscription
|
||||||
|
await this.pubsub.publish(
|
||||||
|
`user_history/${updatedUserHistory.userUid}/updated`,
|
||||||
updatedUserHistory,
|
updatedUserHistory,
|
||||||
SubscriptionType.Updated,
|
|
||||||
);
|
);
|
||||||
return E.right(updatedUserHistory);
|
return E.right(updatedUserHistory);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -167,9 +162,10 @@ export class UserHistoryService {
|
|||||||
reqType: delUserHistory.type,
|
reqType: delUserHistory.type,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.publishUserHistorySubscription(
|
// Publish deleted user history subscription
|
||||||
|
await this.pubsub.publish(
|
||||||
|
`user_history/${deletedUserHistory.userUid}/deleted`,
|
||||||
deletedUserHistory,
|
deletedUserHistory,
|
||||||
SubscriptionType.Deleted,
|
|
||||||
);
|
);
|
||||||
return E.right(deletedUserHistory);
|
return E.right(deletedUserHistory);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -193,42 +189,23 @@ export class UserHistoryService {
|
|||||||
type: requestType.right,
|
type: requestType.right,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Publish multiple user history deleted subscription
|
||||||
|
await this.pubsub.publish(
|
||||||
|
`user_history/${uid}/deleted_many`,
|
||||||
|
deletedCount.count,
|
||||||
|
);
|
||||||
return E.right(deletedCount.count);
|
return E.right(deletedCount.count);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method that 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
|
||||||
|
* @returns an either of `ReqType` or error
|
||||||
|
*/
|
||||||
validateReqType(reqType: string) {
|
validateReqType(reqType: string) {
|
||||||
if (reqType == ReqType.REST) return E.right(ReqType.REST);
|
if (reqType == ReqType.REST) return E.right(ReqType.REST);
|
||||||
else if (reqType == ReqType.GQL) return E.right(ReqType.GQL);
|
else if (reqType == ReqType.GQL) return E.right(ReqType.GQL);
|
||||||
return E.left(USER_HISTORY_INVALID_REQ_TYPE);
|
return E.left(USER_HISTORY_INVALID_REQ_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to publish subscriptions based on the subscription type of the history
|
|
||||||
async publishUserHistorySubscription(
|
|
||||||
userHistory: UserHistory,
|
|
||||||
subscriptionType: SubscriptionType,
|
|
||||||
) {
|
|
||||||
switch (subscriptionType) {
|
|
||||||
case SubscriptionType.Created:
|
|
||||||
await this.pubsub.publish(
|
|
||||||
`user_history/${userHistory.userUid}/created`,
|
|
||||||
userHistory,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case SubscriptionType.Updated:
|
|
||||||
await this.pubsub.publish(
|
|
||||||
`user_history/${userHistory.userUid}/updated`,
|
|
||||||
userHistory,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case SubscriptionType.Deleted:
|
|
||||||
await this.pubsub.publish(
|
|
||||||
`user_history/${userHistory.userUid}/deleted`,
|
|
||||||
userHistory,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user