@@ -24,8 +24,10 @@ import type {
2424import { Prisma } from '@prisma/client' ;
2525import type { Session } from '@prisma/client' ;
2626import { isNumber , mergeWith , pickBy } from 'lodash-es' ;
27+ import { ObjectId } from 'mongodb' ;
2728
2829import { accessibleQuery } from '@/auth/ability.utils' ;
30+ import type { AppAbility } from '@/auth/auth.types' ;
2931import type { EntityOperationOptions } from '@/core/types' ;
3032import { GroupsService } from '@/groups/groups.service' ;
3133import { InstrumentsService } from '@/instruments/instruments.service' ;
@@ -36,8 +38,6 @@ import { SubjectsService } from '@/subjects/subjects.service';
3638import { InstrumentMeasuresService } from './instrument-measures.service' ;
3739
3840import type { InitData , RecordType } from './thread-types' ;
39- import { P } from 'ts-pattern' ;
40- import type { AppAbility } from '@/auth/auth.types' ;
4141
4242// type ExpandDataType =
4343// | {
@@ -205,21 +205,21 @@ export class InstrumentRecordsService {
205205 // throw new Error()
206206 // }
207207
208- for ( let i = 0 ; i < records . length ; i ++ ) {
209- const record = records [ i ] ;
210- if ( Object . getPrototypeOf ( record ) !== Object . prototype ) {
211- console . log ( record ) ;
212- throw new Error ( 'Bad prototype' ) ;
213- }
214- // for (const key in record) {
215- // structuredClone(record[key])
216- // }
217- records [ i ] = {
218- ...record
219- } ;
220- }
208+ // for (let i = 0; i < records.length; i++) {
209+ // const record = records[i];
210+ // if (Object.getPrototypeOf(record) !== Object.prototype) {
211+ // console.log(record);
212+ // throw new Error('Bad prototype');
213+ // }
214+ // // for (const key in record) {
215+ // // structuredClone(record[key])
216+ // // }
217+ // records[i] = {
218+ // ...record
219+ // };
220+ // }
221221
222- console . log ( records [ 0 ] ) ;
222+ // console.log(records[0]);
223223
224224 // throw new Error("NULL")
225225 structuredClone ( records ) ;
@@ -529,16 +529,8 @@ export class InstrumentRecordsService {
529529
530530 private async queryRecordsRaw ( appAbility ?: AppAbility , groupId ?: string ) {
531531 const permissions = accessibleQuery ( appAbility , 'read' , 'InstrumentRecord' ) ;
532+
532533 const pipeline = [
533- {
534- $match : {
535- $and : [
536- // Filter by groupId if provided
537- ...( groupId ? [ { groupIds : { $in : [ groupId ] } } ] : [ ] ) ,
538- permissions
539- ]
540- }
541- } ,
542534 {
543535 // Join with Session collection
544536 $lookup : {
@@ -559,6 +551,28 @@ export class InstrumentRecordsService {
559551 }
560552 } ,
561553 { $unwind : { path : '$subject' , preserveNullAndEmptyArrays : true } } ,
554+ ...( groupId
555+ ? [
556+ {
557+ $match : {
558+ 'subject.groupIds' : { $in : [ new ObjectId ( groupId ) ] }
559+ }
560+ }
561+ ]
562+ : [ ] ) ,
563+ // {
564+ // $match: {
565+ // $subject: {
566+ // $in: groupId ? [groupId, "groupIds"] : [undefined]
567+ // }
568+ // }
569+ // },
570+ // ...(groupId ? [{
571+ // $match: {
572+ // '$subject.groupIds': { $in: [new ObjectId(groupId)] }
573+ // }
574+ // }] : []),
575+
562576 {
563577 $project : {
564578 computedMeasures : 1 ,
0 commit comments