diff --git a/apps/api/src/routers/lead.ts b/apps/api/src/routers/lead.ts index d4742c8..b063de5 100644 --- a/apps/api/src/routers/lead.ts +++ b/apps/api/src/routers/lead.ts @@ -61,11 +61,13 @@ export const leadRouter = router({ .where(eq(leadTouchpoint.leadId, l.id)) .orderBy(desc(leadTouchpoint.datum), desc(leadTouchpoint.createdAt)); const linkedProject = l.projectId - ? (await ctx.db - .select({ id: project.id, name: project.name }) - .from(project) - .where(eq(project.id, l.projectId)) - .limit(1))[0] ?? null + ? (( + await ctx.db + .select({ id: project.id, name: project.name }) + .from(project) + .where(eq(project.id, l.projectId)) + .limit(1) + )[0] ?? null) : null; return { lead: l, @@ -75,58 +77,55 @@ export const leadRouter = router({ }; }), - create: protectedProcedure - .input(LeadSchemas.createLeadInput) - .mutation(async ({ ctx, input }) => { - const areaId = await getSalesAreaId(ctx.db); - const [created] = await ctx.db - .insert(lead) - .values({ - areaId, - firma: input.firma, - status: input.status, - score: input.score, - ownerUserId: input.ownerUserId ?? null, - projectId: input.projectId ?? null, - branche: input.branche ?? null, - subSegment: input.subSegment ?? null, - website: input.website ?? null, - quelle: input.quelle ?? null, - createdBy: ctx.user.id, - }) - .returning(); - if (!created) throw new Error('Lead konnte nicht angelegt werden.'); - return created; - }), + create: protectedProcedure.input(LeadSchemas.createLeadInput).mutation(async ({ ctx, input }) => { + const areaId = await getSalesAreaId(ctx.db); + const [created] = await ctx.db + .insert(lead) + .values({ + areaId, + firma: input.firma, + status: input.status, + score: input.score, + ownerUserId: input.ownerUserId ?? null, + projectId: input.projectId ?? null, + branche: input.branche ?? null, + subSegment: input.subSegment ?? null, + website: input.website ?? null, + quelle: input.quelle ?? null, + createdBy: ctx.user.id, + }) + .returning(); + if (!created) throw new Error('Lead konnte nicht angelegt werden.'); + return created; + }), - update: protectedProcedure - .input(LeadSchemas.updateLeadInput) - .mutation(async ({ ctx, input }) => { - const { id, ...patch } = input; - if (Object.keys(patch).length === 0) { - const existing = (await ctx.db.select().from(lead).where(eq(lead.id, id)).limit(1))[0]; - if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lead nicht gefunden.' }); - return existing; - } - const [updated] = await ctx.db - .update(lead) - .set({ ...patch, updatedAt: new Date() }) - .where(eq(lead.id, id)) - .returning(); - if (!updated) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lead nicht gefunden.' }); - return updated; - }), + update: protectedProcedure.input(LeadSchemas.updateLeadInput).mutation(async ({ ctx, input }) => { + const { id, ...patch } = input; + if (Object.keys(patch).length === 0) { + const existing = (await ctx.db.select().from(lead).where(eq(lead.id, id)).limit(1))[0]; + if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lead nicht gefunden.' }); + return existing; + } + const [updated] = await ctx.db + .update(lead) + .set({ ...patch, updatedAt: new Date() }) + .where(eq(lead.id, id)) + .returning(); + if (!updated) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lead nicht gefunden.' }); + return updated; + }), // Kontaktpersonen ----------------------------------------------------------- addContact: protectedProcedure .input(LeadSchemas.addContactInput) .mutation(async ({ ctx, input }) => { - const count = ( - await ctx.db - .select({ c: sql`count(*)::int` }) - .from(leadContact) - .where(eq(leadContact.leadId, input.leadId)) - )[0]?.c ?? 0; + const count = + ( + await ctx.db + .select({ c: sql`count(*)::int` }) + .from(leadContact) + .where(eq(leadContact.leadId, input.leadId)) + )[0]?.c ?? 0; const [created] = await ctx.db .insert(leadContact) .values({ @@ -222,7 +221,13 @@ export const leadRouter = router({ .mutation(async ({ ctx, input }) => { const areaId = await getSalesAreaId(ctx.db); const rows = parseCsv(input.csv); - const summary = { created: 0, skippedEmpty: 0, skippedDuplicate: 0, contacts: 0, touchpoints: 0 }; + const summary = { + created: 0, + skippedEmpty: 0, + skippedDuplicate: 0, + contacts: 0, + touchpoints: 0, + }; if (rows.length < 2 || !rows[0]) return summary; const header = rows[0].map(normalizeHeader); @@ -243,7 +248,8 @@ export const leadRouter = router({ branche: col('branche'), notizen: col('notizen'), }; - const get = (row: string[], i: number) => (i >= 0 && i < row.length ? (row[i] ?? '').trim() : ''); + const get = (row: string[], i: number) => + i >= 0 && i < row.length ? (row[i] ?? '').trim() : ''; const existing = await ctx.db.select({ firma: lead.firma }).from(lead); const seen = new Set(existing.map((e) => e.firma.trim().toLowerCase()));