update kanban styling, add more detailed error log for gmail
This commit is contained in:
@@ -98,45 +98,69 @@ public sealed class GmailController : ControllerBase
|
||||
[HttpPost("import")]
|
||||
public async Task<IActionResult> Import([FromBody] ImportGmailMessageRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.JobApplicationId <= 0) return BadRequest("Valid jobApplicationId is required.");
|
||||
if (string.IsNullOrWhiteSpace(request.MessageId)) return BadRequest("MessageId is required.");
|
||||
|
||||
var ownerUserId = GetRequiredOwnerUserId();
|
||||
var job = await _db.JobApplications.Include(x => x.Company).FirstOrDefaultAsync(x => x.Id == request.JobApplicationId, cancellationToken);
|
||||
if (job is null) return NotFound("Job application not found.");
|
||||
|
||||
var existing = await _db.Correspondences.FirstOrDefaultAsync(
|
||||
x => x.JobApplicationId == request.JobApplicationId && x.ExternalMessageId == request.MessageId,
|
||||
cancellationToken);
|
||||
if (existing is not null)
|
||||
try
|
||||
{
|
||||
return Ok(existing);
|
||||
if (request.JobApplicationId <= 0) return BadRequest("Valid jobApplicationId is required.");
|
||||
if (string.IsNullOrWhiteSpace(request.MessageId)) return BadRequest("MessageId is required.");
|
||||
|
||||
var ownerUserId = GetRequiredOwnerUserId();
|
||||
var job = await _db.JobApplications.Include(x => x.Company).FirstOrDefaultAsync(x => x.Id == request.JobApplicationId, cancellationToken);
|
||||
if (job is null) return NotFound("Job application not found.");
|
||||
|
||||
var existing = await _db.Correspondences.FirstOrDefaultAsync(
|
||||
x => x.JobApplicationId == request.JobApplicationId && x.ExternalMessageId == request.MessageId,
|
||||
cancellationToken);
|
||||
if (existing is not null)
|
||||
{
|
||||
return Ok(existing);
|
||||
}
|
||||
|
||||
var detail = await _gmail.GetMessageAsync(ownerUserId, request.MessageId, cancellationToken);
|
||||
var me = await _gmail.GetConnectionAsync(ownerUserId, cancellationToken);
|
||||
var gmailAddress = me?.GmailAddress ?? string.Empty;
|
||||
var isMe = detail.From.Contains(gmailAddress, StringComparison.OrdinalIgnoreCase);
|
||||
var messageDate = detail.Date?.LocalDateTime ?? DateTime.Now;
|
||||
|
||||
var message = new Correspondence
|
||||
{
|
||||
JobApplicationId = request.JobApplicationId,
|
||||
From = isMe ? "Me" : "Company",
|
||||
Subject = string.IsNullOrWhiteSpace(detail.Subject) ? null : detail.Subject.Trim(),
|
||||
Channel = "Email",
|
||||
ExternalMessageId = detail.Id,
|
||||
Content = string.IsNullOrWhiteSpace(detail.BodyText) ? detail.Snippet : detail.BodyText,
|
||||
Date = messageDate,
|
||||
};
|
||||
|
||||
_db.Correspondences.Add(message);
|
||||
if (job.Company is not null)
|
||||
{
|
||||
job.Company.LastContactedAt = messageDate;
|
||||
}
|
||||
|
||||
if (!isMe && (!job.ResponseReceived || job.ResponseDate is null || messageDate < job.ResponseDate.Value))
|
||||
{
|
||||
var oldResponse = $"{job.ResponseReceived}:{job.ResponseDate?.ToString("o")}";
|
||||
job.ResponseReceived = true;
|
||||
job.ResponseDate = messageDate;
|
||||
_db.JobEvents.Add(new JobEvent
|
||||
{
|
||||
JobApplicationId = job.Id,
|
||||
Type = "ReplyReceived",
|
||||
OldValue = oldResponse,
|
||||
NewValue = $"{job.ResponseReceived}:{job.ResponseDate?.ToString("o")}",
|
||||
Note = detail.Subject,
|
||||
At = messageDate
|
||||
});
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
return Ok(message);
|
||||
}
|
||||
|
||||
var detail = await _gmail.GetMessageAsync(ownerUserId, request.MessageId, cancellationToken);
|
||||
var me = await _gmail.GetConnectionAsync(ownerUserId, cancellationToken);
|
||||
var gmailAddress = me?.GmailAddress ?? string.Empty;
|
||||
var isMe = detail.From.Contains(gmailAddress, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
var message = new Correspondence
|
||||
catch (Exception ex)
|
||||
{
|
||||
JobApplicationId = request.JobApplicationId,
|
||||
From = isMe ? "Me" : "Company",
|
||||
Subject = string.IsNullOrWhiteSpace(detail.Subject) ? null : detail.Subject.Trim(),
|
||||
Channel = "Email",
|
||||
ExternalMessageId = detail.Id,
|
||||
Content = string.IsNullOrWhiteSpace(detail.BodyText) ? detail.Snippet : detail.BodyText,
|
||||
Date = detail.Date?.LocalDateTime ?? DateTime.Now,
|
||||
};
|
||||
|
||||
_db.Correspondences.Add(message);
|
||||
if (job.Company is not null)
|
||||
{
|
||||
job.Company.LastContactedAt = DateTime.UtcNow;
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
return Ok(message);
|
||||
}
|
||||
|
||||
private string GetRequiredOwnerUserId()
|
||||
|
||||
Reference in New Issue
Block a user