add message queue table for notification system

This commit is contained in:
Suphonchai Phoonsawat 2023-07-12 21:08:46 +07:00
parent 1c99718a30
commit 841bf32175
12 changed files with 21322 additions and 3 deletions

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.MessageQueue;
using Microsoft.Extensions.DependencyInjection;
namespace BMA.EHR.Application
@ -9,6 +10,7 @@ namespace BMA.EHR.Application
{
services.AddTransient<PrefixRepository>();
services.AddTransient<PlacementRepository>();
services.AddTransient<MessageQueueRepository>();
return services;
}

View file

@ -0,0 +1,49 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Models.Placement;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BMA.EHR.Application.Repositories.MessageQueue
{
public class MessageQueueRepository : GenericRepository<Guid, MessageQueueEntity>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destuctor "
public MessageQueueRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Methods "
public async Task<List<MessageQueueEntity>> GetUnSentAsync(int count = 10)
{
var data = await _dbContext.Set<MessageQueueEntity>().Where(x => !x.IsSend)
.AsNoTracking()
.OrderBy(m => m.CreatedAt)
.Take(count)
.ToListAsync();
return data;
}
#endregion
}
}

View file

@ -1,5 +1,4 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Placement;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;

View file

@ -0,0 +1,39 @@
using BMA.EHR.Domain.Models.Base;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace BMA.EHR.Domain.Models.Notifications
{
public class MessageQueueEntity : EntityBase
{
[Required, MaxLength(200), Comment("ส่งจากระบบงาน")]
public string SenderSystem { get; set; }
[Required, MaxLength(200), Comment("หัวเรื่อง")]
public string Subject { get; set; } = string.Empty;
[Required, Comment("รายละเอียดข้อความ")]
public string MessageContent { get; set; } = string.Empty;
[Comment("สิ่งที่แนบมาด้วย")]
public string MessagePayLoad { get; set; } = string.Empty;
[Required, Comment("รหัสของผู้รับข้อความ")]
public Guid ReceiverUserId { get; set; } = Guid.Empty;
[MaxLength(500), Comment("อีเมล์ของผู้รับ")]
public string ReceiverEmailAddress { get; set; } = string.Empty;
[Required, Comment("ส่งอีเมลล์หรือไม่?")]
public bool IsSendEmail { get; set; } = false;
[Required, Comment("ส่งไปที่กล่องข้อความหรือไม่?")]
public bool IsSendInbox { get; set; } = false;
[Required, Comment("ส่งการแจ้งเตือนหรือไม่?")]
public bool IsSendNotification { get; set; } = true;
[Required, Comment("ทำการส่งข้อความแล้วหรือยัง?")]
public bool IsSend { get; set; } = false;
}
}

View file

@ -1,5 +1,6 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Infrastructure.Messaging;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -13,6 +14,7 @@ namespace BMA.EHR.Infrastructure
IConfiguration configuration)
{
services.AddTransient<MinIOService>();
services.AddTransient<EmailSenderService>();
var connectionString = configuration.GetConnectionString("DefaultConnection");

View file

@ -0,0 +1,60 @@
using BMA.EHR.Domain.Extensions;
using Microsoft.Extensions.Configuration;
using System.Net;
using System.Net.Mail;
namespace BMA.EHR.Infrastructure.Messaging
{
public class EmailSenderService
{
#region " Fields "
private readonly IConfiguration _configuration;
#endregion
#region " Constructor and Destructor "
public EmailSenderService(IConfiguration configuration)
{
_configuration = configuration;
}
#endregion
#region " Methods "
public void SendMail(string subject, string body, string receiver)
{
try
{
var server = _configuration["Mail:Server"];
var user = _configuration["Mail:User"];
var password = _configuration["Mail:Password"];
var port = _configuration["Mail:Port"];
var from = _configuration["Mail:MailFrom"];
var client = new SmtpClient(server, port.ToInteger());
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(user, password);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
var mail = new MailMessage();
mail.From = new MailAddress(from, "eHR Bangkok Automation System");
mail.To.Add(receiver);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
client.Send(mail);
}
catch
{
throw;
}
}
#endregion
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,57 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddMessageQueue : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "MessageQueues",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, comment: "PrimaryKey", collation: "ascii_general_ci"),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "สร้างข้อมูลเมื่อ"),
CreatedUserId = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: false, comment: "User Id ที่สร้างข้อมูล")
.Annotation("MySql:CharSet", "utf8mb4"),
LastUpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true, comment: "แก้ไขข้อมูลล่าสุดเมื่อ"),
LastUpdateUserId = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: false, comment: "User Id ที่แก้ไขข้อมูลล่าสุด")
.Annotation("MySql:CharSet", "utf8mb4"),
CreatedFullName = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "ชื่อ User ที่สร้างข้อมูล")
.Annotation("MySql:CharSet", "utf8mb4"),
LastUpdateFullName = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "ชื่อ User ที่แก้ไขข้อมูลล่าสุด")
.Annotation("MySql:CharSet", "utf8mb4"),
SenderSystem = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "ส่งจากระบบงาน")
.Annotation("MySql:CharSet", "utf8mb4"),
Subject = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "หัวเรื่อง")
.Annotation("MySql:CharSet", "utf8mb4"),
MessageContent = table.Column<string>(type: "longtext", nullable: false, comment: "รายละเอียดข้อความ")
.Annotation("MySql:CharSet", "utf8mb4"),
MessagePayLoad = table.Column<string>(type: "longtext", nullable: false, comment: "สิ่งที่แนบมาด้วย")
.Annotation("MySql:CharSet", "utf8mb4"),
ReceiverUserId = table.Column<Guid>(type: "char(36)", nullable: false, comment: "รหัสของผู้รับข้อความ", collation: "ascii_general_ci"),
IsSendEmail = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "ส่งอีเมลล์หรือไม่?"),
IsSendInbox = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "ส่งไปที่กล่องข้อความหรือไม่?"),
IsSendNotification = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "ส่งการแจ้งเตือนหรือไม่?"),
IsSend = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "ทำการส่งข้อความแล้วหรือยัง?")
},
constraints: table =>
{
table.PrimaryKey("PK_MessageQueues", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "MessageQueues");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddReceiverEmailtoMessageQueue : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ReceiverEmailAddress",
table: "MessageQueues",
type: "varchar(500)",
maxLength: 500,
nullable: false,
defaultValue: "",
comment: "อีเมล์ของผู้รับ")
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ReceiverEmailAddress",
table: "MessageQueues");
}
}
}

