fix: exclude terminated workflows from completed trends

This commit is contained in:
江林铖 2026-09-01 18:52:24 +08:00
parent 40edf22126
commit 57e95ae770
2 changed files with 15 additions and 1 deletions

View File

@ -41,4 +41,14 @@ describe('business trends aggregation', () => {
expect(result.brands.map((brand) => brand.brand)).toEqual([...BUSINESS_TREND_BRANDS])
expect(result.brands.every((brand) => brand.processes.every((process) => process.weeks.every((week) => week.items.length === 0)))).toBe(true)
})
it('does not count terminated workflows as completed', () => {
const result = aggregateBusinessTrends([{
id: 'terminated', businessId: null, title: '终止流程', status: 'TERMINATED', processCode: 'ART', projectType: '美术设计流程', brand: '亿家达',
requesterName: '甲', requesterDepartment: '品牌中心', initiatorName: '甲', initiatorDepartment: '品牌中心',
sourceCreatedAt: '2026-08-31T01:00:00.000Z', sourceUpdatedAt: null, sourceFinishedAt: '2026-09-01T01:00:00.000Z', activeTasks: [],
}], { lastSyncedAt: null, syncStatus: 'ready' }, new Date('2026-09-01T02:00:00.000Z'))
const art = result.brands[0].processes.find((process) => process.key === 'art')!
expect(art.weeks.at(-1)).toMatchObject({ startedCount: 1, completedCount: 0 })
})
})

View File

@ -103,6 +103,10 @@ function inRange(value: string | null, start: string, end: string) {
return Number.isFinite(time) && time >= Date.parse(start) && time < Date.parse(end)
}
function isCompletedStatus(value: string) {
return value.trim().toUpperCase() === 'COMPLETED'
}
function processKeyFor(row: BusinessTrendWorkflowRow): BusinessTrendProcessKey | null {
if (row.projectType === '美术设计流程') return 'art'
if (row.projectType === '内容视频和图片拍摄' || row.projectType === '内容视频流程') return 'content'
@ -133,7 +137,7 @@ export function aggregateBusinessTrends(
if (!process) return
process.weeks.forEach((week) => {
const startedInWeek = inRange(row.sourceCreatedAt, week.start, week.end)
const completedInWeek = inRange(row.sourceFinishedAt, week.start, week.end)
const completedInWeek = isCompletedStatus(row.status) && inRange(row.sourceFinishedAt, week.start, week.end)
if (!startedInWeek && !completedInWeek) return
if (startedInWeek) week.startedCount += 1
if (completedInWeek) week.completedCount += 1