update list on create room event

This commit is contained in:
Peter Maquiran
2024-06-14 12:00:03 +01:00
parent 3da412eef6
commit 66cdb20b04
7 changed files with 122 additions and 22 deletions
@@ -16,7 +16,15 @@ const tableSchema = z.object({
}),
createdAt: z.any(),
expirationDate: z.any(),
roomType: z.any()
roomType: z.any(),
// lastMessage: z.object({
// sentAt: z.string(),
// message: z.string(),
// sender: z.object({
// wxUserId: z.any(),
// wxFullName: z.any(),
// }).optional(),s
// })
})
const TableMemberListSchema = z.object({
@@ -29,15 +37,7 @@ const TableMemberListSchema = z.object({
wxeMail: z.string(),
userPhoto: z.string().nullable(),
}),
joinAt: z.string(),
// lastMessage: z.object({
// sentAt: z.string(),
// message: z.string(),
// sender: z.object({
// wxUserId: z.number(),
// wxFullName: z.string(),
// }).optional(),
// })
joinAt: z.string()
})
export type TableRoom = z.infer<typeof tableSchema>
@@ -50,7 +50,7 @@ export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
};
roomDataSource.version(1).stores({
room: 'id, createdBy, roomName, roomType, expirationDate',
room: 'id, createdBy, roomName, roomType, expirationDate, lastMessage',
memberList: '$roomIdUserId, id, user, joinAt, roomId',
});
@@ -14,8 +14,26 @@ import { RoomUpdateInputDTO } from '../dto/room/roomUpdateInputDTO';
import { SessionStore } from 'src/app/store/session.service';
import { RoomLiveDataSourceService } from '../data-source/room/room-live-data-source.service';
import { isHttpResponse } from 'src/app/services/http.service';
import { value } from '../../../../../../../_________________/src/plugin/src/sql/Operators/args-attributes';
import { TableMessage } from '../data-source/message/message-local-data-source.service';
import { MessageLiveDataSourceService } from '../data-source/message/message-live-data-source.service';
function date(isoDateString) {
let date = new Date(isoDateString);
const tzOffset = -date.getTimezoneOffset(); // in minutes
const diff = tzOffset >= 0 ? '+' : '-';
const pad = (n: number) => (n < 10 ? '0' : '') + n;
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
diff + pad(Math.floor(Math.abs(tzOffset) / 60)) +
':' + pad(Math.abs(tzOffset) % 60);
}
@Injectable({
providedIn: 'root'
})
@@ -25,8 +43,28 @@ export class RoomRepositoryService {
private roomRemoteDataSourceService: RoomRemoteDataSourceService,
private roomMemoryDataSourceService: Store<RoomRemoteDataSourceState>,
private roomLocalDataSourceService: RoomLocalDataSourceService,
private roomLiveDataSourceService: RoomLiveDataSourceService
) { }
private roomLiveDataSourceService: RoomLiveDataSourceService,
private messageLiveDataSourceService: MessageLiveDataSourceService,
) {
// this.messageLiveDataSourceService.socket.messages$.subscribe(({payload, requestId, type}) => {
// if(payload.sender == null) {
// delete payload.sender
// }
// if(type == 'sendMessage') {
// let clone: TableMessage = {
// ...payload,
// messageId: payload.id,
// }
// this.roomLocalDataSourceService.updateRoom({lastMessage: clone})
// }
// })
}
@captureAndReraiseAsync('RoomRepositoryService/list')
async list() {
@@ -38,14 +76,44 @@ export class RoomRepositoryService {
const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges(result.value.data, localList)
for( const roomData of roomsToInsert) {
// roomData["lastMessage"] = {
// sentAt: date(roomData.createdAt),
// message: "",
// sender: {
// wxUserId: "",
// wxFullName: "",
// },
// }
this.roomLocalDataSourceService.createRoom(roomData)
}
for( const roomData of roomsToUpdate) {
// roomData["lastMessage"] = {
// sentAt: date(roomData.createdAt),
// message: "",
// sender: {
// wxUserId: "",
// wxFullName: "",
// },
// }
this.roomLocalDataSourceService.updateRoom(roomData)
}
for( const roomData of roomsToDelete) {
// roomData["lastMessage"] = {
// sentAt: date(roomData.createdAt),
// message: "",
// sender: {
// wxUserId: 0,
// wxFullName: "",
// },
// }
this.roomLocalDataSourceService.deleteRoomById(roomData.id)
}
}
@@ -142,16 +210,34 @@ export class RoomRepositoryService {
if(result.isOk()) {
if(!result.value.data.createdBy) {
let dataObject;
result.value.data.createdBy = {
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId,
}
dataObject = result.value.data
dataObject.lastMessage = {
sentAt: date(new Date()),
message: "",
sender: {
wxUserId: "",
wxFullName: "",
},
}
}
this.roomMemoryDataSourceService.dispatch(addRoom(result.value))
const localResult = await this.roomLocalDataSourceService.createRoom(result.value.data)
this.messageLiveDataSourceService.sendMessage({
type: 'createRoom',
payload: {a: '5'}
})
return localResult.map(e => result.value)
}