From 5cef174e7f31f217a10267c54b64515ed51196d0 Mon Sep 17 00:00:00 2001 From: seaznCode Date: Mon, 19 Jan 2026 19:32:03 +0100 Subject: [PATCH] feat: implement profile data validation and upsert functionality for company and personal profiles --- .../user/company/CompanyUserRepository.js | 43 ++++++++++++------- .../user/personal/PersonalUserRepository.js | 35 ++++++++++++--- .../profile/company/CompanyProfileService.js | 22 ++++++++++ .../personal/PersonalProfileService.js | 27 ++++++++++++ 4 files changed, 105 insertions(+), 22 deletions(-) diff --git a/repositories/user/company/CompanyUserRepository.js b/repositories/user/company/CompanyUserRepository.js index 9abec54..2ce7dd3 100644 --- a/repositories/user/company/CompanyUserRepository.js +++ b/repositories/user/company/CompanyUserRepository.js @@ -148,25 +148,38 @@ class CompanyUserRepository { registrationNumber, businessType, iban, - accountHolderName + accountHolderName, + companyName } = profileData; await conn.query( - `UPDATE company_profiles SET - address = ?, zip_code = ?, city = ?, country = ?, branch = ?, number_of_employees = ?, - registration_number = ?, business_type = ?, account_holder_name = ? - WHERE user_id = ?`, + `INSERT INTO company_profiles ( + user_id, company_name, registration_number, address, zip_code, city, country, + branch, number_of_employees, business_type, account_holder_name + ) 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, - zip_code, - city, - country, // Add country to parameter list - branch, - numberOfEmployees, - registrationNumber, - businessType, - accountHolderName, - userId + userId, + companyName, + registrationNumber || null, + address || null, + zip_code || null, + city || null, + country || null, + branch || null, + numberOfEmployees || null, + businessType || null, + accountHolderName || null ] ); diff --git a/repositories/user/personal/PersonalUserRepository.js b/repositories/user/personal/PersonalUserRepository.js index 1c09598..b9e7e50 100644 --- a/repositories/user/personal/PersonalUserRepository.js +++ b/repositories/user/personal/PersonalUserRepository.js @@ -108,13 +108,35 @@ class PersonalUserRepository { iban // Added field } = profileData; + const firstName = + profileData.firstName || + profileData.first_name || + ''; + const lastName = + profileData.lastName || + profileData.last_name || + ''; + await conn.query( - `UPDATE personal_profiles SET - date_of_birth = ?, nationality = ?, address = ?, zip_code = ?, city = ?, country = ?, - phone_secondary = ?, emergency_contact_name = ?, emergency_contact_phone = ?, - account_holder_name = ? - WHERE user_id = ?`, + `INSERT INTO personal_profiles ( + user_id, first_name, last_name, date_of_birth, nationality, address, zip_code, city, country, + phone_secondary, emergency_contact_name, emergency_contact_phone, account_holder_name + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + 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, nationality, address, @@ -124,8 +146,7 @@ class PersonalUserRepository { phoneSecondary, emergencyContactName, emergencyContactPhone, - accountHolderName, - userId + accountHolderName ] ); diff --git a/services/profile/company/CompanyProfileService.js b/services/profile/company/CompanyProfileService.js index 2b39456..cf07b82 100644 --- a/services/profile/company/CompanyProfileService.js +++ b/services/profile/company/CompanyProfileService.js @@ -5,6 +5,28 @@ class CompanyProfileService { static async completeProfile(userId, profileData, unitOfWork) { logger.info('CompanyProfileService.completeProfile:start', { userId }); 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 const repo = new CompanyUserRepository(unitOfWork); await repo.updateProfileAndMarkCompleted(userId, profileData); diff --git a/services/profile/personal/PersonalProfileService.js b/services/profile/personal/PersonalProfileService.js index 9ab6c85..023943f 100644 --- a/services/profile/personal/PersonalProfileService.js +++ b/services/profile/personal/PersonalProfileService.js @@ -5,6 +5,33 @@ class PersonalProfileService { static async completeProfile(userId, profileData, unitOfWork) { logger.info('PersonalProfileService.completeProfile:start', { userId }); 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); await repo.updateProfileAndMarkCompleted(userId, profileData); logger.info('PersonalProfileService.completeProfile:profile_completed', { userId });