🐛 Fixes in bot

This commit is contained in:
2025-08-23 14:20:21 +08:00
parent 5d7429a416
commit f759b19bcb
4 changed files with 48 additions and 23 deletions

View File

@@ -146,13 +146,17 @@ public class BotAccountController(
if (project is null) if (project is null)
return NotFound("Project not found or you don't have access"); return NotFound("Project not found or you don't have access");
var now = SystemClock.Instance.GetCurrentInstant();
var accountId = Guid.NewGuid();
var account = new Account() var account = new Account()
{ {
Id = accountId.ToString(),
Name = createRequest.Name, Name = createRequest.Name,
Nick = createRequest.Nick, Nick = createRequest.Nick,
Language = createRequest.Language, Language = createRequest.Language,
Profile = new AccountProfile() Profile = new AccountProfile()
{ {
Id = Guid.NewGuid().ToString(),
Bio = createRequest.Bio, Bio = createRequest.Bio,
Gender = createRequest.Gender, Gender = createRequest.Gender,
FirstName = createRequest.FirstName, FirstName = createRequest.FirstName,
@@ -162,14 +166,23 @@ public class BotAccountController(
Pronouns = createRequest.Pronouns, Pronouns = createRequest.Pronouns,
Location = createRequest.Location, Location = createRequest.Location,
Birthday = createRequest.Birthday?.ToTimestamp(), Birthday = createRequest.Birthday?.ToTimestamp(),
Picture = new CloudFile() { Id = createRequest.PictureId }, AccountId = accountId.ToString(),
Background = new CloudFile() { Id = createRequest.BackgroundId } CreatedAt = now.ToTimestamp(),
} UpdatedAt = now.ToTimestamp()
},
CreatedAt = now.ToTimestamp(),
UpdatedAt = now.ToTimestamp()
}; };
try try
{ {
var bot = await botService.CreateBotAsync(project, createRequest.Slug, account); var bot = await botService.CreateBotAsync(
project,
createRequest.Slug,
account,
createRequest.PictureId,
createRequest.BackgroundId
);
return Ok(bot); return Ok(bot);
} }
catch (Exception ex) catch (Exception ex)
@@ -220,15 +233,14 @@ public class BotAccountController(
if (request.Pronouns is not null) botAccount.Profile.Pronouns = request.Pronouns; if (request.Pronouns is not null) botAccount.Profile.Pronouns = request.Pronouns;
if (request.Location is not null) botAccount.Profile.Location = request.Location; if (request.Location is not null) botAccount.Profile.Location = request.Location;
if (request.Birthday is not null) botAccount.Profile.Birthday = request.Birthday?.ToTimestamp(); if (request.Birthday is not null) botAccount.Profile.Birthday = request.Birthday?.ToTimestamp();
if (request.PictureId is not null) botAccount.Profile.Picture = new CloudFile() { Id = request.PictureId };
if (request.BackgroundId is not null)
botAccount.Profile.Background = new CloudFile() { Id = request.BackgroundId };
try try
{ {
var updatedBot = await botService.UpdateBotAsync( var updatedBot = await botService.UpdateBotAsync(
bot, bot,
botAccount, botAccount,
request.PictureId,
request.BackgroundId,
request.Slug, request.Slug,
request.IsActive request.IsActive
); );

View File

@@ -28,23 +28,30 @@ public class BotAccountService(
.ToListAsync(); .ToListAsync();
} }
public async Task<BotAccount> CreateBotAsync(DevProject project, string slug, Account account) public async Task<BotAccount> CreateBotAsync(
DevProject project,
string slug,
Account account,
string? pictureId,
string? backgroundId
)
{ {
// First, check if a bot with this slug already exists in this project // First, check if a bot with this slug already exists in this project
var existingBot = await db.BotAccounts var existingBot = await db.BotAccounts
.FirstOrDefaultAsync(b => b.ProjectId == project.Id && b.Slug == slug); .FirstOrDefaultAsync(b => b.ProjectId == project.Id && b.Slug == slug);
if (existingBot != null) if (existingBot != null)
{
throw new InvalidOperationException("A bot with this slug already exists in this project."); throw new InvalidOperationException("A bot with this slug already exists in this project.");
}
try try
{ {
var automatedId = Guid.NewGuid();
var createRequest = new CreateBotAccountRequest var createRequest = new CreateBotAccountRequest
{ {
AutomatedId = Guid.NewGuid().ToString(), AutomatedId = automatedId.ToString(),
Account = account Account = account,
PictureId = pictureId,
BackgroundId = backgroundId
}; };
var createResponse = await accountReceiver.CreateBotAccountAsync(createRequest); var createResponse = await accountReceiver.CreateBotAccountAsync(createRequest);
@@ -53,7 +60,7 @@ public class BotAccountService(
// Then create the local bot account // Then create the local bot account
var bot = new BotAccount var bot = new BotAccount
{ {
Id = Guid.Parse(botAccount.AutomatedId), Id = automatedId,
Slug = slug, Slug = slug,
ProjectId = project.Id, ProjectId = project.Id,
Project = project, Project = project,
@@ -82,8 +89,13 @@ public class BotAccountService(
} }
} }
public async Task<BotAccount> UpdateBotAsync(BotAccount bot, Account account, string? slug = null, public async Task<BotAccount> UpdateBotAsync(BotAccount bot,
bool? isActive = null) Account account,
string? pictureId,
string? backgroundId,
string? slug = null,
bool? isActive = null
)
{ {
var updated = false; var updated = false;
if (slug != null && bot.Slug != slug) if (slug != null && bot.Slug != slug)
@@ -106,7 +118,9 @@ public class BotAccountService(
var updateRequest = new UpdateBotAccountRequest var updateRequest = new UpdateBotAccountRequest
{ {
AutomatedId = bot.Id.ToString(), AutomatedId = bot.Id.ToString(),
Account = account Account = account,
PictureId = pictureId,
BackgroundId = backgroundId
}; };
var updateResponse = await accountReceiver.UpdateBotAccountAsync(updateRequest); var updateResponse = await accountReceiver.UpdateBotAccountAsync(updateRequest);

View File

@@ -82,11 +82,10 @@ public class Account : ModelBase
: null, : null,
CreatedAt = proto.CreatedAt.ToInstant(), CreatedAt = proto.CreatedAt.ToInstant(),
UpdatedAt = proto.UpdatedAt.ToInstant(), UpdatedAt = proto.UpdatedAt.ToInstant(),
AutomatedId = proto.AutomatedId is not null ? Guid.Parse(proto.AutomatedId) : null AutomatedId = proto.AutomatedId is not null ? Guid.Parse(proto.AutomatedId) : null,
Profile = AccountProfile.FromProtoValue(proto.Profile)
}; };
account.Profile = AccountProfile.FromProtoValue(proto.Profile);
foreach (var contactProto in proto.Contacts) foreach (var contactProto in proto.Contacts)
account.Contacts.Add(AccountContact.FromProtoValue(contactProto)); account.Contacts.Add(AccountContact.FromProtoValue(contactProto));

View File

@@ -33,7 +33,7 @@ message Account {
google.protobuf.Timestamp created_at = 14; google.protobuf.Timestamp created_at = 14;
google.protobuf.Timestamp updated_at = 15; google.protobuf.Timestamp updated_at = 15;
optional string automated_id = 17; google.protobuf.StringValue automated_id = 17;
} }
// Enum for status attitude // Enum for status attitude