feat: implement profile data validation and upsert functionality for company and personal profiles

This commit is contained in:
seaznCode 2026-01-19 19:32:03 +01:00
parent 2296b521db
commit 5cef174e7f
4 changed files with 105 additions and 22 deletions

View File

@ -148,25 +148,38 @@ class CompanyUserRepository {
registrationNumber, registrationNumber,
businessType, businessType,
iban, iban,
accountHolderName accountHolderName,
companyName
} = profileData; } = profileData;
await conn.query( await conn.query(
`UPDATE company_profiles SET `INSERT INTO company_profiles (
address = ?, zip_code = ?, city = ?, country = ?, branch = ?, number_of_employees = ?, user_id, company_name, registration_number, address, zip_code, city, country,
registration_number = ?, business_type = ?, account_holder_name = ? branch, number_of_employees, business_type, account_holder_name
WHERE user_id = ?`, ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
company_name = VALUES(company_name),
registration_number = VALUES(registration_number),
address = VALUES(address),
zip_code = VALUES(zip_code),
city = VALUES(city),
country = VALUES(country),
branch = VALUES(branch),
number_of_employees = VALUES(number_of_employees),
business_type = VALUES(business_type),
account_holder_name = VALUES(account_holder_name)`,
[ [
address, userId,
zip_code, companyName,
city, registrationNumber || null,
country, // Add country to parameter list address || null,
branch, zip_code || null,
numberOfEmployees, city || null,
registrationNumber, country || null,
businessType, branch || null,
accountHolderName, numberOfEmployees || null,
userId businessType || null,
accountHolderName || null
] ]
); );

View File

@ -108,13 +108,35 @@ class PersonalUserRepository {
iban // Added field iban // Added field
} = profileData; } = profileData;
const firstName =
profileData.firstName ||
profileData.first_name ||
'';
const lastName =
profileData.lastName ||
profileData.last_name ||
'';
await conn.query( await conn.query(
`UPDATE personal_profiles SET `INSERT INTO personal_profiles (
date_of_birth = ?, nationality = ?, address = ?, zip_code = ?, city = ?, country = ?, user_id, first_name, last_name, date_of_birth, nationality, address, zip_code, city, country,
phone_secondary = ?, emergency_contact_name = ?, emergency_contact_phone = ?, phone_secondary, emergency_contact_name, emergency_contact_phone, account_holder_name
account_holder_name = ? ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
WHERE user_id = ?`, ON DUPLICATE KEY UPDATE
date_of_birth = VALUES(date_of_birth),
nationality = VALUES(nationality),
address = VALUES(address),
zip_code = VALUES(zip_code),
city = VALUES(city),
country = VALUES(country),
phone_secondary = VALUES(phone_secondary),
emergency_contact_name = VALUES(emergency_contact_name),
emergency_contact_phone = VALUES(emergency_contact_phone),
account_holder_name = VALUES(account_holder_name)`,
[ [
userId,
firstName,
lastName,
dateOfBirth, dateOfBirth,
nationality, nationality,
address, address,
@ -124,8 +146,7 @@ class PersonalUserRepository {
phoneSecondary, phoneSecondary,
emergencyContactName, emergencyContactName,
emergencyContactPhone, emergencyContactPhone,
accountHolderName, accountHolderName
userId
] ]
); );

View File

@ -5,6 +5,28 @@ class CompanyProfileService {
static async completeProfile(userId, profileData, unitOfWork) { static async completeProfile(userId, profileData, unitOfWork) {
logger.info('CompanyProfileService.completeProfile:start', { userId }); logger.info('CompanyProfileService.completeProfile:start', { userId });
try { try {
const conn = unitOfWork.connection;
const required = [
{ key: 'companyName', label: 'Company name' },
{ key: 'address', label: 'Street & number' },
{ key: 'zip_code', label: 'Postal code' },
{ key: 'city', label: 'City' },
{ key: 'country', label: 'Country' },
{ key: 'registrationNumber', label: 'VAT' },
{ key: 'accountHolderName', label: 'Account holder' },
{ key: 'iban', label: 'IBAN' }
];
for (const field of required) {
const value = (profileData[field.key] || '').toString().trim();
if (!value) {
throw new Error(`${field.label} is required.`);
}
}
profileData.companyName = (profileData.companyName || '').toString().trim();
// Pass all profileData including country to repository // Pass all profileData including country to repository
const repo = new CompanyUserRepository(unitOfWork); const repo = new CompanyUserRepository(unitOfWork);
await repo.updateProfileAndMarkCompleted(userId, profileData); await repo.updateProfileAndMarkCompleted(userId, profileData);

View File

@ -5,6 +5,33 @@ class PersonalProfileService {
static async completeProfile(userId, profileData, unitOfWork) { static async completeProfile(userId, profileData, unitOfWork) {
logger.info('PersonalProfileService.completeProfile:start', { userId }); logger.info('PersonalProfileService.completeProfile:start', { userId });
try { try {
const required = [
{ key: 'dateOfBirth', label: 'Date of birth' },
{ key: 'nationality', label: 'Nationality' },
{ key: 'address', label: 'Street & house number' },
{ key: 'zip_code', label: 'Postal code' },
{ key: 'city', label: 'City' },
{ key: 'country', label: 'Country' },
{ key: 'accountHolderName', label: 'Account holder' },
{ key: 'iban', label: 'IBAN' }
];
for (const field of required) {
const value = (profileData[field.key] || '').toString().trim();
if (!value) {
throw new Error(`${field.label} is required.`);
}
}
profileData.dateOfBirth = profileData.dateOfBirth.toString().trim();
profileData.nationality = profileData.nationality.toString().trim();
profileData.address = profileData.address.toString().trim();
profileData.zip_code = profileData.zip_code.toString().trim();
profileData.city = profileData.city.toString().trim();
profileData.country = profileData.country.toString().trim();
profileData.accountHolderName = profileData.accountHolderName.toString().trim();
profileData.iban = profileData.iban.toString().trim();
const repo = new PersonalUserRepository(unitOfWork); const repo = new PersonalUserRepository(unitOfWork);
await repo.updateProfileAndMarkCompleted(userId, profileData); await repo.updateProfileAndMarkCompleted(userId, profileData);
logger.info('PersonalProfileService.completeProfile:profile_completed', { userId }); logger.info('PersonalProfileService.completeProfile:profile_completed', { userId });