Files
prism/backend/dist/modules/conversations/conversations.service.js

89 lines
4.3 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConversationsService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const conversation_entity_1 = require("./entities/conversation.entity");
let ConversationsService = class ConversationsService {
conversationsRepository;
constructor(conversationsRepository) {
this.conversationsRepository = conversationsRepository;
}
async create(participants, name, isGroup = false) {
const conversation = this.conversationsRepository.create({
participants,
name,
isGroup,
});
return this.conversationsRepository.save(conversation);
}
async findAllForUser(userId) {
return this.conversationsRepository
.createQueryBuilder('conversation')
.leftJoinAndSelect('conversation.participants', 'participants')
.leftJoinAndSelect('conversation.messages', 'messages')
.leftJoin('conversation.participants', 'user')
.where('user.id = :userId', { userId })
.orderBy('messages.createdAt', 'DESC')
.getMany();
}
async findOne(id, userId) {
const conversation = await this.conversationsRepository
.createQueryBuilder('conversation')
.leftJoinAndSelect('conversation.participants', 'participants')
.leftJoinAndSelect('conversation.messages', 'messages')
.leftJoinAndSelect('messages.sender', 'sender')
.where('conversation.id = :id', { id })
.orderBy('messages.createdAt', 'ASC')
.getOne();
if (!conversation) {
throw new common_1.NotFoundException('Беседа не найдена');
}
const isParticipant = conversation.participants.some(p => p.id === userId);
if (!isParticipant) {
throw new common_1.ForbiddenException('Вы не являетесь участником этой беседы');
}
return conversation;
}
async findOrCreatePrivate(user1Id, user2Id) {
const existingConversation = await this.conversationsRepository
.createQueryBuilder('conversation')
.leftJoin('conversation.participants', 'p1')
.leftJoin('conversation.participants', 'p2')
.where('conversation.isGroup = false')
.andWhere('p1.id = :user1Id', { user1Id })
.andWhere('p2.id = :user2Id', { user2Id })
.getOne();
if (existingConversation) {
return existingConversation;
}
return this.create([], undefined, false);
}
async addParticipant(conversationId, userId, participantId) {
const conversation = await this.findOne(conversationId, userId);
if (!conversation.isGroup) {
throw new common_1.ForbiddenException('Нельзя добавлять участников в приватные беседы');
}
return conversation;
}
};
exports.ConversationsService = ConversationsService;
exports.ConversationsService = ConversationsService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, typeorm_1.InjectRepository)(conversation_entity_1.Conversation)),
__metadata("design:paramtypes", [typeorm_2.Repository])
], ConversationsService);
//# sourceMappingURL=conversations.service.js.map