ensureActions.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { ActionsEnum } from "@server/auth/actions";
  2. import { db } from "@server/db";
  3. import { actions, roles, roleActions } from "../db/schema";
  4. import { eq, inArray } from "drizzle-orm";
  5. import logger from "@server/logger";
  6. export async function ensureActions() {
  7. const actionIds = Object.values(ActionsEnum);
  8. const existingActions = await db.select().from(actions).execute();
  9. const existingActionIds = existingActions.map((action) => action.actionId);
  10. const actionsToAdd = actionIds.filter(
  11. (id) => !existingActionIds.includes(id)
  12. );
  13. const actionsToRemove = existingActionIds.filter(
  14. (id) => !actionIds.includes(id as ActionsEnum)
  15. );
  16. const defaultRoles = await db
  17. .select()
  18. .from(roles)
  19. .where(eq(roles.isAdmin, true))
  20. .execute();
  21. await db.transaction(async (trx) => {
  22. // Add new actions
  23. for (const actionId of actionsToAdd) {
  24. logger.debug(`Adding action: ${actionId}`);
  25. await trx.insert(actions).values({ actionId }).execute();
  26. // Add new actions to the Default role
  27. if (defaultRoles.length != 0) {
  28. await trx
  29. .insert(roleActions)
  30. .values(
  31. defaultRoles.map((role) => ({
  32. roleId: role.roleId!,
  33. actionId,
  34. orgId: role.orgId!
  35. }))
  36. )
  37. .execute();
  38. }
  39. }
  40. // Remove deprecated actions
  41. if (actionsToRemove.length > 0) {
  42. logger.debug(`Removing actions: ${actionsToRemove.join(", ")}`);
  43. await trx
  44. .delete(actions)
  45. .where(inArray(actions.actionId, actionsToRemove))
  46. .execute();
  47. await trx
  48. .delete(roleActions)
  49. .where(inArray(roleActions.actionId, actionsToRemove))
  50. .execute();
  51. }
  52. });
  53. }
  54. export async function createAdminRole(orgId: string) {
  55. let roleId: any;
  56. await db.transaction(async (trx) => {
  57. const [insertedRole] = await trx
  58. .insert(roles)
  59. .values({
  60. orgId,
  61. isAdmin: true,
  62. name: "Admin",
  63. description: "Admin role with the most permissions"
  64. })
  65. .returning({ roleId: roles.roleId })
  66. .execute();
  67. if (!insertedRole || !insertedRole.roleId) {
  68. throw new Error("Failed to create Admin role");
  69. }
  70. roleId = insertedRole.roleId;
  71. const actionIds = await trx.select().from(actions).execute();
  72. if (actionIds.length === 0) {
  73. logger.info("No actions to assign to the Admin role");
  74. return;
  75. }
  76. await trx
  77. .insert(roleActions)
  78. .values(
  79. actionIds.map((action) => ({
  80. roleId,
  81. actionId: action.actionId,
  82. orgId
  83. }))
  84. )
  85. .execute();
  86. });
  87. if (!roleId) {
  88. throw new Error("Failed to create Admin role");
  89. }
  90. return roleId;
  91. }