chore: add new route for the restore from trash

This commit is contained in:
mahyargdz
2025-07-12 12:17:49 +03:30
parent 352cdde13f
commit 97383d25f6
3 changed files with 51 additions and 0 deletions
+1
View File
@@ -203,6 +203,7 @@ export const enum EmailMessage {
MESSAGE_MOVED_TO_TRASH_SUCCESSFULLY = "پیام با موفقیت به سطل زباله منتقل شد",
MESSAGE_MOVED_TO_INBOX_FROM_ARCHIVE_SUCCESSFULLY = "پیام با موفقیت از آرشیو به صندوق ورودی منتقل شد",
MESSAGE_MOVED_TO_INBOX_FROM_FAVORITE_SUCCESSFULLY = "پیام با موفقیت از علاقه مندی ها به صندوق ورودی منتقل شد",
MESSAGE_MOVED_TO_INBOX_FROM_TRASH_SUCCESSFULLY = "پیام با موفقیت از سطل زباله به صندوق ورودی منتقل شد",
MESSAGE_NOT_FOUND = "پیام یافت نشد",
}
+9
View File
@@ -190,4 +190,13 @@ export class EmailController {
moveMessageToInboxFromFavorite(@UserDec("wildduckUserId") userId: string, @Param() params: MessageParamDto) {
return this.emailService.moveMessageToInboxFromFavorite(userId, Number(params.messageId));
}
@Patch("messages/:messageId/restore")
@ApiOperation({ summary: "Restore a message from trash back to inbox" })
@ApiResponse({ status: 200, description: "Message restored from trash to inbox successfully" })
@ApiResponse({ status: 404, description: "Message not found in trash" })
@ApiResponse({ status: 403, description: "Not authorized to restore message" })
moveMessageToInboxFromTrash(@UserDec("wildduckUserId") userId: string, @Param() params: MessageParamDto) {
return this.emailService.moveMessageToInboxFromTrash(userId, Number(params.messageId));
}
}
@@ -663,4 +663,45 @@ export class EmailService {
throw error;
}
}
//==============================================
async moveMessageToInboxFromTrash(userId: string, messageId: number) {
this.logger.log(`Moving message ${messageId} from trash to inbox for user: ${userId}`);
try {
const inboxMailboxId = await this.mailboxResolverService.getInboxMailboxId(userId);
const trashMailboxId = await this.mailboxResolverService.getTrashMailboxId(userId);
const result = await firstValueFrom(
this.mailServerService.messages.updateMessage(userId, trashMailboxId, messageId, {
moveTo: inboxMailboxId,
}),
);
// Emit WebSocket notification
const payload: EmailMovePayload = {
messageId,
userId,
fromMailboxId: trashMailboxId,
toMailboxId: inboxMailboxId,
fromMailboxName: "Trash",
toMailboxName: "Inbox",
timestamp: new Date().toISOString(),
};
await this.emailGateway.notifyMessageMoved(payload);
this.logger.log(`Message ${messageId} moved from trash to inbox successfully`);
return {
success: true,
message: EmailMessage.MESSAGE_MOVED_TO_INBOX_FROM_TRASH_SUCCESSFULLY,
result,
};
} catch (error) {
this.logger.error(
`Failed to move message ${messageId} from trash to inbox for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`,
);
throw error;
}
}
}