fixed the invitation status for minglar admin registration
This commit is contained in:
@@ -17,7 +17,7 @@ export const handler = safeHandler(async (
|
||||
): Promise<APIGatewayProxyResult> => {
|
||||
// Parse request body
|
||||
let body: { email?: string };
|
||||
|
||||
|
||||
try {
|
||||
body = event.body ? JSON.parse(event.body) : {};
|
||||
} catch (error) {
|
||||
@@ -35,11 +35,16 @@ export const handler = safeHandler(async (
|
||||
select: { emailAddress: true, id: true, userPassword: true, roleXid: true },
|
||||
});
|
||||
|
||||
if(!user && ![ROLE.MINGLAR_ADMIN, ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER].includes(user.roleXid)){
|
||||
if (!user) {
|
||||
throw new ApiError(403, 'You are not allowed to register directly. Please contact minglar admin.');
|
||||
}
|
||||
|
||||
if (user && user.userPassword) {
|
||||
if (![ROLE.MINGLAR_ADMIN, ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER].includes(user.roleXid)) {
|
||||
throw new ApiError(403, 'You are not allowed to register directly. Please contact minglar admin.');
|
||||
}
|
||||
|
||||
|
||||
if (user.userPassword) {
|
||||
throw new ApiError(404, 'User is already registered. Please login.');
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { hostCompanyDetailsSchema } from '../../../common/utils/validation/host/
|
||||
import { CreateMinglarDto, UpdateMinglarDto } from '../dto/minglar.dto';
|
||||
import { User } from '@prisma/client';
|
||||
import { ROLE } from '@/common/utils/constants/common.constant';
|
||||
import { MINGLAR_INVITATION_STATUS } from '@/common/utils/constants/minglar.constant';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -14,35 +15,53 @@ export class MinglarService {
|
||||
constructor(private prisma: PrismaService) { }
|
||||
|
||||
async createPassword(user_xid: number, password: string): Promise<boolean> {
|
||||
// Find user by id
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: user_xid },
|
||||
select: { id: true, emailAddress: true, userPassword: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new ApiError(404, 'User not found');
|
||||
}
|
||||
|
||||
// Check if password already exists
|
||||
if (user.userPassword) {
|
||||
throw new ApiError(400, 'Password already exists. Use update password instead.');
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
const saltRounds = parseInt(process.env.SALT_ROUNDS || '10', 10);
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
|
||||
// Update user with hashed password
|
||||
await this.prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { userPassword: hashedPassword },
|
||||
});
|
||||
|
||||
return true;
|
||||
// Find user by id
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: user_xid },
|
||||
select: { id: true, emailAddress: true, userPassword: true },
|
||||
});
|
||||
|
||||
const invitationDetails = await this.prisma.inviteDetails.findMany({
|
||||
where: {
|
||||
userXid: user.id,
|
||||
isActive: true,
|
||||
isMinglarInvitation: true,
|
||||
},
|
||||
});
|
||||
if (invitationDetails.length > 0) {
|
||||
await this.prisma.inviteDetails.update({
|
||||
where: { id: invitationDetails[0].id },
|
||||
data: {
|
||||
invitation_status: MINGLAR_INVITATION_STATUS.ACCEPTED,
|
||||
accepted_on: new Date(),
|
||||
is_accepted: true,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async createHost(data: CreateMinglarDto) {
|
||||
if (!user) {
|
||||
throw new ApiError(404, 'User not found');
|
||||
}
|
||||
|
||||
// Check if password already exists
|
||||
if (user.userPassword) {
|
||||
throw new ApiError(400, 'Password already exists. Use update password instead.');
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
const saltRounds = parseInt(process.env.SALT_ROUNDS || '10', 10);
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
|
||||
// Update user with hashed password
|
||||
await this.prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { userPassword: hashedPassword },
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async createHost(data: CreateMinglarDto) {
|
||||
return this.prisma.user.create({ data });
|
||||
}
|
||||
|
||||
@@ -167,7 +186,8 @@ export class MinglarService {
|
||||
invited_on: new Date(),
|
||||
is_accepted: false,
|
||||
invitation_status: invitationStatus,
|
||||
isActive: true
|
||||
isActive: true,
|
||||
isMinglarInvitation: true,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -356,5 +376,5 @@ export class MinglarService {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user