👔 Prevent duplicate contact method

This commit is contained in:
2025-08-10 01:50:05 +08:00
parent 42af09034c
commit e0107f189d

View File

@@ -88,6 +88,12 @@ public class AccountService(
if (dupeNameCount > 0) if (dupeNameCount > 0)
throw new InvalidOperationException("Account name has already been taken."); throw new InvalidOperationException("Account name has already been taken.");
var dupeEmailCount = await db.AccountContacts
.Where(c => c.Content == email && c.Type == AccountContactType.Email
).CountAsync();
if (dupeEmailCount > 0)
throw new InvalidOperationException("Account email has already been used.");
var account = new Account var account = new Account
{ {
Name = name, Name = name,
@@ -504,6 +510,12 @@ public class AccountService(
public async Task<AccountContact> CreateContactMethod(Account account, AccountContactType type, string content) public async Task<AccountContact> CreateContactMethod(Account account, AccountContactType type, string content)
{ {
var isExists = await db.AccountContacts
.Where(x => x.AccountId == account.Id && x.Type == type && x.Content == content)
.AnyAsync();
if (isExists)
throw new InvalidOperationException("Contact method already exists.");
var contact = new AccountContact var contact = new AccountContact
{ {
Type = type, Type = type,