2026-05-27 21:33:48 +03:30
|
|
|
using FluentValidation;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Meezi.API.Models.Crm;
|
|
|
|
|
using Meezi.API.Services;
|
2026-06-21 05:43:07 +03:30
|
|
|
using Meezi.Core.Authorization;
|
2026-05-27 21:33:48 +03:30
|
|
|
using Meezi.Core.Interfaces;
|
|
|
|
|
using Meezi.Shared;
|
|
|
|
|
|
|
|
|
|
namespace Meezi.API.Controllers;
|
|
|
|
|
|
2026-06-12 09:23:50 +03:30
|
|
|
/// <summary>
|
|
|
|
|
/// Marketing SMS — bring-your-own-provider. Each café configures its OWN
|
|
|
|
|
/// Kavenegar API key + sender line; the platform does not sell SMS.
|
|
|
|
|
/// </summary>
|
2026-05-27 21:33:48 +03:30
|
|
|
[Route("api/cafes/{cafeId}/sms")]
|
|
|
|
|
public class SmsController : CafeApiControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ISmsMarketingService _smsMarketingService;
|
|
|
|
|
private readonly IValidator<SendSmsCampaignRequest> _campaignValidator;
|
|
|
|
|
|
|
|
|
|
public SmsController(
|
|
|
|
|
ISmsMarketingService smsMarketingService,
|
|
|
|
|
IValidator<SendSmsCampaignRequest> campaignValidator)
|
|
|
|
|
{
|
|
|
|
|
_smsMarketingService = smsMarketingService;
|
|
|
|
|
_campaignValidator = campaignValidator;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 09:23:50 +03:30
|
|
|
[HttpGet("settings")]
|
|
|
|
|
public async Task<IActionResult> GetSettings(string cafeId, ITenantContext tenant, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
|
|
|
if (EnsureManager(tenant) is { } forbidden) return forbidden;
|
|
|
|
|
|
|
|
|
|
var data = await _smsMarketingService.GetSettingsAsync(cafeId, cancellationToken);
|
|
|
|
|
return Ok(new ApiResponse<SmsSettingsDto>(true, data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("settings")]
|
|
|
|
|
public async Task<IActionResult> UpdateSettings(
|
|
|
|
|
string cafeId,
|
|
|
|
|
[FromBody] UpdateSmsSettingsRequest request,
|
|
|
|
|
ITenantContext tenant,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
2026-06-21 05:43:07 +03:30
|
|
|
if (EnsurePermission(tenant, Permission.ManageSmsSettings) is { } permDenied) return permDenied;
|
2026-06-12 09:23:50 +03:30
|
|
|
|
|
|
|
|
var (success, data, code, message) = await _smsMarketingService.UpdateSettingsAsync(
|
|
|
|
|
cafeId, request, cancellationToken);
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
return code switch
|
|
|
|
|
{
|
|
|
|
|
"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<SmsSettingsDto>(true, data));
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2026-06-12 09:23:50 +03:30
|
|
|
var dto = await _smsMarketingService.GetBalanceAsync(cafeId, cancellationToken);
|
2026-05-29 02:38:06 +03:30
|
|
|
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;
|
|
|
|
|
|
2026-06-12 09:23:50 +03:30
|
|
|
var data = await _smsMarketingService.GetUsageAsync(cafeId, cancellationToken);
|
2026-05-27 21:33:48 +03:30
|
|
|
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;
|
2026-06-21 05:43:07 +03:30
|
|
|
if (EnsurePermission(tenant, Permission.SendSms) is { } permDenied) return permDenied;
|
2026-05-27 21:33:48 +03:30
|
|
|
|
|
|
|
|
var validation = await _campaignValidator.ValidateAsync(request, cancellationToken);
|
|
|
|
|
if (!validation.IsValid) return BadRequest(ValidationError(validation));
|
|
|
|
|
|
|
|
|
|
var (success, data, code, message) = await _smsMarketingService.SendCampaignAsync(
|
2026-06-12 09:23:50 +03:30
|
|
|
cafeId, request, cancellationToken);
|
2026-05-27 21:33:48 +03:30
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
return code switch
|
|
|
|
|
{
|
2026-06-12 09:23:50 +03:30
|
|
|
"SMS_NOT_CONFIGURED" => BadRequest(
|
2026-05-27 21:33:48 +03:30
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|