Skip to content

Commit 3a9e77f

Browse files
committed
feat: adding user as an optional attachment to session creation
1 parent 663abd6 commit 3a9e77f

5 files changed

Lines changed: 27 additions & 7 deletions

File tree

apps/api/prisma/schema.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ model User {
202202
basePermissionLevel BasePermissionLevel?
203203
additionalPermissions AuthRule[]
204204
firstName String
205+
sessions Session[]
205206
groupIds String[] @db.ObjectId
206207
groups Group[] @relation(fields: [groupIds], references: [id])
207208
lastName String
@@ -229,6 +230,8 @@ model Session {
229230
instrumentRecords InstrumentRecord[]
230231
subject Subject? @relation(fields: [subjectId], references: [id])
231232
subjectId String
233+
user User? @relation(fields: [userId], references: [id])
234+
userId String?
232235
type SessionType
233236
234237
@@map("SessionModel")

apps/api/src/sessions/sessions.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { Module } from '@nestjs/common';
22

33
import { GroupsModule } from '@/groups/groups.module';
44
import { SubjectsModule } from '@/subjects/subjects.module';
5+
import { UsersModule } from '@/users/users.module';
56

67
import { SessionsController } from './sessions.controller';
78
import { SessionsService } from './sessions.service';
89

910
@Module({
1011
controllers: [SessionsController],
1112
exports: [SessionsService],
12-
imports: [GroupsModule, SubjectsModule],
13+
imports: [GroupsModule, SubjectsModule, UsersModule],
1314
providers: [SessionsService]
1415
})
1516
export class SessionsModule {}

apps/api/src/sessions/sessions.service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import { InternalServerErrorException, NotFoundException } from '@nestjs/common/
55
import type { Group } from '@opendatacapture/schemas/group';
66
import type { CreateSessionData } from '@opendatacapture/schemas/session';
77
import 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

1010
import type { EntityOperationOptions } from '@/core/types';
1111
import { GroupsService } from '@/groups/groups.service';
1212
import { SubjectsService } from '@/subjects/subjects.service';
13+
import { UsersService } from '@/users/users.service';
1314

1415
@Injectable()
1516
export 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

packages/schemas/src/session/session.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ export const $Session = $BaseModel.extend({
1212
groupId: z.string().nullable(),
1313
subject: $Subject,
1414
subjectId: z.string(),
15-
type: $SessionType
15+
type: $SessionType,
16+
userId: z.string().nullable()
1617
});
1718

1819
export type CreateSessionData = z.infer<typeof $CreateSessionData>;
1920
export const $CreateSessionData = z.object({
2021
date: z.coerce.date(),
2122
groupId: z.string().nullable(),
2223
subjectData: $CreateSubjectData,
23-
type: $SessionType
24+
type: $SessionType,
25+
userId: z.string().nullish()
2426
});

packages/schemas/src/subject/subject.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const $Subject = $BaseModel.extend({
2222
firstName: z.string().min(1).nullish(),
2323
groupIds: z.array(z.string().min(1)),
2424
lastName: z.string().min(1).nullish(),
25+
sessionIds: z.array(z.string()).nullish(),
2526
sex: $Sex.nullish()
2627
});
2728

0 commit comments

Comments
 (0)