Skip to content

Commit

Permalink
feat(envited.ascs.digital): add company profile table (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
royscheeren authored Jan 23, 2024
2 parents e412b7e + b8d16fe commit 2d78844
Show file tree
Hide file tree
Showing 9 changed files with 1,422 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const COMPANY_CATEGORIES = [
{
id: 'oem',
name: 'OEM',
description: 'Original Equipment Manufacturer',
},
{
id: 'supplier',
name: 'Supplier',
description: 'Supplier',
},
{
id: 'data-provider',
name: 'Data Provider',
description: 'Data Provider',
},
{
id: 'service-provider',
name: 'Service Provider',
description: 'Service Provider',
},
{
id: 'tool-vendor',
name: 'Tool Vendor',
description: 'Tool Vendor',
},
{
id: 'academics',
name: 'Academics',
description: 'Academics',
},
]
47 changes: 45 additions & 2 deletions apps/envited.ascs.digital/common/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relations } from 'drizzle-orm'
import { boolean, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'
import { boolean, jsonb, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'

export const user = pgTable('user', {
id: text('id').unique().primaryKey(),
Expand All @@ -9,7 +9,7 @@ export const user = pgTable('user', {
isEnvitedMember: boolean('is_envited_member'),
streetAddress: text('street_address'),
postalCode: text('postal_code'),
addressLocality: text('address_location'),
addressLocality: text('address_locality'),
addressCountry: text('address_country'),
vatId: text('vat_id'),
privacyPolicyAccepted: text('privacy_policy_accepted'),
Expand Down Expand Up @@ -102,3 +102,46 @@ export const usersToRolesRelations = relations(usersToRoles, ({ one }) => ({
references: [user.id],
}),
}))

export const profile = pgTable('profile', {
id: uuid('id').defaultRandom().primaryKey(),
name: text('name').unique(),
description: text('description'),
logo: text('logo'),
streetAddress: text('street_address'),
postalCode: text('postal_code'),
addressLocality: text('address_locality'),
addressCountry: text('address_country'),
firstName: text('first_name'),
lastName: text('last_name'),
phone: text('phone'),
email: text('email'),
website: text('website'),
offerings: jsonb('offerings'),
})

export const companyCategory = pgTable('companyCategory', {
id: text('id').unique().primaryKey(),
name: text('name').unique(),
description: text('description'),
})

export const profilesToCompanyCategories = pgTable('profilesToCompanyCategories', {
profileId: uuid('profile_id')
.references(() => profile.id)
.notNull(),
companyCategoryId: text('company_category_id')
.references(() => companyCategory.id)
.notNull(),
})

export const profilesToCompanyCategoriesRelations = relations(profilesToCompanyCategories, ({ one }) => ({
companyCategory: one(companyCategory, {
fields: [profilesToCompanyCategories.companyCategoryId],
references: [companyCategory.id],
}),
profile: one(profile, {
fields: [profilesToCompanyCategories.profileId],
references: [profile.id],
}),
}))
9 changes: 7 additions & 2 deletions apps/envited.ascs.digital/common/database/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { RDSDataClient } from '@aws-sdk/client-rds-data'
import { fromIni } from '@aws-sdk/credential-providers'
import { drizzle as RDSDrizzle } from 'drizzle-orm/aws-data-api/pg'

import { COMPANY_CATEGORIES } from './data/companyCategories'
import { ROLES } from './data/roles'
import { connectDb } from './database'
import { role } from './schema'
import { companyCategory, role } from './schema'
import * as schema from './schema'

const insertRoles = (connection: any) => async (roles: any[]) => connection.insert(role).values(roles).execute()
const insertRoles = (connection: any) => async (roles: any[]) =>
connection.insert(role).values(roles).onConflictDoNothing().execute()
const insertCompanyCategories = (connection: any) => async (companyCategories: any[]) =>
connection.insert(companyCategory).values(companyCategories).onConflictDoNothing().execute()

const seed = async () => {
try {
Expand All @@ -30,6 +34,7 @@ const seed = async () => {
})
}
await insertRoles(connection)(ROLES)
await insertCompanyCategories(connection)(COMPANY_CATEGORIES)
return
} catch (error) {
console.error(error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE IF NOT EXISTS "companyCategory" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"description" text,
CONSTRAINT "companyCategory_id_unique" UNIQUE("id"),
CONSTRAINT "companyCategory_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "profile" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" text,
"description" text,
"logo" text,
"street_address" text,
"postal_code" text,
"address_locality" text,
"address_country" text,
"first_name" text,
"last_name" text,
"phone" text,
"email" text,
"website" text,
"offerings" jsonb,
CONSTRAINT "profile_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "profilesToCompanyCategories" (
"profile_id" uuid NOT NULL,
"company_category_id" text NOT NULL
);
--> statement-breakpoint
ALTER TABLE "user" RENAME COLUMN "address_location" TO "address_locality";--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "profilesToCompanyCategories" ADD CONSTRAINT "profilesToCompanyCategories_profile_id_profile_id_fk" FOREIGN KEY ("profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "profilesToCompanyCategories" ADD CONSTRAINT "profilesToCompanyCategories_company_category_id_companyCategory_id_fk" FOREIGN KEY ("company_category_id") REFERENCES "public"."companyCategory"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit 2d78844

Please sign in to comment.