Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
flipvh committed Oct 16, 2024
2 parents f2d9587 + a14f985 commit a97513b
Show file tree
Hide file tree
Showing 134 changed files with 1,678 additions and 1,294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ CREATE TABLE IF NOT EXISTS "attachments" (
"filename" varchar NOT NULL,
"content_type" varchar NOT NULL,
"size" varchar NOT NULL,
"entity" varchar DEFAULT 'attachment' NOT NULL,
"url" varchar NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"created_by" varchar
"created_by" varchar,
"modified_at" timestamp,
"modified_by" varchar,
"organization_id" varchar NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "memberships" (
"id" varchar PRIMARY KEY NOT NULL,
"type" varchar NOT NULL,
"organization_id" varchar NOT NULL,
"user_id" varchar NOT NULL,
"role" varchar DEFAULT 'member' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
Expand All @@ -20,7 +23,8 @@ CREATE TABLE IF NOT EXISTS "memberships" (
"modified_by" varchar,
"archived" boolean DEFAULT false NOT NULL,
"muted" boolean DEFAULT false NOT NULL,
"sort_order" double precision NOT NULL
"sort_order" double precision NOT NULL,
"organization_id" varchar NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "oauth_accounts" (
Expand Down Expand Up @@ -49,7 +53,6 @@ CREATE TABLE IF NOT EXISTS "organizations" (
"logo_url" varchar,
"website_url" varchar,
"welcome_text" varchar,
"is_production" boolean DEFAULT false NOT NULL,
"auth_strategies" json DEFAULT '[]'::json NOT NULL,
"chat_support" boolean DEFAULT false NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
Expand Down Expand Up @@ -117,7 +120,7 @@ CREATE TABLE IF NOT EXISTS "users" (
"thumbnail_url" varchar,
"newsletter" boolean DEFAULT false NOT NULL,
"last_seen_at" timestamp,
"last_visit_at" timestamp,
"last_started_at" timestamp,
"last_sign_in_at" timestamp,
"created_at" timestamp DEFAULT now() NOT NULL,
"modified_at" timestamp,
Expand All @@ -135,7 +138,13 @@ EXCEPTION
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "memberships" ADD CONSTRAINT "memberships_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_modified_by_users_id_fk" FOREIGN KEY ("modified_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Expand All @@ -158,6 +167,12 @@ EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "memberships" ADD CONSTRAINT "memberships_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "oauth_accounts" ADD CONSTRAINT "oauth_accounts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
Expand Down
102 changes: 73 additions & 29 deletions backend/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "135b8700-1927-4cd8-ac84-12cf4d5e09a1",
"id": "36f89a8d-02da-4e6b-b6b0-9355c9dada94",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -32,6 +32,13 @@
"primaryKey": false,
"notNull": true
},
"entity": {
"name": "entity",
"type": "varchar",
"primaryKey": false,
"notNull": true,
"default": "'attachment'"
},
"url": {
"name": "url",
"type": "varchar",
Expand All @@ -50,6 +57,24 @@
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"modified_at": {
"name": "modified_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
},
"modified_by": {
"name": "modified_by",
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"organization_id": {
"name": "organization_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
Expand All @@ -66,6 +91,32 @@
],
"onDelete": "set null",
"onUpdate": "no action"
},
"attachments_modified_by_users_id_fk": {
"name": "attachments_modified_by_users_id_fk",
"tableFrom": "attachments",
"tableTo": "users",
"columnsFrom": [
"modified_by"
],
"columnsTo": [
"id"
],
"onDelete": "set null",
"onUpdate": "no action"
},
"attachments_organization_id_organizations_id_fk": {
"name": "attachments_organization_id_organizations_id_fk",
"tableFrom": "attachments",
"tableTo": "organizations",
"columnsFrom": [
"organization_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
Expand All @@ -87,12 +138,6 @@
"primaryKey": false,
"notNull": true
},
"organization_id": {
"name": "organization_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "varchar",
Expand Down Expand Up @@ -150,23 +195,16 @@
"type": "double precision",
"primaryKey": false,
"notNull": true
},
"organization_id": {
"name": "organization_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"memberships_organization_id_organizations_id_fk": {
"name": "memberships_organization_id_organizations_id_fk",
"tableFrom": "memberships",
"tableTo": "organizations",
"columnsFrom": [
"organization_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"memberships_user_id_users_id_fk": {
"name": "memberships_user_id_users_id_fk",
"tableFrom": "memberships",
Expand Down Expand Up @@ -205,6 +243,19 @@
],
"onDelete": "set null",
"onUpdate": "no action"
},
"memberships_organization_id_organizations_id_fk": {
"name": "memberships_organization_id_organizations_id_fk",
"tableFrom": "memberships",
"tableTo": "organizations",
"columnsFrom": [
"organization_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
Expand Down Expand Up @@ -377,13 +428,6 @@
"primaryKey": false,
"notNull": false
},
"is_production": {
"name": "is_production",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"auth_strategies": {
"name": "auth_strategies",
"type": "json",
Expand Down Expand Up @@ -934,8 +978,8 @@
"primaryKey": false,
"notNull": false
},
"last_visit_at": {
"name": "last_visit_at",
"last_started_at": {
"name": "last_started_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
Expand Down
4 changes: 2 additions & 2 deletions backend/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1725866768212,
"tag": "0000_huge_amazoness",
"when": 1729106983063,
"tag": "0000_rare_stick",
"breakpoints": true
}
]
Expand Down
18 changes: 14 additions & 4 deletions backend/scripts/seeds/organizations/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { generateUnsubscribeToken } from '#/modules/users/helpers/unsubscribe-to
import type { Status } from '../progress';
import { adminUser } from '../user/seed';

const ORGANIZATIONS_COUNT = 100;
const MEMBERS_COUNT = 100;
const SYSTEM_ADMIN_MEMBERSHIP_COUNT = 10;

// Seed organizations with data
export const organizationsSeed = async (progressCallback?: (stage: string, count: number, status: Status) => void) => {
const organizationsInTable = await db.select().from(organizationsTable).limit(1);
Expand All @@ -28,7 +32,7 @@ export const organizationsSeed = async (progressCallback?: (stage: string, count
const organizations: (InsertOrganizationModel & {
id: string;
})[] = Array.from({
length: 10,
length: ORGANIZATIONS_COUNT,
}).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());

Expand Down Expand Up @@ -57,13 +61,14 @@ export const organizationsSeed = async (progressCallback?: (stage: string, count
let organizationsCount = 0;
let membershipsCount = 0;
let adminMembershipsOrder = 1;
let adminOrganizationsCount = 0;

// Create 100 users for each organization
for (const organization of organizations) {
organizationsCount++;
if (progressCallback) progressCallback('organizations', organizationsCount, 'inserting');

const insertUsers: InsertUserModel[] = Array.from({ length: 100 }).map(() => {
const insertUsers: InsertUserModel[] = Array.from({ length: MEMBERS_COUNT }).map(() => {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const firstAndLastName = { firstName, lastName };
Expand Down Expand Up @@ -109,19 +114,24 @@ export const organizationsSeed = async (progressCallback?: (stage: string, count
};
});

// add Admin user to every even organization
if (organizationsCount % 2 === 0) {
// Loop over organizations

// Add Admin user to every even organization, but limit to a certain number
if (organizationsCount % 2 === 0 && adminOrganizationsCount < SYSTEM_ADMIN_MEMBERSHIP_COUNT) {
memberships.push({
id: nanoid(),
userId: adminUser.id,
organizationId: organization.id,
type: 'organization',
archived: faker.datatype.boolean(0.5),
role: faker.helpers.arrayElement(['admin', 'member']),
createdAt: faker.date.past(),
order: adminMembershipsOrder,
});
adminMembershipsOrder++;
adminOrganizationsCount++; // Increment the counter
}

membershipsCount += memberships.length;
if (progressCallback) progressCallback('memberships', membershipsCount, 'inserting');

Expand Down
13 changes: 13 additions & 0 deletions backend/src/db/schema/attachments.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { pgTable, timestamp, varchar } from 'drizzle-orm/pg-core';
import { nanoid } from '#/utils/nanoid';
import { organizationsTable } from './organizations';
import { usersTable } from './users';

export const attachmentsTable = pgTable('attachments', {
id: varchar('id').primaryKey().$defaultFn(nanoid),
filename: varchar('filename').notNull(),
contentType: varchar('content_type').notNull(),
size: varchar('size').notNull(),
entity: varchar('entity', { enum: ['attachment'] })
.notNull()
.default('attachment'),
url: varchar('url').notNull(),
createdAt: timestamp('created_at').defaultNow().notNull(),
createdBy: varchar('created_by').references(() => usersTable.id, {
onDelete: 'set null',
}),
modifiedAt: timestamp('modified_at'),
modifiedBy: varchar('modified_by').references(() => usersTable.id, {
onDelete: 'set null',
}),
organizationId: varchar('organization_id')
.notNull()
.references(() => organizationsTable.id, {
onDelete: 'cascade',
}),
});

export type AttachmentModel = typeof attachmentsTable.$inferSelect;
Expand Down
26 changes: 5 additions & 21 deletions backend/src/db/schema/memberships.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { config } from 'config';
import { relations } from 'drizzle-orm';
import { boolean, doublePrecision, pgTable, timestamp, varchar } from 'drizzle-orm/pg-core';
import { usersTable } from '#/db/schema/users';
import { nanoid } from '#/utils/nanoid';
Expand All @@ -10,37 +9,22 @@ const roleEnum = config.rolesByType.entityRoles;
export const membershipsTable = pgTable('memberships', {
id: varchar('id').primaryKey().$defaultFn(nanoid),
type: varchar('type', { enum: config.contextEntityTypes }).notNull(),
organizationId: varchar('organization_id')
.notNull()
.references(() => organizationsTable.id, { onDelete: 'cascade' }),
userId: varchar('user_id')
.notNull()
.references(() => usersTable.id, { onDelete: 'cascade' }),
role: varchar('role', { enum: roleEnum }).notNull().default('member'),
createdAt: timestamp('created_at').defaultNow().notNull(),
createdBy: varchar('created_by').references(() => usersTable.id, {
onDelete: 'set null',
}),
createdBy: varchar('created_by').references(() => usersTable.id, { onDelete: 'set null' }),
modifiedAt: timestamp('modified_at'),
modifiedBy: varchar('modified_by').references(() => usersTable.id, {
onDelete: 'set null',
}),
modifiedBy: varchar('modified_by').references(() => usersTable.id, { onDelete: 'set null' }),
archived: boolean('archived').default(false).notNull(),
muted: boolean('muted').default(false).notNull(),
order: doublePrecision('sort_order').notNull(),
organizationId: varchar('organization_id')
.notNull()
.references(() => organizationsTable.id, { onDelete: 'cascade' }),
});

export const membershipsTableRelations = relations(membershipsTable, ({ one }) => ({
user: one(usersTable, {
fields: [membershipsTable.userId],
references: [usersTable.id],
}),
organization: one(organizationsTable, {
fields: [membershipsTable.organizationId],
references: [organizationsTable.id],
}),
}));

export const membershipSelect = {
id: membershipsTable.id,
role: membershipsTable.role,
Expand Down
Loading

0 comments on commit a97513b

Please sign in to comment.