Skip to content

Commit 0755164

Browse files
committed
feat: add match groupId clause, add filter with users accessibility, convert groupid to string
1 parent 8023457 commit 0755164

1 file changed

Lines changed: 18 additions & 28 deletions

File tree

apps/api/src/instrument-records/instrument-records.service.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
import { Prisma } from '@prisma/client';
2525
import type { Session } from '@prisma/client';
2626
import { isNumber, mergeWith, pickBy } from 'lodash-es';
27-
import { ObjectId } from 'mongodb';
2827

2928
import { accessibleQuery } from '@/auth/ability.utils';
3029
import type { AppAbility } from '@/auth/auth.types';
@@ -146,7 +145,7 @@ export class InstrumentRecordsService {
146145
return this.instrumentRecordModel.exists(where);
147146
}
148147

149-
async exportRecords({ groupId }: { groupId?: string } = {}, { ability }: EntityOperationOptions = {}) {
148+
async exportRecords({ groupId }: { groupId?: string } = {}, { ability }: Required<EntityOperationOptions>) {
150149
//separate this into seperate queries that are done within the thread (ie find session and subject info in thread instead with prisma model)
151150
// const records = await this.instrumentRecordModel.findMany({
152151
// include: {
@@ -527,9 +526,7 @@ export class InstrumentRecordsService {
527526
return JSON.parse(JSON.stringify(data), reviver) as unknown;
528527
}
529528

530-
private async queryRecordsRaw(appAbility?: AppAbility, groupId?: string) {
531-
const permissions = accessibleQuery(appAbility, 'read', 'InstrumentRecord');
532-
529+
private async queryRecordsRaw(appAbility: AppAbility, groupId?: string) {
533530
const pipeline = [
534531
{
535532
// Join with Session collection
@@ -551,28 +548,15 @@ export class InstrumentRecordsService {
551548
}
552549
},
553550
{ $unwind: { path: '$subject', preserveNullAndEmptyArrays: true } },
554-
...(groupId
555-
? [
556-
{
557-
$match: {
558-
'subject.groupIds': { $in: [new ObjectId(groupId)] }
551+
{
552+
$match: {
553+
$expr: groupId
554+
? {
555+
$eq: ['$groupId', { $toObjectId: groupId }]
559556
}
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-
557+
: {}
558+
}
559+
},
576560
{
577561
$project: {
578562
computedMeasures: 1,
@@ -582,6 +566,9 @@ export class InstrumentRecordsService {
582566
format: '%Y-%m-%d'
583567
}
584568
},
569+
groupId: {
570+
$toString: '$groupId'
571+
},
585572
id: {
586573
$toString: '$_id'
587574
},
@@ -619,9 +606,12 @@ export class InstrumentRecordsService {
619606
}
620607
];
621608

622-
const records = await this.instrumentRecordModel.aggregateRaw({ pipeline });
609+
const records = (await this.instrumentRecordModel.aggregateRaw({ pipeline })) as unknown as unknown[];
610+
611+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
612+
const filteredRecords = records.filter((record) => appAbility?.can('read', record as any));
623613

624-
return JSON.parse(JSON.stringify(records)) as unknown as RecordType[];
614+
return JSON.parse(JSON.stringify(filteredRecords)) as unknown as RecordType[];
625615
}
626616

627617
private serializeData(data: unknown) {

0 commit comments

Comments
 (0)