feat: improve custom fields admin and date filters
This commit is contained in:
parent
11a4b903c4
commit
b721348e19
14 changed files with 491 additions and 205 deletions
|
|
@ -541,11 +541,22 @@ async function fetchTicketFieldsByScopes(
|
|||
scopes: string[],
|
||||
companyId: Id<"companies"> | null
|
||||
): Promise<TicketFieldScopeMap> {
|
||||
const uniqueScopes = Array.from(new Set(scopes.filter((scope) => Boolean(scope))));
|
||||
if (uniqueScopes.length === 0) {
|
||||
const scopeLookup = new Map<string, string>();
|
||||
scopes.forEach((scope) => {
|
||||
const trimmed = scope?.trim();
|
||||
if (!trimmed) {
|
||||
return;
|
||||
}
|
||||
const normalized = trimmed.toLowerCase();
|
||||
if (!scopeLookup.has(normalized)) {
|
||||
scopeLookup.set(normalized, trimmed);
|
||||
}
|
||||
});
|
||||
|
||||
if (!scopeLookup.size) {
|
||||
return new Map();
|
||||
}
|
||||
const scopeSet = new Set(uniqueScopes);
|
||||
|
||||
const companyIdStr = companyId ? String(companyId) : null;
|
||||
const result: TicketFieldScopeMap = new Map();
|
||||
const allFields = await ctx.db
|
||||
|
|
@ -553,21 +564,45 @@ async function fetchTicketFieldsByScopes(
|
|||
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
||||
.collect();
|
||||
|
||||
for (const field of allFields) {
|
||||
const scope = field.scope ?? "";
|
||||
if (!scopeSet.has(scope)) {
|
||||
continue;
|
||||
const addFieldToScope = (scopeKey: string, field: Doc<"ticketFields">) => {
|
||||
const originalKey = scopeLookup.get(scopeKey);
|
||||
if (!originalKey) {
|
||||
return;
|
||||
}
|
||||
const current = result.get(originalKey);
|
||||
if (current) {
|
||||
current.push(field);
|
||||
} else {
|
||||
result.set(originalKey, [field]);
|
||||
}
|
||||
};
|
||||
|
||||
const normalizedScopeSet = new Set(scopeLookup.keys());
|
||||
|
||||
for (const field of allFields) {
|
||||
const rawScope = field.scope?.trim();
|
||||
const normalizedFieldScope = rawScope && rawScope.length > 0 ? rawScope.toLowerCase() : "all";
|
||||
const fieldCompanyId = field.companyId ? String(field.companyId) : null;
|
||||
if (fieldCompanyId && (!companyIdStr || companyIdStr !== fieldCompanyId)) {
|
||||
continue;
|
||||
}
|
||||
const current = result.get(scope);
|
||||
if (current) {
|
||||
current.push(field);
|
||||
} else {
|
||||
result.set(scope, [field]);
|
||||
|
||||
if (normalizedFieldScope === "all") {
|
||||
normalizedScopeSet.forEach((scopeKey) => addFieldToScope(scopeKey, field));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (normalizedFieldScope === "default") {
|
||||
if (normalizedScopeSet.has("default")) {
|
||||
addFieldToScope("default", field);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!normalizedScopeSet.has(normalizedFieldScope)) {
|
||||
continue;
|
||||
}
|
||||
addFieldToScope(normalizedFieldScope, field);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -2915,7 +2950,16 @@ export const listTicketForms = query({
|
|||
const viewerRole = (viewer.role ?? "").toUpperCase()
|
||||
const templates = await fetchTemplateSummaries(ctx, tenantId)
|
||||
|
||||
const scopes = templates.map((template) => template.key)
|
||||
const defaultTemplate: TemplateSummary = {
|
||||
key: "default",
|
||||
label: "Chamado",
|
||||
description: "Campos adicionais exibidos em chamados gerais ou sem template específico.",
|
||||
defaultEnabled: true,
|
||||
}
|
||||
|
||||
const templatesWithDefault = [defaultTemplate, ...templates.filter((template) => template.key !== "default")]
|
||||
|
||||
const scopes = templatesWithDefault.map((template) => template.key)
|
||||
const fieldsByScope = await fetchTicketFieldsByScopes(ctx, tenantId, scopes, viewerCompanyId)
|
||||
|
||||
const staffOverride = viewerRole === "ADMIN" || viewerRole === "AGENT"
|
||||
|
|
@ -2938,7 +2982,7 @@ export const listTicketForms = query({
|
|||
}>
|
||||
}>
|
||||
|
||||
for (const template of templates) {
|
||||
for (const template of templatesWithDefault) {
|
||||
const templateSettings = settingsByTemplate.get(template.key) ?? []
|
||||
let enabled = staffOverride
|
||||
? true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue