Skip to content

Commit

Permalink
fix homeProject being null when filtering by projectId
Browse files Browse the repository at this point in the history
  • Loading branch information
valya committed Aug 6, 2024
1 parent d01f90e commit b07f637
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/cli/src/credentials/credentials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ export class CredentialsService {
let credentials = await this.credentialsRepository.findMany(options.listQueryOptions);

if (isDefaultSelect) {
// Since we're filtering using project ID as part of the relation,
// we end up filtering out all the other relations, meaning that if
// it's shared to a project, it won't be able to find the home project.
// To solve this, we have to get all the relation now, even though
// we're deleting them later.
if ((options.listQueryOptions?.filter?.shared as { projectId?: string })?.projectId) {
const relations = await this.sharedCredentialsRepository.getAllRelationsForCredentials(
credentials.map((c) => c.id),
);
credentials.forEach((c) => {
c.shared = relations.filter((r) => r.credentialsId === c.id);
});
}
credentials = credentials.map((c) => this.ownershipService.addOwnedByAndSharedWith(c));
}

Expand Down Expand Up @@ -130,6 +143,20 @@ export class CredentialsService {
);

if (isDefaultSelect) {
// Since we're filtering using project ID as part of the relation,
// we end up filtering out all the other relations, meaning that if
// it's shared to a project, it won't be able to find the home project.
// To solve this, we have to get all the relation now, even though
// we're deleting them later.
if ((options.listQueryOptions?.filter?.shared as { projectId?: string })?.projectId) {
const relations = await this.sharedCredentialsRepository.getAllRelationsForCredentials(
credentials.map((c) => c.id),
);
credentials.forEach((c) => {
c.shared = relations.filter((r) => r.credentialsId === c.id);
});
}

credentials = credentials.map((c) => this.ownershipService.addOwnedByAndSharedWith(c));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,13 @@ export class SharedCredentialsRepository extends Repository<SharedCredentials> {
})
)?.project;
}

async getAllRelationsForCredentials(credentialIds: string[]) {
return await this.find({
where: {
credentialsId: In(credentialIds),
},
relations: ['project'],
});
}
}
15 changes: 15 additions & 0 deletions packages/cli/test/integration/credentials/credentials.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ describe('GET /credentials', () => {
expect(response2.body.data).toHaveLength(0);
});

test('should return homeProject when filtering credentials by projectId', async () => {
const project = await createTeamProject(undefined, member);
const credential = await saveCredential(payload(), { user: owner, role: 'credential:owner' });
await shareCredentialWithProjects(credential, [project]);

const response: GetAllResponse = await testServer
.authAgentFor(member)
.get('/credentials')
.query(`filter={ "projectId": "${project.id}" }`)
.expect(200);

expect(response.body.data).toHaveLength(1);
expect(response.body.data[0].homeProject).not.toBeNull();
});

test('should return all credentials in a team project that member is part of', async () => {
const teamProjectWithMember = await createTeamProject('Team Project With member', owner);
void (await linkUserToProject(member, teamProjectWithMember, 'project:editor'));
Expand Down

0 comments on commit b07f637

Please sign in to comment.