feat: Synchronize agent phone numbers between profile and field values, and add optional service areas to agent profile DTO.

This commit is contained in:
pradeepkumar
2026-02-09 02:04:12 +05:30
parent bbbd83084a
commit 6bc0b41e1f
2 changed files with 85 additions and 3 deletions

View File

@@ -112,9 +112,16 @@ export class AgentsService {
throw new NotFoundException('Agent profile not found');
}
// Filter out fields that don't exist in the database schema
// serviceAreas is accepted by DTO but stored in field values, not direct column
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { serviceAreas, ...profileData } = dto as UpdateAgentProfileDto & {
serviceAreas?: string[];
};
const updatedProfile = await this.prisma.agentProfile.update({
where: { userId },
data: dto,
data: profileData,
include: {
user: {
select: {
@@ -127,9 +134,50 @@ export class AgentsService {
},
});
// Sync phone to field values if phone was updated
// This keeps AgentProfile.phone and AgentProfileFieldValue in sync
if (dto.phone !== undefined) {
await this.syncPhoneToFieldValues(profile.id, dto.phone);
}
return updatedProfile;
}
/**
* Sync phone from AgentProfile to AgentProfileFieldValue
* This ensures both storage locations have the same phone value
*/
private async syncPhoneToFieldValues(
agentProfileId: string,
phone: string | null,
) {
// Find any phone-related field definitions
const phoneField = await this.prisma.profileField.findFirst({
where: {
slug: { in: ['phone', 'phone_number', 'cell_number', 'office_number'] },
},
});
if (phoneField) {
await this.prisma.agentProfileFieldValue.upsert({
where: {
agentProfileId_fieldId: {
agentProfileId,
fieldId: phoneField.id,
},
},
create: {
agentProfileId,
fieldId: phoneField.id,
textValue: phone,
},
update: {
textValue: phone,
},
});
}
}
async searchAgents(dto: SearchAgentsDto) {
const {
search,
@@ -574,6 +622,26 @@ export class AgentsService {
results.push(result);
}
// Sync phone from field values to AgentProfile.phone
// This keeps both storage locations in sync
const phoneSlugs = ['phone', 'phone_number', 'cell_number', 'office_number'];
const phoneFieldValue = dto.fieldValues.find((fv) =>
phoneSlugs.includes(fv.fieldSlug),
);
if (phoneFieldValue !== undefined) {
const phoneValue = phoneFieldValue.value;
await this.prisma.agentProfile.update({
where: { id: profile.id },
data: {
phone:
phoneValue === '' || phoneValue === null
? null
: String(phoneValue),
},
});
}
return {
message: 'Field values saved successfully',
savedCount: results.length,