Skip to content

Commit

Permalink
refactor: filter recordIds method (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
tea-artist committed Jun 13, 2024
1 parent 130e5b7 commit 1117e8c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { FieldKeyType } from '@teable/core';
import { PrismaService } from '@teable/db-main-prisma';
import type {
Expand Down Expand Up @@ -129,7 +129,11 @@ export class RecordOpenApiService {
});

if (usedFields.length !== usedFieldIdsOrNames.length) {
throw new BadRequestException('some fields not found');
const usedSet = new Set(map(usedFields, fieldKeyType));
const missedFields = usedFieldIdsOrNames.filter(
(fieldIdOrName) => !usedSet.has(fieldIdOrName)
);
throw new NotFoundException(`Field ${fieldKeyType}: ${missedFields.join()} not found`);
}
return map(usedFields, createFieldInstanceByRaw);
}
Expand Down
12 changes: 10 additions & 2 deletions apps/nestjs-backend/src/features/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,11 @@ export class RecordService {
return this.prismaService.txClient().$queryRawUnsafe<{ id: string; title: string }[]>(querySql);
}

async getDiffIdsByIdAndFilter(tableId: string, recordIds: string[], filter?: IFilter | null) {
async filterRecordIdsByFilter(
tableId: string,
recordIds: string[],
filter?: IFilter | null
): Promise<string[]> {
const { queryBuilder, dbTableName } = await this.buildFilterSortQuery(tableId, {
filter,
});
Expand All @@ -1017,7 +1021,11 @@ export class RecordService {
const result = await this.prismaService
.txClient()
.$queryRawUnsafe<{ __id: string }[]>(queryBuilder.toQuery());
const ids = result.map((r) => r.__id);
return result.map((r) => r.__id);
}

async getDiffIdsByIdAndFilter(tableId: string, recordIds: string[], filter?: IFilter | null) {
const ids = await this.filterRecordIdsByFilter(tableId, recordIds, filter);
return difference(recordIds, ids);
}
}

0 comments on commit 1117e8c

Please sign in to comment.