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
+5 -8
View File
@@ -1,12 +1,13 @@
import { z } from "zod";
import { MessageAttachmentFileType, MessageAttachmentSource } from "../../data/dto/message/messageOutputDTO";
import { SafeResourceUrl } from "@angular/platform-browser";
import { base64Schema } from "src/app/utils/zod";
const MessageEntitySchema = z.object({
export const MessageEntitySchema = z.object({
$id: z.any().optional(),
id: z.string().optional(),
roomId: z.string().uuid(),
message: z.string(),
roomId: z.string(),
message: z.string().optional(),
messageType: z.number(),
canEdit: z.boolean(),
oneShot: z.boolean(),
@@ -22,15 +23,11 @@ const MessageEntitySchema = z.object({
attachments: z.array(z.object({
fileType: z.nativeEnum(MessageAttachmentFileType),
source: z.nativeEnum(MessageAttachmentSource),
file: z.string().optional(),
file: base64Schema.optional(),
fileName: z.string().optional(),
applicationId: z.string().optional(),
docId: z.string().optional()
})),
attachmentsSource: z.array(z.object({
file: z.string(),
id: z.string(),
})),
})
type Message = z.infer<typeof MessageEntitySchema>;
@@ -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
}
}