2024-06-04 16:21:11 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2024-06-05 10:28:38 +01:00
|
|
|
import { RoomListItemOutPutDTO, RoomListOutPutDTO } from '../../dto/room/roomListOutputDTO';
|
2024-06-10 16:34:43 +01:00
|
|
|
import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
|
2024-06-10 22:13:10 +01:00
|
|
|
import { err, ok, Result } from 'neverthrow';
|
2024-06-04 16:21:11 +01:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
|
|
const tableSchema = z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
roomName: z.string(),
|
|
|
|
|
createdBy: z.any(),
|
|
|
|
|
createdAt: z.any(),
|
|
|
|
|
expirationDate: z.any(),
|
|
|
|
|
roomType: z.any()
|
|
|
|
|
})
|
2024-06-07 12:29:53 +01:00
|
|
|
|
|
|
|
|
const TableMemberListSchema = z.object({
|
|
|
|
|
$roomIdUserId: z.string().optional(),
|
|
|
|
|
id: z.string(),
|
|
|
|
|
roomId: z.string(),
|
|
|
|
|
user: z.object({
|
|
|
|
|
wxUserId: z.number(),
|
|
|
|
|
wxFullName: z.string(),
|
|
|
|
|
wxeMail: z.string(),
|
|
|
|
|
userPhoto: z.string().nullable(),
|
|
|
|
|
}),
|
|
|
|
|
joinAt: z.string(),
|
|
|
|
|
})
|
|
|
|
|
|
2024-06-04 16:26:50 +01:00
|
|
|
export type TableRoom = z.infer<typeof tableSchema>
|
2024-06-07 12:29:53 +01:00
|
|
|
export type TableMemberList = z.infer<typeof TableMemberListSchema>
|
2024-06-04 16:21:11 +01:00
|
|
|
|
|
|
|
|
// Database declaration (move this to its own module also)
|
|
|
|
|
export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
|
2024-06-04 16:26:50 +01:00
|
|
|
room: EntityTable<TableRoom, 'id'>;
|
2024-06-07 12:29:53 +01:00
|
|
|
memberList: EntityTable<TableMemberList, '$roomIdUserId'>;
|
2024-06-04 16:21:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
roomDataSource.version(1).stores({
|
2024-06-07 12:29:53 +01:00
|
|
|
room: 'id, createdBy, roomName, roomType, expirationDate',
|
|
|
|
|
memberList: '$roomIdUserId, id, user, joinAt, roomId',
|
2024-06-04 16:21:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class RoomLocalDataSourceService {
|
|
|
|
|
|
|
|
|
|
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
|
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
2024-06-05 11:55:38 +01:00
|
|
|
async createRoom(data: TableRoom) {
|
2024-06-04 16:21:11 +01:00
|
|
|
try {
|
2024-06-05 11:09:03 +01:00
|
|
|
const result = await roomDataSource.room.add(data)
|
2024-06-04 16:21:11 +01:00
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
2024-06-05 11:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateRoom(data: TableRoom) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await roomDataSource.room.update(data.id, data);
|
|
|
|
|
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createOrUpdateRoom(data: TableRoom) {
|
|
|
|
|
const createResult = await this.createRoom(data)
|
|
|
|
|
|
|
|
|
|
if(createResult.isOk()) {
|
|
|
|
|
return this.updateRoom(data)
|
|
|
|
|
} else {
|
|
|
|
|
return createResult
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 12:29:53 +01:00
|
|
|
|
|
|
|
|
async addMember(data: TableMemberList) {
|
|
|
|
|
try {
|
|
|
|
|
data.$roomIdUserId = data.roomId + data.user.wxUserId
|
|
|
|
|
const result = await roomDataSource.memberList.add(data)
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 22:13:10 +01:00
|
|
|
|
|
|
|
|
async removeMemberFromRoom($roomIdUserId): Promise<Result<any ,any>> {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const member = await roomDataSource.memberList.where({ $roomIdUserId: $roomIdUserId }).first();
|
|
|
|
|
|
|
|
|
|
if (member) {
|
|
|
|
|
const result = await ok(roomDataSource.memberList.delete($roomIdUserId));
|
|
|
|
|
console.log(`Member with $roomIdUserId ${$roomIdUserId} removed from room ${member.roomId}.`);
|
|
|
|
|
return result
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`No member found with $roomIdUserId ${$roomIdUserId}`);
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 11:55:38 +01:00
|
|
|
async getRoomById(id: any) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await roomDataSource.room.get(id)
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
2024-06-04 16:21:11 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-07 17:00:30 +01:00
|
|
|
|
|
|
|
|
async getRoomList() {
|
|
|
|
|
return await roomDataSource.room.toArray()
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 17:04:04 +01:00
|
|
|
|
2024-06-04 16:21:11 +01:00
|
|
|
getItemsLive(): Observable<RoomListOutPutDTO[]> {
|
|
|
|
|
return liveQuery(() => roomDataSource.room.toArray()) as any;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 10:28:38 +01:00
|
|
|
getRoomByIdLive(id: any): Observable<RoomListItemOutPutDTO | undefined> {
|
|
|
|
|
return liveQuery(() => roomDataSource.room.get(id)) as any;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 16:34:43 +01:00
|
|
|
async getRoomMemberById(roomId: any) {
|
|
|
|
|
return await roomDataSource.memberList.where('roomId').equals(roomId).toArray()
|
|
|
|
|
}
|
|
|
|
|
getRoomMemberByIdLive(roomId: any) {
|
|
|
|
|
return liveQuery(() => roomDataSource.memberList.where('roomId').equals(roomId).toArray())
|
2024-06-07 12:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 16:21:11 +01:00
|
|
|
}
|
|
|
|
|
|