chore: resolved all merge conflicts

This commit is contained in:
Balu Babu
2023-01-24 19:35:40 +05:30
31 changed files with 2864 additions and 14 deletions

View File

@@ -4,10 +4,17 @@ import * as O from 'fp-ts/Option';
import * as E from 'fp-ts/Either';
import { AuthUser } from 'src/types/AuthUser';
import { USER_NOT_FOUND } from 'src/errors';
import { SessionType, User } from './user.model';
import { USER_UPDATE_FAILED } from 'src/errors';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { stringToJson } from 'src/utils';
@Injectable()
export class UserService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private readonly pubsub: PubSubService,
) {}
/**
* Find User with given email id
@@ -152,4 +159,68 @@ export class UserService {
return E.left(USER_NOT_FOUND);
}
}
/**
* Update a user's sessions
* @param user User object
* @param currentRESTSession user's current REST session
* @param currentGQLSession user's current GQL session
* @returns a Either of User or error
*/
async updateUserSessions(
user: User,
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);
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: sessionObj,
});
const updatedUser: User = {
...dbUpdatedUser,
currentGQLSession: dbUpdatedUser.currentGQLSession
? JSON.stringify(dbUpdatedUser.currentGQLSession)
: null,
currentRESTSession: dbUpdatedUser.currentRESTSession
? JSON.stringify(dbUpdatedUser.currentRESTSession)
: null,
};
// Publish subscription for user updates
await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser);
return E.right(updatedUser);
} catch (e) {
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);
}
}