1- import { accessibleQuery , InjectModel , LoggingService } from '@douglasneuroinformatics/libnest' ;
2- import type { Model } from '@douglasneuroinformatics/libnest' ;
1+ import { accessibleQuery , InjectModel , InjectPrismaClient , LoggingService } from '@douglasneuroinformatics/libnest' ;
2+ import type { ExtendedPrismaClient , Model } from '@douglasneuroinformatics/libnest' ;
33import { Injectable } from '@nestjs/common' ;
44import { InternalServerErrorException , NotFoundException } from '@nestjs/common/exceptions' ;
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' ;
@@ -14,6 +14,7 @@ import { SubjectsService } from '@/subjects/subjects.service';
1414@Injectable ( )
1515export class SessionsService {
1616 constructor (
17+ @InjectPrismaClient ( ) private readonly prismaClient : ExtendedPrismaClient ,
1718 @InjectModel ( 'Session' ) private readonly sessionModel : Model < 'Session' > ,
1819 private readonly groupsService : GroupsService ,
1920 private readonly loggingService : LoggingService ,
@@ -26,10 +27,20 @@ export class SessionsService {
2627 } ) ;
2728 }
2829
29- async create ( { date, groupId, subjectData, type } : CreateSessionData ) : Promise < Session > {
30+ async create ( { date, groupId, subjectData, type, username } : CreateSessionData ) : Promise < Session > {
3031 this . loggingService . debug ( { message : 'Attempting to create session' } ) ;
3132 const subject = await this . resolveSubject ( subjectData ) ;
3233
34+ let user : null | Omit < User , 'hashedPassword' > = null ;
35+
36+ if ( username ) {
37+ user = await this . prismaClient . user . findFirst ( {
38+ where : {
39+ username : username
40+ }
41+ } ) ;
42+ }
43+
3344 // If the subject is not yet associated with the group, check it exists then append it
3445 let group : Group | null = null ;
3546 if ( groupId && ! subject . groupIds . includes ( groupId ) ) {
@@ -48,7 +59,12 @@ export class SessionsService {
4859 subject : {
4960 connect : { id : subject . id }
5061 } ,
51- type
62+ type,
63+ user : user
64+ ? {
65+ connect : { id : user . id }
66+ }
67+ : undefined
5268 }
5369 } ) ;
5470
0 commit comments