-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdriver-chat.service.ts
More file actions
52 lines (45 loc) · 1.78 KB
/
driver-chat.service.ts
File metadata and controls
52 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { DriverChatRoom } from "@schemas/driver-chat-room.entity";
import { IChatService } from "./chat.interface";
import { UserDto } from "@auth/presentation/user.dto";
import { DriverService } from "@driver/application/driver.service";
import { ChatRoomDto } from "../presentation/chat.dto";
import { toDto } from "@common/function/util.function";
@Injectable()
export class DriverChatService implements IChatService {
private readonly logger = new Logger(DriverChatService.name);
constructor(
@InjectRepository(DriverChatRoom)
private readonly driverChatRoomRepository: Repository<DriverChatRoom>,
private readonly driverService: DriverService,
) {}
async exitsUserRoom(user: UserDto, chatRoomId: number): Promise<boolean> {
return await this.driverChatRoomRepository.exists({
where: { driverId: user.userId, chatRoomId },
});
}
async findChatRooms(user: UserDto): Promise<ChatRoomDto[]> {
const driverChatRooms = await this.driverChatRoomRepository.find({
where: { driverId: user.userId },
});
return await Promise.all(driverChatRooms.map((room) => room.chatRoom)).then(
(rooms) => {
return rooms.map((room) => {
return toDto(ChatRoomDto, room);
});
},
);
}
async createChatRoom(dto: ChatRoomDto): Promise<ChatRoomDto> {
const newRoom = this.driverChatRoomRepository.create({
chatRoomId: dto.chatRoomId,
driverId: dto.inviteUser.userId,
});
return await this.driverChatRoomRepository.save(newRoom).then((room) => {
this.logger.log(`Driver Chat room created: ${room.chatRoomId}`);
return toDto(ChatRoomDto, room);
});
}
}