Skip to content

Commit

Permalink
Fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Feb 7, 2025
1 parent 6007aa8 commit cf58211
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion apps/backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async function seed() {
console.log('Internal project created');
}

const internalTenancy = await getSoleTenancyFromProject('internal');
const internalTenancy = await getSoleTenancyFromProject("internal");

if (internalProject.config.signUpEnabled !== signUpEnabled) {
await prisma.projectConfig.update({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const signInVerificationCodeHandler = createVerificationCodeHandler({
body: signInResponseSchema.defined(),
}),
async send(codeObj, createOptions, sendOptions: { email: string }) {
const tenancy = await getSoleTenancyFromProject(createOptions.project.id);
const tenancy = await getSoleTenancyFromProject(createOptions.project);
await sendEmailFromTemplate({
tenancy,
email: createOptions.method.email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const resetPasswordVerificationCodeHandler = createVerificationCodeHandle
bodyType: yupString().oneOf(["success"]).defined(),
}),
async send(codeObj, createOptions, sendOptions: { user: UsersCrud["Admin"]["Read"] }) {
const tenancy = await getSoleTenancyFromProject(createOptions.project.id);
const tenancy = await getSoleTenancyFromProject(createOptions.project);
await sendEmailFromTemplate({
tenancy,
user: sendOptions.user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const contactChannelVerificationCodeHandler = createVerificationCodeHandl
bodyType: yupString().oneOf(["success"]).defined(),
}),
async send(codeObj, createOptions, sendOptions: { user: UsersCrud["Admin"]["Read"] }) {
const tenancy = await getSoleTenancyFromProject(createOptions.project.id);
const tenancy = await getSoleTenancyFromProject(createOptions.project);

await sendEmailFromTemplate({
tenancy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const POST = createSmartRouteHandler({
}

const transferCodeObj = await neonIntegrationProjectTransferCodeHandler.createCode({
tenancy: await getSoleTenancyFromProject(internalProject.id),
tenancy: await getSoleTenancyFromProject(internalProject),
method: {},
data: {
project_id: neonProvisionedProject.projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const teamInvitationCodeHandler = createVerificationCodeHandler({
});

await sendEmailFromTemplate({
tenancy: await getSoleTenancyFromProject(createOptions.project.id),
tenancy: await getSoleTenancyFromProject(createOptions.project),
user: null,
email: createOptions.method.email,
templateType: "team_invitation",
Expand Down
48 changes: 32 additions & 16 deletions apps/backend/src/lib/tenancies.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prismaClient } from "@/prisma-client";
import { Prisma } from "@prisma/client";
import { ProjectsCrud } from "@stackframe/stack-shared/dist/interface/crud/projects";
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
import { fullProjectInclude, getProject, projectPrismaToCrud } from "./projects";

Expand All @@ -22,7 +23,7 @@ export function tenancyPrismaToCrud(prisma: Prisma.TenancyGetPayload<{ include:
id: prisma.id,
config: projectCrud.config,
branchId: prisma.branchId,
organization: {
organization: prisma.organizationId === null ? null : {
// TODO actual organization type
id: prisma.organizationId,
},
Expand All @@ -34,35 +35,50 @@ export type Tenancy = Awaited<ReturnType<typeof tenancyPrismaToCrud>>;

/**
* while not necessary, this cache just makes performance a little better
*
*
* eventually, we'll nicely pass around tenancies and won't need this function anymore, so the cache is a good temp
* solution
*/
const soleTenanciesCache = new Map<string, Tenancy>();
const soleTenancyIdsCache = new Map<string, string>();

/**
* @deprecated This is a temporary function for the situation where every project has exactly one tenancy. Later,
* we will support multiple tenancies per project, and all uses of this function will be refactored.
*/
export async function getSoleTenancyFromProject(projectId: string): Promise<Tenancy>;
export async function getSoleTenancyFromProject(project: ProjectsCrud["Admin"]["Read"] | string): Promise<Tenancy>;
/**
* @deprecated This is a temporary function for the situation where every project has exactly one tenancy. Later,
* we will support multiple tenancies per project, and all uses of this function will be refactored.
*/
export async function getSoleTenancyFromProject(projectId: string, returnNullIfNotFound: boolean): Promise<Tenancy | null>;
export async function getSoleTenancyFromProject(projectId: string, returnNullIfNotFound: boolean = false) {
const tenancy = soleTenanciesCache.get(projectId) ?? await getTenancyFromProject(projectId, 'main', null);
if (!tenancy) {
export async function getSoleTenancyFromProject(project: ProjectsCrud["Admin"]["Read"] | string, returnNullIfNotFound: boolean): Promise<Tenancy | null>;
export async function getSoleTenancyFromProject(projectOrId: ProjectsCrud["Admin"]["Read"] | string, returnNullIfNotFound: boolean = false): Promise<Tenancy | null> {
let project;
if (!projectOrId) {
throw new StackAssertionError("Project is required", { projectOrId });
}
if (typeof projectOrId === 'string') {
project = await getProject(projectOrId);
} else {
project = projectOrId;
}
if (!project) {
console.log(new Error());
if (returnNullIfNotFound) return null;

const project = await getProject(projectId);
if (!project) {
throw new StackAssertionError(`Tried to find sole tenancy for project, but project not found for ID ${projectId}`, { projectId });
}
throw new StackAssertionError(`No tenancy found for project ${projectId}`, { projectId });
throw new StackAssertionError(`Project ${projectOrId} does not exist`, { projectOrId });
}
soleTenanciesCache.set(projectId, tenancy);
return tenancy;
const tenancyId = soleTenancyIdsCache.get(project.id) ?? (await getTenancyFromProject(project.id, 'main', null))?.id;
if (!tenancyId) {
if (returnNullIfNotFound) return null;
throw new StackAssertionError(`No tenancy found for project ${project.id}`, { project });
}
soleTenancyIdsCache.set(project.id, tenancyId);
return {
id: tenancyId,
config: project.config,
branchId: "main",
organization: null,
project: project,
};
}

export async function getTenancy(tenancyId: string) {
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/route-handlers/crud-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function createCrudHandlers<
project = tenancy.project;
branchId = tenancy.branchId;
} else if (project) {
tenancy = await getSoleTenancyFromProject(project.id);
tenancy = await getSoleTenancyFromProject(project);
} else {
throw new StackAssertionError("Must specify either project or tenancy");
}
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/route-handlers/smart-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,11 @@ const parseAuth = withTraceSpan('smart request parseAuth', async (req: NextReque
throw new KnownErrors.ProjectNotFound(projectId);
}

console.log("AAAAA", project);
return {
project,
branchId: "main",
tenancy: await getSoleTenancyFromProject(project.id),
tenancy: await getSoleTenancyFromProject(project),
user: queriesResults.user ?? undefined,
type: requestType,
};
Expand Down

0 comments on commit cf58211

Please sign in to comment.