Skip to content

Commit

Permalink
Fix: user org assign
Browse files Browse the repository at this point in the history
  • Loading branch information
meta-d committed Oct 9, 2024
1 parent ed9c0fe commit 4102092
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IsString, IsNotEmpty } from 'class-validator';
import { TenantOrganizationBaseEntity, User } from '../core/entities/internal';

@Entity('user_organization')
@Index(['tenantId', 'organizationId', 'userId'], {unique: true})
export class UserOrganization
extends TenantOrganizationBaseEntity
implements IUserOrganization {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { IUser, IUserOrganization, RolesEnum } from '@metad/contracts';
import { IOrganization, IUser, IUserOrganization, RolesEnum } from '@metad/contracts';
import { TenantAwareCrudService } from './../core/crud';
import { Organization, User, UserOrganization } from './../core/entities/internal';
import { Organization, UserOrganization } from './../core/entities/internal';

@Injectable()
export class UserOrganizationService extends TenantAwareCrudService<UserOrganization> {
Expand All @@ -17,20 +17,27 @@ export class UserOrganizationService extends TenantAwareCrudService<UserOrganiza
super(userOrganizationRepository);
}

/**
* Adds a user to all organizations within a specific tenant.
*
* @param userId The unique identifier of the user to be added to the organizations.
* @param tenantId The unique identifier of the tenant whose organizations the user will be added to.
* @returns A promise that resolves to an array of IUserOrganization, where each element represents the user's association with an organization in the tenant.
*/
async addUserToOrganization(
user: IUser,
organizationId: string
organizationId: IOrganization['id']
): Promise<IUserOrganization | IUserOrganization[]> {
const roleName: string = user.role.name;

if (roleName === RolesEnum.SUPER_ADMIN)
return this._addUserToAllOrganizations(user.id, user.tenant.id);
/** If role is SUPER_ADMIN, add user to all organizations in the tenant */
if (user.role.name === RolesEnum.SUPER_ADMIN) {
return await this._addUserToAllOrganizations(user.id, user.tenantId);
}

const entity: IUserOrganization = new UserOrganization();
entity.organizationId = organizationId;
entity.tenantId = user.tenantId;
entity.userId = user.id;
return await this.create(entity);
return await this.repository.save(entity)
}

private async _addUserToAllOrganizations(
Expand Down

0 comments on commit 4102092

Please sign in to comment.