42 lines
1.9 KiB
JavaScript
42 lines
1.9 KiB
JavaScript
import { dv, fv } from "./dataverse";
|
|
import { resolveOwnerIds } from "./queries";
|
|
import { parseCodigos } from "./participantes";
|
|
|
|
// Oportunidades creadas desde `desde` y citas completadas desde `desde`, con los campos de participación.
|
|
// El indicador se calcula en la página (lib/participantes.js → tieneGerencia suma el Sí/No actual y el campo
|
|
// múltiple histórico). scope: { emails } de ownerFilter(); emails=[] no filtra por propietario.
|
|
export async function getParticipacion(accessToken, scope, desde) {
|
|
let owner = "";
|
|
if (scope.emails?.length) {
|
|
const ids = await resolveOwnerIds(scope.emails, accessToken);
|
|
if (!ids.length) return { opps: [], citas: [] };
|
|
owner = ` and (${ids.map((id) => `_ownerid_value eq ${id}`).join(" or ")})`;
|
|
}
|
|
const [o, c] = await Promise.all([
|
|
dv(
|
|
`opportunities?$select=opportunityid,createdon,statecode,alfa_typeopportunity,_ownerid_value,alfa_participaciondegv,alfa_participantes` +
|
|
`&$filter=${encodeURIComponent(`createdon ge ${desde}${owner}`)}&$top=5000`,
|
|
accessToken
|
|
),
|
|
dv(
|
|
`appointments?$select=activityid,actualend,_ownerid_value,_regardingobjectid_value,alfa_participantes` +
|
|
`&$filter=${encodeURIComponent(`statecode eq 1 and actualend ge ${desde}${owner}`)}&$top=5000`,
|
|
accessToken
|
|
),
|
|
]);
|
|
const opps = (o.value || []).map((r) => ({
|
|
id: r.opportunityid,
|
|
mes: r.createdon ? r.createdon.slice(0, 7) : null,
|
|
tipo: fv(r, "alfa_typeopportunity"),
|
|
owner: fv(r, "_ownerid_value") || "—",
|
|
gv: r.alfa_participaciondegv === true,
|
|
participantes: parseCodigos(r.alfa_participantes),
|
|
}));
|
|
const citas = (c.value || []).map((r) => ({
|
|
id: r.activityid,
|
|
owner: fv(r, "_ownerid_value") || "—",
|
|
oppId: r._regardingobjectid_value || null,
|
|
participantes: parseCodigos(r.alfa_participantes),
|
|
}));
|
|
return { opps, citas, truncado: (o.value || []).length >= 5000 || (c.value || []).length >= 5000 };
|
|
}
|