import { Service } from 'typedi'; import { DataSource, In, Not, Repository, Like } from 'typeorm'; import type { FindManyOptions, DeleteResult, EntityManager, FindOptionsWhere } from 'typeorm'; import { CredentialsEntity } from '../entities/CredentialsEntity'; import { SharedCredentials } from '../entities/SharedCredentials'; import type { ListQuery } from '@/requests'; @Service() export class CredentialsRepository extends Repository { constructor(dataSource: DataSource) { super(CredentialsEntity, dataSource.manager); } async pruneSharings( transaction: EntityManager, credentialId: string, userIds: string[], ): Promise { const conditions: FindOptionsWhere = { credentialsId: credentialId, userId: Not(In(userIds)), }; return await transaction.delete(SharedCredentials, conditions); } async findStartingWith(credentialName: string) { return await this.find({ select: ['name'], where: { name: Like(`${credentialName}%`) }, }); } async findMany(listQueryOptions?: ListQuery.Options, credentialIds?: string[]) { const findManyOptions = this.toFindManyOptions(listQueryOptions); if (credentialIds) { findManyOptions.where = { ...findManyOptions.where, id: In(credentialIds) }; } return await this.find(findManyOptions); } private toFindManyOptions(listQueryOptions?: ListQuery.Options) { const findManyOptions: FindManyOptions = {}; type Select = Array; const defaultRelations = ['shared', 'shared.user']; const defaultSelect: Select = ['id', 'name', 'type', 'nodesAccess', 'createdAt', 'updatedAt']; if (!listQueryOptions) return { select: defaultSelect, relations: defaultRelations }; const { filter, select, take, skip } = listQueryOptions; if (typeof filter?.name === 'string' && filter?.name !== '') { filter.name = Like(`%${filter.name}%`); } if (typeof filter?.type === 'string' && filter?.type !== '') { filter.type = Like(`%${filter.type}%`); } if (filter) findManyOptions.where = filter; if (select) findManyOptions.select = select; if (take) findManyOptions.take = take; if (skip) findManyOptions.skip = skip; if (take && select && !select?.id) { findManyOptions.select = { ...findManyOptions.select, id: true }; // pagination requires id } if (!findManyOptions.select) { findManyOptions.select = defaultSelect; findManyOptions.relations = defaultRelations; } return findManyOptions; } async getManyByIds(ids: string[], { withSharings } = { withSharings: false }) { const findManyOptions: FindManyOptions = { where: { id: In(ids) } }; if (withSharings) { findManyOptions.relations = ['shared', 'shared.user']; } return await this.find(findManyOptions); } }