This commit is contained in:
Milo Schwartz 2024-12-25 15:55:50 -05:00
commit 4cdaa9b588
No known key found for this signature in database
22 changed files with 520 additions and 438 deletions

View file

@ -22,13 +22,15 @@ export async function ensureActions() {
.where(eq(roles.isAdmin, true))
.execute();
await db.transaction(async (trx) => {
// Add new actions
for (const actionId of actionsToAdd) {
logger.debug(`Adding action: ${actionId}`);
await db.insert(actions).values({ actionId }).execute();
await trx.insert(actions).values({ actionId }).execute();
// Add new actions to the Default role
if (defaultRoles.length != 0) {
await db
await trx
.insert(roleActions)
.values(
defaultRoles.map((role) => ({
@ -44,19 +46,23 @@ export async function ensureActions() {
// Remove deprecated actions
if (actionsToRemove.length > 0) {
logger.debug(`Removing actions: ${actionsToRemove.join(", ")}`);
await db
await trx
.delete(actions)
.where(inArray(actions.actionId, actionsToRemove))
.execute();
await db
await trx
.delete(roleActions)
.where(inArray(roleActions.actionId, actionsToRemove))
.execute();
}
});
}
export async function createAdminRole(orgId: string) {
const [insertedRole] = await db
let roleId: any;
await db.transaction(async (trx) => {
const [insertedRole] = await trx
.insert(roles)
.values({
orgId,
@ -67,16 +73,20 @@ export async function createAdminRole(orgId: string) {
.returning({ roleId: roles.roleId })
.execute();
const roleId = insertedRole.roleId;
if (!insertedRole || !insertedRole.roleId) {
throw new Error("Failed to create Admin role");
}
const actionIds = await db.select().from(actions).execute();
roleId = insertedRole.roleId;
const actionIds = await trx.select().from(actions).execute();
if (actionIds.length === 0) {
logger.info("No actions to assign to the Admin role");
return;
}
await db
await trx
.insert(roleActions)
.values(
actionIds.map((action) => ({
@ -86,6 +96,11 @@ export async function createAdminRole(orgId: string) {
}))
)
.execute();
});
if (!roleId) {
throw new Error("Failed to create Admin role");
}
return roleId;
}