mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 02:24:54 +00:00
Add Invite User Module and Update User and Space Entities
This commit introduces a new module for handling user invitations, including DTOs, entities, and repositories. It also updates the User and Space entities to include relationships with the new Invite User entities.
This commit is contained in:
103
src/invite-user/services/invite-user.service.ts
Normal file
103
src/invite-user/services/invite-user.service.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import { InviteUserSpaceRepository } from './../../../libs/common/src/modules/Invite-user/repositories/Invite-user.repository';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { AddUserInvitationDto } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositories';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { generateRandomString } from '@app/common/helper/randomString';
|
||||
import { IsNull, Not } from 'typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { UserEntity } from '@app/common/modules/user/entities';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserService {
|
||||
constructor(
|
||||
private readonly inviteUserRepository: InviteUserRepository,
|
||||
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async createUserInvitation(
|
||||
dto: AddUserInvitationDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
jobTitle,
|
||||
phoneNumber,
|
||||
roleUuid,
|
||||
spaceUuids,
|
||||
} = dto;
|
||||
|
||||
const invitationCode = generateRandomString(6);
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
try {
|
||||
const userRepo = queryRunner.manager.getRepository(UserEntity);
|
||||
|
||||
const user = await userRepo.findOne({
|
||||
where: {
|
||||
email,
|
||||
project: Not(IsNull()),
|
||||
},
|
||||
});
|
||||
|
||||
if (user) {
|
||||
throw new HttpException(
|
||||
'User already has a project',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
const inviteUser = this.inviteUserRepository.create({
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
jobTitle,
|
||||
phoneNumber,
|
||||
roleType: { uuid: roleUuid },
|
||||
status: UserStatusEnum.INVITED,
|
||||
invitationCode,
|
||||
});
|
||||
|
||||
const invitedUser = await queryRunner.manager.save(inviteUser);
|
||||
|
||||
const spacePromises = spaceUuids.map(async (spaceUuid) => {
|
||||
const inviteUserSpace = this.inviteUserSpaceRepository.create({
|
||||
inviteUser: { uuid: invitedUser.uuid },
|
||||
space: { uuid: spaceUuid },
|
||||
});
|
||||
return queryRunner.manager.save(inviteUserSpace);
|
||||
});
|
||||
|
||||
await Promise.all(spacePromises);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.CREATED,
|
||||
success: true,
|
||||
data: {
|
||||
invitationCode: invitedUser.invitationCode,
|
||||
},
|
||||
message: 'User invited successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
console.error('Error creating user invitation:', error);
|
||||
throw new HttpException(
|
||||
error.message || 'An unexpected error occurred while inviting the user',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user