import type { RoutineCategoryDto, RoutineDto, RoutineEntityLink, RoutineEntityRefDto, RoutineTaskDto, RoutineTaskEventDto, } from '@mento/shared'; import type { Prisma } from '@prisma/client'; import { describeRecurrence } from './recurrence'; import { dateToDayKey } from './routine-dates'; const userSelect = { select: { id: true, fullName: true } } as const; export const routineTaskInclude = { category: true, assignee: userSelect, createdBy: userSelect, routine: { select: { id: true, allowSkip: true, skipReasonRequired: true, reportRequired: true, allowAfterDeadline: true, entityLink: true, }, }, lead: { select: { id: true, title: true, contactName: true } }, person: { select: { id: true, firstName: true, lastName: true } }, company: { select: { id: true, name: true } }, deal: { select: { id: true, title: true } }, invoice: { select: { id: true, number: true, title: true } }, activity: { select: { id: true, title: true } }, } satisfies Prisma.RoutineTaskInclude; export type RoutineTaskRow = Prisma.RoutineTaskGetPayload<{ include: typeof routineTaskInclude; }>; export const routineInclude = { category: true, assignee: userSelect, createdBy: userSelect, team: { select: { id: true, name: true } }, } satisfies Prisma.RoutineInclude; export type RoutineRow = Prisma.RoutineGetPayload<{ include: typeof routineInclude }>; export function toCategoryDto(row: { id: string; name: string; color: string; sortOrder: number; }): RoutineCategoryDto { return { id: row.id, name: row.name, color: row.color, sortOrder: row.sortOrder }; } function normalizeEntityLink(value: string | null | undefined): RoutineEntityLink { return value === 'optional' || value === 'lead' || value === 'customer' ? value : 'none'; } export function toTaskDto(row: RoutineTaskRow): RoutineTaskDto { const entities: RoutineEntityRefDto[] = []; if (row.lead) { entities.push({ type: 'lead', id: row.lead.id, label: row.lead.title || row.lead.contactName || 'لید' }); } if (row.person) { entities.push({ type: 'person', id: row.person.id, label: `${row.person.firstName} ${row.person.lastName}`.trim(), }); } if (row.company) entities.push({ type: 'company', id: row.company.id, label: row.company.name }); if (row.deal) entities.push({ type: 'deal', id: row.deal.id, label: row.deal.title }); if (row.invoice) { entities.push({ type: 'invoice', id: row.invoice.id, label: row.invoice.title || `فاکتور ${row.invoice.number}`, }); } if (row.activity) entities.push({ type: 'activity', id: row.activity.id, label: row.activity.title }); const r = row.routine; return { id: row.id, routineId: row.routineId, title: row.title, description: row.description, status: row.status, priority: row.priority, category: row.category ? toCategoryDto(row.category) : null, scheduledFor: dateToDayKey(row.scheduledFor), dueAt: row.dueAt?.toISOString() ?? null, startedAt: row.startedAt?.toISOString() ?? null, completedAt: row.completedAt?.toISOString() ?? null, resultNote: row.resultNote, skipReason: row.skipReason, source: row.source, isMandatory: row.isMandatory, rules: { allowSkip: r ? r.allowSkip : true, skipReasonRequired: r ? r.skipReasonRequired : false, reportRequired: r ? r.reportRequired : false, allowAfterDeadline: r ? r.allowAfterDeadline : true, entityLink: normalizeEntityLink(r?.entityLink ?? 'optional'), }, assignee: row.assignee, createdBy: row.createdBy, entities, createdAt: row.createdAt.toISOString(), updatedAt: row.updatedAt.toISOString(), }; } export function toEventDto(row: { id: string; type: string; fromStatus: RoutineTaskDto['status'] | null; toStatus: RoutineTaskDto['status'] | null; note: string | null; createdAt: Date; actor: { id: string; fullName: string } | null; }): RoutineTaskEventDto { return { id: row.id, type: row.type as RoutineTaskEventDto['type'], fromStatus: row.fromStatus, toStatus: row.toStatus, note: row.note, actor: row.actor, createdAt: row.createdAt.toISOString(), }; } export function routineRule(row: { recurrence: RoutineDto['recurrence']; startDate: Date; endDate: Date | null; weekdays: number[]; intervalDays: number | null; monthDay: number | null; specificDates: Date[]; }) { return { recurrence: row.recurrence, startDate: dateToDayKey(row.startDate), endDate: row.endDate ? dateToDayKey(row.endDate) : null, weekdays: row.weekdays, intervalDays: row.intervalDays, monthDay: row.monthDay, specificDates: row.specificDates.map(dateToDayKey), }; } export function toRoutineDto( row: RoutineRow, execution: { total: number; done: number } = { total: 0, done: 0 }, ): RoutineDto { const rule = routineRule(row); return { id: row.id, title: row.title, description: row.description, assignee: row.assignee, team: row.team, category: row.category ? toCategoryDto(row.category) : null, priority: row.priority, timeOfDay: row.timeOfDay, durationMinutes: row.durationMinutes, startDate: rule.startDate, endDate: rule.endDate, recurrence: row.recurrence, weekdays: row.weekdays, intervalDays: row.intervalDays, monthDay: row.monthDay, specificDates: rule.specificDates, isMandatory: row.isMandatory, allowSkip: row.allowSkip, skipReasonRequired: row.skipReasonRequired, reportRequired: row.reportRequired, allowAfterDeadline: row.allowAfterDeadline, carryOverOverdue: row.carryOverOverdue, entityLink: normalizeEntityLink(row.entityLink), reminderMinutesBefore: row.reminderMinutesBefore, isActive: row.isActive, createdById: row.createdById, createdBy: row.createdBy, recurrenceLabel: describeRecurrence(rule), executionRate: execution.total ? Math.round((execution.done / execution.total) * 100) : null, executionTotal: execution.total, executionDone: execution.done, createdAt: row.createdAt.toISOString(), updatedAt: row.updatedAt.toISOString(), }; }