mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2026-09-27 04:01:44 +02:00
feat(core): Add includeData parameter to GET /credentials (#12220)
Co-authored-by: r00gm <raul00gm@gmail.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
import { CredentialsEntity } from '@/databases/entities/credentials-entity';
|
||||
import { mockEntityManager } from '@test/mocking';
|
||||
|
||||
import { CredentialsRepository } from '../credentials.repository';
|
||||
|
||||
const entityManager = mockEntityManager(CredentialsEntity);
|
||||
const repository = Container.get(CredentialsRepository);
|
||||
|
||||
describe('findMany', () => {
|
||||
const credentialsId = 'cred_123';
|
||||
const credential = mock<CredentialsEntity>({ id: credentialsId });
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
test('return `data` property if `includeData:true` and select is using the record syntax', async () => {
|
||||
// ARRANGE
|
||||
entityManager.find.mockResolvedValueOnce([credential]);
|
||||
|
||||
// ACT
|
||||
const credentials = await repository.findMany({ includeData: true, select: { id: true } });
|
||||
|
||||
// ASSERT
|
||||
expect(credentials).toHaveLength(1);
|
||||
expect(credentials[0]).toHaveProperty('data');
|
||||
});
|
||||
|
||||
test('return `data` property if `includeData:true` and select is using the record syntax', async () => {
|
||||
// ARRANGE
|
||||
entityManager.find.mockResolvedValueOnce([credential]);
|
||||
|
||||
// ACT
|
||||
const credentials = await repository.findMany({
|
||||
includeData: true,
|
||||
//TODO: fix this
|
||||
// The function's type does not support this but this is what it
|
||||
// actually gets from the service because the middlewares are typed
|
||||
// loosely.
|
||||
select: ['id'] as never,
|
||||
});
|
||||
|
||||
// ASSERT
|
||||
expect(credentials).toHaveLength(1);
|
||||
expect(credentials[0]).toHaveProperty('data');
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,10 @@ export class CredentialsRepository extends Repository<CredentialsEntity> {
|
||||
});
|
||||
}
|
||||
|
||||
async findMany(listQueryOptions?: ListQuery.Options, credentialIds?: string[]) {
|
||||
async findMany(
|
||||
listQueryOptions?: ListQuery.Options & { includeData?: boolean },
|
||||
credentialIds?: string[],
|
||||
) {
|
||||
const findManyOptions = this.toFindManyOptions(listQueryOptions);
|
||||
|
||||
if (credentialIds) {
|
||||
@@ -35,7 +38,7 @@ export class CredentialsRepository extends Repository<CredentialsEntity> {
|
||||
return await this.find(findManyOptions);
|
||||
}
|
||||
|
||||
private toFindManyOptions(listQueryOptions?: ListQuery.Options) {
|
||||
private toFindManyOptions(listQueryOptions?: ListQuery.Options & { includeData?: boolean }) {
|
||||
const findManyOptions: FindManyOptions<CredentialsEntity> = {};
|
||||
|
||||
type Select = Array<keyof CredentialsEntity>;
|
||||
@@ -74,6 +77,14 @@ export class CredentialsRepository extends Repository<CredentialsEntity> {
|
||||
findManyOptions.relations = defaultRelations;
|
||||
}
|
||||
|
||||
if (listQueryOptions.includeData) {
|
||||
if (Array.isArray(findManyOptions.select)) {
|
||||
findManyOptions.select.push('data');
|
||||
} else {
|
||||
findManyOptions.select.data = true;
|
||||
}
|
||||
}
|
||||
|
||||
return findManyOptions;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user