no message

This commit is contained in:
Kittapath 2024-05-21 14:31:29 +07:00
parent 2a17eff17d
commit fb6a83b36a
11 changed files with 17862 additions and 70 deletions

View file

@ -41,17 +41,17 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
{ {
try try
{ {
var profile = await _dbContext.Set<Profile>() // var profile = await _dbContext.Set<Profile>()
.FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); // .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!));
if (profile == null) // if (profile == null)
{ // {
return new List<NotificationResponse>(); // return new List<NotificationResponse>();
// throw new Exception(GlobalMessages.DataNotFound); // // throw new Exception(GlobalMessages.DataNotFound);
} // }
var data_search = await _dbContext.Set<Notification>() var data_search = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id) .Where(x => x.KeycloakUserId == UserId)
.Where(x => x.DeleteDate == null) .Where(x => x.DeleteDate == null)
.OrderByDescending(x => x.ReceiveDate) .OrderByDescending(x => x.ReceiveDate)
.ToListAsync(); .ToListAsync();
@ -64,6 +64,7 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
Id = x.Id, Id = x.Id,
Body = x.Body, Body = x.Body,
ReceiverUserId = x.ReceiverUserId, ReceiverUserId = x.ReceiverUserId,
KeycloakUserId = x.KeycloakUserId,
IsOpen = x.IsOpen, IsOpen = x.IsOpen,
Type = x.Type, Type = x.Type,
ReceiveDate = x.ReceiveDate, ReceiveDate = x.ReceiveDate,
@ -72,7 +73,7 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
}).ToList(); }).ToList();
var data_opens = await _dbContext.Set<Notification>() var data_opens = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id) .Where(x => x.KeycloakUserId == UserId)
.Where(x => x.DeleteDate == null) .Where(x => x.DeleteDate == null)
.OrderByDescending(x => x.ReceiveDate) .OrderByDescending(x => x.ReceiveDate)
.Skip((page - 1) * pageSize) .Skip((page - 1) * pageSize)
@ -99,16 +100,16 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
{ {
try try
{ {
var profile = await _dbContext.Set<Profile>() // var profile = await _dbContext.Set<Profile>()
.FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); // .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!));
if (profile == null) // if (profile == null)
{ // {
return 0; // return 0;
} // }
var data_search = await _dbContext.Set<Notification>() var data_search = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id) .Where(x => x.KeycloakUserId == UserId)
.Where(x => x.DeleteDate == null) .Where(x => x.DeleteDate == null)
.Where(x => x.IsOpen == false) .Where(x => x.IsOpen == false)
.OrderByDescending(x => x.ReceiveDate) .OrderByDescending(x => x.ReceiveDate)
@ -190,6 +191,53 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
throw; throw;
} }
} }
public async Task PushNotificationAsyncV2(string? ReceiverUserId, string Subject, string Body, string Payload = "", bool IsSendInbox = false, bool IsSendMail = false)
{
try
{
if (ReceiverUserId != null)
{
_dbContext.Set<Notification>().Add(new Notification
{
Body = Body,
KeycloakUserId = ReceiverUserId,
Type = "",
Payload = Payload,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
if (IsSendInbox == true)
{
_dbContext.Set<Inbox>().Add(new Inbox
{
Subject = Subject,
Body = Body,
KeycloakUserId = ReceiverUserId,
Payload = Payload,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
}
if (IsSendMail == true)
{
_emailSenderService.SendMail(Subject, Body, "kittapath@frappet.com");
}
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
#endregion #endregion
} }

View file

@ -7,6 +7,8 @@
public Guid ReceiverUserId { get; set; } = Guid.Empty; public Guid ReceiverUserId { get; set; } = Guid.Empty;
public string KeycloakUserId { get; set; }
public string Type { get; set; } = "TEXT"; public string Type { get; set; } = "TEXT";
public CommandPayload Payload { get; set; } = new(); public CommandPayload Payload { get; set; } = new();

View file

@ -13,9 +13,11 @@ namespace BMA.EHR.Domain.Models.Notifications
[Required, Column(TypeName = "text"), Comment("รายละเอียดข้อความ")] [Required, Column(TypeName = "text"), Comment("รายละเอียดข้อความ")]
public string Body { get; set; } public string Body { get; set; }
[Required, Comment("รหัสผู้รับข้อความ")] [Comment("รหัสผู้รับข้อความ")]
public Guid ReceiverUserId { get; set; } public Guid ReceiverUserId { get; set; }
public string KeycloakUserId { get; set; }
[Column(TypeName = "text"), Comment("สิงที่แนบมาด้วย")] [Column(TypeName = "text"), Comment("สิงที่แนบมาด้วย")]
public string Payload { get; set; } public string Payload { get; set; }

View file

@ -10,9 +10,11 @@ namespace BMA.EHR.Domain.Models.Notifications
[Required, Column(TypeName = "text"), Comment("รายละเอียดข้อความ")] [Required, Column(TypeName = "text"), Comment("รายละเอียดข้อความ")]
public string Body { get; set; } public string Body { get; set; }
[Required, Comment("รหัสผู้รับข้อความ")] [Comment("รหัสผู้รับข้อความ")]
public Guid ReceiverUserId { get; set; } public Guid ReceiverUserId { get; set; }
public string KeycloakUserId { get; set; }
[Required, Comment("ประเภทการแจ้งเตือน")] [Required, Comment("ประเภทการแจ้งเตือน")]
public string Type { get; set; } public string Type { get; set; }

View file

@ -10,8 +10,8 @@ namespace BMA.EHR.Domain.Models.Retirement
{ {
public class RetirementDeceasedNoti : EntityBase public class RetirementDeceasedNoti : EntityBase
{ {
// [Comment("รหัสอ้างอิงผู้ใช้งานระบบ")] [Comment("profile Id")]
// public Profile ReceiveUser { get; set; } public string? profileId { get; set; }
[Comment("Fk Table RetirementDeceased")] [Comment("Fk Table RetirementDeceased")]
public RetirementDeceased RetirementDeceased { get; set; } public RetirementDeceased RetirementDeceased { get; set; }

View file

@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class updatetableNotificationaddKeycloakUserId : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "profileId",
table: "RetirementDeceasedNotis",
type: "longtext",
nullable: true,
comment: "profile Id")
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "KeycloakUserId",
table: "Notifications",
type: "longtext",
nullable: false)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "KeycloakUserId",
table: "Inboxes",
type: "longtext",
nullable: false)
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "profileId",
table: "RetirementDeceasedNotis");
migrationBuilder.DropColumn(
name: "KeycloakUserId",
table: "Notifications");
migrationBuilder.DropColumn(
name: "KeycloakUserId",
table: "Inboxes");
}
}
}

View file

@ -9250,6 +9250,10 @@ namespace BMA.EHR.Infrastructure.Migrations
.HasColumnType("tinyint(1)") .HasColumnType("tinyint(1)")
.HasComment("เปิดอ่านแล้วหรือยัง"); .HasComment("เปิดอ่านแล้วหรือยัง");
b.Property<string>("KeycloakUserId")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("LastUpdateFullName") b.Property<string>("LastUpdateFullName")
.IsRequired() .IsRequired()
.HasMaxLength(200) .HasMaxLength(200)
@ -9438,6 +9442,10 @@ namespace BMA.EHR.Infrastructure.Migrations
.HasColumnType("tinyint(1)") .HasColumnType("tinyint(1)")
.HasComment("เปิดอ่านแล้วหรือยัง"); .HasComment("เปิดอ่านแล้วหรือยัง");
b.Property<string>("KeycloakUserId")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("LastUpdateFullName") b.Property<string>("LastUpdateFullName")
.IsRequired() .IsRequired()
.HasMaxLength(200) .HasMaxLength(200)
@ -14158,6 +14166,10 @@ namespace BMA.EHR.Infrastructure.Migrations
b.Property<Guid>("RetirementDeceasedId") b.Property<Guid>("RetirementDeceasedId")
.HasColumnType("char(36)"); .HasColumnType("char(36)");
b.Property<string>("profileId")
.HasColumnType("longtext")
.HasComment("profile Id");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("DocumentId"); b.HasIndex("DocumentId");

View file

@ -393,7 +393,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
IsSendNotification = true, IsSendNotification = true,
// OrganizationName = retirementDeceased.OrganizationName, // OrganizationName = retirementDeceased.OrganizationName,
PositionName = retirementDeceased.position, PositionName = retirementDeceased.position,
// ReceiveUser = profile, profileId = req.ProfileId,
CreatedFullName = FullName ?? "System Administrator", CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "", CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now, CreatedAt = DateTime.Now,
@ -502,7 +502,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
p.IsSendNotification, p.IsSendNotification,
p.OrganizationName, p.OrganizationName,
p.PositionName, p.PositionName,
// ProfileId = p.ReceiveUser.Id, p.profileId,
}) })
.ToListAsync(); .ToListAsync();
@ -529,32 +529,53 @@ namespace BMA.EHR.Retirement.Service.Controllers
foreach (var item in req.Persons) foreach (var item in req.Persons)
{ {
var profile = await _context.Profiles.AsQueryable() // var profile = await _context.Profiles.AsQueryable()
.Include(x => x.Prefix) // .Include(x => x.Prefix)
.Include(x => x.Position) // .Include(x => x.Position)
.FirstOrDefaultAsync(x => x.Id == item.ProfileId); // .FirstOrDefaultAsync(x => x.Id == item.ProfileId);
if (profile == null) // if (profile == null)
continue; // continue;
var retirementDeceasedNoti = new RetirementDeceasedNoti
retirementDeceased.RetirementDeceasedNotis.Add(new RetirementDeceasedNoti
{ {
CitizenId = profile.CitizenId == null ? "" : profile.CitizenId, profileId = item.ProfileId,
Prefix = profile.Prefix == null ? "" : profile.Prefix.Name,
FirstName = profile.FirstName == null ? "" : profile.FirstName,
LastName = profile.LastName == null ? "" : profile.LastName,
IsSendMail = item.IsSendMail, IsSendMail = item.IsSendMail,
IsSendInbox = item.IsSendInbox, IsSendInbox = item.IsSendInbox,
IsSendNotification = item.IsSendNotification, IsSendNotification = item.IsSendNotification,
OrganizationName = profile.OrganizationOrganization == null ? "" : profile.OrganizationOrganization,
PositionName = profile.Position == null ? null : profile.Position.Name,
// ReceiveUser = profile,
CreatedFullName = FullName ?? "System Administrator", CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "", CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now, CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator", LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "", LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now, LastUpdatedAt = DateTime.Now,
}); };
var apiUrl = $"{_configuration["API"]}org/profile/profileid/position/{item.ProfileId}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
var _res = await client.SendAsync(_req);
var _result = await _res.Content.ReadAsStringAsync();
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
if (org == null || org.result == null)
continue;
retirementDeceasedNoti.Prefix = org.result.prefix == null ? "" : org.result.prefix;
retirementDeceasedNoti.FirstName = org.result.firstName == null ? "" : org.result.firstName;
retirementDeceasedNoti.LastName = org.result.lastName == null ? "" : org.result.lastName;
retirementDeceasedNoti.CitizenId = org.result.citizenId == null ? "" : org.result.citizenId;
retirementDeceasedNoti.PositionName = org.result.position == null ? "" : org.result.position;
retirementDeceasedNoti.OrganizationName = (org.result.child4 == null ? "" : org.result.child4 + "/") +
(org.result.child3 == null ? "" : org.result.child3 + "/") +
(org.result.child2 == null ? "" : org.result.child2 + "/") +
(org.result.child1 == null ? "" : org.result.child1 + "/") +
(org.result.root == null ? "" : org.result.root + "/");
}
retirementDeceased.RetirementDeceasedNotis.Add(retirementDeceasedNoti);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
@ -625,36 +646,37 @@ namespace BMA.EHR.Retirement.Service.Controllers
}; };
var payload_str = JsonConvert.SerializeObject(payload); var payload_str = JsonConvert.SerializeObject(payload);
// foreach (var item in items) foreach (var item in items)
// { {
// var prefix = item.RetirementDeceased.prefix; // var profile = item.profileId;
// var profile = req.Persons.FirstOrDefault(x => x.ProfileId == item.ReceiveUser.Id); // var prefix = item.profileId;
// if (profile != null) var profile = req.Persons.FirstOrDefault(x => x.ProfileId == item.profileId);
// { if (profile != null)
// await _repositoryNoti.PushNotificationAsync( {
// item.ReceiveUser.Id, await _repositoryNoti.PushNotificationAsyncV2(
// $"หนังสือเวียนถึงแก่กรรมของ {prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", item.profileId,
// $"แจ้งข่าวการถึงแก่กรรมของ {prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", $"หนังสือเวียนถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}",
// payload_str, $"แจ้งข่าวการถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}",
// profile.IsSendInbox, payload_str,
// profile.IsSendMail profile.IsSendInbox,
// ); profile.IsSendMail
// item.IsSendMail = profile.IsSendMail; );
// item.IsSendInbox = profile.IsSendInbox; item.IsSendMail = profile.IsSendMail;
// item.IsSendNotification = profile.IsSendNotification; item.IsSendInbox = profile.IsSendInbox;
// } item.IsSendNotification = profile.IsSendNotification;
// else }
// { else
// await _repositoryNoti.PushNotificationAsync( {
// item.ReceiveUser.Id, await _repositoryNoti.PushNotificationAsyncV2(
// $"หนังสือเวียนถึงแก่กรรมของ {prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", item.profileId,
// $"แจ้งข่าวการถึงแก่กรรมของ {prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", $"หนังสือเวียนถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}",
// payload_str, $"แจ้งข่าวการถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}",
// item.IsSendInbox, payload_str,
// item.IsSendMail item.IsSendInbox,
// ); item.IsSendMail
// } );
// } }
}
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Success(); return Success();

View file

@ -9,7 +9,7 @@ namespace BMA.EHR.Retirement.Service.Requests
} }
public class RetirementDeceasedAddNotiPerson public class RetirementDeceasedAddNotiPerson
{ {
public Guid ProfileId { get; set; } public string ProfileId { get; set; }
public bool IsSendMail { get; set; } = true; public bool IsSendMail { get; set; } = true;
public bool IsSendInbox { get; set; } = true; public bool IsSendInbox { get; set; } = true;
public bool IsSendNotification { get; set; } = true; public bool IsSendNotification { get; set; } = true;

View file

@ -10,6 +10,6 @@ namespace BMA.EHR.Retirement.Service.Requests
public DateTime? Date { get; set; } public DateTime? Date { get; set; }
public string? Location { get; set; } public string? Location { get; set; }
public string? Reason { get; set; } public string? Reason { get; set; }
public Guid ProfileId { get; set; } public string ProfileId { get; set; }
} }
} }