Subscription plans

This commit is contained in:
Thomas Fransolet 2026-04-01 17:00:13 +02:00
parent 09b7c75dac
commit f72d94f30f
15 changed files with 1914 additions and 2 deletions

View File

@ -53,6 +53,15 @@ namespace ManagerService.Controllers
if (appInstance == null || !appInstance.IsAssistant)
return Forbid();
var monthKey = DateTime.UtcNow.ToString("yyyy-MM");
if (instance.AiUsageMonthKey != monthKey)
{
instance.AiRequestsThisMonth = 0;
instance.AiUsageMonthKey = monthKey;
}
instance.AiRequestsThisMonth++;
_context.SaveChanges();
var result = await _assistantService.ChatAsync(request);
return Ok(result);
}

View File

@ -10,6 +10,7 @@ using ManagerService.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using NSwag.Annotations;
namespace ManagerService.Controllers
@ -170,6 +171,10 @@ namespace ManagerService.Controllers
instance.DateCreation = updatedInstance.dateCreation != null ? updatedInstance.dateCreation.Value : instance.DateCreation;
instance.Name= updatedInstance.name != null ? updatedInstance.name : instance.Name;
instance.PinCode = updatedInstance.pinCode != null ? updatedInstance.pinCode : instance.PinCode;
if (updatedInstance.subscriptionPlanId == "")
instance.SubscriptionPlanId = null;
else if (updatedInstance.subscriptionPlanId != null)
instance.SubscriptionPlanId = updatedInstance.subscriptionPlanId;
//OldInstance instanceModified = _instanceService.Update(updatedInstance.Id, instance);
_myInfoMateDbContext.SaveChanges();
@ -253,6 +258,47 @@ namespace ManagerService.Controllers
}
}
/// <summary>
/// Get quota usage for an instance
/// </summary>
/// <param name="id">Id instance</param>
[Authorize(Policy = ManagerService.Service.Security.Policies.Viewer)]
[ProducesResponseType(typeof(InstanceQuotaDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}/quota")]
public ObjectResult GetQuota(string id)
{
try
{
var instance = _myInfoMateDbContext.Instances
.Include(i => i.SubscriptionPlan)
.FirstOrDefault(i => i.Id == id);
if (instance == null)
return new NotFoundObjectResult("Instance not found");
var storageUsed = _myInfoMateDbContext.Resources
.Where(r => r.InstanceId == id)
.Sum(r => (long?)r.SizeBytes) ?? 0;
var monthKey = DateTime.UtcNow.ToString("yyyy-MM");
var aiUsed = instance.AiUsageMonthKey == monthKey ? instance.AiRequestsThisMonth : 0;
return new OkObjectResult(new InstanceQuotaDTO
{
storageUsedBytes = storageUsed,
storageQuotaBytes = instance.SubscriptionPlan?.StorageQuotaBytes ?? 0,
aiRequestsUsed = aiUsed,
aiRequestsPerMonth = instance.SubscriptionPlan?.AiRequestsPerMonth ?? 0
});
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Delete an instance
/// </summary>

View File

@ -269,6 +269,7 @@ namespace ManagerService.Controllers
resource.DateCreation = DateTime.Now.ToUniversalTime();
resource.InstanceId = instanceId;
resource.Id = idService.GenerateHexId();
resource.SizeBytes = file.Length;
_myInfoMateDbContext.Add(resource);
_myInfoMateDbContext.SaveChanges();

View File

@ -0,0 +1,150 @@
using ManagerService.Data;
using ManagerService.DTOs;
using ManagerService.Helpers;
using ManagerService.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ManagerService.Controllers
{
[Authorize(Policy = ManagerService.Service.Security.Policies.SuperAdmin)]
[ApiController, Route("api/[controller]")]
[OpenApiTag("SubscriptionPlan", Description = "Subscription plan management")]
public class SubscriptionPlanController : ControllerBase
{
private readonly MyInfoMateDbContext _myInfoMateDbContext;
private readonly ILogger<SubscriptionPlanController> _logger;
IHexIdGeneratorService idService = new HexIdGeneratorService();
public SubscriptionPlanController(ILogger<SubscriptionPlanController> logger, MyInfoMateDbContext myInfoMateDbContext)
{
_logger = logger;
_myInfoMateDbContext = myInfoMateDbContext;
}
[ProducesResponseType(typeof(List<SubscriptionPlanDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
try
{
var plans = _myInfoMateDbContext.SubscriptionPlans.ToList();
return new OkObjectResult(plans.Select(p => p.ToDTO()).ToList());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
public ObjectResult GetById(string id)
{
try
{
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] SubscriptionPlanDTO dto)
{
try
{
if (dto == null)
return new BadRequestObjectResult("Plan data is required");
var plan = new SubscriptionPlan().FromDTO(dto);
plan.Id = idService.GenerateHexId();
_myInfoMateDbContext.SubscriptionPlans.Add(plan);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Update([FromBody] SubscriptionPlanDTO dto)
{
try
{
if (dto == null || dto.id == null)
return new BadRequestObjectResult("Plan id is required");
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == dto.id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
plan.FromDTO(dto);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(string), 202)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpDelete("{id}")]
public ObjectResult Delete(string id)
{
try
{
if (id == null)
return new BadRequestObjectResult("Plan id is required");
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
var isUsed = _myInfoMateDbContext.Instances.Any(i => i.SubscriptionPlanId == id);
if (isUsed)
return new ConflictObjectResult("This plan is assigned to one or more instances");
_myInfoMateDbContext.SubscriptionPlans.Remove(plan);
_myInfoMateDbContext.SaveChanges();
return new ObjectResult("The subscription plan has been deleted") { StatusCode = 202 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}

View File

@ -18,6 +18,11 @@ namespace ManagerService.DTOs
public bool isAssistant { get; set; }
public string? subscriptionPlanId { get; set; }
public SubscriptionPlanDTO? subscriptionPlan { get; set; }
public int aiRequestsThisMonth { get; set; }
public string? aiUsageMonthKey { get; set; }
public List<ApplicationInstanceDTO> applicationInstanceDTOs { get; set; }
}
}

View File

@ -0,0 +1,10 @@
namespace ManagerService.DTOs
{
public class InstanceQuotaDTO
{
public long storageUsedBytes { get; set; }
public long storageQuotaBytes { get; set; }
public int aiRequestsUsed { get; set; }
public int aiRequestsPerMonth { get; set; }
}
}

View File

@ -11,5 +11,6 @@ namespace ManagerService.DTOs
public string url { get; set; } // firebase url
public DateTime dateCreation { get; set; }
public string instanceId { get; set; }
public long sizeBytes { get; set; }
}
}

View File

@ -0,0 +1,10 @@
namespace ManagerService.DTOs
{
public class SubscriptionPlanDTO
{
public string? id { get; set; }
public string name { get; set; }
public long storageQuotaBytes { get; set; }
public int aiRequestsPerMonth { get; set; }
}
}

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ManagerService.Data
{
@ -35,6 +36,15 @@ namespace ManagerService.Data
public bool IsAssistant { get; set; }
public string? SubscriptionPlanId { get; set; }
[ForeignKey("SubscriptionPlanId")]
public SubscriptionPlan? SubscriptionPlan { get; set; }
public int AiRequestsThisMonth { get; set; } = 0;
public string AiUsageMonthKey { get; set; } = "";
public InstanceDTO ToDTO(List<ApplicationInstanceDTO> applicationInstanceDTOs)
{
@ -51,6 +61,10 @@ namespace ManagerService.Data
isWeb = IsWeb,
isVR = IsVR,
isAssistant = IsAssistant,
subscriptionPlanId = SubscriptionPlanId,
subscriptionPlan = SubscriptionPlan?.ToDTO(),
aiRequestsThisMonth = AiRequestsThisMonth,
aiUsageMonthKey = AiUsageMonthKey,
applicationInstanceDTOs = applicationInstanceDTOs
};
}
@ -67,6 +81,8 @@ namespace ManagerService.Data
IsWeb = instanceDTO.isWeb;
IsVR = instanceDTO.isVR;
IsAssistant = instanceDTO.isAssistant;
if (instanceDTO.subscriptionPlanId != null)
SubscriptionPlanId = instanceDTO.subscriptionPlanId;
return this;
}

View File

@ -13,6 +13,7 @@ namespace ManagerService.Data
public MyInfoMateDbContext(DbContextOptions<MyInfoMateDbContext> options) : base(options) { }
public DbSet<Instance> Instances { get; set; }
public DbSet<SubscriptionPlan> SubscriptionPlans { get; set; }
public DbSet<Configuration> Configurations { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<Device> Devices { get; set; }

View File

@ -36,7 +36,9 @@ namespace ManagerService.Data
/*[BsonElement("URL")]*/
public string Url { get; set; } // Firebase url
public ResourceDTO ToDTO()
public long SizeBytes { get; set; } = 0;
public ResourceDTO ToDTO()
{
return new ResourceDTO()
{
@ -45,7 +47,8 @@ namespace ManagerService.Data
type = Type,
url = Url,
dateCreation = DateCreation,
instanceId = InstanceId
instanceId = InstanceId,
sizeBytes = SizeBytes
};
}
}

View File

@ -0,0 +1,38 @@
using ManagerService.DTOs;
using System.ComponentModel.DataAnnotations;
namespace ManagerService.Data
{
public class SubscriptionPlan
{
[Key]
[Required]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public long StorageQuotaBytes { get; set; } = 0;
public int AiRequestsPerMonth { get; set; } = 0;
public SubscriptionPlanDTO ToDTO()
{
return new SubscriptionPlanDTO
{
id = Id,
name = Name,
storageQuotaBytes = StorageQuotaBytes,
aiRequestsPerMonth = AiRequestsPerMonth
};
}
public SubscriptionPlan FromDTO(SubscriptionPlanDTO dto)
{
Name = dto.name;
StorageQuotaBytes = dto.storageQuotaBytes;
AiRequestsPerMonth = dto.aiRequestsPerMonth;
return this;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,97 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ManagerService.Migrations
{
/// <inheritdoc />
public partial class AddSubscriptionPlanAndQuota : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<long>(
name: "SizeBytes",
table: "Resources",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn<int>(
name: "AiRequestsThisMonth",
table: "Instances",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "AiUsageMonthKey",
table: "Instances",
type: "text",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "SubscriptionPlanId",
table: "Instances",
type: "text",
nullable: true);
migrationBuilder.CreateTable(
name: "SubscriptionPlans",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
StorageQuotaBytes = table.Column<long>(type: "bigint", nullable: false),
AiRequestsPerMonth = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SubscriptionPlans", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Instances_SubscriptionPlanId",
table: "Instances",
column: "SubscriptionPlanId");
migrationBuilder.AddForeignKey(
name: "FK_Instances_SubscriptionPlans_SubscriptionPlanId",
table: "Instances",
column: "SubscriptionPlanId",
principalTable: "SubscriptionPlans",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Instances_SubscriptionPlans_SubscriptionPlanId",
table: "Instances");
migrationBuilder.DropTable(
name: "SubscriptionPlans");
migrationBuilder.DropIndex(
name: "IX_Instances_SubscriptionPlanId",
table: "Instances");
migrationBuilder.DropColumn(
name: "SizeBytes",
table: "Resources");
migrationBuilder.DropColumn(
name: "AiRequestsThisMonth",
table: "Instances");
migrationBuilder.DropColumn(
name: "AiUsageMonthKey",
table: "Instances");
migrationBuilder.DropColumn(
name: "SubscriptionPlanId",
table: "Instances");
}
}
}

View File

@ -296,6 +296,12 @@ namespace ManagerService.Migrations
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("AiRequestsThisMonth")
.HasColumnType("integer");
b.Property<string>("AiUsageMonthKey")
.HasColumnType("text");
b.Property<DateTime>("DateCreation")
.HasColumnType("timestamp with time zone");
@ -327,8 +333,13 @@ namespace ManagerService.Migrations
b.Property<string>("PinCode")
.HasColumnType("text");
b.Property<string>("SubscriptionPlanId")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("SubscriptionPlanId");
b.ToTable("Instances");
});
@ -391,6 +402,9 @@ namespace ManagerService.Migrations
.IsRequired()
.HasColumnType("text");
b.Property<long>("SizeBytes")
.HasColumnType("bigint");
b.Property<int>("Type")
.HasColumnType("integer");
@ -857,6 +871,26 @@ namespace ManagerService.Migrations
b.ToTable("ProgrammeBlocks");
});
modelBuilder.Entity("ManagerService.Data.SubscriptionPlan", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("AiRequestsPerMonth")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<long>("StorageQuotaBytes")
.HasColumnType("bigint");
b.HasKey("Id");
b.ToTable("SubscriptionPlans");
});
modelBuilder.Entity("ManagerService.Data.User", b =>
{
b.Property<string>("Id")
@ -1217,6 +1251,15 @@ namespace ManagerService.Migrations
b.Navigation("Configuration");
});
modelBuilder.Entity("ManagerService.Data.Instance", b =>
{
b.HasOne("ManagerService.Data.SubscriptionPlan", "SubscriptionPlan")
.WithMany()
.HasForeignKey("SubscriptionPlanId");
b.Navigation("SubscriptionPlan");
});
modelBuilder.Entity("ManagerService.Data.Section", b =>
{
b.HasOne("ManagerService.Data.SubSection.SectionMenu", null)