feat: separate prior records behind an owner switch and count last-period questions

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-15 10:48:30 +03:30
co-authored by Cursor
parent 3a8a684aa4
commit 6bc5e1bec7
29 changed files with 775 additions and 77 deletions
+45 -15
View File
@@ -26,6 +26,11 @@ import {
ticketAccessWhere,
} from '../common/record-scope';
import { decimalToBase, loadOrgRates, toBaseAmount } from '../common/currency';
import {
activityPriorWhere,
dealPriorWhere,
personPriorWhere,
} from '../common/prior-archive';
import {
addJalaliMonths,
dateToJalali,
@@ -57,6 +62,9 @@ export class ReportsService {
const prevStart = new Date(prevEnd.getTime() - durationMs);
const now = new Date();
const peoplePrior = personPriorWhere(actor.priorLens);
const dealsPrior = dealPriorWhere(actor.priorLens, now);
const activitiesPrior = activityPriorWhere(actor.priorLens, now);
const { jy, jm } = dateToJalali(now);
const sixAgo = addJalaliMonths(jy, jm, -5);
const sixMonthStart = startOfJalaliMonth(sixAgo.jy, sixAgo.jm);
@@ -76,15 +84,20 @@ export class ReportsService {
await Promise.all([
this.prisma.deal.findMany({
where: {
organizationId: orgId,
...dealScope,
OR: [
{ status: 'open' },
AND: [
{
status: { in: ['won', 'lost'] },
updatedAt: { gte: dealEarliest },
organizationId: orgId,
...dealScope,
OR: [
{ status: 'open' },
{
status: { in: ['won', 'lost'] },
updatedAt: { gte: dealEarliest },
},
{ createdAt: { gte: dealEarliest } },
],
},
{ createdAt: { gte: dealEarliest } },
dealsPrior,
],
},
select: {
@@ -118,6 +131,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...activityScope,
...activitiesPrior,
createdAt: { gte: prevStart, lte: end },
},
select: {
@@ -146,6 +160,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...personScope,
...peoplePrior,
},
select: { mood: true, createdAt: true },
}),
@@ -1536,6 +1551,9 @@ export class ReportsService {
const yesterdayEnd = endOfDay(yesterday);
const weekAgo = new Date(now);
weekAgo.setDate(weekAgo.getDate() - 7);
const peoplePrior = personPriorWhere(actor.priorLens);
const dealsPrior = dealPriorWhere(actor.priorLens, now);
const activitiesPrior = activityPriorWhere(actor.priorLens, now);
const [
wonDeals,
@@ -1554,6 +1572,7 @@ export class ReportsService {
organizationId: orgId,
status: 'won',
...dealScope,
...dealsPrior,
updatedAt: { gte: addMonthsStart(now, -5) },
},
select: {
@@ -1568,17 +1587,22 @@ export class ReportsService {
},
}),
this.prisma.deal.count({
where: { organizationId: orgId, status: 'open', ...dealScope },
where: { organizationId: orgId, status: 'open', ...dealScope, ...dealsPrior },
}),
this.prisma.deal.findMany({
where: {
organizationId: orgId,
status: 'open',
...dealScope,
OR: [
{ isStale: true },
{ isRotten: true },
{ updatedAt: { lt: daysAgo(5) } },
AND: [
{
organizationId: orgId,
status: 'open',
...dealScope,
OR: [
{ isStale: true },
{ isRotten: true },
{ updatedAt: { lt: daysAgo(5) } },
],
},
dealsPrior,
],
},
select: {
@@ -1594,6 +1618,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...personScope,
...peoplePrior,
createdAt: { gte: curMonthStart, lte: curMonthEnd },
},
}),
@@ -1601,6 +1626,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...personScope,
...peoplePrior,
createdAt: { gte: prevMonthStart, lte: prevMonthEnd },
},
}),
@@ -1608,6 +1634,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...activityScope,
...activitiesPrior,
status: 'open',
dueAt: { gte: todayStart, lte: todayEnd },
},
@@ -1616,6 +1643,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...activityScope,
...activitiesPrior,
dueAt: { gte: yesterdayStart, lte: yesterdayEnd },
},
}),
@@ -1624,6 +1652,7 @@ export class ReportsService {
organizationId: orgId,
status: 'open',
...dealScope,
...dealsPrior,
updatedAt: { lte: weekAgo },
},
}),
@@ -1639,6 +1668,7 @@ export class ReportsService {
where: {
organizationId: orgId,
...activityScope,
...activitiesPrior,
status: 'open',
dueAt: { lt: now },
},