1- import { accessibleQuery , InjectModel } from '@douglasneuroinformatics/libnest' ;
2- import type { Model } from '@douglasneuroinformatics/libnest' ;
1+ import { accessibleQuery , InjectModel , InjectPrismaClient } from '@douglasneuroinformatics/libnest' ;
2+ import type { ExtendedPrismaClient , Model } from '@douglasneuroinformatics/libnest' ;
33import { ConflictException , Injectable , NotFoundException } from '@nestjs/common' ;
44import type { Prisma } from '@prisma/client' ;
55
@@ -9,7 +9,10 @@ import { CreateSubjectDto } from './dto/create-subject.dto';
99
1010@Injectable ( )
1111export class SubjectsService {
12- constructor ( @InjectModel ( 'Subject' ) private readonly subjectModel : Model < 'Subject' > ) { }
12+ constructor (
13+ @InjectPrismaClient ( ) private readonly prismaClient : ExtendedPrismaClient ,
14+ @InjectModel ( 'Subject' ) private readonly subjectModel : Model < 'Subject' >
15+ ) { }
1316
1417 async addGroupForSubject ( subjectId : string , groupId : string , { ability } : EntityOperationOptions = { } ) {
1518 return this . subjectModel . update ( {
@@ -82,11 +85,36 @@ export class SubjectsService {
8285 } ) ;
8386 }
8487
85- async deleteById ( id : string , { ability } : EntityOperationOptions = { } ) {
88+ async deleteById ( id : string , { ability, force } : EntityOperationOptions & { force ?: boolean } = { } ) {
8689 const subject = await this . findById ( id ) ;
87- return this . subjectModel . delete ( {
88- where : { AND : [ accessibleQuery ( ability , 'delete' , 'Subject' ) ] , id : subject . id }
89- } ) ;
90+ if ( ! force ) {
91+ await this . subjectModel . delete ( {
92+ where : { AND : [ accessibleQuery ( ability , 'delete' , 'Subject' ) ] , id : subject . id }
93+ } ) ;
94+ return { success : true } ;
95+ }
96+ await this . prismaClient . $transaction ( [
97+ this . prismaClient . session . deleteMany ( {
98+ where : {
99+ subject : {
100+ id : subject . id
101+ }
102+ }
103+ } ) ,
104+ this . prismaClient . instrumentRecord . deleteMany ( {
105+ where : {
106+ subject : {
107+ id : subject . id
108+ }
109+ }
110+ } ) ,
111+ this . prismaClient . subject . deleteMany ( {
112+ where : {
113+ id : subject . id
114+ }
115+ } )
116+ ] ) ;
117+ return { success : true } ;
90118 }
91119
92120 async find ( { groupId } : { groupId ?: string } = { } , { ability } : EntityOperationOptions = { } ) {
0 commit comments