send all file

This commit is contained in:
Peter Maquiran
2024-08-15 14:29:11 +01:00
parent bf50c923d1
commit 2aae4da3cd
9 changed files with 123 additions and 63 deletions
@@ -48,6 +48,6 @@ export class MessageAttachmentByMessageIdUseCase {
return dataUrl
})
}
}
}
@@ -1,12 +1,13 @@
import { Injectable } from '@angular/core';
import { MessageEntity } from '../entity/message';
import { MessageEntity, MessageEntitySchema, } from '../entity/message';
import { MessageRepositoryService } from "src/app/module/chat/data/repository/message-respository.service"
import { AttachmentRepositoryService } from "src/app/module/chat/data/repository/attachment-repository.service"
import { z } from 'zod';
import { z, ZodError } from 'zod';
import { v4 as uuidv4 } from 'uuid'
import { InstanceId } from '../chat-service.service';
import { createDataURL } from 'src/app/utils/ToBase64';
import { DomSanitizer } from '@angular/platform-browser';
import { zodSafeValidation } from 'src/app/utils/zodValidation';
const MessageInputUseCaseSchema = z.object({
memberId: z.number(),
@@ -24,48 +25,61 @@ export class MessageCreateUseCaseService {
constructor(
private MessageRepositoryService: MessageRepositoryService,
private AttachmentRepositoryService: AttachmentRepositoryService,
private sanitizer: DomSanitizer
private sanitizer: DomSanitizer,
) { }
async execute(message: MessageEntity) {
message.sendAttemp++;
const validation = zodSafeValidation<MessageEntity>(MessageEntitySchema, message)
message.requestId = InstanceId +'@'+ uuidv4();
if(validation.isOk()) {
message.sendAttemp++;
const createMessageLocally = await this.MessageRepositoryService.createMessageLocally(message)
message.requestId = InstanceId +'@'+ uuidv4();
if(createMessageLocally.isOk()) {
const createMessageLocally = await this.MessageRepositoryService.createMessageLocally(message)
console.log('==========================',message);
if(message.hasAttachment) {
if(createMessageLocally.isOk()) {
for (const attachment of message.attachments) {
console.log('==========================',message);
if(message.hasAttachment) {
const createAttachmentLocally = this.AttachmentRepositoryService.create({
$messageId: createMessageLocally.value.$id,
file: createDataURL(attachment.file, attachment.mimeType)
})
for (const attachment of message.attachments) {
const createAttachmentLocally = this.AttachmentRepositoryService.create({
$messageId: createMessageLocally.value.$id,
file: createDataURL(attachment.file, attachment.mimeType)
})
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
}
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
}
const sendToServer = await this.MessageRepositoryService.sendMessage(message)
if(!sendToServer.isOk()) {
console.log('sendToServer error', sendToServer.error)
}
return sendToServer
}
const sendToServer = await this.MessageRepositoryService.sendMessage(message)
const result = await this.MessageRepositoryService.sendMessage(message)
return result
} else {
if(!sendToServer.isOk()) {
console.log('sendToServer error', sendToServer.error)
if(validation.error.formErrors.fieldErrors.attachments) {
console.log(message.attachments)
console.error('invalid attachment', validation.error.formErrors.fieldErrors.attachments)
}
return sendToServer
}
const result = await this.MessageRepositoryService.sendMessage(message)
return result
}
}