feat: user session resolver added for sessions updates

This commit is contained in:
Mir Arif Hasan
2023-01-24 17:19:24 +06:00
parent e6fcb1272a
commit 08ca57cba2
4 changed files with 115 additions and 107 deletions

View File

@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { UpdateUserInput, User } from './user.model';
import { User as DbUser, Prisma } from '@prisma/client';
import { SessionType, User } from './user.model';
import * as E from 'fp-ts/lib/Either';
import { USER_UPDATE_FAILED } from 'src/errors';
import { PubSubService } from 'src/pubsub/pubsub.service';
@@ -15,37 +14,36 @@ export class UserService {
) {}
/**
* Update a user's information
* Update a user's sessions
* @param user User object
* @param updateData Properties to update
* @param currentRESTSession user's current REST session
* @param currentGQLSession user's current GQL session
* @returns a Either of User or error
*/
async updateUser(
async updateUserSessions(
user: User,
updateData: UpdateUserInput,
): Promise<E.Either<string, User>> {
let { currentGQLSession, currentRESTSession, ...rest } = updateData;
let updateUserObj: Partial<DbUser> = rest;
currentSession: string,
sessionType: string,
): Promise<E.Right<User> | E.Left<string>> {
const validatedSession = await this.validateSession(currentSession);
if (E.isLeft(validatedSession)) return E.left(validatedSession.left);
// Convert stringified JSON to JSON
if (updateData?.currentGQLSession !== undefined) {
const jsonGql = stringToJson(updateData.currentGQLSession);
if (E.isLeft<string>(jsonGql)) return jsonGql;
updateUserObj.currentGQLSession = jsonGql?.right ?? Prisma.DbNull;
}
if (updateData?.currentRESTSession !== undefined) {
const jsonRest = stringToJson(updateData.currentRESTSession);
if (E.isLeft<string>(jsonRest)) return jsonRest;
updateUserObj.currentRESTSession = jsonRest?.right ?? Prisma.DbNull;
}
// Update user
try {
const sessionObj = {};
switch (sessionType) {
case SessionType.GQL:
sessionObj['currentGQLSession'] = validatedSession.right;
break;
case SessionType.REST:
sessionObj['currentRESTSession'] = validatedSession.right;
break;
default:
return E.left(USER_UPDATE_FAILED);
}
const dbUpdatedUser = await this.prisma.user.update({
where: { uid: user.uid },
data: updateUserObj,
data: sessionObj,
});
const updatedUser: User = {
@@ -66,4 +64,16 @@ export class UserService {
return E.left(USER_UPDATE_FAILED);
}
}
/**
* Validate and parse currentRESTSession and currentGQLSession
* @param sessionData string of the session
* @returns a Either of JSON object or error
*/
async validateSession(sessionData: string) {
const jsonSession = stringToJson(sessionData);
if (E.isLeft(jsonSession)) return E.left(jsonSession.left);
return E.right(jsonSession.right);
}
}