Files
doneit-web/src/app/module/chat/domain/use-case/socket/socket-message-update-use-case.service.ts
T

64 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-08-02 16:20:26 +01:00
import { Injectable } from '@angular/core';
2024-08-19 16:01:58 +01:00
import { MessageLocalDataSourceService } from '../../../data/repository/message/message-local-data-source.service';
2024-08-08 11:44:41 +01:00
import { MessageOutPutDataDTO, MessageOutPutDataDTOSchema } from '../../../data/dto/message/messageOutputDTO';
2024-08-08 15:53:00 +01:00
import { ParamsValidation, SafeValidateSchema, ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
2024-08-27 20:29:57 +01:00
import { MessageEntitySchema } from 'src/app/core/chat/entity/message';
import { z } from 'zod';
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
const SocketMessageUpdateOutputSchema = MessageEntitySchema.pick({
id: true,
attachments: true,
canEdit: true,
editedAt: true,
info: true,
isDeleted: true,
message: true,
messageType: true,
oneShot: true,
reactions: true,
receiverId: true,
requireUnlock: true,
roomId: true,
sender: true,
sending: true,
sentAt: true,
})
2024-08-02 16:20:26 +01:00
2024-08-27 20:29:57 +01:00
export type ISocketMessageUpdateOutput = z.infer<typeof SocketMessageUpdateOutputSchema>
2024-08-02 16:20:26 +01:00
@Injectable({
providedIn: 'root'
})
export class SocketMessageUpdateUseCaseService {
constructor(
private messageLocalDataSourceService: MessageLocalDataSourceService
) { }
2024-08-20 16:34:47 +01:00
@XTracerAsync({name:'Socket-Message-Update-UseCase', bugPrint: true, module:'chat',})
2024-08-27 20:29:57 +01:00
async execute(input: ISocketMessageUpdateOutput, tracing?: TracingType) {
2024-08-08 15:53:00 +01:00
2024-08-27 20:29:57 +01:00
ParamsValidation(MessageOutPutDataDTOSchema, input, tracing)
2024-08-08 15:53:00 +01:00
tracing?.addEvent("Message existe?")
2024-08-27 20:29:57 +01:00
const result = await this.messageLocalDataSourceService.findOne({id: input.id})
2024-08-02 16:20:26 +01:00
2024-08-27 20:29:57 +01:00
const messageToSave: MessageTable = input
messageToSave.sending = false
2024-08-02 16:20:26 +01:00
2024-08-27 09:14:59 +01:00
if(result.isOk() && result.value) {
2024-08-08 15:53:00 +01:00
tracing?.addEvent("Message found")
2024-08-27 20:29:57 +01:00
const updateResult = await this.messageLocalDataSourceService.update(result.value.$id, messageToSave)
2024-08-20 16:34:47 +01:00
tracing.setAttribute('outcome', 'success')
return updateResult
2024-08-27 09:14:59 +01:00
} else if(result.isOk() && !result.value) {
2024-08-08 15:53:00 +01:00
tracing?.addEvent("Message not found")
2024-08-02 16:20:26 +01:00
}
2024-08-20 16:34:47 +01:00
2024-08-02 16:20:26 +01:00
}
}