@@ -5,19 +5,21 @@ import { InternalServerErrorException, NotFoundException } from '@nestjs/common/
55import type { Group } from '@opendatacapture/schemas/group' ;
66import type { CreateSessionData } from '@opendatacapture/schemas/session' ;
77import type { CreateSubjectData } from '@opendatacapture/schemas/subject' ;
8- import type { Prisma , Session , Subject } from '@prisma/client' ;
8+ import type { Prisma , Session , Subject , User } from '@prisma/client' ;
99
1010import type { EntityOperationOptions } from '@/core/types' ;
1111import { GroupsService } from '@/groups/groups.service' ;
1212import { SubjectsService } from '@/subjects/subjects.service' ;
13+ import { UsersService } from '@/users/users.service' ;
1314
1415@Injectable ( )
1516export class SessionsService {
1617 constructor (
1718 @InjectModel ( 'Session' ) private readonly sessionModel : Model < 'Session' > ,
1819 private readonly groupsService : GroupsService ,
1920 private readonly loggingService : LoggingService ,
20- private readonly subjectsService : SubjectsService
21+ private readonly subjectsService : SubjectsService ,
22+ private readonly userService : UsersService
2123 ) { }
2224
2325 async count ( where : Prisma . SessionWhereInput = { } , { ability } : EntityOperationOptions = { } ) {
@@ -26,10 +28,16 @@ export class SessionsService {
2628 } ) ;
2729 }
2830
29- async create ( { date, groupId, subjectData, type } : CreateSessionData ) : Promise < Session > {
31+ async create ( { date, groupId, subjectData, type, userId } : CreateSessionData ) : Promise < Session > {
3032 this . loggingService . debug ( { message : 'Attempting to create session' } ) ;
3133 const subject = await this . resolveSubject ( subjectData ) ;
3234
35+ let user : null | Omit < User , 'hashedPassword' > = null ;
36+
37+ if ( userId ) {
38+ user = await this . userService . findById ( userId ) ;
39+ }
40+
3341 // If the subject is not yet associated with the group, check it exists then append it
3442 let group : Group | null = null ;
3543 if ( groupId && ! subject . groupIds . includes ( groupId ) ) {
@@ -48,7 +56,12 @@ export class SessionsService {
4856 subject : {
4957 connect : { id : subject . id }
5058 } ,
51- type
59+ type,
60+ user : user
61+ ? {
62+ connect : { id : user . id }
63+ }
64+ : undefined
5265 }
5366 } ) ;
5467
0 commit comments