mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Compare commits
10 Commits
5cf45c30f4
...
dev
Author | SHA1 | Date | |
---|---|---|---|
a9cb1b6704 | |||
a17a271213 | |||
712b7443ac | |||
945328c0ce | |||
009deaf403 | |||
3cfed2b452 | |||
09322c5b80 | |||
74d3620d0e | |||
83be80d9f6 | |||
2589e391ed |
@ -1,5 +1,6 @@
|
|||||||
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { UserEntity } from '@app/common/modules/user/entities';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
Injectable,
|
Injectable,
|
||||||
@ -32,7 +33,7 @@ export class AuthService {
|
|||||||
pass: string,
|
pass: string,
|
||||||
regionUuid?: string,
|
regionUuid?: string,
|
||||||
platform?: PlatformType,
|
platform?: PlatformType,
|
||||||
): Promise<any> {
|
): Promise<Omit<UserEntity, 'password'>> {
|
||||||
const user = await this.userRepository.findOne({
|
const user = await this.userRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
email,
|
email,
|
||||||
@ -70,8 +71,9 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const { password, ...result } = user;
|
// const { password, ...result } = user;
|
||||||
return result;
|
delete user.password;
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSession(data): Promise<UserSessionEntity> {
|
async createSession(data): Promise<UserSessionEntity> {
|
||||||
@ -114,6 +116,7 @@ export class AuthService {
|
|||||||
hasAcceptedWebAgreement: user.hasAcceptedWebAgreement,
|
hasAcceptedWebAgreement: user.hasAcceptedWebAgreement,
|
||||||
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
||||||
project: user?.project,
|
project: user?.project,
|
||||||
|
bookingPoints: user?.bookingPoints,
|
||||||
};
|
};
|
||||||
if (payload.googleCode) {
|
if (payload.googleCode) {
|
||||||
const profile = await this.getProfile(payload.googleCode);
|
const profile = await this.getProfile(payload.googleCode);
|
||||||
|
@ -220,6 +220,11 @@ export class ControllerRoute {
|
|||||||
public static readonly UPDATE_SPACE_DESCRIPTION =
|
public static readonly UPDATE_SPACE_DESCRIPTION =
|
||||||
'Updates a space by its UUID and community ID. You can update the name, parent space, and other properties. If a parent space is provided and not already a parent, its `isParent` flag will be set to true.';
|
'Updates a space by its UUID and community ID. You can update the name, parent space, and other properties. If a parent space is provided and not already a parent, its `isParent` flag will be set to true.';
|
||||||
|
|
||||||
|
public static readonly UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_SUMMARY =
|
||||||
|
'Update the order of child spaces under a specific parent space';
|
||||||
|
public static readonly UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_DESCRIPTION =
|
||||||
|
'Updates the order of child spaces under a specific parent space. You can provide a new order for the child spaces.';
|
||||||
|
|
||||||
public static readonly GET_HEIRARCHY_SUMMARY = 'Get space hierarchy';
|
public static readonly GET_HEIRARCHY_SUMMARY = 'Get space hierarchy';
|
||||||
public static readonly GET_HEIRARCHY_DESCRIPTION =
|
public static readonly GET_HEIRARCHY_DESCRIPTION =
|
||||||
'This endpoint retrieves the hierarchical structure of spaces under a given space ID. It returns all the child spaces nested within the specified space, organized by their parent-child relationships. ';
|
'This endpoint retrieves the hierarchical structure of spaces under a given space ID. It returns all the child spaces nested within the specified space, organized by their parent-child relationships. ';
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class InviteUserDto {
|
export class InviteUserDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@ -12,8 +12,12 @@ export class InviteUserDto {
|
|||||||
public email: string;
|
public email: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsOptional()
|
||||||
public jobTitle: string;
|
public jobTitle?: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
public companyName?: string;
|
||||||
|
|
||||||
@IsEnum(UserStatusEnum)
|
@IsEnum(UserStatusEnum)
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
@ -37,6 +37,11 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
|||||||
})
|
})
|
||||||
jobTitle: string;
|
jobTitle: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
companyName: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
enum: Object.values(UserStatusEnum),
|
enum: Object.values(UserStatusEnum),
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
|
||||||
|
|
||||||
export class SpaceLinkEntity extends AbstractEntity {}
|
|
@ -6,9 +6,9 @@ import {
|
|||||||
OneToMany,
|
OneToMany,
|
||||||
OneToOne,
|
OneToOne,
|
||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
import { SpaceDto } from '../dtos';
|
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
||||||
|
import { BookableSpaceEntity } from '../../booking/entities';
|
||||||
import { CommunityEntity } from '../../community/entities';
|
import { CommunityEntity } from '../../community/entities';
|
||||||
import { DeviceEntity } from '../../device/entities';
|
import { DeviceEntity } from '../../device/entities';
|
||||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||||
@ -17,9 +17,9 @@ import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
|||||||
import { SceneEntity } from '../../scene/entities';
|
import { SceneEntity } from '../../scene/entities';
|
||||||
import { SpaceModelEntity } from '../../space-model';
|
import { SpaceModelEntity } from '../../space-model';
|
||||||
import { UserSpaceEntity } from '../../user/entities';
|
import { UserSpaceEntity } from '../../user/entities';
|
||||||
|
import { SpaceDto } from '../dtos';
|
||||||
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
||||||
import { SubspaceEntity } from './subspace/subspace.entity';
|
import { SubspaceEntity } from './subspace/subspace.entity';
|
||||||
import { BookableSpaceEntity } from '../../booking/entities';
|
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||||
@ -64,6 +64,12 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
})
|
})
|
||||||
public disabled: boolean;
|
public disabled: boolean;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
|
public order?: number;
|
||||||
|
|
||||||
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
|
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
|
@ -11,9 +11,6 @@ export class SpaceRepository extends Repository<SpaceEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class SpaceLinkRepository {}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
||||||
constructor(private dataSource: DataSource) {
|
constructor(private dataSource: DataSource) {
|
||||||
|
@ -82,6 +82,12 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
})
|
})
|
||||||
public isActive: boolean;
|
public isActive: boolean;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
|
public bookingPoints?: number;
|
||||||
|
|
||||||
@Column({ default: false })
|
@Column({ default: false })
|
||||||
hasAcceptedWebAgreement: boolean;
|
hasAcceptedWebAgreement: boolean;
|
||||||
|
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
ForbiddenException,
|
ForbiddenException,
|
||||||
Injectable,
|
Injectable,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
|
||||||
import { HelperHashService } from '../../../libs/common/src/helper/services';
|
|
||||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
|
||||||
import { AuthService } from '../../../libs/common/src/auth/services/auth.service';
|
|
||||||
import { UserSessionRepository } from '../../../libs/common/src/modules/session/repositories/session.repository';
|
|
||||||
import { UserOtpRepository } from '../../../libs/common/src/modules/user/repositories/user.repository';
|
|
||||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
|
||||||
import { EmailService } from '../../../libs/common/src/util/email.service';
|
|
||||||
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
|
||||||
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
|
||||||
import * as argon2 from 'argon2';
|
|
||||||
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
|
||||||
import { LessThan, MoreThan } from 'typeorm';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import * as argon2 from 'argon2';
|
||||||
import { RoleService } from 'src/role/services';
|
import { RoleService } from 'src/role/services';
|
||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { LessThan, MoreThan } from 'typeorm';
|
||||||
|
import { AuthService } from '../../../libs/common/src/auth/services/auth.service';
|
||||||
|
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
||||||
|
import { HelperHashService } from '../../../libs/common/src/helper/services';
|
||||||
|
import { UserSessionRepository } from '../../../libs/common/src/modules/session/repositories/session.repository';
|
||||||
|
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
||||||
|
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||||
|
import { UserOtpRepository } from '../../../libs/common/src/modules/user/repositories/user.repository';
|
||||||
|
import { EmailService } from '../../../libs/common/src/util/email.service';
|
||||||
|
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||||
|
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
||||||
|
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserAuthService {
|
export class UserAuthService {
|
||||||
@ -108,7 +108,7 @@ export class UserAuthService {
|
|||||||
|
|
||||||
async userLogin(data: UserLoginDto) {
|
async userLogin(data: UserLoginDto) {
|
||||||
try {
|
try {
|
||||||
let user;
|
let user: Omit<UserEntity, 'password'>;
|
||||||
if (data.googleCode) {
|
if (data.googleCode) {
|
||||||
const googleUserData = await this.authService.login({
|
const googleUserData = await this.authService.login({
|
||||||
googleCode: data.googleCode,
|
googleCode: data.googleCode,
|
||||||
@ -145,7 +145,7 @@ export class UserAuthService {
|
|||||||
}
|
}
|
||||||
const session = await Promise.all([
|
const session = await Promise.all([
|
||||||
await this.sessionRepository.update(
|
await this.sessionRepository.update(
|
||||||
{ userId: user.id },
|
{ userId: user?.['id'] },
|
||||||
{
|
{
|
||||||
isLoggedOut: true,
|
isLoggedOut: true,
|
||||||
},
|
},
|
||||||
@ -166,6 +166,7 @@ export class UserAuthService {
|
|||||||
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
||||||
project: user.project,
|
project: user.project,
|
||||||
sessionId: session[1].uuid,
|
sessionId: session[1].uuid,
|
||||||
|
bookingPoints: user.bookingPoints,
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -347,6 +348,7 @@ export class UserAuthService {
|
|||||||
userId: user.uuid,
|
userId: user.uuid,
|
||||||
uuid: user.uuid,
|
uuid: user.uuid,
|
||||||
type,
|
type,
|
||||||
|
bookingPoints: user.bookingPoints,
|
||||||
sessionId,
|
sessionId,
|
||||||
});
|
});
|
||||||
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||||
|
@ -7,6 +7,7 @@ import {
|
|||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
Param,
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Query,
|
Query,
|
||||||
@ -94,7 +95,7 @@ export class BookableSpaceController {
|
|||||||
.UPDATE_BOOKABLE_SPACES_DESCRIPTION,
|
.UPDATE_BOOKABLE_SPACES_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async update(
|
async update(
|
||||||
@Param('spaceUuid') spaceUuid: string,
|
@Param('spaceUuid', ParseUUIDPipe) spaceUuid: string,
|
||||||
@Body() dto: UpdateBookableSpaceDto,
|
@Body() dto: UpdateBookableSpaceDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
const result = await this.bookableSpaceService.update(spaceUuid, dto);
|
const result = await this.bookableSpaceService.update(spaceUuid, dto);
|
||||||
|
@ -52,7 +52,7 @@ export class BookableSpaceService {
|
|||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
qb = qb.andWhere(
|
qb = qb.andWhere(
|
||||||
'space.spaceName ILIKE :search OR community.name ILIKE :search OR parentSpace.spaceName ILIKE :search',
|
'(space.spaceName ILIKE :search OR community.name ILIKE :search OR parentSpace.spaceName ILIKE :search)',
|
||||||
{ search: `%${search}%` },
|
{ search: `%${search}%` },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -68,7 +68,6 @@ export class BookableSpaceService {
|
|||||||
.leftJoinAndSelect('space.bookableConfig', 'bookableConfig')
|
.leftJoinAndSelect('space.bookableConfig', 'bookableConfig')
|
||||||
.andWhere('bookableConfig.uuid IS NULL');
|
.andWhere('bookableConfig.uuid IS NULL');
|
||||||
}
|
}
|
||||||
|
|
||||||
const customModel = TypeORMCustomModel(this.spaceRepository);
|
const customModel = TypeORMCustomModel(this.spaceRepository);
|
||||||
|
|
||||||
const { baseResponseDto, paginationResponseDto } =
|
const { baseResponseDto, paginationResponseDto } =
|
||||||
|
@ -5,7 +5,6 @@ import { ConfigModule } from '@nestjs/config';
|
|||||||
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space/repositories';
|
} from '@app/common/modules/space/repositories';
|
||||||
@ -16,14 +15,12 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
|||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
} from 'src/space/services';
|
} from 'src/space/services';
|
||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
import { TagService } from 'src/space/services/tag';
|
|
||||||
import {
|
import {
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SubSpaceModelService,
|
SubSpaceModelService,
|
||||||
@ -81,16 +78,13 @@ import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/r
|
|||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
// Todo: find out why this is needed
|
// Todo: find out why this is needed
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
// Todo: find out why this is needed
|
// Todo: find out why this is needed
|
||||||
TagService,
|
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
SpaceModelRepository,
|
SpaceModelRepository,
|
||||||
|
@ -323,7 +323,7 @@ export class DeviceService {
|
|||||||
|
|
||||||
async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) {
|
async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) {
|
||||||
try {
|
try {
|
||||||
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
const device = await this.getNewDeviceDetailsFromTuya(
|
||||||
addDeviceDto.deviceTuyaUuid,
|
addDeviceDto.deviceTuyaUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -349,6 +349,7 @@ export class DeviceService {
|
|||||||
spaceDevice: { uuid: addDeviceDto.spaceUuid },
|
spaceDevice: { uuid: addDeviceDto.spaceUuid },
|
||||||
tag: { uuid: addDeviceDto.tagUuid },
|
tag: { uuid: addDeviceDto.tagUuid },
|
||||||
name: addDeviceDto.deviceName,
|
name: addDeviceDto.deviceName,
|
||||||
|
deviceTuyaConstUuid: device.uuid,
|
||||||
});
|
});
|
||||||
if (deviceSaved.uuid) {
|
if (deviceSaved.uuid) {
|
||||||
const deviceStatus: BaseResponseDto =
|
const deviceStatus: BaseResponseDto =
|
||||||
@ -752,6 +753,45 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getNewDeviceDetailsFromTuya(
|
||||||
|
deviceId: string,
|
||||||
|
): Promise<GetDeviceDetailsInterface> {
|
||||||
|
console.log('fetching device details from Tuya for deviceId:', deviceId);
|
||||||
|
try {
|
||||||
|
const result = await this.tuyaService.getDeviceDetails(deviceId);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
throw new NotFoundException('Device not found');
|
||||||
|
}
|
||||||
|
// Convert keys to camel case
|
||||||
|
const camelCaseResponse = convertKeysToCamelCase(result);
|
||||||
|
|
||||||
|
const product = await this.productRepository.findOne({
|
||||||
|
where: { prodId: camelCaseResponse.productId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!product) {
|
||||||
|
throw new NotFoundException('Product Type is not supported');
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { productId, id, productName, ...rest } = camelCaseResponse;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
productUuid: product.uuid,
|
||||||
|
productName: product.name,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
|
} catch (error) {
|
||||||
|
console.log('error', error);
|
||||||
|
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Error fetching device details from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async getDeviceInstructionByDeviceId(
|
async getDeviceInstructionByDeviceId(
|
||||||
deviceUuid: string,
|
deviceUuid: string,
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
|
IsUUID,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
|
||||||
export class AddUserInvitationDto {
|
export class AddUserInvitationDto {
|
||||||
@ -44,6 +45,15 @@ export class AddUserInvitationDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
public jobTitle?: string;
|
public jobTitle?: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The company name of the user',
|
||||||
|
example: 'Tech Corp',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
public companyName?: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The phone number of the user',
|
description: 'The phone number of the user',
|
||||||
example: '+1234567890',
|
example: '+1234567890',
|
||||||
@ -58,7 +68,7 @@ export class AddUserInvitationDto {
|
|||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsUUID('4')
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public roleUuid: string;
|
public roleUuid: string;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -66,15 +76,17 @@ export class AddUserInvitationDto {
|
|||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsUUID('4')
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public projectUuid: string;
|
public projectUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The array of space UUIDs (at least one required)',
|
description: 'The array of space UUIDs (at least one required)',
|
||||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsArray()
|
@IsArray()
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
@ArrayMinSize(1)
|
@ArrayMinSize(1)
|
||||||
public spaceUuids: string[];
|
public spaceUuids: string[];
|
||||||
constructor(dto: Partial<AddUserInvitationDto>) {
|
constructor(dto: Partial<AddUserInvitationDto>) {
|
||||||
|
@ -1,78 +1,10 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty, OmitType } from '@nestjs/swagger';
|
||||||
import {
|
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
|
||||||
ArrayMinSize,
|
import { AddUserInvitationDto } from './add.invite-user.dto';
|
||||||
IsArray,
|
|
||||||
IsBoolean,
|
|
||||||
IsNotEmpty,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
} from 'class-validator';
|
|
||||||
|
|
||||||
export class UpdateUserInvitationDto {
|
export class UpdateUserInvitationDto extends OmitType(AddUserInvitationDto, [
|
||||||
@ApiProperty({
|
'email',
|
||||||
description: 'The first name of the user',
|
]) {}
|
||||||
example: 'John',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public firstName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The last name of the user',
|
|
||||||
example: 'Doe',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public lastName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The job title of the user',
|
|
||||||
example: 'Software Engineer',
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
public jobTitle?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The phone number of the user',
|
|
||||||
example: '+1234567890',
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
public phoneNumber?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The role uuid of the user',
|
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public roleUuid: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The project uuid of the user',
|
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public projectUuid: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The array of space UUIDs (at least one required)',
|
|
||||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsArray()
|
|
||||||
@ArrayMinSize(1)
|
|
||||||
public spaceUuids: string[];
|
|
||||||
constructor(dto: Partial<UpdateUserInvitationDto>) {
|
|
||||||
Object.assign(this, dto);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class DisableUserInvitationDto {
|
export class DisableUserInvitationDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The disable status of the user',
|
description: 'The disable status of the user',
|
||||||
|
@ -38,7 +38,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -121,7 +120,6 @@ import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/r
|
|||||||
NewTagService,
|
NewTagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
|
@ -8,6 +8,8 @@ import {
|
|||||||
InviteUserRepository,
|
InviteUserRepository,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
} from '@app/common/modules/Invite-user/repositiories';
|
} from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||||
|
import { RoleTypeEntity } from '@app/common/modules/role-type/entities';
|
||||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||||
import { SpaceRepository } from '@app/common/modules/space';
|
import { SpaceRepository } from '@app/common/modules/space';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
@ -61,6 +63,7 @@ export class InviteUserService {
|
|||||||
lastName,
|
lastName,
|
||||||
email,
|
email,
|
||||||
jobTitle,
|
jobTitle,
|
||||||
|
companyName,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
roleUuid,
|
roleUuid,
|
||||||
spaceUuids,
|
spaceUuids,
|
||||||
@ -90,6 +93,8 @@ export class InviteUserService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.checkRole(roleUuid, queryRunner);
|
||||||
|
await this.checkProject(projectUuid, queryRunner);
|
||||||
// Validate spaces
|
// Validate spaces
|
||||||
const validSpaces = await this.validateSpaces(
|
const validSpaces = await this.validateSpaces(
|
||||||
spaceUuids,
|
spaceUuids,
|
||||||
@ -102,6 +107,7 @@ export class InviteUserService {
|
|||||||
lastName,
|
lastName,
|
||||||
email,
|
email,
|
||||||
jobTitle,
|
jobTitle,
|
||||||
|
companyName,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
roleType: { uuid: roleUuid },
|
roleType: { uuid: roleUuid },
|
||||||
status: UserStatusEnum.INVITED,
|
status: UserStatusEnum.INVITED,
|
||||||
@ -157,185 +163,6 @@ export class InviteUserService {
|
|||||||
await queryRunner.release();
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private async validateSpaces(
|
|
||||||
spaceUuids: string[],
|
|
||||||
entityManager: EntityManager,
|
|
||||||
): Promise<SpaceEntity[]> {
|
|
||||||
const spaceRepo = entityManager.getRepository(SpaceEntity);
|
|
||||||
|
|
||||||
const validSpaces = await spaceRepo.find({
|
|
||||||
where: { uuid: In(spaceUuids) },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (validSpaces.length !== spaceUuids.length) {
|
|
||||||
const validSpaceUuids = validSpaces.map((space) => space.uuid);
|
|
||||||
const invalidSpaceUuids = spaceUuids.filter(
|
|
||||||
(uuid) => !validSpaceUuids.includes(uuid),
|
|
||||||
);
|
|
||||||
throw new HttpException(
|
|
||||||
`Invalid space UUIDs: ${invalidSpaceUuids.join(', ')}`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return validSpaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkEmailAndProject(dto: CheckEmailDto): Promise<BaseResponseDto> {
|
|
||||||
const { email } = dto;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const user = await this.userRepository.findOne({
|
|
||||||
where: { email },
|
|
||||||
relations: ['project'],
|
|
||||||
});
|
|
||||||
this.validateUserOrInvite(user, 'User');
|
|
||||||
const invitedUser = await this.inviteUserRepository.findOne({
|
|
||||||
where: { email },
|
|
||||||
relations: ['project'],
|
|
||||||
});
|
|
||||||
this.validateUserOrInvite(invitedUser, 'Invited User');
|
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
|
||||||
statusCode: HttpStatus.OK,
|
|
||||||
success: true,
|
|
||||||
message: 'Valid email',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error checking email and project:', error);
|
|
||||||
throw new HttpException(
|
|
||||||
error.message ||
|
|
||||||
'An unexpected error occurred while checking the email',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private validateUserOrInvite(user: any, userType: string): void {
|
|
||||||
if (user) {
|
|
||||||
if (!user.isActive) {
|
|
||||||
throw new HttpException(
|
|
||||||
`${userType} is deleted`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (user.project) {
|
|
||||||
throw new HttpException(
|
|
||||||
`${userType} already has a project`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async activationCode(dto: ActivateCodeDto): Promise<BaseResponseDto> {
|
|
||||||
const { activationCode, userUuid } = dto;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const user = await this.getUser(userUuid);
|
|
||||||
|
|
||||||
const invitedUser = await this.inviteUserRepository.findOne({
|
|
||||||
where: {
|
|
||||||
email: user.email,
|
|
||||||
status: UserStatusEnum.INVITED,
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
relations: ['project', 'spaces.space.community', 'roleType'],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (invitedUser) {
|
|
||||||
if (invitedUser.invitationCode !== activationCode) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Invalid activation code',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle invited user with valid activation code
|
|
||||||
await this.handleInvitedUser(user, invitedUser);
|
|
||||||
} else {
|
|
||||||
// Handle case for non-invited user
|
|
||||||
await this.handleNonInvitedUser(activationCode, userUuid);
|
|
||||||
}
|
|
||||||
return new SuccessResponseDto({
|
|
||||||
statusCode: HttpStatus.OK,
|
|
||||||
success: true,
|
|
||||||
message: 'The code has been successfully activated',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error activating the code:', error);
|
|
||||||
throw new HttpException(
|
|
||||||
error.message ||
|
|
||||||
'An unexpected error occurred while activating the code',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getUser(userUuid: string): Promise<UserEntity> {
|
|
||||||
const user = await this.userRepository.findOne({
|
|
||||||
where: { uuid: userUuid, isActive: true, isUserVerified: true },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async handleNonInvitedUser(
|
|
||||||
activationCode: string,
|
|
||||||
userUuid: string,
|
|
||||||
): Promise<void> {
|
|
||||||
await this.userSpaceService.verifyCodeAndAddUserSpace(
|
|
||||||
{ inviteCode: activationCode },
|
|
||||||
userUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async handleInvitedUser(
|
|
||||||
user: UserEntity,
|
|
||||||
invitedUser: InviteUserEntity,
|
|
||||||
): Promise<void> {
|
|
||||||
for (const invitedSpace of invitedUser.spaces) {
|
|
||||||
try {
|
|
||||||
const deviceUUIDs = await this.userSpaceService.getDeviceUUIDsForSpace(
|
|
||||||
invitedSpace.space.uuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.userSpaceService.addUserPermissionsToDevices(
|
|
||||||
user.uuid,
|
|
||||||
deviceUUIDs,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.spaceUserService.associateUserToSpace({
|
|
||||||
communityUuid: invitedSpace.space.community.uuid,
|
|
||||||
spaceUuid: invitedSpace.space.uuid,
|
|
||||||
userUuid: user.uuid,
|
|
||||||
projectUuid: invitedUser.project.uuid,
|
|
||||||
});
|
|
||||||
} catch (spaceError) {
|
|
||||||
console.error(
|
|
||||||
`Error processing space ${invitedSpace.space.uuid}:`,
|
|
||||||
spaceError,
|
|
||||||
);
|
|
||||||
continue; // Skip to the next space
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update invited user and associated user data
|
|
||||||
await this.inviteUserRepository.update(
|
|
||||||
{ uuid: invitedUser.uuid },
|
|
||||||
{ status: UserStatusEnum.ACTIVE, user: { uuid: user.uuid } },
|
|
||||||
);
|
|
||||||
await this.userRepository.update(
|
|
||||||
{ uuid: user.uuid },
|
|
||||||
{
|
|
||||||
project: { uuid: invitedUser.project.uuid },
|
|
||||||
roleType: { uuid: invitedUser.roleType.uuid },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateUserInvitation(
|
async updateUserInvitation(
|
||||||
dto: UpdateUserInvitationDto,
|
dto: UpdateUserInvitationDto,
|
||||||
@ -357,6 +184,9 @@ export class InviteUserService {
|
|||||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.checkRole(dto.roleUuid, queryRunner);
|
||||||
|
await this.checkProject(projectUuid, queryRunner);
|
||||||
|
|
||||||
// Perform update actions if status is 'INVITED'
|
// Perform update actions if status is 'INVITED'
|
||||||
if (userOldData.status === UserStatusEnum.INVITED) {
|
if (userOldData.status === UserStatusEnum.INVITED) {
|
||||||
await this.updateWhenUserIsInvite(queryRunner, dto, invitedUserUuid);
|
await this.updateWhenUserIsInvite(queryRunner, dto, invitedUserUuid);
|
||||||
@ -439,174 +269,7 @@ export class InviteUserService {
|
|||||||
await queryRunner.release();
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private async getRoleTypeByUuid(roleUuid: string) {
|
|
||||||
const role = await this.roleTypeRepository.findOne({
|
|
||||||
where: { uuid: roleUuid },
|
|
||||||
});
|
|
||||||
|
|
||||||
return role.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async updateWhenUserIsInvite(
|
|
||||||
queryRunner: QueryRunner,
|
|
||||||
dto: UpdateUserInvitationDto,
|
|
||||||
invitedUserUuid: string,
|
|
||||||
): Promise<void> {
|
|
||||||
const { firstName, lastName, jobTitle, phoneNumber, roleUuid, spaceUuids } =
|
|
||||||
dto;
|
|
||||||
|
|
||||||
// Update user invitation details
|
|
||||||
await queryRunner.manager.update(
|
|
||||||
this.inviteUserRepository.target,
|
|
||||||
{ uuid: invitedUserUuid },
|
|
||||||
{
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
jobTitle,
|
|
||||||
phoneNumber,
|
|
||||||
roleType: { uuid: roleUuid },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove old space associations
|
|
||||||
await queryRunner.manager.delete(this.inviteUserSpaceRepository.target, {
|
|
||||||
inviteUser: { uuid: invitedUserUuid },
|
|
||||||
});
|
|
||||||
|
|
||||||
// Save new space associations
|
|
||||||
const spaceData = spaceUuids.map((spaceUuid) => ({
|
|
||||||
inviteUser: { uuid: invitedUserUuid },
|
|
||||||
space: { uuid: spaceUuid },
|
|
||||||
}));
|
|
||||||
await queryRunner.manager.save(
|
|
||||||
this.inviteUserSpaceRepository.target,
|
|
||||||
spaceData,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
private async updateWhenUserIsActive(
|
|
||||||
queryRunner: QueryRunner,
|
|
||||||
dto: UpdateUserInvitationDto,
|
|
||||||
invitedUserUuid: string,
|
|
||||||
): Promise<void> {
|
|
||||||
const {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
jobTitle,
|
|
||||||
phoneNumber,
|
|
||||||
roleUuid,
|
|
||||||
spaceUuids,
|
|
||||||
projectUuid,
|
|
||||||
} = dto;
|
|
||||||
|
|
||||||
const userData = await this.inviteUserRepository.findOne({
|
|
||||||
where: { uuid: invitedUserUuid },
|
|
||||||
relations: ['user.userSpaces.space', 'user.userSpaces.space.community'],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!userData) {
|
|
||||||
throw new HttpException(
|
|
||||||
`User with invitedUserUuid ${invitedUserUuid} not found`,
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update user details
|
|
||||||
await queryRunner.manager.update(
|
|
||||||
this.inviteUserRepository.target,
|
|
||||||
{ uuid: invitedUserUuid },
|
|
||||||
{
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
jobTitle,
|
|
||||||
phoneNumber,
|
|
||||||
roleType: { uuid: roleUuid },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
await this.userRepository.update(
|
|
||||||
{ uuid: userData.user.uuid },
|
|
||||||
{
|
|
||||||
roleType: { uuid: roleUuid },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
// Disassociate the user from all current spaces
|
|
||||||
const disassociatePromises = userData.user.userSpaces.map((userSpace) =>
|
|
||||||
this.spaceUserService
|
|
||||||
.disassociateUserFromSpace({
|
|
||||||
communityUuid: userSpace.space.community.uuid,
|
|
||||||
spaceUuid: userSpace.space.uuid,
|
|
||||||
userUuid: userData.user.uuid,
|
|
||||||
projectUuid,
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to disassociate user from space ${userSpace.space.uuid}:`,
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
throw error;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.allSettled(disassociatePromises);
|
|
||||||
|
|
||||||
// Process new spaces
|
|
||||||
const associatePromises = spaceUuids.map(async (spaceUuid) => {
|
|
||||||
try {
|
|
||||||
// Fetch space details
|
|
||||||
const spaceDetails = await this.getSpaceByUuid(spaceUuid);
|
|
||||||
|
|
||||||
// Fetch device UUIDs for the space
|
|
||||||
const deviceUUIDs =
|
|
||||||
await this.userSpaceService.getDeviceUUIDsForSpace(spaceUuid);
|
|
||||||
|
|
||||||
// Grant permissions to the user for all devices in the space
|
|
||||||
await this.userSpaceService.addUserPermissionsToDevices(
|
|
||||||
userData.user.uuid,
|
|
||||||
deviceUUIDs,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Associate the user with the new space
|
|
||||||
await this.spaceUserService.associateUserToSpace({
|
|
||||||
communityUuid: spaceDetails.communityUuid,
|
|
||||||
spaceUuid: spaceUuid,
|
|
||||||
userUuid: userData.user.uuid,
|
|
||||||
projectUuid,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to process space ${spaceUuid}:`, error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.all(associatePromises);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getSpaceByUuid(spaceUuid: string) {
|
|
||||||
try {
|
|
||||||
const space = await this.spaceRepository.findOne({
|
|
||||||
where: {
|
|
||||||
uuid: spaceUuid,
|
|
||||||
},
|
|
||||||
relations: ['community'],
|
|
||||||
});
|
|
||||||
if (!space) {
|
|
||||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
uuid: space.uuid,
|
|
||||||
createdAt: space.createdAt,
|
|
||||||
updatedAt: space.updatedAt,
|
|
||||||
name: space.spaceName,
|
|
||||||
spaceTuyaUuid: space.community.externalId,
|
|
||||||
communityUuid: space.community.uuid,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof BadRequestException) {
|
|
||||||
throw err; // Re-throw BadRequestException
|
|
||||||
} else {
|
|
||||||
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async disableUserInvitation(
|
async disableUserInvitation(
|
||||||
dto: DisableUserInvitationDto,
|
dto: DisableUserInvitationDto,
|
||||||
invitedUserUuid: string,
|
invitedUserUuid: string,
|
||||||
@ -686,74 +349,6 @@ export class InviteUserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateUserStatus(
|
|
||||||
invitedUserUuid: string,
|
|
||||||
projectUuid: string,
|
|
||||||
isEnabled: boolean,
|
|
||||||
) {
|
|
||||||
await this.inviteUserRepository.update(
|
|
||||||
{ uuid: invitedUserUuid, project: { uuid: projectUuid } },
|
|
||||||
{ isEnabled },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async disassociateUserFromSpaces(user: any, projectUuid: string) {
|
|
||||||
const disassociatePromises = user.userSpaces.map((userSpace) =>
|
|
||||||
this.spaceUserService.disassociateUserFromSpace({
|
|
||||||
communityUuid: userSpace.space.community.uuid,
|
|
||||||
spaceUuid: userSpace.space.uuid,
|
|
||||||
userUuid: user.uuid,
|
|
||||||
projectUuid,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
const results = await Promise.allSettled(disassociatePromises);
|
|
||||||
|
|
||||||
results.forEach((result, index) => {
|
|
||||||
if (result.status === 'rejected') {
|
|
||||||
console.error(
|
|
||||||
`Failed to disassociate user from space ${user.userSpaces[index].space.uuid}:`,
|
|
||||||
result.reason,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
private async associateUserToSpaces(
|
|
||||||
user: any,
|
|
||||||
userData: any,
|
|
||||||
projectUuid: string,
|
|
||||||
invitedUserUuid: string,
|
|
||||||
disable: boolean,
|
|
||||||
) {
|
|
||||||
const spaceUuids = userData.spaces.map((space) => space.space.uuid);
|
|
||||||
|
|
||||||
const associatePromises = spaceUuids.map(async (spaceUuid) => {
|
|
||||||
try {
|
|
||||||
const spaceDetails = await this.getSpaceByUuid(spaceUuid);
|
|
||||||
|
|
||||||
const deviceUUIDs =
|
|
||||||
await this.userSpaceService.getDeviceUUIDsForSpace(spaceUuid);
|
|
||||||
await this.userSpaceService.addUserPermissionsToDevices(
|
|
||||||
user.uuid,
|
|
||||||
deviceUUIDs,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.spaceUserService.associateUserToSpace({
|
|
||||||
communityUuid: spaceDetails.communityUuid,
|
|
||||||
spaceUuid,
|
|
||||||
userUuid: user.uuid,
|
|
||||||
projectUuid,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.updateUserStatus(invitedUserUuid, projectUuid, disable);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to associate user to space ${spaceUuid}:`, error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.allSettled(associatePromises);
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteUserInvitation(
|
async deleteUserInvitation(
|
||||||
invitedUserUuid: string,
|
invitedUserUuid: string,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
@ -824,4 +419,486 @@ export class InviteUserService {
|
|||||||
await queryRunner.release();
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async activationCode(dto: ActivateCodeDto): Promise<BaseResponseDto> {
|
||||||
|
const { activationCode, userUuid } = dto;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = await this.getUser(userUuid);
|
||||||
|
|
||||||
|
const invitedUser = await this.inviteUserRepository.findOne({
|
||||||
|
where: {
|
||||||
|
email: user.email,
|
||||||
|
status: UserStatusEnum.INVITED,
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
relations: ['project', 'spaces.space.community', 'roleType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (invitedUser) {
|
||||||
|
if (invitedUser.invitationCode !== activationCode) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Invalid activation code',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle invited user with valid activation code
|
||||||
|
await this.handleInvitedUser(user, invitedUser);
|
||||||
|
} else {
|
||||||
|
// Handle case for non-invited user
|
||||||
|
await this.handleNonInvitedUser(activationCode, userUuid);
|
||||||
|
}
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
success: true,
|
||||||
|
message: 'The code has been successfully activated',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error activating the code:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
error.message ||
|
||||||
|
'An unexpected error occurred while activating the code',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkEmailAndProject(dto: CheckEmailDto): Promise<BaseResponseDto> {
|
||||||
|
const { email } = dto;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = await this.userRepository.findOne({
|
||||||
|
where: { email },
|
||||||
|
relations: ['project'],
|
||||||
|
});
|
||||||
|
this.validateUserOrInvite(user, 'User');
|
||||||
|
const invitedUser = await this.inviteUserRepository.findOne({
|
||||||
|
where: { email },
|
||||||
|
relations: ['project'],
|
||||||
|
});
|
||||||
|
this.validateUserOrInvite(invitedUser, 'Invited User');
|
||||||
|
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
success: true,
|
||||||
|
message: 'Valid email',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error checking email and project:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
error.message ||
|
||||||
|
'An unexpected error occurred while checking the email',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getSpaceByUuid(spaceUuid: string) {
|
||||||
|
try {
|
||||||
|
const space = await this.spaceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: spaceUuid,
|
||||||
|
},
|
||||||
|
relations: ['community'],
|
||||||
|
});
|
||||||
|
if (!space) {
|
||||||
|
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
uuid: space.uuid,
|
||||||
|
createdAt: space.createdAt,
|
||||||
|
updatedAt: space.updatedAt,
|
||||||
|
name: space.spaceName,
|
||||||
|
spaceTuyaUuid: space.community.externalId,
|
||||||
|
communityUuid: space.community.uuid,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async validateSpaces(
|
||||||
|
spaceUuids: string[],
|
||||||
|
entityManager: EntityManager,
|
||||||
|
): Promise<SpaceEntity[]> {
|
||||||
|
const spaceRepo = entityManager.getRepository(SpaceEntity);
|
||||||
|
|
||||||
|
const validSpaces = await spaceRepo.find({
|
||||||
|
where: { uuid: In(spaceUuids) },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (validSpaces.length !== spaceUuids.length) {
|
||||||
|
const validSpaceUuids = validSpaces.map((space) => space.uuid);
|
||||||
|
const invalidSpaceUuids = spaceUuids.filter(
|
||||||
|
(uuid) => !validSpaceUuids.includes(uuid),
|
||||||
|
);
|
||||||
|
throw new HttpException(
|
||||||
|
`Invalid space UUIDs: ${invalidSpaceUuids.join(', ')}`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return validSpaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkRole(
|
||||||
|
roleUuid: string,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
try {
|
||||||
|
const role = await queryRunner.manager.findOne(RoleTypeEntity, {
|
||||||
|
where: { uuid: roleUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!role) {
|
||||||
|
throw new HttpException('Role not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
success: true,
|
||||||
|
message: 'Valid role',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error checking role:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'An unexpected error occurred while checking the role',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkProject(
|
||||||
|
projectUuid: string,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
try {
|
||||||
|
const project = await queryRunner.manager.findOne(ProjectEntity, {
|
||||||
|
where: { uuid: projectUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!project) {
|
||||||
|
throw new HttpException('Project not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
success: true,
|
||||||
|
message: 'Valid project',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error checking project:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
error.message ||
|
||||||
|
'An unexpected error occurred while checking the project',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private validateUserOrInvite(user: any, userType: string): void {
|
||||||
|
if (user) {
|
||||||
|
if (!user.isActive) {
|
||||||
|
throw new HttpException(
|
||||||
|
`${userType} is deleted`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (user.project) {
|
||||||
|
throw new HttpException(
|
||||||
|
`${userType} already has a project`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getUser(userUuid: string): Promise<UserEntity> {
|
||||||
|
const user = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid, isActive: true, isUserVerified: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleNonInvitedUser(
|
||||||
|
activationCode: string,
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.userSpaceService.verifyCodeAndAddUserSpace(
|
||||||
|
{ inviteCode: activationCode },
|
||||||
|
userUuid,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleInvitedUser(
|
||||||
|
user: UserEntity,
|
||||||
|
invitedUser: InviteUserEntity,
|
||||||
|
): Promise<void> {
|
||||||
|
for (const invitedSpace of invitedUser.spaces) {
|
||||||
|
try {
|
||||||
|
const deviceUUIDs = await this.userSpaceService.getDeviceUUIDsForSpace(
|
||||||
|
invitedSpace.space.uuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.userSpaceService.addUserPermissionsToDevices(
|
||||||
|
user.uuid,
|
||||||
|
deviceUUIDs,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.spaceUserService.associateUserToSpace({
|
||||||
|
communityUuid: invitedSpace.space.community.uuid,
|
||||||
|
spaceUuid: invitedSpace.space.uuid,
|
||||||
|
userUuid: user.uuid,
|
||||||
|
projectUuid: invitedUser.project.uuid,
|
||||||
|
});
|
||||||
|
} catch (spaceError) {
|
||||||
|
console.error(
|
||||||
|
`Error processing space ${invitedSpace.space.uuid}:`,
|
||||||
|
spaceError,
|
||||||
|
);
|
||||||
|
continue; // Skip to the next space
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update invited user and associated user data
|
||||||
|
await this.inviteUserRepository.update(
|
||||||
|
{ uuid: invitedUser.uuid },
|
||||||
|
{ status: UserStatusEnum.ACTIVE, user: { uuid: user.uuid } },
|
||||||
|
);
|
||||||
|
await this.userRepository.update(
|
||||||
|
{ uuid: user.uuid },
|
||||||
|
{
|
||||||
|
project: { uuid: invitedUser.project.uuid },
|
||||||
|
roleType: { uuid: invitedUser.roleType.uuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getRoleTypeByUuid(roleUuid: string) {
|
||||||
|
const role = await this.roleTypeRepository.findOne({
|
||||||
|
where: { uuid: roleUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
return role.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async updateWhenUserIsInvite(
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
dto: UpdateUserInvitationDto,
|
||||||
|
invitedUserUuid: string,
|
||||||
|
): Promise<void> {
|
||||||
|
const {
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
jobTitle,
|
||||||
|
companyName,
|
||||||
|
phoneNumber,
|
||||||
|
roleUuid,
|
||||||
|
spaceUuids,
|
||||||
|
} = dto;
|
||||||
|
|
||||||
|
// Update user invitation details
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.inviteUserRepository.target,
|
||||||
|
{ uuid: invitedUserUuid },
|
||||||
|
{
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
jobTitle,
|
||||||
|
companyName,
|
||||||
|
phoneNumber,
|
||||||
|
roleType: { uuid: roleUuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Remove old space associations
|
||||||
|
await queryRunner.manager.delete(this.inviteUserSpaceRepository.target, {
|
||||||
|
inviteUser: { uuid: invitedUserUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Save new space associations
|
||||||
|
const spaceData = spaceUuids.map((spaceUuid) => ({
|
||||||
|
inviteUser: { uuid: invitedUserUuid },
|
||||||
|
space: { uuid: spaceUuid },
|
||||||
|
}));
|
||||||
|
await queryRunner.manager.save(
|
||||||
|
this.inviteUserSpaceRepository.target,
|
||||||
|
spaceData,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
private async updateWhenUserIsActive(
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
dto: UpdateUserInvitationDto,
|
||||||
|
invitedUserUuid: string,
|
||||||
|
): Promise<void> {
|
||||||
|
const {
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
jobTitle,
|
||||||
|
companyName,
|
||||||
|
phoneNumber,
|
||||||
|
roleUuid,
|
||||||
|
spaceUuids,
|
||||||
|
projectUuid,
|
||||||
|
} = dto;
|
||||||
|
|
||||||
|
const userData = await this.inviteUserRepository.findOne({
|
||||||
|
where: { uuid: invitedUserUuid },
|
||||||
|
relations: ['user.userSpaces.space', 'user.userSpaces.space.community'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!userData) {
|
||||||
|
throw new HttpException(
|
||||||
|
`User with invitedUserUuid ${invitedUserUuid} not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update user details
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.inviteUserRepository.target,
|
||||||
|
{ uuid: invitedUserUuid },
|
||||||
|
{
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
jobTitle,
|
||||||
|
companyName,
|
||||||
|
phoneNumber,
|
||||||
|
roleType: { uuid: roleUuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await this.userRepository.update(
|
||||||
|
{ uuid: userData.user.uuid },
|
||||||
|
{
|
||||||
|
roleType: { uuid: roleUuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
// Disassociate the user from all current spaces
|
||||||
|
const disassociatePromises = userData.user.userSpaces.map((userSpace) =>
|
||||||
|
this.spaceUserService
|
||||||
|
.disassociateUserFromSpace({
|
||||||
|
communityUuid: userSpace.space.community.uuid,
|
||||||
|
spaceUuid: userSpace.space.uuid,
|
||||||
|
userUuid: userData.user.uuid,
|
||||||
|
projectUuid,
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(
|
||||||
|
`Failed to disassociate user from space ${userSpace.space.uuid}:`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await Promise.allSettled(disassociatePromises);
|
||||||
|
|
||||||
|
// Process new spaces
|
||||||
|
const associatePromises = spaceUuids.map(async (spaceUuid) => {
|
||||||
|
try {
|
||||||
|
// Fetch space details
|
||||||
|
const spaceDetails = await this.getSpaceByUuid(spaceUuid);
|
||||||
|
|
||||||
|
// Fetch device UUIDs for the space
|
||||||
|
const deviceUUIDs =
|
||||||
|
await this.userSpaceService.getDeviceUUIDsForSpace(spaceUuid);
|
||||||
|
|
||||||
|
// Grant permissions to the user for all devices in the space
|
||||||
|
await this.userSpaceService.addUserPermissionsToDevices(
|
||||||
|
userData.user.uuid,
|
||||||
|
deviceUUIDs,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Associate the user with the new space
|
||||||
|
await this.spaceUserService.associateUserToSpace({
|
||||||
|
communityUuid: spaceDetails.communityUuid,
|
||||||
|
spaceUuid: spaceUuid,
|
||||||
|
userUuid: userData.user.uuid,
|
||||||
|
projectUuid,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to process space ${spaceUuid}:`, error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(associatePromises);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async updateUserStatus(
|
||||||
|
invitedUserUuid: string,
|
||||||
|
projectUuid: string,
|
||||||
|
isEnabled: boolean,
|
||||||
|
) {
|
||||||
|
await this.inviteUserRepository.update(
|
||||||
|
{ uuid: invitedUserUuid, project: { uuid: projectUuid } },
|
||||||
|
{ isEnabled },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async disassociateUserFromSpaces(user: any, projectUuid: string) {
|
||||||
|
const disassociatePromises = user.userSpaces.map((userSpace) =>
|
||||||
|
this.spaceUserService.disassociateUserFromSpace({
|
||||||
|
communityUuid: userSpace.space.community.uuid,
|
||||||
|
spaceUuid: userSpace.space.uuid,
|
||||||
|
userUuid: user.uuid,
|
||||||
|
projectUuid,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const results = await Promise.allSettled(disassociatePromises);
|
||||||
|
|
||||||
|
results.forEach((result, index) => {
|
||||||
|
if (result.status === 'rejected') {
|
||||||
|
console.error(
|
||||||
|
`Failed to disassociate user from space ${user.userSpaces[index].space.uuid}:`,
|
||||||
|
result.reason,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private async associateUserToSpaces(
|
||||||
|
user: any,
|
||||||
|
userData: any,
|
||||||
|
projectUuid: string,
|
||||||
|
invitedUserUuid: string,
|
||||||
|
disable: boolean,
|
||||||
|
) {
|
||||||
|
const spaceUuids = userData.spaces.map((space) => space.space.uuid);
|
||||||
|
|
||||||
|
const associatePromises = spaceUuids.map(async (spaceUuid) => {
|
||||||
|
try {
|
||||||
|
const spaceDetails = await this.getSpaceByUuid(spaceUuid);
|
||||||
|
|
||||||
|
const deviceUUIDs =
|
||||||
|
await this.userSpaceService.getDeviceUUIDsForSpace(spaceUuid);
|
||||||
|
await this.userSpaceService.addUserPermissionsToDevices(
|
||||||
|
user.uuid,
|
||||||
|
deviceUUIDs,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.spaceUserService.associateUserToSpace({
|
||||||
|
communityUuid: spaceDetails.communityUuid,
|
||||||
|
spaceUuid,
|
||||||
|
userUuid: user.uuid,
|
||||||
|
projectUuid,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.updateUserStatus(invitedUserUuid, projectUuid, disable);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to associate user to space ${spaceUuid}:`, error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.allSettled(associatePromises);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -96,7 +95,6 @@ import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/r
|
|||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
|
@ -23,7 +23,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -94,7 +93,6 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
|||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
DeviceService,
|
DeviceService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
|
@ -33,6 +33,7 @@ export class ProjectUserService {
|
|||||||
'status',
|
'status',
|
||||||
'phoneNumber',
|
'phoneNumber',
|
||||||
'jobTitle',
|
'jobTitle',
|
||||||
|
'companyName',
|
||||||
'invitedBy',
|
'invitedBy',
|
||||||
'isEnabled',
|
'isEnabled',
|
||||||
],
|
],
|
||||||
@ -91,6 +92,7 @@ export class ProjectUserService {
|
|||||||
'status',
|
'status',
|
||||||
'phoneNumber',
|
'phoneNumber',
|
||||||
'jobTitle',
|
'jobTitle',
|
||||||
|
'companyName',
|
||||||
'invitedBy',
|
'invitedBy',
|
||||||
'isEnabled',
|
'isEnabled',
|
||||||
],
|
],
|
||||||
|
@ -22,7 +22,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -93,7 +92,6 @@ const CommandHandlers = [
|
|||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
TuyaService,
|
TuyaService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
DeviceService,
|
DeviceService,
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
||||||
import { SpaceService } from '../services';
|
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
Get,
|
Get,
|
||||||
Param,
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AddSpaceDto, CommunitySpaceParam, UpdateSpaceDto } from '../dtos';
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import { GetSpaceParam } from '../dtos/get.space.param';
|
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { AddSpaceDto, CommunitySpaceParam, UpdateSpaceDto } from '../dtos';
|
||||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||||
|
import { GetSpaceParam } from '../dtos/get.space.param';
|
||||||
|
import { OrderSpacesDto } from '../dtos/order.spaces.dto';
|
||||||
|
import { SpaceService } from '../services';
|
||||||
|
|
||||||
@ApiTags('Space Module')
|
@ApiTags('Space Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -65,6 +68,30 @@ export class SpaceController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(PermissionsGuard)
|
||||||
|
@Permissions('SPACE_UPDATE')
|
||||||
|
@ApiOperation({
|
||||||
|
summary:
|
||||||
|
ControllerRoute.SPACE.ACTIONS
|
||||||
|
.UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.SPACE.ACTIONS
|
||||||
|
.UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_DESCRIPTION,
|
||||||
|
})
|
||||||
|
@Post(':parentSpaceUuid/spaces/order')
|
||||||
|
async updateSpacesOrder(
|
||||||
|
@Body() orderSpacesDto: OrderSpacesDto,
|
||||||
|
@Param() communitySpaceParam: CommunitySpaceParam,
|
||||||
|
@Param('parentSpaceUuid', ParseUUIDPipe) parentSpaceUuid: string,
|
||||||
|
) {
|
||||||
|
await this.spaceService.updateSpacesOrder(parentSpaceUuid, orderSpacesDto);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: 200,
|
||||||
|
message: 'Spaces order updated successfully',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('SPACE_DELETE')
|
@Permissions('SPACE_DELETE')
|
||||||
|
13
src/space/dtos/order.spaces.dto.ts
Normal file
13
src/space/dtos/order.spaces.dto.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { ArrayUnique, IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class OrderSpacesDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of children spaces associated with the space',
|
||||||
|
type: [String],
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@ArrayUnique()
|
||||||
|
@IsUUID('4', { each: true, message: 'Invalid space UUID provided' })
|
||||||
|
spacesUuids: string[];
|
||||||
|
}
|
@ -2,6 +2,7 @@ import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
|
|||||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
|
ArrayUnique,
|
||||||
IsArray,
|
IsArray,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -49,6 +50,21 @@ export class UpdateSpaceDto {
|
|||||||
description: 'List of subspace modifications',
|
description: 'List of subspace modifications',
|
||||||
type: [UpdateSubspaceDto],
|
type: [UpdateSubspaceDto],
|
||||||
})
|
})
|
||||||
|
@ArrayUnique((subspace) => subspace.subspaceName ?? subspace.uuid, {
|
||||||
|
message(validationArguments) {
|
||||||
|
const subspaces = validationArguments.value;
|
||||||
|
const nameCounts = subspaces.reduce((acc, curr) => {
|
||||||
|
acc[curr.subspaceName ?? curr.uuid] =
|
||||||
|
(acc[curr.subspaceName ?? curr.uuid] || 0) + 1;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
// Find duplicates
|
||||||
|
const duplicates = Object.keys(nameCounts).filter(
|
||||||
|
(name) => nameCounts[name] > 1,
|
||||||
|
);
|
||||||
|
return `Duplicate subspace names found: ${duplicates.join(', ')}`;
|
||||||
|
},
|
||||||
|
})
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
|
@ -2,6 +2,5 @@ export * from './space.service';
|
|||||||
export * from './space-user.service';
|
export * from './space-user.service';
|
||||||
export * from './space-device.service';
|
export * from './space-device.service';
|
||||||
export * from './subspace';
|
export * from './subspace';
|
||||||
export * from './space-link';
|
|
||||||
export * from './space-scene.service';
|
export * from './space-scene.service';
|
||||||
export * from './space-validation.service';
|
export * from './space-validation.service';
|
||||||
|
@ -1 +0,0 @@
|
|||||||
export * from './space-link.service';
|
|
@ -1,6 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
|
|
||||||
// todo: find out why we need to import this
|
|
||||||
// in community module in order for the whole system to work
|
|
||||||
@Injectable()
|
|
||||||
export class SpaceLinkService {}
|
|
@ -33,6 +33,7 @@ import {
|
|||||||
} from '../dtos';
|
} from '../dtos';
|
||||||
import { CreateProductAllocationDto } from '../dtos/create-product-allocation.dto';
|
import { CreateProductAllocationDto } from '../dtos/create-product-allocation.dto';
|
||||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||||
|
import { OrderSpacesDto } from '../dtos/order.spaces.dto';
|
||||||
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
|
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
|
||||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||||
import { ValidationService } from './space-validation.service';
|
import { ValidationService } from './space-validation.service';
|
||||||
@ -355,6 +356,54 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateSpacesOrder(
|
||||||
|
parentSpaceUuid: string,
|
||||||
|
{ spacesUuids }: OrderSpacesDto,
|
||||||
|
) {
|
||||||
|
const parentSpace = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: parentSpaceUuid, disabled: false },
|
||||||
|
relations: ['children'],
|
||||||
|
});
|
||||||
|
if (!parentSpace) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Parent space with ID ${parentSpaceUuid} not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// ensure that all sent spaces belong to the parent space
|
||||||
|
const missingSpaces = spacesUuids.filter(
|
||||||
|
(uuid) => !parentSpace.children.some((child) => child.uuid === uuid),
|
||||||
|
);
|
||||||
|
if (missingSpaces.length > 0) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Some spaces with IDs ${missingSpaces.join(
|
||||||
|
', ',
|
||||||
|
)} do not belong to the parent space with ID ${parentSpaceUuid}`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await this.spaceRepository.update(
|
||||||
|
{ uuid: In(spacesUuids), parent: { uuid: parentSpaceUuid } },
|
||||||
|
{
|
||||||
|
order: () =>
|
||||||
|
'CASE ' +
|
||||||
|
spacesUuids
|
||||||
|
.map((s, index) => `WHEN uuid = '${s}' THEN ${index + 1}`)
|
||||||
|
.join(' ') +
|
||||||
|
' END',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating spaces order:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
'An error occurred while updating spaces order',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async delete(params: GetSpaceParam): Promise<BaseResponseDto> {
|
async delete(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
@ -426,7 +475,7 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async disableSpace(space: SpaceEntity, orphanSpace: SpaceEntity) {
|
private async disableSpace(space: SpaceEntity, orphanSpace: SpaceEntity) {
|
||||||
await this.commandBus.execute(
|
await this.commandBus.execute(
|
||||||
new DisableSpaceCommand({ spaceUuid: space.uuid, orphanSpace }),
|
new DisableSpaceCommand({ spaceUuid: space.uuid, orphanSpace }),
|
||||||
);
|
);
|
||||||
@ -709,10 +758,21 @@ export class SpaceService {
|
|||||||
rootSpaces.push(map.get(space.uuid)!); // Push only root spaces
|
rootSpaces.push(map.get(space.uuid)!); // Push only root spaces
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
rootSpaces.forEach(this.sortSpaceChildren.bind(this));
|
||||||
return rootSpaces;
|
return rootSpaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sortSpaceChildren(space: SpaceEntity) {
|
||||||
|
if (space.children && space.children.length > 0) {
|
||||||
|
space.children.sort((a, b) => {
|
||||||
|
const aOrder = a.order ?? Infinity;
|
||||||
|
const bOrder = b.order ?? Infinity;
|
||||||
|
return aOrder - bOrder;
|
||||||
|
});
|
||||||
|
space.children.forEach(this.sortSpaceChildren.bind(this)); // Recursively sort children of children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private validateSpaceCreationCriteria({
|
private validateSpaceCreationCriteria({
|
||||||
spaceModelUuid,
|
spaceModelUuid,
|
||||||
productAllocations,
|
productAllocations,
|
||||||
|
@ -23,7 +23,7 @@ export class SubspaceProductAllocationService {
|
|||||||
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
|
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
if (!allocationsData.length) return;
|
if (!allocationsData?.length) return;
|
||||||
|
|
||||||
const allocations: SubspaceProductAllocationEntity[] = [];
|
const allocations: SubspaceProductAllocationEntity[] = [];
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ export class SubspaceProductAllocationService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Create the product-tag mapping based on the processed tags
|
// Create the product-tag mapping based on the processed tags
|
||||||
const productTagMapping = subspace.productAllocations.map(
|
const productTagMapping = subspace.productAllocations?.map(
|
||||||
({ tagUuid, tagName, productUuid }) => {
|
({ tagUuid, tagName, productUuid }) => {
|
||||||
const inputTag = tagUuid
|
const inputTag = tagUuid
|
||||||
? createdTagsByUUID.get(tagUuid)
|
? createdTagsByUUID.get(tagUuid)
|
||||||
|
@ -39,7 +39,7 @@ export class SubSpaceService {
|
|||||||
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
|
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createSubspaces(
|
private async createSubspaces(
|
||||||
subspaceData: Array<{
|
subspaceData: Array<{
|
||||||
subspaceName: string;
|
subspaceName: string;
|
||||||
space: SpaceEntity;
|
space: SpaceEntity;
|
||||||
@ -342,26 +342,37 @@ export class SubSpaceService {
|
|||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const existingSubspaces = await this.subspaceRepository.find({
|
||||||
|
where: {
|
||||||
|
uuid: In(
|
||||||
|
subspaceDtos.filter((dto) => dto.uuid).map((dto) => dto.uuid),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
existingSubspaces.length !==
|
||||||
|
subspaceDtos.filter((dto) => dto.uuid).length
|
||||||
|
) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Some subspaces with provided UUIDs do not exist in the space.`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
[
|
newSubspaces,
|
||||||
...newSubspaces,
|
|
||||||
...subspaceDtos
|
|
||||||
.filter((dto) => dto.uuid)
|
|
||||||
.map((dto) => ({
|
|
||||||
subspaceName: dto.subspaceName,
|
|
||||||
space,
|
|
||||||
})),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
|
const allSubspaces = [...updatedSubspaces, ...existingSubspaces];
|
||||||
// create or update allocations for the subspaces
|
// create or update allocations for the subspaces
|
||||||
if (updatedSubspaces.length > 0) {
|
if (allSubspaces.length > 0) {
|
||||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
||||||
subspaceDtos.map((dto) => ({
|
subspaceDtos.map((dto) => ({
|
||||||
...dto,
|
...dto,
|
||||||
uuid:
|
uuid:
|
||||||
dto.uuid ||
|
dto.uuid ||
|
||||||
updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
allSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
||||||
?.uuid,
|
?.uuid,
|
||||||
})),
|
})),
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
@ -1 +0,0 @@
|
|||||||
export * from './tag.service';
|
|
@ -1,8 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
|
|
||||||
// todo: find out why we need to import this
|
|
||||||
// in community module in order for the whole system to work
|
|
||||||
@Injectable()
|
|
||||||
export class TagService {
|
|
||||||
constructor() {}
|
|
||||||
}
|
|
@ -37,7 +37,6 @@ import {
|
|||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space/repositories';
|
} from '@app/common/modules/space/repositories';
|
||||||
@ -116,7 +115,6 @@ export const CommandHandlers = [DisableSpaceHandler];
|
|||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
UserSpaceRepository,
|
UserSpaceRepository,
|
||||||
UserRepository,
|
UserRepository,
|
||||||
SpaceUserService,
|
SpaceUserService,
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
|
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||||
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
import {
|
||||||
|
InviteUserRepository,
|
||||||
|
InviteUserSpaceRepository,
|
||||||
|
} from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { InviteSpaceEntity } from '@app/common/modules/space/entities/invite-space.entity';
|
||||||
|
import {
|
||||||
|
InviteSpaceRepository,
|
||||||
|
SpaceRepository,
|
||||||
|
} from '@app/common/modules/space/repositories';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Injectable,
|
Injectable,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import { AddUserSpaceDto, AddUserSpaceUsingCodeDto } from '../dtos';
|
|
||||||
import {
|
|
||||||
InviteSpaceRepository,
|
|
||||||
SpaceRepository,
|
|
||||||
} from '@app/common/modules/space/repositories';
|
|
||||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
|
||||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
import { AddUserSpaceDto, AddUserSpaceUsingCodeDto } from '../dtos';
|
||||||
import { InviteSpaceEntity } from '@app/common/modules/space/entities/invite-space.entity';
|
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
|
||||||
import {
|
|
||||||
InviteUserRepository,
|
|
||||||
InviteUserSpaceRepository,
|
|
||||||
} from '@app/common/modules/Invite-user/repositiories';
|
|
||||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserSpaceService {
|
export class UserSpaceService {
|
||||||
@ -154,6 +154,7 @@ export class UserSpaceService {
|
|||||||
lastName: user.lastName,
|
lastName: user.lastName,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
jobTitle: null,
|
jobTitle: null,
|
||||||
|
companyName: null,
|
||||||
phoneNumber: null,
|
phoneNumber: null,
|
||||||
roleType: { uuid: user.role.uuid },
|
roleType: { uuid: user.role.uuid },
|
||||||
status: UserStatusEnum.ACTIVE,
|
status: UserStatusEnum.ACTIVE,
|
||||||
|
Reference in New Issue
Block a user