starting implementation of user status changing
This commit is contained in:
@@ -2,6 +2,8 @@ import express, { Application, Request, Response } from "express";
|
||||
|
||||
import { NextFunction } from "express";
|
||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
import { UserService } from "./services/user.service";
|
||||
import { UserStatus } from "@nirvana/core/models";
|
||||
import { connectToDatabase } from "./services/database.service";
|
||||
import cors from "cors";
|
||||
import getContactsRoutes from "./routes/contacts";
|
||||
@@ -101,6 +103,31 @@ io.on("connection", function (socket: any) {
|
||||
);
|
||||
});
|
||||
|
||||
// change of user status to all of users' rooms and db update
|
||||
socket.on(
|
||||
SocketChannels.SEND_USER_STATUS_UPDATE,
|
||||
async (userGoogleId: string, newStatus: UserStatus) => {
|
||||
console.log("new status for user");
|
||||
|
||||
const resultUpdate = await UserService.updateUserStatus(
|
||||
userGoogleId,
|
||||
newStatus
|
||||
);
|
||||
|
||||
// tell all rooms that the user is part of
|
||||
// that this user has updated their status
|
||||
if (resultUpdate?.modifiedCount) {
|
||||
socket.rooms.forEach((roomId: string) => {
|
||||
io.in(roomId).emit(
|
||||
SocketChannels.SEND_USER_STATUS_UPDATE,
|
||||
userGoogleId,
|
||||
newStatus
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// ==== DISCONNECT ====
|
||||
socket.on("disconnect", () => {
|
||||
console.log("user disconnected");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
||||
|
||||
import { ObjectId } from "mongodb";
|
||||
import { UserStatus } from "../../core/models/user.model";
|
||||
import axios from "axios";
|
||||
import { collections } from "./database.service";
|
||||
|
||||
@@ -58,8 +59,9 @@ export class UserService {
|
||||
}
|
||||
|
||||
static async createUserIfNotExists(newUser: User) {
|
||||
const exists = (await collections.users?.findOne({ id: newUser.googleId }))
|
||||
?._id;
|
||||
const exists = (
|
||||
await collections.users?.findOne({ googleId: newUser.googleId })
|
||||
)?._id;
|
||||
|
||||
if (!exists) {
|
||||
return await collections.users?.insertOne(newUser);
|
||||
@@ -78,4 +80,15 @@ export class UserService {
|
||||
)
|
||||
).data;
|
||||
}
|
||||
|
||||
static async updateUserStatus(userGoogleId: string, newStatus: UserStatus) {
|
||||
const query = { googleId: userGoogleId };
|
||||
const updateDoc = {
|
||||
$set: { status: newStatus, lastUpdatedDate: new Date() },
|
||||
};
|
||||
|
||||
const resultUpdate = await collections.users?.updateOne(query, updateDoc);
|
||||
|
||||
return resultUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user