|
| 1 | +import { |
| 2 | + DisplayProperty, |
| 3 | + DisplayPropertySchema, |
| 4 | + EventSpecificationSchema, |
| 5 | +} from "@trigger.dev/internal"; |
| 6 | +import { PrismaClient, Prisma, prisma } from "~/db.server"; |
| 7 | +import { Organization } from "~/models/organization.server"; |
| 8 | +import { Project } from "~/models/project.server"; |
| 9 | +import { User } from "~/models/user.server"; |
| 10 | +import { z } from "zod"; |
| 11 | + |
| 12 | +export class JobListPresenter { |
| 13 | + #prismaClient: PrismaClient; |
| 14 | + |
| 15 | + constructor(prismaClient: PrismaClient = prisma) { |
| 16 | + this.#prismaClient = prismaClient; |
| 17 | + } |
| 18 | + |
| 19 | + public async call({ |
| 20 | + userId, |
| 21 | + projectSlug, |
| 22 | + organizationSlug, |
| 23 | + integrationSlug, |
| 24 | + }: { |
| 25 | + userId: User["id"]; |
| 26 | + projectSlug: Project["slug"]; |
| 27 | + organizationSlug?: Organization["slug"]; |
| 28 | + integrationSlug?: string; |
| 29 | + }) { |
| 30 | + const orgWhere: Prisma.JobWhereInput["organization"] = organizationSlug |
| 31 | + ? { slug: organizationSlug, members: { some: { userId } } } |
| 32 | + : { members: { some: { userId } } }; |
| 33 | + |
| 34 | + const integrationsWhere: Prisma.JobWhereInput["integrations"] = |
| 35 | + integrationSlug |
| 36 | + ? { some: { integration: { slug: integrationSlug } } } |
| 37 | + : {}; |
| 38 | + |
| 39 | + const jobs = await this.#prismaClient.job.findMany({ |
| 40 | + select: { |
| 41 | + id: true, |
| 42 | + slug: true, |
| 43 | + title: true, |
| 44 | + aliases: { |
| 45 | + select: { |
| 46 | + version: { |
| 47 | + select: { |
| 48 | + version: true, |
| 49 | + eventSpecification: true, |
| 50 | + properties: true, |
| 51 | + runs: { |
| 52 | + select: { |
| 53 | + createdAt: true, |
| 54 | + status: true, |
| 55 | + }, |
| 56 | + take: 1, |
| 57 | + orderBy: [{ createdAt: "desc" }], |
| 58 | + }, |
| 59 | + integrations: { |
| 60 | + select: { |
| 61 | + key: true, |
| 62 | + integration: { |
| 63 | + select: { |
| 64 | + slug: true, |
| 65 | + definition: true, |
| 66 | + setupStatus: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + environment: { |
| 74 | + select: { |
| 75 | + type: true, |
| 76 | + orgMember: { |
| 77 | + select: { |
| 78 | + userId: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + where: { |
| 85 | + name: "latest", |
| 86 | + }, |
| 87 | + }, |
| 88 | + dynamicTriggers: { |
| 89 | + select: { |
| 90 | + type: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + where: { |
| 95 | + internal: false, |
| 96 | + organization: orgWhere, |
| 97 | + project: { |
| 98 | + slug: projectSlug, |
| 99 | + }, |
| 100 | + integrations: integrationsWhere, |
| 101 | + }, |
| 102 | + orderBy: [{ title: "asc" }], |
| 103 | + }); |
| 104 | + |
| 105 | + return jobs |
| 106 | + .map((job) => { |
| 107 | + //the best alias to select: |
| 108 | + // 1. Logged-in user dev |
| 109 | + // 2. Prod |
| 110 | + // 3. Any other user's dev |
| 111 | + const sortedAliases = job.aliases.sort((a, b) => { |
| 112 | + if ( |
| 113 | + a.environment.type === "DEVELOPMENT" && |
| 114 | + a.environment.orgMember?.userId === userId |
| 115 | + ) { |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + if ( |
| 120 | + b.environment.type === "DEVELOPMENT" && |
| 121 | + b.environment.orgMember?.userId === userId |
| 122 | + ) { |
| 123 | + return 1; |
| 124 | + } |
| 125 | + |
| 126 | + if (a.environment.type === "PRODUCTION") { |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + if (b.environment.type === "PRODUCTION") { |
| 131 | + return 1; |
| 132 | + } |
| 133 | + |
| 134 | + return 0; |
| 135 | + }); |
| 136 | + |
| 137 | + const alias = sortedAliases.at(0); |
| 138 | + |
| 139 | + if (!alias) { |
| 140 | + throw new Error( |
| 141 | + `No aliases found for job ${job.id}, this should never happen.` |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + const eventSpecification = EventSpecificationSchema.parse( |
| 146 | + alias.version.eventSpecification |
| 147 | + ); |
| 148 | + |
| 149 | + const lastRun = |
| 150 | + alias.version.runs[0] != null ? alias.version.runs[0] : undefined; |
| 151 | + |
| 152 | + const integrations = alias.version.integrations.map((integration) => ({ |
| 153 | + key: integration.key, |
| 154 | + title: integration.integration.slug, |
| 155 | + icon: integration.integration.definition.id, |
| 156 | + setupStatus: integration.integration.setupStatus, |
| 157 | + })); |
| 158 | + |
| 159 | + let properties: DisplayProperty[] = []; |
| 160 | + |
| 161 | + if (eventSpecification.properties) { |
| 162 | + properties = [...properties, ...eventSpecification.properties]; |
| 163 | + } |
| 164 | + |
| 165 | + if (alias.version.properties) { |
| 166 | + const versionProperties = z |
| 167 | + .array(DisplayPropertySchema) |
| 168 | + .parse(alias.version.properties); |
| 169 | + properties = [...properties, ...versionProperties]; |
| 170 | + } |
| 171 | + |
| 172 | + return { |
| 173 | + id: job.id, |
| 174 | + slug: job.slug, |
| 175 | + title: job.title, |
| 176 | + version: alias.version.version, |
| 177 | + dynamic: job.dynamicTriggers.length > 0, |
| 178 | + event: { |
| 179 | + title: eventSpecification.title, |
| 180 | + icon: eventSpecification.icon, |
| 181 | + source: eventSpecification.source, |
| 182 | + }, |
| 183 | + integrations, |
| 184 | + hasIntegrationsRequiringAction: integrations.some( |
| 185 | + (i) => i.setupStatus === "MISSING_FIELDS" |
| 186 | + ), |
| 187 | + lastRun, |
| 188 | + properties, |
| 189 | + }; |
| 190 | + }) |
| 191 | + .filter(Boolean); |
| 192 | + } |
| 193 | +} |
0 commit comments