Files
meezi/src/Meezi.API/Controllers/SmsController.cs
T

82 lines
3.2 KiB
C#
Raw Normal View History

2026-05-27 21:33:48 +03:30
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Meezi.API.Models.Crm;
using Meezi.API.Services;
using Meezi.Core.Interfaces;
using Meezi.Shared;
namespace Meezi.API.Controllers;
[Route("api/cafes/{cafeId}/sms")]
public class SmsController : CafeApiControllerBase
{
private readonly ISmsMarketingService _smsMarketingService;
2026-05-29 02:38:06 +03:30
private readonly ISmsService _smsService;
2026-05-27 21:33:48 +03:30
private readonly IValidator<SendSmsCampaignRequest> _campaignValidator;
public SmsController(
ISmsMarketingService smsMarketingService,
2026-05-29 02:38:06 +03:30
ISmsService smsService,
2026-05-27 21:33:48 +03:30
IValidator<SendSmsCampaignRequest> campaignValidator)
{
_smsMarketingService = smsMarketingService;
2026-05-29 02:38:06 +03:30
_smsService = smsService;
2026-05-27 21:33:48 +03:30
_campaignValidator = campaignValidator;
}
2026-05-29 02:38:06 +03:30
[HttpGet("balance")]
public async Task<IActionResult> GetBalance(string cafeId, ITenantContext tenant, CancellationToken cancellationToken)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var info = await _smsService.GetAccountInfoAsync(cancellationToken);
var dto = info is not null
? new SmsBalanceDto(info.RemainCredit, info.AccountType, true)
: new SmsBalanceDto(0, "master", false);
return Ok(new ApiResponse<SmsBalanceDto>(true, dto));
}
2026-05-27 21:33:48 +03:30
[HttpGet("usage")]
public async Task<IActionResult> GetUsage(string cafeId, ITenantContext tenant, CancellationToken cancellationToken)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
if (tenant.PlanTier is null)
return BadRequest(new ApiResponse<object>(false, null, new ApiError("INVALID", "Plan tier missing.")));
var data = await _smsMarketingService.GetUsageAsync(cafeId, tenant.PlanTier.Value, cancellationToken);
return Ok(new ApiResponse<SmsUsageDto>(true, data));
}
[HttpPost("campaign")]
public async Task<IActionResult> SendCampaign(
string cafeId,
[FromBody] SendSmsCampaignRequest request,
ITenantContext tenant,
CancellationToken cancellationToken)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
if (tenant.PlanTier is null)
return BadRequest(new ApiResponse<object>(false, null, new ApiError("INVALID", "Plan tier missing.")));
var validation = await _campaignValidator.ValidateAsync(request, cancellationToken);
if (!validation.IsValid) return BadRequest(ValidationError(validation));
var (success, data, code, message) = await _smsMarketingService.SendCampaignAsync(
cafeId, tenant.PlanTier.Value, request, cancellationToken);
if (!success)
{
return code switch
{
"PLAN_LIMIT_REACHED" => StatusCode(StatusCodes.Status403Forbidden,
new ApiResponse<object>(false, null, new ApiError(code, message!))),
"NOT_FOUND" => NotFound(new ApiResponse<object>(false, null, new ApiError(code, message!))),
_ => BadRequest(new ApiResponse<object>(false, null, new ApiError(code!, message!)))
};
}
return Ok(new ApiResponse<SmsCampaignResult>(true, data));
}
}