2026-01-08 17:56:00 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class RoomPackService {
|
|
|
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create room pack (called when room is created)
|
|
|
|
|
*/
|
|
|
|
|
async create(roomId: string, sourcePackId?: string) {
|
|
|
|
|
const name = `Room Pack ${Date.now()}`;
|
|
|
|
|
const description = sourcePackId
|
|
|
|
|
? 'Copied from source pack'
|
|
|
|
|
: 'Custom room questions';
|
|
|
|
|
|
2026-01-08 18:17:31 +00:00
|
|
|
let questions: any = [];
|
2026-01-08 17:56:00 +00:00
|
|
|
let questionCount = 0;
|
|
|
|
|
|
|
|
|
|
// If source pack provided, copy questions
|
|
|
|
|
if (sourcePackId) {
|
|
|
|
|
const sourcePack = await this.prisma.questionPack.findUnique({
|
|
|
|
|
where: { id: sourcePackId },
|
|
|
|
|
select: { questions: true, questionCount: true },
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-08 18:17:31 +00:00
|
|
|
if (sourcePack && sourcePack.questions) {
|
2026-01-08 17:56:00 +00:00
|
|
|
questions = sourcePack.questions;
|
|
|
|
|
questionCount = sourcePack.questionCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.prisma.roomPack.create({
|
|
|
|
|
data: {
|
|
|
|
|
roomId,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
sourcePackId,
|
|
|
|
|
questions,
|
|
|
|
|
questionCount,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get room pack by roomId
|
|
|
|
|
*/
|
|
|
|
|
async findByRoomId(roomId: string) {
|
|
|
|
|
return this.prisma.roomPack.findUnique({
|
|
|
|
|
where: { roomId },
|
|
|
|
|
include: { sourcePack: true },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update room pack questions
|
|
|
|
|
*/
|
|
|
|
|
async updateQuestions(roomId: string, questions: any[]) {
|
|
|
|
|
const questionCount = Array.isArray(questions) ? questions.length : 0;
|
|
|
|
|
|
|
|
|
|
return this.prisma.roomPack.update({
|
|
|
|
|
where: { roomId },
|
|
|
|
|
data: {
|
|
|
|
|
questions,
|
|
|
|
|
questionCount,
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add questions from another pack (import/copy)
|
|
|
|
|
*/
|
|
|
|
|
async importQuestions(roomId: string, sourcePackId: string, questionIndices: number[]) {
|
|
|
|
|
const roomPack = await this.findByRoomId(roomId);
|
|
|
|
|
const sourcePack = await this.prisma.questionPack.findUnique({
|
|
|
|
|
where: { id: sourcePackId },
|
|
|
|
|
select: { questions: true },
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-08 18:17:31 +00:00
|
|
|
if (!roomPack) {
|
|
|
|
|
throw new Error('Room pack not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sourcePack || !sourcePack.questions || !Array.isArray(sourcePack.questions)) {
|
2026-01-08 17:56:00 +00:00
|
|
|
throw new Error('Source pack not found or invalid');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get existing questions
|
|
|
|
|
const existingQuestions = Array.isArray(roomPack.questions)
|
2026-01-08 18:17:31 +00:00
|
|
|
? (roomPack.questions as any[])
|
2026-01-08 17:56:00 +00:00
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
// Import selected questions (create copies)
|
2026-01-08 18:17:31 +00:00
|
|
|
const sourceQuestions = sourcePack.questions as any[];
|
2026-01-08 17:56:00 +00:00
|
|
|
const questionsToImport = questionIndices
|
2026-01-08 18:17:31 +00:00
|
|
|
.map(idx => sourceQuestions[idx])
|
2026-01-08 17:56:00 +00:00
|
|
|
.filter(Boolean)
|
|
|
|
|
.map(q => ({ ...q })); // Deep copy
|
|
|
|
|
|
|
|
|
|
const updatedQuestions = [...existingQuestions, ...questionsToImport];
|
|
|
|
|
|
|
|
|
|
return this.updateQuestions(roomId, updatedQuestions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Soft delete room pack
|
|
|
|
|
*/
|
|
|
|
|
async softDelete(roomId: string) {
|
|
|
|
|
return this.prisma.roomPack.update({
|
|
|
|
|
where: { roomId },
|
|
|
|
|
data: { deletedAt: new Date() },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hard delete room pack (for cleanup)
|
|
|
|
|
*/
|
|
|
|
|
async hardDelete(roomId: string) {
|
|
|
|
|
return this.prisma.roomPack.delete({
|
|
|
|
|
where: { roomId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|