mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 10:46:17 +00:00
Merge branch 'dev' of https://github.com/SyncrowIOT/backend into feat/link-space-to-space-model
This commit is contained in:
@ -4,5 +4,5 @@ export class AuthInterface {
|
||||
uuid: string;
|
||||
sessionId: string;
|
||||
id: number;
|
||||
roles?: string[];
|
||||
role?: object;
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ export class AuthService {
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
relations: ['roles.roleType'],
|
||||
relations: ['roleType'],
|
||||
});
|
||||
|
||||
if (!user.isUserVerified) {
|
||||
@ -85,9 +85,8 @@ export class AuthService {
|
||||
email: user.email,
|
||||
userId: user.userId,
|
||||
uuid: user.uuid,
|
||||
type: user.type,
|
||||
sessionId: user.sessionId,
|
||||
roles: user?.roles,
|
||||
role: user?.role,
|
||||
googleCode: user.googleCode,
|
||||
};
|
||||
if (payload.googleCode) {
|
||||
|
@ -31,7 +31,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
userUuid: payload.uuid,
|
||||
uuid: payload.uuid,
|
||||
sessionId: payload.sessionId,
|
||||
roles: payload?.roles,
|
||||
role: payload?.role,
|
||||
};
|
||||
} else {
|
||||
throw new BadRequestException('Unauthorized');
|
||||
|
@ -34,7 +34,7 @@ export class RefreshTokenStrategy extends PassportStrategy(
|
||||
userUuid: payload.uuid,
|
||||
uuid: payload.uuid,
|
||||
sessionId: payload.sessionId,
|
||||
roles: payload?.roles,
|
||||
role: payload?.role,
|
||||
};
|
||||
} else {
|
||||
throw new BadRequestException('Unauthorized');
|
||||
|
@ -721,4 +721,24 @@ export class ControllerRoute {
|
||||
'This endpoint deletes a user’s subscription for device messages.';
|
||||
};
|
||||
};
|
||||
static INVITE_USER = class {
|
||||
public static readonly ROUTE = 'invite-user';
|
||||
static ACTIONS = class {
|
||||
public static readonly CREATE_USER_INVITATION_SUMMARY =
|
||||
'Create user invitation';
|
||||
|
||||
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
||||
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
||||
};
|
||||
};
|
||||
static PERMISSION = class {
|
||||
public static readonly ROUTE = 'permission';
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_PERMISSION_BY_ROLE_SUMMARY =
|
||||
'Get permissions by role';
|
||||
|
||||
public static readonly GET_PERMISSION_BY_ROLE_DESCRIPTION =
|
||||
'This endpoint retrieves the permissions associated with a specific role.';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
43
libs/common/src/constants/permissions-mapping.ts
Normal file
43
libs/common/src/constants/permissions-mapping.ts
Normal file
@ -0,0 +1,43 @@
|
||||
export const PermissionMapping = {
|
||||
DEVICE_MANAGEMENT: {
|
||||
DEVICE: ['SINGLE_CONTROL', 'VIEW', 'DELETE', 'UPDATE', 'BATCH_CONTROL'],
|
||||
FIRMWARE: ['CONTROL', 'VIEW'],
|
||||
},
|
||||
COMMUNITY_MANAGEMENT: {
|
||||
COMMUNITY: ['VIEW', 'ADD', 'UPDATE', 'DELETE'],
|
||||
},
|
||||
SPACE_MANAGEMENT: {
|
||||
SPACE: [
|
||||
'VIEW',
|
||||
'ADD',
|
||||
'UPDATE',
|
||||
'DELETE',
|
||||
'MODULE_ADD',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
],
|
||||
SUBSPACE: [
|
||||
'VIEW',
|
||||
'ADD',
|
||||
'UPDATE',
|
||||
'DELETE',
|
||||
'ASSIGN_DEVICE_TO_SUBSPACE',
|
||||
'DELETE_DEVICE_FROM_SUBSPACE',
|
||||
],
|
||||
},
|
||||
DEVICE_WIZARD: {
|
||||
DEVICE_WIZARD: ['VIEW_DEVICE_WIZARD'],
|
||||
SPACE_DEVICE: ['VIEW_DEVICE_IN_SPACE', 'ASSIGN_DEVICE_TO_SPACE'],
|
||||
SUBSPACE_DEVICE: ['VIEW_DEVICE_IN_SUBSPACE', 'UPDATE_DEVICE_IN_SUBSPACE'],
|
||||
},
|
||||
AUTOMATION_MANAGEMENT: {
|
||||
AUTOMATION: ['VIEW', 'ADD', 'UPDATE', 'DELETE', 'CONTROL'],
|
||||
SCENES: ['VIEW', 'ADD', 'UPDATE', 'DELETE', 'CONTROL'],
|
||||
},
|
||||
VISITOR_PASSWORD_MANAGEMENT: {
|
||||
VISITOR_PASSWORD: ['VIEW', 'ADD', 'UPDATE', 'DELETE'],
|
||||
},
|
||||
USER_MANAGEMENT: {
|
||||
USER: ['ADD'],
|
||||
},
|
||||
};
|
130
libs/common/src/constants/role-permissions.ts
Normal file
130
libs/common/src/constants/role-permissions.ts
Normal file
@ -0,0 +1,130 @@
|
||||
import { RoleType } from './role.type.enum';
|
||||
|
||||
export const RolePermissions = {
|
||||
[RoleType.SUPER_ADMIN]: [
|
||||
'DEVICE_SINGLE_CONTROL',
|
||||
'DEVICE_VIEW',
|
||||
'DEVICE_DELETE',
|
||||
'DEVICE_UPDATE',
|
||||
'DEVICE_BATCH_CONTROL',
|
||||
'COMMUNITY_VIEW',
|
||||
'COMMUNITY_ADD',
|
||||
'COMMUNITY_UPDATE',
|
||||
'COMMUNITY_DELETE',
|
||||
'FIRMWARE_CONTROL',
|
||||
'SPACE_VIEW',
|
||||
'SPACE_ADD',
|
||||
'SPACE_UPDATE',
|
||||
'SPACE_DELETE',
|
||||
'SPACE_MODULE_ADD',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
'SUBSPACE_VIEW',
|
||||
'SUBSPACE_ADD',
|
||||
'SUBSPACE_UPDATE',
|
||||
'SUBSPACE_DELETE',
|
||||
'ASSIGN_DEVICE_TO_SUBSPACE',
|
||||
'DELETE_DEVICE_FROM_SUBSPACE',
|
||||
'VIEW_DEVICE_WIZARD',
|
||||
'VIEW_DEVICE_IN_SUBSPACE',
|
||||
'VIEW_DEVICE_IN_SPACE',
|
||||
'UPDATE_DEVICE_IN_SUBSPACE',
|
||||
'ASSIGN_DEVICE_TO_SPACE',
|
||||
'AUTOMATION_VIEW',
|
||||
'AUTOMATION_ADD',
|
||||
'AUTOMATION_UPDATE',
|
||||
'AUTOMATION_DELETE',
|
||||
'AUTOMATION_CONTROL',
|
||||
'SCENES_VIEW',
|
||||
'SCENES_ADD',
|
||||
'SCENES_UPDATE',
|
||||
'SCENES_DELETE',
|
||||
'SCENES_CONTROL',
|
||||
'VISITOR_PASSWORD_VIEW',
|
||||
'VISITOR_PASSWORD_ADD',
|
||||
'USER_ADD',
|
||||
],
|
||||
[RoleType.ADMIN]: [
|
||||
'DEVICE_SINGLE_CONTROL',
|
||||
'DEVICE_VIEW',
|
||||
'DEVICE_DELETE',
|
||||
'DEVICE_UPDATE',
|
||||
'DEVICE_BATCH_CONTROL',
|
||||
'COMMUNITY_VIEW',
|
||||
'COMMUNITY_ADD',
|
||||
'COMMUNITY_UPDATE',
|
||||
'COMMUNITY_DELETE',
|
||||
'FIRMWARE_CONTROL',
|
||||
'SPACE_VIEW',
|
||||
'SPACE_ADD',
|
||||
'SPACE_UPDATE',
|
||||
'SPACE_DELETE',
|
||||
'SPACE_MODULE_ADD',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
'SUBSPACE_VIEW',
|
||||
'SUBSPACE_ADD',
|
||||
'SUBSPACE_UPDATE',
|
||||
'SUBSPACE_DELETE',
|
||||
'ASSIGN_DEVICE_TO_SUBSPACE',
|
||||
'DELETE_DEVICE_FROM_SUBSPACE',
|
||||
'VIEW_DEVICE_WIZARD',
|
||||
'VIEW_DEVICE_IN_SUBSPACE',
|
||||
'VIEW_DEVICE_IN_SPACE',
|
||||
'UPDATE_DEVICE_IN_SUBSPACE',
|
||||
'ASSIGN_DEVICE_TO_SPACE',
|
||||
'AUTOMATION_VIEW',
|
||||
'AUTOMATION_ADD',
|
||||
'AUTOMATION_UPDATE',
|
||||
'AUTOMATION_DELETE',
|
||||
'AUTOMATION_CONTROL',
|
||||
'SCENES_VIEW',
|
||||
'SCENES_ADD',
|
||||
'SCENES_UPDATE',
|
||||
'SCENES_DELETE',
|
||||
'SCENES_CONTROL',
|
||||
'VISITOR_PASSWORD_VIEW',
|
||||
'VISITOR_PASSWORD_ADD',
|
||||
'USER_ADD',
|
||||
],
|
||||
[RoleType.SPACE_MEMBER]: [
|
||||
'DEVICE_SINGLE_CONTROL',
|
||||
'DEVICE_VIEW',
|
||||
'SPACE_VIEW',
|
||||
'SUBSPACE_VIEW',
|
||||
'VIEW_DEVICE_WIZARD',
|
||||
'VIEW_DEVICE_IN_SUBSPACE',
|
||||
'VIEW_DEVICE_IN_SPACE',
|
||||
'AUTOMATION_VIEW',
|
||||
'AUTOMATION_CONTROL',
|
||||
'SCENES_VIEW',
|
||||
'SCENES_CONTROL',
|
||||
'VISITOR_PASSWORD_VIEW',
|
||||
],
|
||||
[RoleType.SPACE_OWNER]: [
|
||||
'DEVICE_SINGLE_CONTROL',
|
||||
'DEVICE_VIEW',
|
||||
'FIRMWARE_CONTROL',
|
||||
'FIRMWARE_VIEW',
|
||||
'SPACE_VIEW',
|
||||
'SPACE_MEMBER_ADD',
|
||||
'SUBSPACE_VIEW',
|
||||
'SUBSPACE_ADD',
|
||||
'SUBSPACE_UPDATE',
|
||||
'SUBSPACE_DELETE',
|
||||
'AUTOMATION_VIEW',
|
||||
'AUTOMATION_ADD',
|
||||
'AUTOMATION_UPDATE',
|
||||
'AUTOMATION_DELETE',
|
||||
'AUTOMATION_CONTROL',
|
||||
'SCENES_VIEW',
|
||||
'SCENES_ADD',
|
||||
'SCENES_UPDATE',
|
||||
'SCENES_DELETE',
|
||||
'SCENES_CONTROL',
|
||||
'VISITOR_PASSWORD_VIEW',
|
||||
'VISITOR_PASSWORD_ADD',
|
||||
'VISITOR_PASSWORD_UPDATE',
|
||||
'VISITOR_PASSWORD_DELETE',
|
||||
],
|
||||
};
|
@ -1,4 +1,6 @@
|
||||
export enum RoleType {
|
||||
SUPER_ADMIN = 'SUPER_ADMIN',
|
||||
ADMIN = 'ADMIN',
|
||||
SPACE_OWNER = 'SPACE_OWNER',
|
||||
SPACE_MEMBER = 'SPACE_MEMBER',
|
||||
}
|
||||
|
5
libs/common/src/constants/user-status.enum.ts
Normal file
5
libs/common/src/constants/user-status.enum.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export enum UserStatusEnum {
|
||||
ACTIVE = 'active',
|
||||
INVITED = 'invited',
|
||||
DISABLED = 'disabled',
|
||||
}
|
@ -18,7 +18,6 @@ import {
|
||||
} from '../modules/space/entities';
|
||||
import { UserSpaceEntity } from '../modules/user/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device/entities';
|
||||
import { UserRoleEntity } from '../modules/user/entities';
|
||||
import { RoleTypeEntity } from '../modules/role-type/entities';
|
||||
import { UserNotificationEntity } from '../modules/user/entities';
|
||||
import { DeviceNotificationEntity } from '../modules/device/entities';
|
||||
@ -39,6 +38,10 @@ import {
|
||||
SubspaceProductItemModelEntity,
|
||||
SubspaceProductModelEntity,
|
||||
} from '../modules/space-model/entities';
|
||||
import {
|
||||
InviteUserEntity,
|
||||
InviteUserSpaceEntity,
|
||||
} from '../modules/Invite-user/entities';
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
@ -68,7 +71,6 @@ import {
|
||||
SpaceProductEntity,
|
||||
UserSpaceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
UserRoleEntity,
|
||||
RoleTypeEntity,
|
||||
UserNotificationEntity,
|
||||
DeviceNotificationEntity,
|
||||
@ -89,6 +91,8 @@ import {
|
||||
SubspaceProductItemModelEntity,
|
||||
SubspaceProductEntity,
|
||||
SubspaceProductItemEntity,
|
||||
InviteUserEntity,
|
||||
InviteUserSpaceEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
|
@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { InviteUserEntity, InviteUserSpaceEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([InviteUserEntity, InviteUserSpaceEntity]),
|
||||
],
|
||||
})
|
||||
export class InviteUserRepositoryModule {}
|
50
libs/common/src/modules/Invite-user/dtos/Invite-user.dto.ts
Normal file
50
libs/common/src/modules/Invite-user/dtos/Invite-user.dto.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class InviteUserDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public email: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public jobTitle: string;
|
||||
|
||||
@IsEnum(UserStatusEnum)
|
||||
@IsNotEmpty()
|
||||
public status: UserStatusEnum;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public firstName: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public lastName: string;
|
||||
|
||||
@IsEnum(RoleType)
|
||||
@IsNotEmpty()
|
||||
public invitedBy: RoleType;
|
||||
}
|
||||
export class InviteUserSpaceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public inviteUserUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public spaceUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public invitationCode: string;
|
||||
}
|
1
libs/common/src/modules/Invite-user/dtos/index.ts
Normal file
1
libs/common/src/modules/Invite-user/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './Invite-user.dto';
|
@ -0,0 +1,120 @@
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { SpaceEntity } from '../../space/entities';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { InviteUserDto, InviteUserSpaceDto } from '../dtos';
|
||||
|
||||
@Entity({ name: 'invite-user' })
|
||||
@Unique(['email', 'invitationCode'])
|
||||
export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()',
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
unique: true,
|
||||
})
|
||||
email: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
jobTitle: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
enum: Object.values(UserStatusEnum),
|
||||
})
|
||||
status: string;
|
||||
|
||||
@Column()
|
||||
public firstName: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public lastName: string;
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public phoneNumber: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: true,
|
||||
})
|
||||
public isEnabled: boolean;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: true,
|
||||
})
|
||||
public isActive: boolean;
|
||||
@Column({
|
||||
nullable: false,
|
||||
unique: true,
|
||||
})
|
||||
public invitationCode: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
enum: Object.values(RoleType),
|
||||
})
|
||||
public invitedBy: string;
|
||||
|
||||
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.invitedUsers, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
public roleType: RoleTypeEntity;
|
||||
@OneToOne(() => UserEntity, (user) => user.inviteUser, { nullable: true })
|
||||
@JoinColumn({ name: 'user_uuid' })
|
||||
user: UserEntity;
|
||||
@OneToMany(
|
||||
() => InviteUserSpaceEntity,
|
||||
(inviteUserSpace) => inviteUserSpace.inviteUser,
|
||||
)
|
||||
spaces: InviteUserSpaceEntity[];
|
||||
constructor(partial: Partial<InviteUserEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
@Entity({ name: 'invite-user-space' })
|
||||
@Unique(['inviteUser', 'space'])
|
||||
export class InviteUserSpaceEntity extends AbstractEntity<InviteUserSpaceDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()',
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@ManyToOne(() => InviteUserEntity, (inviteUser) => inviteUser.spaces)
|
||||
@JoinColumn({ name: 'invite_user_uuid' })
|
||||
public inviteUser: InviteUserEntity;
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.invitedUsers)
|
||||
@JoinColumn({ name: 'space_uuid' })
|
||||
public space: SpaceEntity;
|
||||
constructor(partial: Partial<InviteUserSpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
1
libs/common/src/modules/Invite-user/entities/index.ts
Normal file
1
libs/common/src/modules/Invite-user/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './Invite-user.entity';
|
1
libs/common/src/modules/Invite-user/index.ts
Normal file
1
libs/common/src/modules/Invite-user/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './Invite-user.repository.module';
|
@ -0,0 +1 @@
|
||||
export * from './invite-user.repository';
|
@ -0,0 +1,16 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InviteUserEntity, InviteUserSpaceEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserRepository extends Repository<InviteUserEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(InviteUserEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class InviteUserSpaceRepository extends Repository<InviteUserSpaceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(InviteUserSpaceEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProjectDto } from '../dtos';
|
||||
import { CommunityEntity } from '../../community/entities';
|
||||
import { SpaceModelEntity } from '../../space-model';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
|
||||
@Entity({ name: 'project' })
|
||||
@Unique(['name'])
|
||||
@ -28,6 +29,9 @@ export class ProjectEntity extends AbstractEntity<ProjectDto> {
|
||||
@OneToMany(() => CommunityEntity, (community) => community.project)
|
||||
communities: CommunityEntity[];
|
||||
|
||||
@OneToMany(() => UserEntity, (user) => user.project)
|
||||
public users: UserEntity[];
|
||||
|
||||
constructor(partial: Partial<ProjectEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -2,7 +2,8 @@ import { Column, Entity, OneToMany, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { RoleTypeDto } from '../dtos/role.type.dto';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserRoleEntity } from '../../user/entities';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { InviteUserEntity } from '../../Invite-user/entities';
|
||||
|
||||
@Entity({ name: 'role-type' })
|
||||
@Unique(['type'])
|
||||
@ -12,10 +13,14 @@ export class RoleTypeEntity extends AbstractEntity<RoleTypeDto> {
|
||||
enum: Object.values(RoleType),
|
||||
})
|
||||
type: string;
|
||||
@OneToMany(() => UserRoleEntity, (role) => role.roleType, {
|
||||
@OneToMany(() => UserEntity, (inviteUser) => inviteUser.roleType, {
|
||||
nullable: true,
|
||||
})
|
||||
roles: UserRoleEntity[];
|
||||
users: UserEntity[];
|
||||
@OneToMany(() => InviteUserEntity, (inviteUser) => inviteUser.roleType, {
|
||||
nullable: true,
|
||||
})
|
||||
invitedUsers: InviteUserEntity[];
|
||||
constructor(partial: Partial<RoleTypeEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -16,6 +16,7 @@ import { SpaceLinkEntity } from './space-link.entity';
|
||||
import { SpaceProductEntity } from './space-product.entity';
|
||||
import { SceneEntity } from '../../scene/entities';
|
||||
import { SpaceModelEntity } from '../../space-model';
|
||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||
|
||||
@Entity({ name: 'space' })
|
||||
@Unique(['invitationCode'])
|
||||
@ -103,6 +104,11 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
@JoinColumn({ name: 'space_model_uuid' })
|
||||
spaceModel?: SpaceModelEntity;
|
||||
|
||||
@OneToMany(
|
||||
() => InviteUserSpaceEntity,
|
||||
(inviteUserSpace) => inviteUserSpace.space,
|
||||
)
|
||||
invitedUsers: InviteUserSpaceEntity[];
|
||||
constructor(partial: Partial<SpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -58,20 +58,6 @@ export class UserOtpDto {
|
||||
public expiryTime: string;
|
||||
}
|
||||
|
||||
export class UserRoleDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roleTypeUuid: string;
|
||||
}
|
||||
|
||||
export class UserSpaceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
|
@ -2,15 +2,16 @@ import {
|
||||
Column,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
import {
|
||||
UserDto,
|
||||
UserNotificationDto,
|
||||
UserOtpDto,
|
||||
UserRoleDto,
|
||||
UserSpaceDto,
|
||||
} from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
@ -26,6 +27,8 @@ import { OtpType } from '../../../../src/constants/otp-type.enum';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { SpaceEntity } from '../../space/entities';
|
||||
import { VisitorPasswordEntity } from '../../visitor-password/entities';
|
||||
import { InviteUserEntity } from '../../Invite-user/entities';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
|
||||
@Entity({ name: 'user' })
|
||||
export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@ -100,10 +103,7 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
(deviceUserNotification) => deviceUserNotification.user,
|
||||
)
|
||||
deviceUserNotification: DeviceNotificationEntity[];
|
||||
@OneToMany(() => UserRoleEntity, (role) => role.user, {
|
||||
nullable: true,
|
||||
})
|
||||
roles: UserRoleEntity[];
|
||||
|
||||
@ManyToOne(() => RegionEntity, (region) => region.users, { nullable: true })
|
||||
region: RegionEntity;
|
||||
@ManyToOne(() => TimeZoneEntity, (timezone) => timezone.users, {
|
||||
@ -116,6 +116,21 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
)
|
||||
public visitorPasswords: VisitorPasswordEntity[];
|
||||
|
||||
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.users, {
|
||||
nullable: false,
|
||||
})
|
||||
public roleType: RoleTypeEntity;
|
||||
@OneToOne(() => InviteUserEntity, (inviteUser) => inviteUser.user, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'invite_user_uuid' })
|
||||
inviteUser: InviteUserEntity;
|
||||
|
||||
@ManyToOne(() => ProjectEntity, (project) => project.users, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'project_uuid' })
|
||||
public project: ProjectEntity;
|
||||
constructor(partial: Partial<UserEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
@ -125,7 +140,7 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@Entity({ name: 'user-notification' })
|
||||
@Unique(['user', 'subscriptionUuid'])
|
||||
export class UserNotificationEntity extends AbstractEntity<UserNotificationDto> {
|
||||
@ManyToOne(() => UserEntity, (user) => user.roles, {
|
||||
@ManyToOne(() => UserEntity, (user) => user.roleType, {
|
||||
nullable: false,
|
||||
})
|
||||
user: UserEntity;
|
||||
@ -178,25 +193,6 @@ export class UserOtpEntity extends AbstractEntity<UserOtpDto> {
|
||||
}
|
||||
}
|
||||
|
||||
@Entity({ name: 'user-role' })
|
||||
@Unique(['user', 'roleType'])
|
||||
export class UserRoleEntity extends AbstractEntity<UserRoleDto> {
|
||||
@ManyToOne(() => UserEntity, (user) => user.roles, {
|
||||
nullable: false,
|
||||
})
|
||||
user: UserEntity;
|
||||
|
||||
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.roles, {
|
||||
nullable: false,
|
||||
})
|
||||
roleType: RoleTypeEntity;
|
||||
|
||||
constructor(partial: Partial<UserRoleEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity({ name: 'user-space' })
|
||||
@Unique(['user', 'space'])
|
||||
export class UserSpaceEntity extends AbstractEntity<UserSpaceDto> {
|
||||
|
@ -4,7 +4,6 @@ import {
|
||||
UserEntity,
|
||||
UserNotificationEntity,
|
||||
UserOtpEntity,
|
||||
UserRoleEntity,
|
||||
UserSpaceEntity,
|
||||
} from '../entities/';
|
||||
|
||||
@ -29,13 +28,6 @@ export class UserOtpRepository extends Repository<UserOtpEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UserRoleRepository extends Repository<UserRoleEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(UserRoleEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UserSpaceRepository extends Repository<UserSpaceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
|
@ -4,7 +4,6 @@ import {
|
||||
UserEntity,
|
||||
UserNotificationEntity,
|
||||
UserOtpEntity,
|
||||
UserRoleEntity,
|
||||
UserSpaceEntity,
|
||||
} from './entities';
|
||||
|
||||
@ -17,7 +16,6 @@ import {
|
||||
UserEntity,
|
||||
UserNotificationEntity,
|
||||
UserOtpEntity,
|
||||
UserRoleEntity,
|
||||
UserSpaceEntity,
|
||||
]),
|
||||
],
|
||||
|
@ -10,7 +10,6 @@ import { RoleTypeSeeder } from './services/role.type.seeder';
|
||||
import { SpaceRepositoryModule } from '../modules/space/space.repository.module';
|
||||
import { SuperAdminSeeder } from './services/supper.admin.seeder';
|
||||
import { UserRepository } from '../modules/user/repositories';
|
||||
import { UserRoleRepository } from '../modules/user/repositories';
|
||||
import { UserRepositoryModule } from '../modules/user/user.repository.module';
|
||||
import { RegionSeeder } from './services/regions.seeder';
|
||||
import { RegionRepository } from '../modules/region/repositories';
|
||||
@ -28,7 +27,6 @@ import { SceneIconRepository } from '../modules/scene/repositories';
|
||||
RoleTypeRepository,
|
||||
SuperAdminSeeder,
|
||||
UserRepository,
|
||||
UserRoleRepository,
|
||||
RegionSeeder,
|
||||
RegionRepository,
|
||||
TimeZoneSeeder,
|
||||
|
@ -19,7 +19,12 @@ export class RoleTypeSeeder {
|
||||
if (!roleTypeNames.includes(RoleType.ADMIN)) {
|
||||
missingRoleTypes.push(RoleType.ADMIN);
|
||||
}
|
||||
|
||||
if (!roleTypeNames.includes(RoleType.SPACE_OWNER)) {
|
||||
missingRoleTypes.push(RoleType.SPACE_OWNER);
|
||||
}
|
||||
if (!roleTypeNames.includes(RoleType.SPACE_MEMBER)) {
|
||||
missingRoleTypes.push(RoleType.SPACE_MEMBER);
|
||||
}
|
||||
if (missingRoleTypes.length > 0) {
|
||||
await this.addRoleTypeData(missingRoleTypes);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserRoleRepository } from '@app/common/modules/user/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { HelperHashService } from '../../helper/services';
|
||||
@ -11,19 +10,23 @@ export class SuperAdminSeeder {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
private readonly helperHashService: HelperHashService,
|
||||
) {}
|
||||
|
||||
async createSuperAdminIfNotFound(): Promise<void> {
|
||||
try {
|
||||
const superAdminData = await this.userRoleRepository.find({
|
||||
where: { roleType: { type: RoleType.SUPER_ADMIN } },
|
||||
const superAdmin = await this.userRepository.findOne({
|
||||
where: {
|
||||
roleType: { type: RoleType.SUPER_ADMIN },
|
||||
email: this.configService.get<string>(
|
||||
'super-admin.SUPER_ADMIN_EMAIL',
|
||||
),
|
||||
},
|
||||
relations: ['roleType'],
|
||||
});
|
||||
|
||||
if (superAdminData.length <= 0) {
|
||||
if (!superAdmin) {
|
||||
// Create the super admin user if not found
|
||||
console.log('Creating super admin user...');
|
||||
|
||||
@ -48,20 +51,16 @@ export class SuperAdminSeeder {
|
||||
salt,
|
||||
);
|
||||
try {
|
||||
const user = await this.userRepository.save({
|
||||
const defaultUserRoleUuid = await this.getRoleUuidByRoleType(
|
||||
RoleType.SUPER_ADMIN,
|
||||
);
|
||||
await this.userRepository.save({
|
||||
email: this.configService.get<string>('super-admin.SUPER_ADMIN_EMAIL'),
|
||||
password: hashedPassword,
|
||||
firstName: 'Super',
|
||||
lastName: 'Admin',
|
||||
isUserVerified: true,
|
||||
isActive: true,
|
||||
});
|
||||
const defaultUserRoleUuid = await this.getRoleUuidByRoleType(
|
||||
RoleType.SUPER_ADMIN,
|
||||
);
|
||||
|
||||
await this.userRoleRepository.save({
|
||||
user: { uuid: user.uuid },
|
||||
roleType: { uuid: defaultUserRoleUuid },
|
||||
});
|
||||
} catch (err) {
|
||||
|
Reference in New Issue
Block a user