feat: implement service area field syncing and move privacy visibility filtering to application layer
This commit is contained in:
@@ -97,7 +97,28 @@ export class AgentsService {
|
|||||||
throw new NotFoundException('Agent profile not found');
|
throw new NotFoundException('Agent profile not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
return profile;
|
// Fetch serviceAreas from field values (not a direct column)
|
||||||
|
const serviceAreasField = await this.prisma.profileField.findFirst({
|
||||||
|
where: { slug: { in: ['service_areas', 'serviceAreas', 'city'] } },
|
||||||
|
});
|
||||||
|
let serviceAreas: string[] = [];
|
||||||
|
if (serviceAreasField) {
|
||||||
|
const fv = await this.prisma.agentProfileFieldValue.findUnique({
|
||||||
|
where: {
|
||||||
|
agentProfileId_fieldId: {
|
||||||
|
agentProfileId: profile.id,
|
||||||
|
fieldId: serviceAreasField.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (fv?.jsonValue && Array.isArray(fv.jsonValue)) {
|
||||||
|
serviceAreas = (fv.jsonValue as string[]);
|
||||||
|
} else if (fv?.textValue) {
|
||||||
|
serviceAreas = [fv.textValue];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...profile, serviceAreas };
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProfileById(profileId: string, requestingUserId?: string) {
|
async getProfileById(profileId: string, requestingUserId?: string) {
|
||||||
@@ -183,6 +204,11 @@ export class AgentsService {
|
|||||||
await this.syncPhoneToFieldValues(profile.id, dto.phone);
|
await this.syncPhoneToFieldValues(profile.id, dto.phone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sync serviceAreas to field values if provided
|
||||||
|
if (serviceAreas && serviceAreas.length > 0) {
|
||||||
|
await this.syncServiceAreasToFieldValues(profile.id, serviceAreas);
|
||||||
|
}
|
||||||
|
|
||||||
return updatedProfile;
|
return updatedProfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,6 +247,39 @@ export class AgentsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync serviceAreas to AgentProfileFieldValue
|
||||||
|
*/
|
||||||
|
private async syncServiceAreasToFieldValues(
|
||||||
|
agentProfileId: string,
|
||||||
|
serviceAreas: string[],
|
||||||
|
) {
|
||||||
|
const serviceAreasField = await this.prisma.profileField.findFirst({
|
||||||
|
where: {
|
||||||
|
slug: { in: ['service_areas', 'serviceAreas', 'city'] },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (serviceAreasField) {
|
||||||
|
await this.prisma.agentProfileFieldValue.upsert({
|
||||||
|
where: {
|
||||||
|
agentProfileId_fieldId: {
|
||||||
|
agentProfileId,
|
||||||
|
fieldId: serviceAreasField.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
agentProfileId,
|
||||||
|
fieldId: serviceAreasField.id,
|
||||||
|
jsonValue: serviceAreas,
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
jsonValue: serviceAreas,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async searchAgents(dto: SearchAgentsDto) {
|
async searchAgents(dto: SearchAgentsDto) {
|
||||||
const {
|
const {
|
||||||
search,
|
search,
|
||||||
@@ -240,17 +299,8 @@ export class AgentsService {
|
|||||||
const skip = (page - 1) * limit;
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
// Build where clause — only show admin-approved profiles in search
|
// Build where clause — only show admin-approved profiles in search
|
||||||
// Exclude agents who set profile_visibility to 'private'
|
|
||||||
const where: Prisma.AgentProfileWhereInput = {
|
const where: Prisma.AgentProfileWhereInput = {
|
||||||
verificationStatus: 'APPROVED',
|
verificationStatus: 'APPROVED',
|
||||||
NOT: {
|
|
||||||
user: {
|
|
||||||
privacyPreferences: {
|
|
||||||
path: ['privacySettings', 'profile_visibility'],
|
|
||||||
equals: 'private',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
@@ -500,6 +550,7 @@ export class AgentsService {
|
|||||||
id: true,
|
id: true,
|
||||||
email: true,
|
email: true,
|
||||||
avatar: true,
|
avatar: true,
|
||||||
|
privacyPreferences: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
agentType: true,
|
agentType: true,
|
||||||
@@ -519,8 +570,14 @@ export class AgentsService {
|
|||||||
this.prisma.agentProfile.count({ where }),
|
this.prisma.agentProfile.count({ where }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Filter out private profiles (safe for null privacyPreferences)
|
||||||
|
const visibleAgents = agents.filter((agent: any) => {
|
||||||
|
const visibility = this.getProfileVisibility(agent.user?.privacyPreferences);
|
||||||
|
return visibility !== 'private';
|
||||||
|
});
|
||||||
|
|
||||||
// Compute match score for each agent based on search context
|
// Compute match score for each agent based on search context
|
||||||
const agentsWithScore = agents.map((agent: any) => {
|
const agentsWithScore = visibleAgents.map((agent: any) => {
|
||||||
let score = 0;
|
let score = 0;
|
||||||
let maxScore = 0;
|
let maxScore = 0;
|
||||||
|
|
||||||
@@ -592,11 +649,14 @@ export class AgentsService {
|
|||||||
const rawPercentage = maxScore > 0 ? Math.round((score / maxScore) * 100) : 50;
|
const rawPercentage = maxScore > 0 ? Math.round((score / maxScore) * 100) : 50;
|
||||||
const matchPercentage = Math.max(50, Math.min(99, rawPercentage));
|
const matchPercentage = Math.max(50, Math.min(99, rawPercentage));
|
||||||
|
|
||||||
return { ...agent, matchPercentage };
|
// Strip privacyPreferences from response
|
||||||
|
const { user, ...agentWithoutUser } = agent;
|
||||||
|
const { privacyPreferences, ...userWithoutPrefs } = user || {};
|
||||||
|
return { ...agentWithoutUser, user: userWithoutPrefs, matchPercentage };
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort by match percentage (highest first)
|
// Sort by match percentage (highest first)
|
||||||
agentsWithScore.sort((a, b) => b.matchPercentage - a.matchPercentage);
|
agentsWithScore.sort((a: any, b: any) => b.matchPercentage - a.matchPercentage);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: agentsWithScore,
|
data: agentsWithScore,
|
||||||
@@ -615,14 +675,6 @@ export class AgentsService {
|
|||||||
isVerified: true,
|
isVerified: true,
|
||||||
isFeatured: true,
|
isFeatured: true,
|
||||||
verificationStatus: 'APPROVED',
|
verificationStatus: 'APPROVED',
|
||||||
NOT: {
|
|
||||||
user: {
|
|
||||||
privacyPreferences: {
|
|
||||||
path: ['privacySettings', 'profile_visibility'],
|
|
||||||
equals: 'private',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
take: limit,
|
take: limit,
|
||||||
orderBy: [{ averageRating: 'desc' }, { totalReviews: 'desc' }],
|
orderBy: [{ averageRating: 'desc' }, { totalReviews: 'desc' }],
|
||||||
@@ -647,14 +699,6 @@ export class AgentsService {
|
|||||||
isVerified: true,
|
isVerified: true,
|
||||||
totalReviews: { gte: 5 },
|
totalReviews: { gte: 5 },
|
||||||
verificationStatus: 'APPROVED',
|
verificationStatus: 'APPROVED',
|
||||||
NOT: {
|
|
||||||
user: {
|
|
||||||
privacyPreferences: {
|
|
||||||
path: ['privacySettings', 'profile_visibility'],
|
|
||||||
equals: 'private',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
take: limit,
|
take: limit,
|
||||||
orderBy: [{ averageRating: 'desc' }, { totalReviews: 'desc' }],
|
orderBy: [{ averageRating: 'desc' }, { totalReviews: 'desc' }],
|
||||||
|
|||||||
Reference in New Issue
Block a user