fix: sidebar accordion + koja slug + support ticket LINQ crash
CI/CD / CI · API (dotnet build + test) (push) Successful in 5m50s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 32s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m3s
CI/CD / CI · Admin Web (tsc) (push) Successful in 35s
CI/CD / CI · Website (tsc) (push) Successful in 44s
CI/CD / CI · Koja (tsc) (push) Successful in 48s
CI/CD / Deploy · all services (push) Has been cancelled

Sidebar:
- All groups start collapsed on first load (v4 storage key resets old state)
- Opening one group closes all others (accordion)
- Navigating to a section opens only that section's group

Koja slug:
- SlugHelper: Persian->Latin transliteration, slug validation
- Registration accepts optional custom slug; auto-derives from cafe name
- Slug can be updated from dashboard Settings -> Profile
- Settings PATCH validates uniqueness (SLUG_TAKEN) and format (INVALID_SLUG)
- koja.meezi.ir/{slug} now redirects to /fa/cafe/{slug} (short URL support)

Bug fix:
- SupportTicketService: cafeId/status filters applied before Select() projection
  to fix EF "could not be translated" crash on the support tickets page

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-05-31 22:28:25 +03:30
parent 38e3f6a5a2
commit cd1af30bbc
17 changed files with 401 additions and 58 deletions
@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Meezi.API.Models.Cafes;
using Meezi.API.Services;
using Meezi.Core.Interfaces;
using Meezi.Core.Utilities;
using Meezi.Infrastructure.Branding;
using Meezi.Infrastructure.Data;
using Meezi.Shared;
@@ -57,6 +58,21 @@ public class CafeSettingsController : CafeApiControllerBase
if (cafe is null) return NotFoundError();
if (request.Name is not null) cafe.Name = request.Name.Trim();
if (request.Slug is not null)
{
var newSlug = request.Slug.Trim().ToLowerInvariant();
if (!SlugHelper.IsValidSlug(newSlug))
return BadRequest(new ApiResponse<object>(false, null,
new ApiError("INVALID_SLUG", "Slug must be 2-80 lowercase letters, digits, or hyphens.")));
var taken = await _db.Cafes.AnyAsync(c => c.Slug == newSlug && c.Id != cafeId, ct);
if (taken)
return Conflict(new ApiResponse<object>(false, null,
new ApiError("SLUG_TAKEN", "This Koja profile address is already in use. Please choose another.")));
cafe.Slug = newSlug;
}
if (request.Phone is not null) cafe.Phone = request.Phone.Trim();
if (request.Address is not null) cafe.Address = request.Address.Trim();
if (request.City is not null) cafe.City = request.City.Trim();
+2 -1
View File
@@ -15,7 +15,8 @@ public record SwitchCafeRequest(string CafeId);
public record SwitchBranchRequest(string? BranchId);
/// <summary>Step 1 of self-registration: send OTP to a new phone number.</summary>
public record RegisterRequest(string Phone, string CafeName);
/// <param name="Slug">Optional custom Koja slug (e.g. "lamiz-enghelab"). Auto-derived from CafeName if omitted.</param>
public record RegisterRequest(string Phone, string CafeName, string? Slug = null);
/// <summary>Step 2 of self-registration: verify OTP and create the cafe account.</summary>
public record VerifyRegisterRequest(string Phone, string Code);
@@ -19,6 +19,8 @@ public record CafeSettingsDto(
public record PatchCafeSettingsRequest(
string? Name,
/// <summary>Custom Koja profile slug (e.g. "lamiz-enghelab"). Must be unique across all cafés.</summary>
string? Slug,
string? Phone,
string? Address,
string? City,
+45 -8
View File
@@ -303,8 +303,15 @@ public class AuthService : IAuthService
var otp = Random.Shared.Next(100000, 999999).ToString();
await redis.StringSetAsync($"otp:{phone}", otp, TimeSpan.FromSeconds(OtpTtlSeconds));
// Store the cafe name alongside the OTP so verify-register can create the cafe
await redis.StringSetAsync($"reg_meta:{phone}", cafeName, TimeSpan.FromSeconds(OtpTtlSeconds));
// Determine the requested slug: use provided slug, or auto-derive from café name.
// Format stored: "cafeName||slug" (double-pipe delimiter). Slug may be empty.
var requestedSlug = string.IsNullOrWhiteSpace(request.Slug)
? Meezi.Core.Utilities.SlugHelper.Slugify(cafeName)
: request.Slug.Trim().ToLowerInvariant();
var regMeta = $"{cafeName}||{requestedSlug}";
await redis.StringSetAsync($"reg_meta:{phone}", regMeta, TimeSpan.FromSeconds(OtpTtlSeconds));
try
{
@@ -342,10 +349,25 @@ public class AuthService : IAuthService
if (storedOtp.IsNullOrEmpty || storedOtp.ToString() != code)
return (false, null, "INVALID_OTP", "Invalid or expired verification code.");
var cafeName = (await redis.StringGetAsync($"reg_meta:{phone}")).ToString();
if (string.IsNullOrWhiteSpace(cafeName))
var regMetaRaw = (await redis.StringGetAsync($"reg_meta:{phone}")).ToString();
if (string.IsNullOrWhiteSpace(regMetaRaw))
return (false, null, "REGISTRATION_EXPIRED", "Registration session expired. Please start again.");
// Parse "cafeName||slug" format (double-pipe delimiter)
string cafeName;
string? requestedSlug;
var sepIdx = regMetaRaw.IndexOf("||", StringComparison.Ordinal);
if (sepIdx >= 0)
{
cafeName = regMetaRaw[..sepIdx];
requestedSlug = regMetaRaw[(sepIdx + 2)..];
}
else
{
cafeName = regMetaRaw;
requestedSlug = null;
}
// Double-check no owner was created in the meantime (race condition guard)
var alreadyOwner = await _db.Employees
.AnyAsync(e => e.Phone == phone && e.Role == EmployeeRole.Owner && e.DeletedAt == null, cancellationToken);
@@ -356,8 +378,8 @@ public class AuthService : IAuthService
return (false, null, "ALREADY_REGISTERED", "An account already exists for this phone number. Please sign in.");
}
// Generate a unique slug
var slug = await GenerateUniqueSlugAsync(cancellationToken);
// Generate a unique slug (try requested slug first, fall back to random)
var slug = await GenerateUniqueSlugAsync(requestedSlug, cancellationToken);
var cafe = new Cafe
{
@@ -402,12 +424,27 @@ public class AuthService : IAuthService
return (true, tokens, null, null);
}
private async Task<string> GenerateUniqueSlugAsync(CancellationToken ct)
private async Task<string> GenerateUniqueSlugAsync(string? preferred, CancellationToken ct)
{
// Try the preferred/derived slug first
if (Meezi.Core.Utilities.SlugHelper.IsValidSlug(preferred))
{
if (!await _db.Cafes.AnyAsync(c => c.Slug == preferred, ct))
return preferred!;
// Preferred slug is taken — append a short random suffix
for (var i = 0; i < 10; i++)
{
var candidate = $"{preferred}-{Guid.NewGuid().ToString("N")[..4]}";
if (!await _db.Cafes.AnyAsync(c => c.Slug == candidate, ct))
return candidate;
}
}
// Full random fallback
string slug;
do
{
// e.g. "cafe-a3f9b2c"
slug = "cafe-" + Guid.NewGuid().ToString("N")[..7];
} while (await _db.Cafes.AnyAsync(c => c.Slug == slug, ct));
return slug;
@@ -1,6 +1,7 @@
using FluentValidation;
using Meezi.API.Models.Auth;
using Meezi.Core.Utilities;
using System.Text.RegularExpressions;
namespace Meezi.API.Validators;
@@ -51,6 +52,10 @@ public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
.NotEmpty()
.MaximumLength(100)
.WithMessage("Cafe name must be between 1 and 100 characters.");
RuleFor(x => x.Slug)
.Must(s => s == null || SlugHelper.IsValidSlug(s))
.WithMessage("Slug must be 2-80 lowercase letters, digits, or hyphens (e.g. my-cafe).");
}
}
@@ -1,5 +1,6 @@
using FluentValidation;
using Meezi.API.Models.Cafes;
using Meezi.Core.Utilities;
namespace Meezi.API.Validators;
@@ -8,6 +9,10 @@ public class PatchCafeSettingsRequestValidator : AbstractValidator<PatchCafeSett
public PatchCafeSettingsRequestValidator()
{
RuleFor(x => x.Name).MaximumLength(200).When(x => x.Name is not null);
RuleFor(x => x.Slug)
.Must(s => s == null || SlugHelper.IsValidSlug(s))
.WithMessage("Slug must be 2-80 lowercase letters, digits, or hyphens (e.g. my-cafe).")
.When(x => x.Slug is not null);
RuleFor(x => x.Phone).MaximumLength(20).When(x => x.Phone is not null);
RuleFor(x => x.Address).MaximumLength(500).When(x => x.Address is not null);
RuleFor(x => x.City).MaximumLength(100).When(x => x.City is not null);
+87
View File
@@ -0,0 +1,87 @@
using System.Text;
using System.Text.RegularExpressions;
namespace Meezi.Core.Utilities;
/// <summary>
/// Converts Persian/Arabic café names to URL-safe Latin slugs.
/// Used for Koja profile URLs (koja.meezi.ir/fa/cafe/{slug}).
/// </summary>
public static partial class SlugHelper
{
private static readonly Dictionary<char, string> PersianToLatin = new()
{
// Alef variants
{ 'آ', "a" }, { 'ا', "a" }, { 'أ', "a" }, { 'إ', "a" },
// Ba, Pa, Ta, Tha
{ 'ب', "b" }, { 'پ', "p" }, { 'ت', "t" }, { 'ث', "s" },
// Jim, Che, He, Khe
{ 'ج', "j" }, { 'چ', "ch" }, { 'ح', "h" }, { 'خ', "kh" },
// Dal, Zal, Re, Ze, Zhe
{ 'د', "d" }, { 'ذ', "z" }, { 'ر', "r" }, { 'ز', "z" }, { 'ژ', "zh" },
// Sin, Shin, Sad, Zad
{ 'س', "s" }, { 'ش', "sh" }, { 'ص', "s" }, { 'ض', "z" },
// Ta, Za, Ain, Ghain
{ 'ط', "t" }, { 'ظ', "z" }, { 'ع', "a" }, { 'غ', "gh" },
// Fa, Ghaf, Kaf (Arabic+Persian), Gaf
{ 'ف', "f" }, { 'ق', "gh" }, { 'ک', "k" }, { 'ك', "k" }, { 'گ', "g" },
// Lam, Mim, Nun, Vav, He, Ye
{ 'ل', "l" }, { 'م', "m" }, { 'ن', "n" }, { 'و', "v" },
{ 'ه', "h" }, { 'ی', "i" }, { 'ي', "i" },
// Special
{ 'ئ', "y" }, { 'ء', "" }, { 'ة', "t" }, { 'ى', "a" }, { 'ؤ', "o" },
// Persian digits
{ '۰', "0" }, { '۱', "1" }, { '۲', "2" }, { '۳', "3" }, { '۴', "4" },
{ '۵', "5" }, { '۶', "6" }, { '۷', "7" }, { '۸', "8" }, { '۹', "9" },
// Arabic-Indic digits
{ '٠', "0" }, { '١', "1" }, { '٢', "2" }, { '٣', "3" }, { '٤', "4" },
{ '٥', "5" }, { '٦', "6" }, { '٧', "7" }, { '٨', "8" }, { '٩', "9" },
};
/// <summary>
/// Converts a café name (Persian or Latin) to a URL-safe lowercase slug.
/// Returns an empty string if no valid characters can be extracted.
/// </summary>
public static string Slugify(string input)
{
if (string.IsNullOrWhiteSpace(input)) return string.Empty;
var sb = new StringBuilder(input.Length * 2);
foreach (var ch in input)
{
if (PersianToLatin.TryGetValue(ch, out var latin))
{
sb.Append(latin);
}
else if (char.IsAsciiLetterOrDigit(ch))
{
sb.Append(char.ToLowerInvariant(ch));
}
else if (ch is ' ' or '-' or '_' or '\t')
{
sb.Append('-');
}
// else: skip punctuation/unsupported characters
}
// Collapse consecutive hyphens and trim
return MultipleHyphen().Replace(sb.ToString(), "-").Trim('-');
}
/// <summary>
/// Returns true if the slug is a valid Koja URL slug:
/// 280 lowercase letters, digits, or internal hyphens. Must start and end with a letter/digit.
/// </summary>
public static bool IsValidSlug(string? slug)
{
if (string.IsNullOrWhiteSpace(slug)) return false;
if (slug.Length < 2 || slug.Length > 80) return false;
return ValidSlugPattern().IsMatch(slug);
}
[GeneratedRegex(@"-{2,}")]
private static partial Regex MultipleHyphen();
[GeneratedRegex(@"^[a-z0-9][a-z0-9\-]*[a-z0-9]$")]
private static partial Regex ValidSlugPattern();
}
@@ -50,8 +50,10 @@ public class SupportTicketService : ISupportTicketService
string cafeId,
CancellationToken cancellationToken = default)
{
return await QueryTickets()
.Where(t => t.CafeId == cafeId)
// NOTE: The Where MUST be applied on the EF entity set BEFORE the Select projection.
// Applying Where() after Select() onto a DTO record causes an EF translation error
// because EF can't translate "new SupportTicketDto(...).CafeId == x".
return await QueryTickets(cafeId)
.OrderByDescending(t => t.UpdatedAt)
.ToListAsync(cancellationToken);
}
@@ -119,11 +121,10 @@ public class SupportTicketService : ISupportTicketService
SupportTicketStatus? status,
CancellationToken cancellationToken = default)
{
var q = QueryTickets();
if (status.HasValue)
q = q.Where(t => t.Status == status.Value);
return await q.OrderByDescending(t => t.UpdatedAt).ToListAsync(cancellationToken);
// status filter is applied on the entity before projection — safe for EF translation.
return await QueryTickets(cafeId: null, status: status)
.OrderByDescending(t => t.UpdatedAt)
.ToListAsync(cancellationToken);
}
public async Task<SupportTicketDetailDto?> GetAdminAsync(
@@ -185,22 +186,36 @@ public class SupportTicketService : ISupportTicketService
return await GetAdminAsync(ticketId, cancellationToken);
}
private IQueryable<SupportTicketDto> QueryTickets() =>
_db.SupportTickets
.AsNoTracking()
.Select(t => new SupportTicketDto(
t.Id,
t.CafeId,
t.Cafe != null ? t.Cafe.Name : "",
t.Subject,
t.Status,
t.Priority,
t.CreatedByEmployeeId,
t.CreatedByEmployee != null ? t.CreatedByEmployee.Name : null,
t.AssignedAdminId,
t.CreatedAt,
t.UpdatedAt,
t.Messages.Count));
/// <summary>
/// Builds an EF-translatable query for support ticket list rows.
/// Filters are applied on the entity BEFORE the Select projection to avoid EF translation errors.
/// </summary>
private IQueryable<SupportTicketDto> QueryTickets(
string? cafeId = null,
SupportTicketStatus? status = null)
{
var q = _db.SupportTickets.AsNoTracking().AsQueryable();
// Apply entity-level filters BEFORE Select so EF can translate them.
if (cafeId is not null)
q = q.Where(t => t.CafeId == cafeId);
if (status.HasValue)
q = q.Where(t => t.Status == status.Value);
return q.Select(t => new SupportTicketDto(
t.Id,
t.CafeId,
t.Cafe != null ? t.Cafe.Name : "",
t.Subject,
t.Status,
t.Priority,
t.CreatedByEmployeeId,
t.CreatedByEmployee != null ? t.CreatedByEmployee.Name : null,
t.AssignedAdminId,
t.CreatedAt,
t.UpdatedAt,
t.Messages.Count));
}
private static SupportTicketDto MapTicket(SupportTicket t) =>
new(