113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { Injectable, NotFoundException, InternalServerErrorException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Realm } from './entity/realm.entity';
|
|
import { CreateRealmDto } from './dto/create-realm.dto';
|
|
import { UpdateRealmDto } from './dto/update-realm.dto';
|
|
import { Participant } from '../participant/entity/participant.entity';
|
|
import axios from 'axios';
|
|
import { decode } from 'jsonwebtoken';
|
|
import { AuthRequest } from '../middleware/jwtMiddleware';
|
|
|
|
@Injectable()
|
|
export class RealmService {
|
|
constructor(
|
|
@InjectRepository(Realm)
|
|
private readonly realmRepo: Repository<Realm>,
|
|
@InjectRepository(Participant)
|
|
private readonly participantRepo: Repository<Participant>,
|
|
) {}
|
|
|
|
// service
|
|
async create(data: CreateRealmDto, user: AuthRequest['user']): Promise<Realm> {
|
|
const realm = this.realmRepo.create({
|
|
...data,
|
|
ownerId: user.sub, // we save userId as ownerId (not participantId)
|
|
});
|
|
return await this.realmRepo.save(realm);
|
|
}
|
|
|
|
|
|
async findAll(
|
|
page = 1,
|
|
limit = 10,
|
|
): Promise<{
|
|
docs: Realm[];
|
|
totalDocs: number;
|
|
limit: number;
|
|
page: number;
|
|
totalPages: number;
|
|
hasNextPage: boolean;
|
|
hasPrevPage: boolean;
|
|
nextPage: number | null;
|
|
prevPage: number | null;
|
|
}> {
|
|
const [docs, totalDocs] = await this.realmRepo.findAndCount({
|
|
skip: (page - 1) * limit,
|
|
take: limit,
|
|
relations: [
|
|
'forms',
|
|
'forms.pages',
|
|
'forms.pages.formSections',
|
|
'forms.pages.formSections.attachments',
|
|
'forms.pages.formSections.questions',
|
|
'forms.pages.formSections.questions.options',
|
|
'forms.pages.formSections.questions.answers',
|
|
'participants',
|
|
'owner',
|
|
],
|
|
});
|
|
|
|
const totalPages = Math.ceil(totalDocs / limit);
|
|
return {
|
|
docs,
|
|
totalDocs,
|
|
limit,
|
|
page,
|
|
totalPages,
|
|
hasNextPage: page < totalPages,
|
|
hasPrevPage: page > 1,
|
|
nextPage: page < totalPages ? page + 1 : null,
|
|
prevPage: page > 1 ? page - 1 : null,
|
|
};
|
|
}
|
|
|
|
async findById(id: string): Promise<Realm | null> {
|
|
const realm = await this.realmRepo.findOne({
|
|
where: { id },
|
|
relations: [
|
|
'forms',
|
|
'forms.pages',
|
|
'forms.pages.formSections',
|
|
'forms.pages.formSections.attachments',
|
|
'forms.pages.formSections.questions',
|
|
'forms.pages.formSections.questions.options',
|
|
'forms.pages.formSections.questions.answers',
|
|
'participants',
|
|
'owner',
|
|
],
|
|
});
|
|
if (!realm) {
|
|
throw new NotFoundException(`Realm with ID "${id}" not found`);
|
|
}
|
|
return realm;
|
|
}
|
|
|
|
async update(id: string, data: UpdateRealmDto): Promise<Realm | null> {
|
|
const result = await this.realmRepo.update(
|
|
{ id },
|
|
{ ...data, updatedAt: new Date() },
|
|
);
|
|
if (result.affected === 0) {
|
|
throw new NotFoundException(`Realm with ID "${id}" not found`);
|
|
}
|
|
return await this.findById(id);
|
|
}
|
|
|
|
async remove(id: string): Promise<void> {
|
|
const result = await this.realmRepo.delete({ id });
|
|
if (result.affected === 0) {
|
|
throw new NotFoundException(`Realm with ID "${id}" not found`);
|
|
}
|
|
}
|
|
} |