Files
SerpentRace/SerpentRace_Backend/dist/Infrastructure/Repository/ChatArchiveRepository.js
T

132 lines
5.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatArchiveRepository = void 0;
const ormconfig_1 = require("../ormconfig");
const ChatArchiveAggregate_1 = require("../../Domain/Chat/ChatArchiveAggregate");
const Logger_1 = require("../../Application/Services/Logger");
class ChatArchiveRepository {
constructor() {
this.repo = ormconfig_1.AppDataSource.getRepository(ChatArchiveAggregate_1.ChatArchiveAggregate);
}
async create(archive) {
const startTime = Date.now();
try {
const result = await this.repo.save(archive);
(0, Logger_1.logDatabase)('Chat archive created successfully', undefined, Date.now() - startTime, {
archiveId: result.id,
chatId: result.chatId,
messageCount: result.archivedMessages?.length || 0
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.create error', error);
throw new Error('Failed to create chat archive in database');
}
}
async findAll() {
const startTime = Date.now();
try {
const result = await this.repo.find();
(0, Logger_1.logDatabase)('All chat archives retrieved', undefined, Date.now() - startTime, {
count: result.length
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.findAll error', error);
throw new Error('Failed to retrieve chat archives from database');
}
}
async findById(id) {
const startTime = Date.now();
try {
const result = await this.repo.findOneBy({ id });
(0, Logger_1.logDatabase)('Chat archive retrieved by id', `findById(${id})`, Date.now() - startTime, {
archiveId: id,
found: !!result
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.findById error', error);
throw new Error('Failed to find chat archive by id');
}
}
async findByChatId(chatId) {
const startTime = Date.now();
try {
const result = await this.repo
.find({
where: { chatId },
order: { archivedAt: 'DESC' }
});
(0, Logger_1.logDatabase)('Chat archives retrieved by chat id', `findByChatId(${chatId})`, Date.now() - startTime, {
chatId,
count: result.length
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.findByChatId error', error);
throw new Error('Failed to find chat archives by chat id');
}
}
async findByGameId(gameId) {
const startTime = Date.now();
try {
const result = await this.repo
.find({
where: { gameId },
order: { archivedAt: 'DESC' }
});
(0, Logger_1.logDatabase)('Chat archives retrieved by game id', `findByGameId(${gameId})`, Date.now() - startTime, {
gameId,
count: result.length
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.findByGameId error', error);
throw new Error('Failed to find chat archives by game id');
}
}
async delete(id) {
const startTime = Date.now();
try {
const result = await this.repo.delete(id);
(0, Logger_1.logDatabase)('Chat archive deleted', `delete(${id})`, Date.now() - startTime, {
archiveId: id,
affected: result.affected
});
return result;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.delete error', error);
throw new Error('Failed to delete chat archive');
}
}
async cleanup(olderThanDays) {
const startTime = Date.now();
try {
const cutoffDate = new Date(Date.now() - olderThanDays * 24 * 60 * 60 * 1000);
const result = await this.repo
.createQueryBuilder()
.delete()
.where('archivedAt < :cutoffDate', { cutoffDate })
.execute();
(0, Logger_1.logDatabase)('Chat archive cleanup completed', `cleanup(${olderThanDays} days)`, Date.now() - startTime, {
olderThanDays,
deleted: result.affected,
cutoffDate
});
return result.affected || 0;
}
catch (error) {
(0, Logger_1.logError)('ChatArchiveRepository.cleanup error', error);
throw new Error('Failed to cleanup old chat archives');
}
}
}
exports.ChatArchiveRepository = ChatArchiveRepository;
//# sourceMappingURL=ChatArchiveRepository.js.map