Skip to content

Commit 4102092

Browse files
committed
Fix: user org assign
1 parent ed9c0fe commit 4102092

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

packages/server/src/user-organization/user-organization.entity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IsString, IsNotEmpty } from 'class-validator';
1212
import { TenantOrganizationBaseEntity, User } from '../core/entities/internal';
1313

1414
@Entity('user_organization')
15+
@Index(['tenantId', 'organizationId', 'userId'], {unique: true})
1516
export class UserOrganization
1617
extends TenantOrganizationBaseEntity
1718
implements IUserOrganization {

packages/server/src/user-organization/user-organization.services.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
33
import { Repository } from 'typeorm';
4-
import { IUser, IUserOrganization, RolesEnum } from '@metad/contracts';
4+
import { IOrganization, IUser, IUserOrganization, RolesEnum } from '@metad/contracts';
55
import { TenantAwareCrudService } from './../core/crud';
6-
import { Organization, User, UserOrganization } from './../core/entities/internal';
6+
import { Organization, UserOrganization } from './../core/entities/internal';
77

88
@Injectable()
99
export class UserOrganizationService extends TenantAwareCrudService<UserOrganization> {
@@ -17,20 +17,27 @@ export class UserOrganizationService extends TenantAwareCrudService<UserOrganiza
1717
super(userOrganizationRepository);
1818
}
1919

20+
/**
21+
* Adds a user to all organizations within a specific tenant.
22+
*
23+
* @param userId The unique identifier of the user to be added to the organizations.
24+
* @param tenantId The unique identifier of the tenant whose organizations the user will be added to.
25+
* @returns A promise that resolves to an array of IUserOrganization, where each element represents the user's association with an organization in the tenant.
26+
*/
2027
async addUserToOrganization(
2128
user: IUser,
22-
organizationId: string
29+
organizationId: IOrganization['id']
2330
): Promise<IUserOrganization | IUserOrganization[]> {
24-
const roleName: string = user.role.name;
25-
26-
if (roleName === RolesEnum.SUPER_ADMIN)
27-
return this._addUserToAllOrganizations(user.id, user.tenant.id);
31+
/** If role is SUPER_ADMIN, add user to all organizations in the tenant */
32+
if (user.role.name === RolesEnum.SUPER_ADMIN) {
33+
return await this._addUserToAllOrganizations(user.id, user.tenantId);
34+
}
2835

2936
const entity: IUserOrganization = new UserOrganization();
3037
entity.organizationId = organizationId;
3138
entity.tenantId = user.tenantId;
3239
entity.userId = user.id;
33-
return await this.create(entity);
40+
return await this.repository.save(entity)
3441
}
3542

3643
private async _addUserToAllOrganizations(

0 commit comments

Comments
 (0)