135dika revised this gist 2 days ago. Go to revision
1 file changed, 58 insertions
deletion-part.rooms.grpc.service.ts(file created)
| @@ -0,0 +1,58 @@ | |||
| 1 | + | /** | |
| 2 | + | * DeleteAnnotationsByUser — soft-delete all annotation layers + annotations | |
| 3 | + | * created by a specific user within the given rooms. | |
| 4 | + | * Used when canceling/switching a user's assignment on a transmittal. | |
| 5 | + | * Caller should resolve room IDs via getRoomByDocument first. | |
| 6 | + | */ | |
| 7 | + | async deleteAnnotationsByUser(request: DeleteAnnotationsByUserRequest): Promise<DeleteAnnotationsByUserResponse> { | |
| 8 | + | const roomIds = request.roomIds ?? []; | |
| 9 | + | if (roomIds.length === 0) { | |
| 10 | + | return { deletedAnnotations: 0, deletedLayers: 0 }; | |
| 11 | + | } | |
| 12 | + | ||
| 13 | + | const hard = request.hardDelete === true; | |
| 14 | + | let layerResult: { count: number }; | |
| 15 | + | let annotationResult: { count: number }; | |
| 16 | + | ||
| 17 | + | if (hard) { | |
| 18 | + | // Hard-delete layers + annotations | |
| 19 | + | annotationResult = await this.prisma.annotations.deleteMany({ | |
| 20 | + | where: { | |
| 21 | + | layer: { room_id: { in: roomIds } }, | |
| 22 | + | created_by: request.userId, | |
| 23 | + | }, | |
| 24 | + | }); | |
| 25 | + | layerResult = await this.prisma.annotationLayers.deleteMany({ | |
| 26 | + | where: { | |
| 27 | + | room_id: { in: roomIds }, | |
| 28 | + | created_by: request.userId, | |
| 29 | + | }, | |
| 30 | + | }); | |
| 31 | + | } else { | |
| 32 | + | // Soft-delete | |
| 33 | + | const now = new Date(); | |
| 34 | + | annotationResult = await this.prisma.annotations.updateMany({ | |
| 35 | + | where: { | |
| 36 | + | layer: { room_id: { in: roomIds } }, | |
| 37 | + | created_by: request.userId, | |
| 38 | + | deleted_at: null, | |
| 39 | + | }, | |
| 40 | + | data: { deleted_at: now }, | |
| 41 | + | }); | |
| 42 | + | layerResult = await this.prisma.annotationLayers.updateMany({ | |
| 43 | + | where: { | |
| 44 | + | room_id: { in: roomIds }, | |
| 45 | + | created_by: request.userId, | |
| 46 | + | deleted_at: null, | |
| 47 | + | }, | |
| 48 | + | data: { deleted_at: now }, | |
| 49 | + | }); | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | this.logger.log( | |
| 53 | + | `${hard ? 'Hard' : 'Soft'}-deleted ${annotationResult.count} annotations / ${layerResult.count} layers ` + | |
| 54 | + | `for user ${request.userId} in ${roomIds.length} room(s)` | |
| 55 | + | ); | |
| 56 | + | ||
| 57 | + | return { deletedAnnotations: annotationResult.count, deletedLayers: layerResult.count }; | |
| 58 | + | } | |
Newer
Older