diff --git a/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/dao/ProfileRepository.kt b/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/dao/ProfileRepository.kt index 030184ac9..c4c512299 100644 --- a/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/dao/ProfileRepository.kt +++ b/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/dao/ProfileRepository.kt @@ -39,7 +39,13 @@ interface ProfileRepository : ReactiveCrudRepository { fun searchUsersBy(userId: String?, mobile: String?, email: String?, firstName: String?, lastName: String?, nationalCode: String?, createDateFrom: LocalDateTime?, createDateTo: LocalDateTime?, pageable: Pageable): Flow? - fun findByUserIdOrEmail(userId: String, email: String): Mono? + @Query(""" + SELECT * FROM profile + WHERE user_id = :userId + OR ( :mobile IS NOT NULL AND mobile = :mobile ) + OR ( :email IS NOT NULL AND lower(email) = lower(:email) ) + """) + fun findByUserIdOrEmailOrMobile(userId: String, email: String? , mobile : String?): Mono? } \ No newline at end of file diff --git a/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/imp/ProfileManagementImp.kt b/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/imp/ProfileManagementImp.kt index 33214e55e..df35d92dc 100644 --- a/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/imp/ProfileManagementImp.kt +++ b/profile/profile-ports/profile-postgres/src/main/kotlin/co/nilin/opex/profile/ports/postgres/imp/ProfileManagementImp.kt @@ -113,7 +113,10 @@ class ProfileManagementImp( } override suspend fun createProfile(data: Profile): Mono { - profileRepository.findByUserIdOrEmail(data.userId!!, data.email!!)?.awaitFirstOrNull()?.let { + if (data.email.isNullOrBlank() && data.mobile.isNullOrBlank()) { + throw OpexError.BadRequest.exception("email and mobile is null or empty") + } + profileRepository.findByUserIdOrEmailOrMobile(data.userId!!, data.email, data.mobile)?.awaitFirstOrNull()?.let { throw OpexError.UserIdAlreadyExists.exception() } ?: run { val profile: ProfileModel = data.convert(ProfileModel::class.java) diff --git a/profile/profile-ports/profile-postgres/src/main/resources/schema.sql b/profile/profile-ports/profile-postgres/src/main/resources/schema.sql index d10b8c7e4..a4eb00a93 100644 --- a/profile/profile-ports/profile-postgres/src/main/resources/schema.sql +++ b/profile/profile-ports/profile-postgres/src/main/resources/schema.sql @@ -292,4 +292,34 @@ $$ ALTER COLUMN gender SET DATA TYPE VARCHAR(50); END IF; END +$$; + +DO +$$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'profile' + AND column_name = 'email') THEN ALTER TABLE profile + ALTER COLUMN email DROP NOT NULL; + END IF; + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'profile' + AND column_name = 'identifier') THEN ALTER TABLE profile + ADD CONSTRAINT unique_identifier UNIQUE (identifier); + END IF; + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'profile' + AND column_name = 'mobile') THEN ALTER TABLE profile + ADD CONSTRAINT unique_mobile UNIQUE (mobile); + END IF; + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'profile_history' + AND column_name = 'email') THEN ALTER TABLE profile_history + ALTER COLUMN email DROP NOT NULL; + END IF; +END $$; \ No newline at end of file