* feat(db): add support for multiple users and organizations * feat: backfill entities with new organization id * refactor: filter all backend queries to surface only organization specific entities * refactor: each org has its own restic password * test: ensure organization is created * chore: pr feedbacks * refactor: filter by org id in all places * refactor: download restic password from stored db password * refactor(navigation): use volume id in urls instead of name * feat: disable registrations * refactor(auth): bubble up auth error to hono * refactor: use async local storage for cleaner context sharing * refactor: enable user registration vs disabling it * test: multi-org isolation * chore: final cleanup
35 lines
961 B
TypeScript
35 lines
961 B
TypeScript
import { db } from "~/server/db/db";
|
|
import { organization, type OrganizationMetadata } from "~/server/db/schema";
|
|
import { faker } from "@faker-js/faker";
|
|
|
|
export const TEST_ORG_ID = "test-org-00000001";
|
|
|
|
export const createTestOrganization = async (overrides: Partial<typeof organization.$inferInsert> = {}) => {
|
|
const metadata: OrganizationMetadata = {
|
|
resticPassword: "test-encrypted-restic-password",
|
|
};
|
|
|
|
const org: typeof organization.$inferInsert = {
|
|
id: TEST_ORG_ID,
|
|
name: "Test Organization",
|
|
slug: `test-org-${faker.string.alphanumeric(6)}`,
|
|
createdAt: new Date(),
|
|
metadata,
|
|
...overrides,
|
|
};
|
|
|
|
const existing = await db.query.organization.findFirst({
|
|
where: (o, { eq }) => eq(o.id, org.id ?? TEST_ORG_ID),
|
|
});
|
|
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
const data = await db.insert(organization).values(org).returning();
|
|
return data[0];
|
|
};
|
|
|
|
export const ensureTestOrganization = async () => {
|
|
return createTestOrganization();
|
|
};
|