import type { IncludeItem, ListQuery, WhereClause } from '../../../core' import type { Campaign, CampaignStatus } from '../types' export type GetManyPaginatedFilters = { title?: string status?: CampaignStatus serviceId?: string createdAtFrom?: string createdAtTo?: string startedAtFrom?: string startedAtTo?: string quality?: string page?: number perPage?: number sort?: string order?: 'ASC' | 'DESC' paginate?: boolean } export function getManyViewScope( filters: GetManyPaginatedFilters = {}, ): ListQuery & { paginate: true } { const where: WhereClause = {} const $and: WhereClause[] = [] if (filters.title) { where.title = { $iLike: `%${filters.title}%` } } if (filters.status) { where.status = filters.status } if (filters.serviceId) { where.serviceId = filters.serviceId } if (filters.createdAtFrom) { $and.push({ createdAt: { $gte: filters.createdAtFrom } }) } if (filters.createdAtTo) { $and.push({ createdAt: { $lte: filters.createdAtTo } }) } if (filters.startedAtFrom) { $and.push({ startedAt: { $gte: filters.startedAtFrom } }) } if (filters.startedAtTo) { $and.push({ startedAt: { $lte: filters.startedAtTo } }) } if ($and.length > 0) { where.$and = $and } const include: IncludeItem[] = [ 'service', 'tags', { model: 'messages', ...(filters.quality && { required: true }), include: [ 'file', { model: 'hsm', ...(filters.quality && { where: { quality: filters.quality }, required: true }), }, ], }, { model: 'createdBy', attributes: ['id', 'name'] }, { model: 'sentBy', attributes: ['id', 'name'] }, ] return { where, order: [[(filters.sort ?? 'createdAt') as keyof Campaign & string, filters.order ?? 'DESC']], page: filters.page ?? 1, perPage: filters.perPage ?? 15, paginate: true as const, ...(filters.quality && { subQuery: false }), include, } }