@@ -2,10 +2,11 @@ import crypto from 'node:crypto';
22
33import { HybridCrypto } from '@douglasneuroinformatics/libcrypto' ;
44import { ConfigService , InjectModel } from '@douglasneuroinformatics/libnest' ;
5- import type { Model } from '@douglasneuroinformatics/libnest' ;
5+ import type { Model , RequestUser } from '@douglasneuroinformatics/libnest' ;
66import { Injectable , NotFoundException } from '@nestjs/common' ;
77import type { Assignment , UpdateAssignmentData } from '@opendatacapture/schemas/assignment' ;
88
9+ import { AuditLogger } from '@/audit/audit.logger' ;
910import { accessibleQuery } from '@/auth/ability.utils' ;
1011import type { EntityOperationOptions } from '@/core/types' ;
1112import { GatewayService } from '@/gateway/gateway.service' ;
@@ -19,6 +20,7 @@ export class AssignmentsService {
1920 constructor (
2021 @InjectModel ( 'Assignment' ) private readonly assignmentModel : Model < 'Assignment' > ,
2122 configService : ConfigService ,
23+ private readonly auditLogger : AuditLogger ,
2224 private readonly gatewayService : GatewayService
2325 ) {
2426 if ( configService . get ( 'NODE_ENV' ) === 'production' ) {
@@ -30,7 +32,10 @@ export class AssignmentsService {
3032 }
3133 }
3234
33- async create ( { expiresAt, groupId, instrumentId, subjectId } : CreateAssignmentDto ) : Promise < Assignment > {
35+ async create (
36+ { expiresAt, groupId, instrumentId, subjectId } : CreateAssignmentDto ,
37+ currentUser : RequestUser
38+ ) : Promise < Assignment > {
3439 const { privateKey, publicKey } = await HybridCrypto . generateKeyPair ( ) ;
3540 const id = crypto . randomUUID ( ) ;
3641 const assignment = await this . assignmentModel . create ( {
@@ -68,6 +73,7 @@ export class AssignmentsService {
6873 await this . assignmentModel . delete ( { where : { id } } ) ;
6974 throw err ;
7075 }
76+ await this . auditLogger . log ( 'CREATE' , 'ASSIGNMENT' , { groupId : groupId ?? null , userId : currentUser . id } ) ;
7177 return assignment ;
7278 }
7379
@@ -92,13 +98,27 @@ export class AssignmentsService {
9298 return assignment ;
9399 }
94100
95- async updateById ( id : string , data : UpdateAssignmentData , { ability } : EntityOperationOptions = { } ) {
101+ async updateById ( id : string , data : UpdateAssignmentData , currentUser : RequestUser ) {
96102 if ( data . status === 'CANCELED' ) {
97103 await this . gatewayService . deleteRemoteAssignment ( id ) ;
98104 }
99- return this . assignmentModel . update ( {
105+ const assignment = await this . assignmentModel . update ( {
100106 data,
101- where : { AND : [ accessibleQuery ( ability , 'update' , 'Assignment' ) ] , id }
107+ where : { AND : [ accessibleQuery ( currentUser . ability , 'update' , 'Assignment' ) ] , id }
108+ } ) ;
109+ await this . auditLogger . log ( 'UPDATE' , 'ASSIGNMENT' , { groupId : assignment . groupId , userId : currentUser . id } ) ;
110+ return assignment ;
111+ }
112+
113+ /** used by the gateway internal system */
114+ async updateStatusById ( id : string , status : UpdateAssignmentData [ 'status' ] ) {
115+ return this . assignmentModel . update ( {
116+ data : {
117+ status
118+ } ,
119+ where : {
120+ id
121+ }
102122 } ) ;
103123 }
104124}
0 commit comments