feat: add organization membership data model
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
CREATE TABLE `organization_memberships` (
|
||||||
|
`id` text PRIMARY KEY NOT NULL,
|
||||||
|
`organization_id` text NOT NULL,
|
||||||
|
`user_id` text NOT NULL,
|
||||||
|
`role` text DEFAULT 'user' NOT NULL,
|
||||||
|
`created_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||||
|
`updated_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||||
|
`archived_at` integer,
|
||||||
|
FOREIGN KEY (`organization_id`) REFERENCES `organizations`(`id`) ON UPDATE no action ON DELETE cascade,
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX `organization_memberships_organization_user_unique` ON `organization_memberships` (`organization_id`,`user_id`);--> statement-breakpoint
|
||||||
|
CREATE INDEX `organization_memberships_organization_id_idx` ON `organization_memberships` (`organization_id`);--> statement-breakpoint
|
||||||
|
CREATE INDEX `organization_memberships_user_id_idx` ON `organization_memberships` (`user_id`);--> statement-breakpoint
|
||||||
|
INSERT INTO `organization_memberships` (`id`, `organization_id`, `user_id`, `role`)
|
||||||
|
SELECT lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(6))),
|
||||||
|
organizations.`id`,
|
||||||
|
user.`id`,
|
||||||
|
CASE WHEN user.`role` = 'admin' THEN 'admin' ELSE 'user' END
|
||||||
|
FROM `organizations`
|
||||||
|
CROSS JOIN `user`
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM `organization_memberships`
|
||||||
|
WHERE `organization_memberships`.`organization_id` = organizations.`id`
|
||||||
|
AND `organization_memberships`.`user_id` = user.`id`
|
||||||
|
);
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
PRAGMA foreign_keys=OFF;--> statement-breakpoint
|
||||||
|
CREATE TABLE `__new_organizations` (
|
||||||
|
`id` text PRIMARY KEY NOT NULL,
|
||||||
|
`name` text NOT NULL,
|
||||||
|
`address_line_1` text DEFAULT '' NOT NULL,
|
||||||
|
`address_line_2` text,
|
||||||
|
`address_line_3` text,
|
||||||
|
`city` text,
|
||||||
|
`region` text,
|
||||||
|
`postcode` text,
|
||||||
|
`country` text DEFAULT 'United Kingdom' NOT NULL,
|
||||||
|
`bank_name` text,
|
||||||
|
`bank_account_name` text,
|
||||||
|
`bank_account_number` text,
|
||||||
|
`bank_sort_code` text,
|
||||||
|
`bank_iban` text,
|
||||||
|
`bank_swift` text,
|
||||||
|
`created_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||||
|
`updated_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||||
|
`archived_at` integer
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
INSERT INTO `__new_organizations` (
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"address_line_1",
|
||||||
|
"address_line_2",
|
||||||
|
"address_line_3",
|
||||||
|
"city",
|
||||||
|
"region",
|
||||||
|
"postcode",
|
||||||
|
"country",
|
||||||
|
"bank_name",
|
||||||
|
"bank_account_name",
|
||||||
|
"bank_account_number",
|
||||||
|
"bank_sort_code",
|
||||||
|
"bank_iban",
|
||||||
|
"bank_swift",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
|
"archived_at"
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
COALESCE(NULLIF("address_line_1", ''), "workspace", ''),
|
||||||
|
"address_line_2",
|
||||||
|
NULL,
|
||||||
|
"city",
|
||||||
|
"region",
|
||||||
|
"postcode",
|
||||||
|
"country",
|
||||||
|
"bank_name",
|
||||||
|
"bank_account_name",
|
||||||
|
"bank_account_number",
|
||||||
|
"bank_sort_code",
|
||||||
|
"bank_iban",
|
||||||
|
"bank_swift",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
|
"archived_at"
|
||||||
|
FROM `organizations`;--> statement-breakpoint
|
||||||
|
DROP TABLE `organizations`;--> statement-breakpoint
|
||||||
|
ALTER TABLE `__new_organizations` RENAME TO `organizations`;--> statement-breakpoint
|
||||||
|
PRAGMA foreign_keys=ON;
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -85,6 +85,20 @@
|
|||||||
"when": 1782291600000,
|
"when": 1782291600000,
|
||||||
"tag": "0011_billing_flow",
|
"tag": "0011_billing_flow",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 12,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1782295200000,
|
||||||
|
"tag": "0012_organization_memberships",
|
||||||
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 13,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1782313131621,
|
||||||
|
"tag": "0013_flippant_jamie_braddock",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,9 @@ import { idSchema, optionalText, requiredText } from './shared.schema';
|
|||||||
|
|
||||||
const organizationDetailsSchema = z.object({
|
const organizationDetailsSchema = z.object({
|
||||||
name: requiredText('Organization name'),
|
name: requiredText('Organization name'),
|
||||||
workspace: requiredText('Workspace', 120).default('Blueprint Workspace'),
|
addressLine1: requiredText('Address line 1'),
|
||||||
addressLine1: optionalText(160),
|
|
||||||
addressLine2: optionalText(160),
|
addressLine2: optionalText(160),
|
||||||
|
addressLine3: optionalText(160),
|
||||||
city: optionalText(100),
|
city: optionalText(100),
|
||||||
region: optionalText(100),
|
region: optionalText(100),
|
||||||
postcode: optionalText(24),
|
postcode: optionalText(24),
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { index, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
|
||||||
|
import { user } from './auth.schema';
|
||||||
|
import { organizations } from './organizations.schema';
|
||||||
|
import { timestamps } from './shared.schema';
|
||||||
|
|
||||||
|
export const organizationMemberships = sqliteTable(
|
||||||
|
'organization_memberships',
|
||||||
|
{
|
||||||
|
id: text('id')
|
||||||
|
.primaryKey()
|
||||||
|
.$defaultFn(() => crypto.randomUUID()),
|
||||||
|
organizationId: text('organization_id')
|
||||||
|
.notNull()
|
||||||
|
.references(() => organizations.id, { onDelete: 'cascade' }),
|
||||||
|
userId: text('user_id')
|
||||||
|
.notNull()
|
||||||
|
.references(() => user.id, { onDelete: 'cascade' }),
|
||||||
|
role: text('role').notNull().default('user'),
|
||||||
|
...timestamps
|
||||||
|
},
|
||||||
|
(table) => [
|
||||||
|
uniqueIndex('organization_memberships_organization_user_unique').on(
|
||||||
|
table.organizationId,
|
||||||
|
table.userId
|
||||||
|
),
|
||||||
|
index('organization_memberships_organization_id_idx').on(table.organizationId),
|
||||||
|
index('organization_memberships_user_id_idx').on(table.userId)
|
||||||
|
]
|
||||||
|
);
|
||||||
@@ -6,9 +6,9 @@ export const organizations = sqliteTable('organizations', {
|
|||||||
.primaryKey()
|
.primaryKey()
|
||||||
.$defaultFn(() => crypto.randomUUID()),
|
.$defaultFn(() => crypto.randomUUID()),
|
||||||
name: text('name').notNull(),
|
name: text('name').notNull(),
|
||||||
workspace: text('workspace').notNull().default('Default Workspace'),
|
addressLine1: text('address_line_1').notNull().default(''),
|
||||||
addressLine1: text('address_line_1'),
|
|
||||||
addressLine2: text('address_line_2'),
|
addressLine2: text('address_line_2'),
|
||||||
|
addressLine3: text('address_line_3'),
|
||||||
city: text('city'),
|
city: text('city'),
|
||||||
region: text('region'),
|
region: text('region'),
|
||||||
postcode: text('postcode'),
|
postcode: text('postcode'),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ export const task = sqliteTable('task', {
|
|||||||
|
|
||||||
export * from './clients.schema';
|
export * from './clients.schema';
|
||||||
export * from './organizations.schema';
|
export * from './organizations.schema';
|
||||||
|
export * from './organization-memberships.schema';
|
||||||
export * from './addresses.schema';
|
export * from './addresses.schema';
|
||||||
export * from './contacts.schema';
|
export * from './contacts.schema';
|
||||||
export * from './rooms.schema';
|
export * from './rooms.schema';
|
||||||
|
|||||||
Reference in New Issue
Block a user