View file

@ -7326,6 +7326,106 @@ namespace BMA.EHR.Infrastructure.Migrations
b.ToTable("SubDistricts");
});
modelBuilder.Entity("BMA.EHR.Domain.Models.Notifications.MessageQueueEntity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)")
.HasColumnOrder(0)
.HasComment("PrimaryKey")
.HasAnnotation("Relational:JsonPropertyName", "id");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)")
.HasColumnOrder(100)
.HasComment("สร้างข้อมูลเมื่อ");
b.Property<string>("CreatedFullName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)")
.HasColumnOrder(104)
.HasComment("ชื่อ User ที่สร้างข้อมูล");
b.Property<string>("CreatedUserId")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("varchar(40)")
.HasColumnOrder(101)
.HasComment("User Id ที่สร้างข้อมูล");
b.Property<bool>("IsSend")
.HasColumnType("tinyint(1)")
.HasComment("ทำการส่งข้อความแล้วหรือยัง?");
b.Property<bool>("IsSendEmail")
.HasColumnType("tinyint(1)")
.HasComment("ส่งอีเมลล์หรือไม่?");
b.Property<bool>("IsSendInbox")
.HasColumnType("tinyint(1)")
.HasComment("ส่งไปที่กล่องข้อความหรือไม่?");
b.Property<bool>("IsSendNotification")
.HasColumnType("tinyint(1)")
.HasComment("ส่งการแจ้งเตือนหรือไม่?");
b.Property<string>("LastUpdateFullName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)")
.HasColumnOrder(105)
.HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด");
b.Property<string>("LastUpdateUserId")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("varchar(40)")
.HasColumnOrder(103)
.HasComment("User Id ที่แก้ไขข้อมูลล่าสุด");
b.Property<DateTime?>("LastUpdatedAt")
.HasColumnType("datetime(6)")
.HasColumnOrder(102)
.HasComment("แก้ไขข้อมูลล่าสุดเมื่อ");
b.Property<string>("MessageContent")
.IsRequired()
.HasColumnType("longtext")
.HasComment("รายละเอียดข้อความ");
b.Property<string>("MessagePayLoad")
.IsRequired()
.HasColumnType("longtext")
.HasComment("สิ่งที่แนบมาด้วย");
b.Property<string>("ReceiverEmailAddress")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("varchar(500)")
.HasComment("อีเมล์ของผู้รับ");
b.Property<Guid>("ReceiverUserId")
.HasColumnType("char(36)")
.HasComment("รหัสของผู้รับข้อความ");
b.Property<string>("SenderSystem")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)")
.HasComment("ส่งจากระบบงาน");
b.Property<string>("Subject")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)")
.HasComment("หัวเรื่อง");
b.HasKey("Id");
b.ToTable("MessageQueues");
});
modelBuilder.Entity("BMA.EHR.Domain.Models.Organizations.AvailablePositionLevelEntity", b =>
{
b.Property<Guid>("Id")

View file

@ -3,6 +3,7 @@ using BMA.EHR.Domain.Models.Commands;
using BMA.EHR.Domain.Models.Documents;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Models.Organizations;
using BMA.EHR.Domain.Models.Organizations.Report2;
using BMA.EHR.Domain.Models.Placement;
@ -236,6 +237,8 @@ namespace BMA.EHR.Infrastructure.Persistence
#endregion
#endregion
#region " Placements "
public DbSet<Placement> Placements { get; set; }
@ -247,14 +250,18 @@ namespace BMA.EHR.Infrastructure.Persistence
#endregion
#endregion
#region " Command "
public DbSet<DeploymentChannel> DeploymentChannels { get; set; }
#endregion
#region " Message Queue "
public DbSet<MessageQueueEntity> MessageQueues { get; set; }
#endregion
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{
}