package authz import ( "bssapp-backend/auth" "context" "fmt" "strings" ) func BuildMSSQLPiyasaFilter( ctx context.Context, column string, ) string { claims, ok := auth.GetClaimsFromContext(ctx) if ok && claims != nil && claims.IsAdmin() { return "1=1" } codes := GetPiyasaCodesFromCtx(ctx) if len(codes) == 0 { return "1=0" } return BuildMSSQLPiyasaFilterWithCodes(column, codes) } func BuildMSSQLPiyasaFilterWithCodes(column string, codes []string) string { normalizedCol := fmt.Sprintf("UPPER(LTRIM(RTRIM(%s)))", column) exact := BuildINClause(normalizedCol, codes) prefixCodes := first3Codes(codes) if len(prefixCodes) == 0 { return exact } prefix := BuildINClause( fmt.Sprintf("LEFT(%s, 3)", normalizedCol), prefixCodes, ) return fmt.Sprintf("(%s OR %s)", exact, prefix) } func first3Codes(codes []string) []string { seen := make(map[string]struct{}, len(codes)) out := make([]string, 0, len(codes)) for _, c := range codes { n := strings.ToUpper(strings.TrimSpace(c)) if len(n) < 3 { continue } n = n[:3] if _, ok := seen[n]; ok { continue } seen[n] = struct{}{} out = append(out, n) } return out }