Files
doneit-web/src/app/module/chat/domain/use-case/message/message-attachment-by-message-id.service.ts
T

126 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-08-13 17:05:46 +01:00
import { Injectable } from '@angular/core';
import { z } from 'zod';
2024-08-27 09:14:59 +01:00
import { AttachmentRemoteDataSourceService } from 'src/app/module/chat/data/repository/attachment/attachment-remote-repository.service'
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/repository/attachment/attachment-local-repository.service'
import { createBlobUrl } from 'src/app/utils/ToBase64';
import { err, Result } from 'neverthrow';
2024-09-03 16:26:54 +01:00
import { MessageEntitySchema } from '../../../../../core/chat/entity/message';
2024-09-02 12:33:43 +01:00
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
import { isHttpResponse } from 'src/app/infra/http/http.service';
2024-08-13 17:05:46 +01:00
2024-09-03 16:26:54 +01:00
const MessageAttachmentByMessageIdSchema = MessageEntitySchema.pick({
$id: true,
id: true,
attachments: true,
2024-08-13 17:05:46 +01:00
})
export type MessageAttachmentByMessageIdInput = z.infer<typeof MessageAttachmentByMessageIdSchema>
@Injectable({
providedIn: 'root'
})
export class MessageAttachmentByMessageIdUseCase {
constructor(
private AttachmentRemoteDataSourceService: AttachmentRemoteDataSourceService,
private AttachmentLocalDataSource: AttachmentLocalDataSource
) { }
2024-09-04 12:15:44 +01:00
@XTracerAsync({name:'Message-Attachment-By-MessageIdUseCase', module:'chat', bugPrint: true, waitNThrow: 15000})
2024-09-03 16:26:54 +01:00
async execute(input: MessageAttachmentByMessageIdInput, tracing?: TracingType): Promise<Result<string, any>> {
tracing.setAttribute('messageId', input.id)
2024-08-13 17:05:46 +01:00
const getLocalAttachment = await this.AttachmentLocalDataSource.findOne({
2024-08-17 22:05:57 +01:00
$messageId: input.$id
2024-08-13 17:05:46 +01:00
})
if(getLocalAttachment.isOk() && getLocalAttachment.value) {
2024-09-04 12:15:44 +01:00
tracing.setAttribute('download', 'false')
// has blob
if(getLocalAttachment.value.file) {
const dataUrl = await createBlobUrl(getLocalAttachment.value.file)
if(dataUrl.isOk()) {
return dataUrl
} else {
return dataUrl
}
} else {
// has data url
return getLocalAttachment.map((e) => {
2024-09-02 12:33:43 +01:00
// Logger.info('restored file .', {
// data: e.base64.slice(0, 100)+'...'
// })
return e.base64
})
2024-08-13 17:05:46 +01:00
}
2024-09-02 12:33:43 +01:00
2024-08-13 17:05:46 +01:00
} else {
2024-09-02 12:33:43 +01:00
tracing.setAttribute('download', 'true')
tracing.setAttribute('attachmentId', input.attachments[0].id.toString())
2024-09-03 16:26:54 +01:00
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input.attachments[0].id)
2024-08-17 22:05:57 +01:00
if(result.isErr()) {
2024-09-02 12:33:43 +01:00
tracing.hasError('failed to download message attachment', {
2024-08-17 22:05:57 +01:00
error: result.error,
data: 'document id '+ input.attachments[0].id,
messageId: input.id,
$messageId: input.$id
})
2024-09-02 12:33:43 +01:00
if(isHttpResponse(result.error)) {
tracing.setAttribute('attachmentUrl', result.error.message)
}
2024-08-17 22:05:57 +01:00
}
2024-08-13 17:05:46 +01:00
if(result.isOk()) {
const dataUrl = await createBlobUrl(result.value)
if(dataUrl.isOk()) {
//console.log('done convert')
2024-09-02 12:33:43 +01:00
//Logger.info('downloaded file .', {
// data: dataUrl.value.slice(0, 100)+'...'
2024-09-02 12:33:43 +01:00
//})
this.AttachmentLocalDataSource.insert({
$messageId: input.$id,
file: result.value,
fileType: input.attachments[0].fileType,
source: input.attachments[0].source,
fileName: input.attachments[0].fileName,
applicationId: input.attachments[0].applicationId,
docId: input.attachments[0].docId,
mimeType: input.attachments[0].mimeType,
}).then((e) => {
if(e.isErr()) {
2024-09-02 12:33:43 +01:00
tracing.hasError('failed to create attachment locally on send message', {
error: e.error,
// data: dataUrl.value.slice(0, 100)+'...'
})
}
})
return dataUrl
} else {
console.log('dataUrl eerror', dataUrl.error)
return err(false)
}
} else {
return result as any
}
2024-08-13 17:05:46 +01:00
}
2024-08-15 14:29:11 +01:00
2024-08-13 17:05:46 +01:00
}
}