feat: Introduce an admin endpoint to update agent types and enhance user profile retrieval to include agent type details.
This commit is contained in:
@@ -116,4 +116,20 @@ export class UsersController {
|
|||||||
message: 'User status updated successfully',
|
message: 'User status updated successfully',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch(':id/agent-type')
|
||||||
|
@Roles(UserRole.ADMIN)
|
||||||
|
@ApiOperation({ summary: 'Update agent type for an agent user (Admin only)' })
|
||||||
|
@ApiResponse({
|
||||||
|
status: 200,
|
||||||
|
description: 'Agent type updated successfully',
|
||||||
|
})
|
||||||
|
@ApiResponse({ status: 400, description: 'Invalid agent type or user is not an agent' })
|
||||||
|
@ApiResponse({ status: 404, description: 'User not found' })
|
||||||
|
async updateAgentType(
|
||||||
|
@Param('id') id: string,
|
||||||
|
@Body('agentTypeId') agentTypeId: string,
|
||||||
|
) {
|
||||||
|
return this.usersService.updateAgentType(id, agentTypeId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
import { UserStatus, UserRole, Prisma } from '@prisma/client';
|
import { UserStatus, UserRole, Prisma } from '@prisma/client';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
|
|
||||||
type UserWithProfiles = Prisma.UserGetPayload<{
|
type UserWithProfiles = Prisma.UserGetPayload<{
|
||||||
include: { userProfile: true; agentProfile: true };
|
include: { userProfile: true; agentProfile: { include: { agentType: true } } };
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -24,7 +24,11 @@ export class UsersService {
|
|||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
include: {
|
include: {
|
||||||
userProfile: true,
|
userProfile: true,
|
||||||
agentProfile: true,
|
agentProfile: {
|
||||||
|
include: {
|
||||||
|
agentType: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}) as Promise<UserWithProfiles[]>,
|
}) as Promise<UserWithProfiles[]>,
|
||||||
this.prisma.user.count({ where }),
|
this.prisma.user.count({ where }),
|
||||||
@@ -69,7 +73,11 @@ export class UsersService {
|
|||||||
where: { id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
userProfile: true,
|
userProfile: true,
|
||||||
agentProfile: true,
|
agentProfile: {
|
||||||
|
include: {
|
||||||
|
agentType: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})) as UserWithProfiles | null;
|
})) as UserWithProfiles | null;
|
||||||
|
|
||||||
@@ -77,27 +85,60 @@ export class UsersService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the appropriate profile based on role
|
// Base user data
|
||||||
const profile = user.role === UserRole.AGENT ? user.agentProfile : user.userProfile;
|
const baseData = {
|
||||||
|
|
||||||
return {
|
|
||||||
id: user.id,
|
id: user.id,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
role: user.role,
|
role: user.role,
|
||||||
status: user.status,
|
status: user.status,
|
||||||
emailVerified: user.emailVerified,
|
emailVerified: user.emailVerified,
|
||||||
|
emailVerifiedAt: user.emailVerifiedAt,
|
||||||
authProvider: user.authProvider,
|
authProvider: user.authProvider,
|
||||||
createdAt: user.createdAt,
|
createdAt: user.createdAt,
|
||||||
lastLoginAt: user.lastLoginAt,
|
lastLoginAt: user.lastLoginAt,
|
||||||
profile: profile
|
};
|
||||||
|
|
||||||
|
// For agents, return full agent profile
|
||||||
|
if (user.role === UserRole.AGENT && user.agentProfile) {
|
||||||
|
return {
|
||||||
|
...baseData,
|
||||||
|
profile: {
|
||||||
|
firstName: user.agentProfile.firstName,
|
||||||
|
lastName: user.agentProfile.lastName,
|
||||||
|
avatar: user.agentProfile.avatar,
|
||||||
|
phone: user.agentProfile.phone,
|
||||||
|
city: user.agentProfile.city,
|
||||||
|
state: user.agentProfile.state,
|
||||||
|
country: user.agentProfile.country,
|
||||||
|
},
|
||||||
|
agentProfile: {
|
||||||
|
id: user.agentProfile.id,
|
||||||
|
slug: user.agentProfile.slug,
|
||||||
|
bio: user.agentProfile.bio,
|
||||||
|
headline: user.agentProfile.headline,
|
||||||
|
companyName: user.agentProfile.companyName,
|
||||||
|
licenseNumber: user.agentProfile.licenseNumber,
|
||||||
|
yearsOfExperience: user.agentProfile.yearsOfExperience,
|
||||||
|
isProfileComplete: user.agentProfile.isProfileComplete,
|
||||||
|
profileCompleteness: user.agentProfile.profileCompleteness,
|
||||||
|
agentTypeId: user.agentProfile.agentTypeId,
|
||||||
|
agentType: user.agentProfile.agentType,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// For regular users
|
||||||
|
return {
|
||||||
|
...baseData,
|
||||||
|
profile: user.userProfile
|
||||||
? {
|
? {
|
||||||
firstName: profile.firstName,
|
firstName: user.userProfile.firstName,
|
||||||
lastName: profile.lastName,
|
lastName: user.userProfile.lastName,
|
||||||
avatar: profile.avatar,
|
avatar: user.userProfile.avatar,
|
||||||
phone: profile.phone,
|
phone: user.userProfile.phone,
|
||||||
city: profile.city,
|
city: user.userProfile.city,
|
||||||
state: profile.state,
|
state: user.userProfile.state,
|
||||||
country: profile.country,
|
country: user.userProfile.country,
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
};
|
};
|
||||||
@@ -109,4 +150,47 @@ export class UsersService {
|
|||||||
data: { status },
|
data: { status },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateAgentType(userId: string, agentTypeId: 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify agent type exists
|
||||||
|
const agentType = await this.prisma.agentType.findUnique({
|
||||||
|
where: { id: agentTypeId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!agentType) {
|
||||||
|
throw new BadRequestException('Invalid agent type');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update agent profile with new agent type
|
||||||
|
const updatedProfile = await this.prisma.agentProfile.update({
|
||||||
|
where: { id: user.agentProfile.id },
|
||||||
|
data: { agentTypeId },
|
||||||
|
include: { agentType: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: user.id,
|
||||||
|
agentTypeId: updatedProfile.agentTypeId,
|
||||||
|
agentType: updatedProfile.agentType,
|
||||||
|
message: 'Agent type updated successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user