()
| 19 | } |
| 20 | |
| 21 | export async function lookupByClient() { |
| 22 | console.log(chalk.blue('\n🔑 Lookup by Client ID\n')); |
| 23 | console.log('Loading clients...\n'); |
| 24 | |
| 25 | const clients = await db.client.findMany({ |
| 26 | include: { |
| 27 | organization: true, |
| 28 | project: true, |
| 29 | }, |
| 30 | orderBy: { |
| 31 | name: 'asc', |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | if (clients.length === 0) { |
| 36 | console.log(chalk.red('No clients found.')); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | const searchItems: ClientSearchItem[] = clients.map((client) => ({ |
| 41 | id: client.id, |
| 42 | name: client.name, |
| 43 | organizationId: client.organizationId, |
| 44 | organizationName: client.organization.name, |
| 45 | projectId: client.projectId, |
| 46 | projectName: client.project?.name || null, |
| 47 | displayText: `${client.organization.name} → ${client.project?.name || '[No Project]'} → ${client.name} ${chalk.gray(`(${client.id})`)}`, |
| 48 | })); |
| 49 | |
| 50 | const searchFunction = async (_answers: unknown, input = '') => { |
| 51 | const fuzzyResult = fuzzy.filter(input, searchItems, { |
| 52 | extract: (item: ClientSearchItem) => |
| 53 | `${item.organizationName} ${item.projectName || ''} ${item.name} ${item.id}`, |
| 54 | }); |
| 55 | |
| 56 | return fuzzyResult.map((result: fuzzy.FilterResult<ClientSearchItem>) => ({ |
| 57 | name: result.original.displayText, |
| 58 | value: result.original, |
| 59 | })); |
| 60 | }; |
| 61 | |
| 62 | const { selectedClient } = (await inquirer.prompt([ |
| 63 | { |
| 64 | type: 'autocomplete', |
| 65 | name: 'selectedClient', |
| 66 | message: 'Search for a client:', |
| 67 | source: searchFunction, |
| 68 | pageSize: 15, |
| 69 | }, |
| 70 | ])) as { selectedClient: ClientSearchItem }; |
| 71 | |
| 72 | // Fetch full organization details |
| 73 | const organization = await db.organization.findUnique({ |
| 74 | where: { |
| 75 | id: selectedClient.organizationId, |
| 76 | }, |
| 77 | include: { |
| 78 | projects: { |
no test coverage detected