|
1 | 1 | import { Initiative } from '../utils/enums/application.ts'; |
2 | 2 |
|
3 | 3 | import type { PrismaTransactionClient } from '../db/dataConnection.ts'; |
4 | | -import type { ListPermitsOptions, Permit, PermitBase, PermitSearchParams, PermitType } from '../types/index.ts'; |
| 4 | +import type { |
| 5 | + ListPermitsOptions, |
| 6 | + Permit, |
| 7 | + PermitBase, |
| 8 | + PermitSearchParams, |
| 9 | + PermitType, |
| 10 | + SearchPermitsOptions |
| 11 | +} from '../types/index.ts'; |
5 | 12 |
|
6 | 13 | /** |
7 | 14 | * Delete a specific permit |
@@ -166,6 +173,144 @@ export const searchPermits = async (tx: PrismaTransactionClient, params: PermitS |
166 | 173 | return response; |
167 | 174 | }; |
168 | 175 |
|
| 176 | +/** |
| 177 | + * Retrieve all permits if no activityId is provided, otherwise retrieve permits for a specific activity |
| 178 | + * @param tx Prisma transaction client |
| 179 | + * @param options Optional filtering parameters |
| 180 | + * @param options.activityId Optional PCNS Activity ID |
| 181 | + * @param options.includeNotes Optional flag to include permit notes |
| 182 | + * @returns A Promise that resolves to an object with permits array and total count |
| 183 | + */ |
| 184 | +export const searchPermitsPCNS = async ( |
| 185 | + tx: PrismaTransactionClient, |
| 186 | + options: SearchPermitsOptions |
| 187 | +): Promise<{ permits: Permit[]; totalRecords: number }> => { |
| 188 | + console.log('\n\nsearchPermitsPCNS options:\n\n', options); |
| 189 | + |
| 190 | + // Determine project table based on initiative |
| 191 | + const projectTableMap: Record<Initiative, string> = { |
| 192 | + [Initiative.HOUSING]: 'housingProject', |
| 193 | + [Initiative.ELECTRIFICATION]: 'electrificationProject', |
| 194 | + [Initiative.PCNS]: 'housingProject' // FIXME remove |
| 195 | + }; |
| 196 | + |
| 197 | + const projectTable = projectTableMap[options.initiative]; |
| 198 | + |
| 199 | + // Build dynamic orderBy clause |
| 200 | + const sortDirection = options?.sortOrder === '1' ? 'asc' : 'desc'; |
| 201 | + const validSortFields = ['submittedDate', 'state', 'stage', 'statusLastChanged', 'decisionDate', 'projectName']; |
| 202 | + |
| 203 | + //FIXME |
| 204 | + //eslint-disable-next-line |
| 205 | + let orderBy: any = { submittedDate: 'desc' }; // default sorting |
| 206 | + |
| 207 | + if (options?.sortField && validSortFields.includes(options.sortField)) { |
| 208 | + orderBy = { [options.sortField]: sortDirection }; |
| 209 | + } |
| 210 | + |
| 211 | + console.log('\n\nsearchPermitsPCNS orderBy clause:\n\n', orderBy); |
| 212 | + |
| 213 | + // Build where clause (reused for both count and findMany) |
| 214 | + const whereClause = { |
| 215 | + AND: [ |
| 216 | + // Ensure permit has at least one project |
| 217 | + { |
| 218 | + activity: { |
| 219 | + [projectTable]: { |
| 220 | + some: {} |
| 221 | + } |
| 222 | + } |
| 223 | + }, |
| 224 | + options.dateRange |
| 225 | + ? { |
| 226 | + OR: [ |
| 227 | + { submittedDate: { gte: options.dateRange[0], lte: options.dateRange[1] } }, |
| 228 | + { decisionDate: { gte: options.dateRange[0], lte: options.dateRange[1] } }, |
| 229 | + { statusLastChanged: { gte: options.dateRange[0], lte: options.dateRange[1] } } |
| 230 | + ] |
| 231 | + } |
| 232 | + : {}, |
| 233 | + options?.permitTypeId ? { permitTypeId: parseInt(options.permitTypeId) } : {}, |
| 234 | + options?.sourceSystemKindId |
| 235 | + ? { |
| 236 | + permitTracking: { |
| 237 | + some: { |
| 238 | + sourceSystemKindId: parseInt(options.sourceSystemKindId) |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + : {}, |
| 243 | + options?.searchTag |
| 244 | + ? { |
| 245 | + OR: [ |
| 246 | + { activityId: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 247 | + { stage: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 248 | + { state: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 249 | + { permitType: { name: { contains: options.searchTag, mode: 'insensitive' as const } } }, |
| 250 | + { permitType: { businessDomain: { contains: options.searchTag, mode: 'insensitive' as const } } }, |
| 251 | + { |
| 252 | + activity: { |
| 253 | + [projectTable]: { |
| 254 | + some: { |
| 255 | + OR: [ |
| 256 | + { projectName: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 257 | + { companyNameRegistered: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 258 | + { streetAddress: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 259 | + { locality: { contains: options.searchTag, mode: 'insensitive' as const } }, |
| 260 | + { province: { contains: options.searchTag, mode: 'insensitive' as const } } |
| 261 | + ] |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + ] |
| 267 | + } |
| 268 | + : {} |
| 269 | + ] |
| 270 | + }; |
| 271 | + |
| 272 | + // Get total count (without pagination) |
| 273 | + const totalRecords = await tx.permit.count({ |
| 274 | + where: whereClause |
| 275 | + }); |
| 276 | + |
| 277 | + // Get paginated data |
| 278 | + const permits = await tx.permit.findMany({ |
| 279 | + skip: options?.skip ? parseInt(options.skip) : 0, |
| 280 | + take: options?.take ? parseInt(options.take) : 10, |
| 281 | + where: whereClause, |
| 282 | + orderBy: orderBy, |
| 283 | + include: { |
| 284 | + permitType: true, |
| 285 | + permitTracking: { |
| 286 | + include: { |
| 287 | + sourceSystemKind: true |
| 288 | + } |
| 289 | + }, |
| 290 | + activity: { |
| 291 | + include: { |
| 292 | + [projectTable]: true |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + }); |
| 297 | + |
| 298 | + // Map the results to alias the project table as 'project' |
| 299 | + const permitsWithProjectAlias = permits.map((permit) => { |
| 300 | + const { [projectTable]: projectData, ...activityData } = permit.activity; |
| 301 | + |
| 302 | + return { |
| 303 | + ...permit, |
| 304 | + activity: { |
| 305 | + ...activityData, |
| 306 | + project: projectData |
| 307 | + } |
| 308 | + }; |
| 309 | + }); |
| 310 | + |
| 311 | + return { permits: permitsWithProjectAlias, totalRecords }; |
| 312 | +}; |
| 313 | + |
169 | 314 | /** |
170 | 315 | * Upsert a Permit |
171 | 316 | * @param tx Prisma transaction client |
|
0 commit comments