* refactor: extract restic in core package * chore: add turbo task runner * refactor: split server utils * chore: simplify withDeps signature and fix non-null assertion
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { db } from "../../../db/db";
|
|
import { member } from "../../../db/schema";
|
|
import { logger } from "@zerobyte/core/node";
|
|
import { toMessage } from "~/server/utils/errors";
|
|
|
|
const execute = async () => {
|
|
const errors: Array<{ name: string; error: string }> = [];
|
|
|
|
try {
|
|
const allUsers = await db.query.usersTable.findMany({
|
|
where: { role: "admin" },
|
|
});
|
|
const allOrganizations = await db.query.organization.findMany({});
|
|
|
|
if (allUsers.length === 0) {
|
|
logger.info("No users found, skipping migration");
|
|
return { success: true, errors };
|
|
}
|
|
|
|
if (allOrganizations.length === 0) {
|
|
logger.info("No organizations found, skipping migration");
|
|
return { success: true, errors };
|
|
}
|
|
|
|
if (allUsers.length !== 1) {
|
|
logger.info(`Found ${allUsers.length} users, expected exactly 1, skipping migration`);
|
|
return { success: true, errors };
|
|
}
|
|
|
|
if (allOrganizations.length !== 1) {
|
|
logger.info(`Found ${allOrganizations.length} organizations, expected exactly 1, skipping migration`);
|
|
return { success: true, errors };
|
|
}
|
|
|
|
const user = allUsers[0];
|
|
const org = allOrganizations[0];
|
|
|
|
const existingMembers = await db.query.member.findMany({
|
|
where: { userId: user.id },
|
|
});
|
|
|
|
if (existingMembers.length > 0) {
|
|
logger.info(`User ${user.username} already belongs to organization(s), skipping migration`);
|
|
return { success: true, errors };
|
|
}
|
|
|
|
logger.info(`Assigning user ${user.username} to organization ${org.name}`);
|
|
|
|
await db.insert(member).values({
|
|
id: Bun.randomUUIDv7(),
|
|
organizationId: org.id,
|
|
userId: user.id,
|
|
role: "owner",
|
|
createdAt: new Date(),
|
|
});
|
|
|
|
logger.info(`Successfully assigned user ${user.username} to organization ${org.name}`);
|
|
} catch (err) {
|
|
errors.push({ name: "assign-organization", error: toMessage(err) });
|
|
}
|
|
|
|
return { success: errors.length === 0, errors };
|
|
};
|
|
|
|
export const v00003 = {
|
|
execute,
|
|
id: "00003-assign-organization",
|
|
type: "maintenance" as const,
|
|
};
|