Skip to content

Commit

Permalink
remove transfer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
valya committed Aug 8, 2024
1 parent 5a897aa commit 8f76c37
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,12 @@ export = {
transferCredential: [
projectScope('credential:move', 'credential'),
async (req: CredentialRequest.Transfer, res: express.Response) => {
const body = z
.object({
destinationProjectId: z.string(),
shareWithOriginalProject: z.boolean().optional(),
})
.parse(req.body);
const body = z.object({ destinationProjectId: z.string() }).parse(req.body);

await Container.get(EnterpriseCredentialsService).transferOne(
req.user,
req.params.workflowId,
body.destinationProjectId,
body.shareWithOriginalProject ?? false,
);

res.status(204).send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ put:
destinationProjectId:
type: string
description: The ID of the project to transfer the credential to.
shareWithOriginalProject:
type: boolean
description: Share the transferred credential to the project it originated from.
required:
- destinationProjectId
required: true
Expand Down
10 changes: 1 addition & 9 deletions packages/cli/src/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,12 @@ export class CredentialsController {
@Put('/:credentialId/transfer')
@ProjectScope('credential:move')
async transfer(req: CredentialRequest.Transfer) {
// TODO: make shareWithOriginalProject non-optional once the frontend
// has support
const body = z
.object({
destinationProjectId: z.string(),
shareWithOriginalProject: z.boolean().optional(),
})
.parse(req.body);
const body = z.object({ destinationProjectId: z.string() }).parse(req.body);

return await this.enterpriseCredentialsService.transferOne(
req.user,
req.params.credentialId,
body.destinationProjectId,
body.shareWithOriginalProject ?? false,
);
}
}
28 changes: 2 additions & 26 deletions packages/cli/src/credentials/credentials.service.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ export class EnterpriseCredentialsService {
return { ...rest };
}

async transferOne(
user: User,
credentialId: string,
destinationProjectId: string,
shareWithSource: boolean,
) {
async transferOne(user: User, credentialId: string, destinationProjectId: string) {
// 1. get credential
const credential = await this.sharedCredentialsRepository.findCredentialForUser(
credentialId,
Expand Down Expand Up @@ -171,26 +166,7 @@ export class EnterpriseCredentialsService {

await this.sharedCredentialsRepository.manager.transaction(async (trx) => {
// 6. transfer the credential

// remove original owner sharing
await trx.remove(ownerSharing);

// share it back as a user if asked to
if (shareWithSource) {
await trx.save(
trx.create(SharedCredentials, {
credentialsId: credential.id,
projectId: sourceProject.id,
role: 'credential:user',
}),
);
}

// remove any previous sharings with the new owner
await trx.delete(SharedCredentials, {
credentialsId: credential.id,
projectId: destinationProjectId,
});
await trx.remove(credential.shared);

// create new owner-sharing
await trx.save(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export declare namespace CredentialRequest {
type Transfer = AuthenticatedRequest<
{ credentialId: string },
{},
{ destinationProjectId: string; shareWithOriginalProject?: boolean }
{ destinationProjectId: string }
>;

type ForWorkflow = AuthenticatedRequest<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1182,13 +1182,13 @@ describe('PUT /:credentialId/transfer', () => {
.expect(403);
});

test('transferring from a personal project to a team project should not sever all sharings', async () => {
test('transferring from a personal project to a team project severs all sharings', async () => {
//
// ARRANGE
//
const credential = await saveCredential(randomCredentialPayload(), { user: member });

// these sharings should not be deleted by the transfer
// these sharings should be deleted by the transfer
await shareCredentialWithUsers(credential, [anotherMember, owner]);

const destinationProject = await createTeamProject('Destination Project', member);
Expand All @@ -1208,72 +1208,12 @@ describe('PUT /:credentialId/transfer', () => {
expect(response.body).toEqual({});

const allSharings = await getCredentialSharings(credential);
expect(allSharings).toHaveLength(3);
expect(allSharings).toContainEqual(
expect.objectContaining({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
}),
);
for (const projectId of [anotherMemberPersonalProject.id, ownerPersonalProject.id]) {
expect(allSharings).toContainEqual(
expect.objectContaining({
credentialsId: credential.id,
projectId,
role: 'credential:user',
}),
);
}
});

test('transferring should share with original owner when asked', async () => {
//
// ARRANGE
//
const credential = await saveCredential(randomCredentialPayload(), { user: member });

// these sharings should not be deleted by the transfer
await shareCredentialWithUsers(credential, [anotherMember, owner]);

const destinationProject = await createTeamProject('Destination Project', member);

//
// ACT
//
const response = await testServer
.authAgentFor(member)
.put(`/credentials/${credential.id}/transfer`)
.send({ destinationProjectId: destinationProject.id, shareWithOriginalProject: true })
.expect(200);

//
// ASSERT
//
expect(response.body).toEqual({});

const allSharings = await getCredentialSharings(credential);
expect(allSharings).toHaveLength(4);
expect(allSharings).toContainEqual(
expect.objectContaining({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
}),
);
for (const projectId of [
anotherMemberPersonalProject.id,
ownerPersonalProject.id,
memberPersonalProject.id,
]) {
expect(allSharings).toContainEqual(
expect.objectContaining({
credentialsId: credential.id,
projectId,
role: 'credential:user',
}),
);
}
expect(allSharings).toHaveLength(1);
expect(allSharings[0]).toMatchObject({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
});
});

test('can transfer from team to another team project', async () => {
Expand Down

0 comments on commit 8f76c37

Please sign in to comment.