import socketsInRoom from '../models/socketsInRoom' ; import roomCodeOnSocket from '../models/roomCodeOnSocket' ; import dateUtil from '../utils/date' ; import { Socket } from 'socket.io' ; import { ReceiveChatType , ParticipantType } from '../@types/dataType' ; const getParticipants = async ( roomCode ) => { const participantsInDB = await socketsInRoom . getSockets ( roomCode ) ; const participants = Object . entries ( participantsInDB ) . map ( ( [ key , value ] ) => { const { nickname , language , imageLink } = JSON . parse ( value ) ; return { socketId : key , nickname , language , imageLink } ; } ) ; return participants ; } ; const insertSocketInfoIntoDB = async ( socketId , roomCode , nickname , language , imageLink ) => { await socketsInRoom . setSocket ( roomCode , socketId , JSON . stringify ( { nickname , language , imageLink } ) ) ; await roomCodeOnSocket . saveRoomCode ( socketId , roomCode ) ; return true ; } ; const createReceiveChat = async ( socketId , Korean , English ) => { const roomCode = await roomCodeOnSocket . getRoomCode ( socketId ) ; const socket = await socketsInRoom . getSocket ( roomCode , socketId ) ; const { nickname , imageLink } = JSON . parse ( socket ) ; const receiveChat = { Korean , English , senderId : socketId , nickname , imageLink , createdAt : dateUtil . getNow ( ) , } ; return { roomCode , receiveChat } ; } ; const removeSocket = async ( socketId , roomCode ) => { await socketsInRoom . removeSocket ( roomCode , socketId ) ; await roomCodeOnSocket . removeSocket ( socketId ) ; return true ; } ; const emitSocketError = ( socket , errorMessage ) => { socket . emit ( 'socket error' , JSON . stringify ( { errorMessage } ) ) ; } ; const socketService = { getParticipants , insertSocketInfoIntoDB , createReceiveChat , removeSocket , emitSocketError , } ; export default socketService ;