feat: Implement agent verification system with a new status enum, admin update functionality, and document retrieval.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { UserStatus, UserRole, Prisma } from '@prisma/client';
|
||||
import { UserStatus, UserRole, Prisma, VerificationStatus } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
type UserWithProfiles = Prisma.UserGetPayload<{
|
||||
@@ -123,6 +123,12 @@ export class UsersService {
|
||||
profileCompleteness: user.agentProfile.profileCompleteness,
|
||||
agentTypeId: user.agentProfile.agentTypeId,
|
||||
agentType: user.agentProfile.agentType,
|
||||
// Verification fields
|
||||
isVerified: user.agentProfile.isVerified,
|
||||
verificationStatus: user.agentProfile.verificationStatus,
|
||||
verificationNote: user.agentProfile.verificationNote,
|
||||
verifiedAt: user.agentProfile.verifiedAt,
|
||||
verifiedBy: user.agentProfile.verifiedBy,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -193,4 +199,106 @@ export class UsersService {
|
||||
message: 'Agent type updated successfully',
|
||||
};
|
||||
}
|
||||
|
||||
async getVerificationDocuments(userId: string) {
|
||||
// Get user to verify they are an agent
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { agentProfile: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
if (user.role !== UserRole.AGENT) {
|
||||
throw new BadRequestException('User is not an agent');
|
||||
}
|
||||
|
||||
if (!user.agentProfile) {
|
||||
throw new NotFoundException('Agent profile not found');
|
||||
}
|
||||
|
||||
// Find the upload-documents section field
|
||||
const field = await this.prisma.profileField.findFirst({
|
||||
where: {
|
||||
section: { slug: 'upload-documents' },
|
||||
slug: 'documents',
|
||||
},
|
||||
});
|
||||
|
||||
if (!field) return [];
|
||||
|
||||
const fieldValue = await this.prisma.agentProfileFieldValue.findUnique({
|
||||
where: {
|
||||
agentProfileId_fieldId: {
|
||||
agentProfileId: user.agentProfile.id,
|
||||
fieldId: field.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return fieldValue?.jsonValue || [];
|
||||
}
|
||||
|
||||
async updateVerification(
|
||||
userId: string,
|
||||
data: {
|
||||
status: VerificationStatus;
|
||||
note?: string;
|
||||
},
|
||||
adminId: string,
|
||||
) {
|
||||
// Get user to verify they are an agent
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { agentProfile: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
if (user.role !== UserRole.AGENT) {
|
||||
throw new BadRequestException('User is not an agent');
|
||||
}
|
||||
|
||||
if (!user.agentProfile) {
|
||||
throw new NotFoundException('Agent profile not found');
|
||||
}
|
||||
|
||||
const updateData: Prisma.AgentProfileUpdateInput = {
|
||||
verificationStatus: data.status,
|
||||
verificationNote: data.note || null,
|
||||
};
|
||||
|
||||
if (data.status === VerificationStatus.APPROVED) {
|
||||
updateData.isVerified = true;
|
||||
updateData.verifiedAt = new Date();
|
||||
updateData.verifiedBy = adminId;
|
||||
} else if (data.status === VerificationStatus.REJECTED) {
|
||||
updateData.isVerified = false;
|
||||
updateData.verifiedAt = null;
|
||||
updateData.verifiedBy = null;
|
||||
}
|
||||
|
||||
const updatedProfile = await this.prisma.agentProfile.update({
|
||||
where: { id: user.agentProfile.id },
|
||||
data: updateData,
|
||||
include: {
|
||||
agentType: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
agentProfileId: updatedProfile.id,
|
||||
isVerified: updatedProfile.isVerified,
|
||||
verificationStatus: updatedProfile.verificationStatus,
|
||||
verificationNote: updatedProfile.verificationNote,
|
||||
verifiedAt: updatedProfile.verifiedAt,
|
||||
verifiedBy: updatedProfile.verifiedBy,
|
||||
message: `Verification ${data.status.toLowerCase()} successfully`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user