mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
`waitUntil` existed because a serverless invocation is frozen the moment it responds — anything still running is killed unless the platform is told to wait. This app is a long-lived Node process on Kubernetes. Nothing freezes, and those promises were never at risk. lib/after.ts is the one call sites make. Inside the App Router it delegates to Next's own `after()`, which schedules the work against the request so it runs after the response is flushed instead of racing it. That needs App Router request context and THROWS without one, and ten of these fifteen sites are Pages Router API routes — so outside that context the promise runs detached, which on a persistent server is all waitUntil ever bought. Either path catches. An unhandled background rejection takes the process down under Node's default policy, and losing a pod because an analytics call 500'd is not a trade worth making — waitUntil swallowed those, so this is not a behaviour we are giving up. @vercel/functions now has no importers. Verified with npx tsc --noEmit: 100 errors before and after, 0 introduced, compared by file+code+message so inserted imports cannot masquerade as new ones. Co-authored-by: Hanzo Dev <[email protected]>
99 lines
2.2 KiB
TypeScript
99 lines
2.2 KiB
TypeScript
import { after } from "@/lib/after";
|
|
|
|
import prisma from "@/lib/prisma";
|
|
import { sendEmail } from "@/lib/resend";
|
|
|
|
import IntegrationInstalled from "@/components/emails/installed-integration-notification";
|
|
|
|
interface InstallIntegration {
|
|
userId: string;
|
|
teamId: string;
|
|
integrationId: string;
|
|
credentials?: Record<string, any>;
|
|
}
|
|
|
|
// Install an integration for a user in a workspace
|
|
export const installIntegration = async ({
|
|
userId,
|
|
teamId,
|
|
integrationId,
|
|
credentials,
|
|
}: InstallIntegration) => {
|
|
const installation = await prisma.installedIntegration.upsert({
|
|
create: {
|
|
userId,
|
|
teamId,
|
|
integrationId,
|
|
credentials,
|
|
},
|
|
update: {
|
|
credentials,
|
|
},
|
|
where: {
|
|
teamId_integrationId: {
|
|
teamId,
|
|
integrationId,
|
|
},
|
|
},
|
|
});
|
|
|
|
after(
|
|
(async () => {
|
|
const team = await prisma.team.findUniqueOrThrow({
|
|
where: {
|
|
id: teamId,
|
|
},
|
|
select: {
|
|
name: true,
|
|
users: {
|
|
where: { userId },
|
|
select: {
|
|
user: {
|
|
select: { email: true },
|
|
},
|
|
},
|
|
},
|
|
installedIntegrations: {
|
|
where: { integrationId },
|
|
select: {
|
|
integration: {
|
|
select: {
|
|
name: true,
|
|
slug: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const email = team.users.length > 0 ? team.users[0].user.email : null;
|
|
const integration =
|
|
team.installedIntegrations.length > 0
|
|
? team.installedIntegrations[0].integration
|
|
: null;
|
|
|
|
if (email && integration) {
|
|
await sendEmail({
|
|
to: email,
|
|
system: true,
|
|
test: process.env.NODE_ENV === "development",
|
|
subject: `The "${integration.name}" integration has been added to your team`,
|
|
react: IntegrationInstalled({
|
|
email: email,
|
|
team: {
|
|
name: team.name,
|
|
},
|
|
integration: {
|
|
name: integration.name,
|
|
slug: integration.slug,
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
})(),
|
|
);
|
|
|
|
return installation;
|
|
};
|