Skip to content

Commit 34b55fa

Browse files
committed
feat: add force option to delete subject method to delete associated records and sessions
1 parent 0f29b89 commit 34b55fa

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

apps/api/src/subjects/subjects.controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { CurrentUser, RouteAccess } from '@douglasneuroinformatics/libnest';
1+
import { $BooleanLike } from '@douglasneuroinformatics/libjs';
2+
import { CurrentUser, ParseSchemaPipe, RouteAccess } from '@douglasneuroinformatics/libnest';
23
import type { AppAbility } from '@douglasneuroinformatics/libnest';
34
import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
45
import { ApiOperation, ApiTags } from '@nestjs/swagger';
@@ -21,8 +22,12 @@ export class SubjectsController {
2122
@ApiOperation({ summary: 'Delete Subject' })
2223
@Delete(':id')
2324
@RouteAccess({ action: 'delete', subject: 'Subject' })
24-
deleteById(@Param('id') id: string, @CurrentUser('ability') ability: AppAbility) {
25-
return this.subjectsService.deleteById(id, { ability });
25+
deleteById(
26+
@Param('id') id: string,
27+
@Query('force', new ParseSchemaPipe({ isOptional: true, schema: $BooleanLike })) force: boolean | undefined,
28+
@CurrentUser('ability') ability: AppAbility
29+
) {
30+
return this.subjectsService.deleteById(id, { ability, force });
2631
}
2732

2833
@ApiOperation({ summary: 'Get All Subjects' })

apps/api/src/subjects/subjects.service.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
33
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
44
import type { Prisma } from '@prisma/client';
55

@@ -9,7 +9,10 @@ import { CreateSubjectDto } from './dto/create-subject.dto';
99

1010
@Injectable()
1111
export 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

Comments
 (0)