Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement rename organization #2634

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/api/src/app/organization/e2e/update-organization.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { OrganizationRepository } from '@novu/dal';
import { UserSession } from '@novu/testing';
import { expect } from 'chai';

describe('Update Organization - /organizations (PUT)', function () {
let session: UserSession;
const organizationRepository = new OrganizationRepository();

beforeEach(async () => {
session = new UserSession();
await session.initialize();
});

it('should update the organization name', async function () {
const payload = {
name: 'Liberty Powers',
};

await session.testAgent.put('/v1/organizations').send(payload);

const organization = await organizationRepository.findById(session.organization._id);

expect(organization.name).to.equal(payload.name);
});
});
17 changes: 16 additions & 1 deletion apps/api/src/app/organization/organization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { IGetOrganizationsDto } from './dtos/get-organizations.dto';
import { GetMyOrganization } from './usecases/get-my-organization/get-my-organization.usecase';
import { GetMyOrganizationCommand } from './usecases/get-my-organization/get-my-organization.command';
import { IGetMyOrganizationDto } from './dtos/get-my-organization.dto';
import { UpdateOrganizationCommand } from './usecases/update-organization/update-organization-command';
import { UpdateOrganization } from './usecases/update-organization/update-organization.usecase';

@Controller('/organizations')
@UseInterceptors(ClassSerializerInterceptor)
Expand All @@ -47,7 +49,8 @@ export class OrganizationController {
private changeMemberRoleUsecase: ChangeMemberRole,
private updateBrandingDetailsUsecase: UpdateBrandingDetails,
private getOrganizationsUsecase: GetOrganizations,
private getMyOrganizationUsecase: GetMyOrganization
private getMyOrganizationUsecase: GetMyOrganization,
private updateOrganizationUsecase: UpdateOrganization
) {}

@Post('/')
Expand Down Expand Up @@ -154,4 +157,16 @@ export class OrganizationController {
})
);
}

@Put('/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the member role enum here:

  @Roles(MemberRoleEnum.ADMIN)

Copy link
Contributor Author

@Abobos Abobos Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. we want to restrict the update to only members of an organisation with role admin?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scopsy done

scopsy marked this conversation as resolved.
Show resolved Hide resolved
@Roles(MemberRoleEnum.ADMIN)
async updateOrganization(@UserSession() user: IJwtPayload, @Body() body: { name: string }) {
return await this.updateOrganizationUsecase.execute(
UpdateOrganizationCommand.create({
name: body.name,
userId: user._id,
id: user.organizationId,
})
);
}
}
2 changes: 2 additions & 0 deletions apps/api/src/app/organization/usecases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChangeMemberRole } from './membership/change-member-role/change-member-
import { UpdateBrandingDetails } from './update-branding-details/update-branding-details.usecase';
import { GetOrganizations } from './get-organizations/get-organizations.usecase';
import { GetMyOrganization } from './get-my-organization/get-my-organization.usecase';
import { UpdateOrganization } from './update-organization/update-organization.usecase';

export const USE_CASES = [
AddMember,
Expand All @@ -18,4 +19,5 @@ export const USE_CASES = [
UpdateBrandingDetails,
GetOrganizations,
GetMyOrganization,
UpdateOrganization,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsDefined, MaxLength } from 'class-validator';
import { AuthenticatedCommand } from '../../../shared/commands/authenticated.command';

export class UpdateOrganizationCommand extends AuthenticatedCommand {
@IsDefined()
public readonly id: string;

@MaxLength(50)
name: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abobos due you think we should add here some maxLen for the organization name? Would use something permissive here, but maybe providing some limit will be a good idea to not break UI and etc...

Copy link
Contributor Author

@Abobos Abobos Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you throw more perspective?.. How would this break the UI if we don't limit it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example everywhere we show the organization name it can overflow (the settings dropdown, organization select) and maybe other places. This is quite and edge case to be honest. Really not a must.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scopsy yes. this makes sense. done

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import { OrganizationRepository } from '@novu/dal';
import { UpdateOrganizationCommand } from './update-organization-command';

@Injectable()
export class UpdateOrganization {
constructor(private organizationRepository: OrganizationRepository) {}

async execute(command: UpdateOrganizationCommand) {
const payload = {
name: command.name,
};

await this.organizationRepository.updateOrganization(command.id, payload);

return payload;
}
}
13 changes: 13 additions & 0 deletions libs/dal/src/repositories/organization/organization.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export class OrganizationRepository extends BaseRepository<
);
}

async updateOrganization(organizationId: string, payload: { name: string }) {
return this.update(
{
_id: organizationId,
},
{
$set: {
name: payload.name,
},
}
);
}

async findPartnerConfigurationDetails(organizationId: string, userId: string, configurationId: string) {
const members = await this.memberRepository.findUserActiveMembers(userId);

Expand Down