Skip to content

Commit

Permalink
feat: add early_access_invitees table (#143)
Browse files Browse the repository at this point in the history
* feat: add early_access_invitees table schema

* feat: add getEarlyAccessInviteeByEmail repo

* feat: add user when early access invitee exists
  • Loading branch information
faiq-naufal authored Feb 6, 2024
1 parent 97d6518 commit 319505d
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/(server)/_features/early-access-invitee/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { db } from '@/(server)/_shared/database/database';

export const getEarlyAccessInviteeByEmail = async (email: string) => {
return db.query.earlyAccessInvitees.findFirst({
where(fields, operators) {
return operators.eq(fields.email, email);
},
});
};
15 changes: 15 additions & 0 deletions app/(server)/_features/early-access-invitee/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { serial, text, pgTable, timestamp } from 'drizzle-orm/pg-core';
import { type InferSelectModel, sql } from 'drizzle-orm';

export const earlyAccessInvitees = pgTable('early_access_invitees', {
id: serial('id').primaryKey(),
email: text('email').notNull().unique(),
name: text('name').notNull(),
whitelistFeature: text('whitelist_feature')
.array()
.notNull()
.default(sql`array[]::text[]`),
createdAt: timestamp('created_at').notNull().defaultNow(),
});

export type EarlyAccessInvitee = InferSelectModel<typeof earlyAccessInvitees>;
2 changes: 2 additions & 0 deletions app/(server)/_schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
participant,
eventHasParticipant,
} from '../_features/event/schema';
import { earlyAccessInvitees } from '@/(server)/_features/early-access-invitee/schema';

// export all schema here so we can utilize using the drizzle query
const models = {
Expand All @@ -13,6 +14,7 @@ const models = {
events,
eventHasParticipant,
participant,
earlyAccessInvitees,
};

export default models;
18 changes: 15 additions & 3 deletions app/(server)/api/auth/[provider]/authenticate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse, type NextRequest } from 'next/server';
import * as Sentry from '@sentry/nextjs';
import { InliveApiFetcher } from '@/_shared/utils/fetcher';
import { getUserByEmail, addUser } from '@/(server)/_features/user/repository';
import { getEarlyAccessInviteeByEmail } from '@/(server)/_features/early-access-invitee/repository';
import type { AuthType } from '@/_shared/types/auth';

const APP_ORIGIN = process.env.NEXT_PUBLIC_APP_ORIGIN || '';
Expand Down Expand Up @@ -114,13 +115,24 @@ export async function GET(
const existingUser = await getUserByEmail(currentAuth.data.email);

if (!existingUser) {
await addUser({
const userData = {
email: currentAuth.data.email,
name: currentAuth.data.name,
accountId: currentAuth.data.id,
pictureUrl: currentAuth.data.picture_url,
whitelistFeature: [],
});
whitelistFeature: [] as string[],
};

const existingEarlyAccessInvitee =
await getEarlyAccessInviteeByEmail(userData.email);

if (existingEarlyAccessInvitee) {
userData.whitelistFeature =
existingEarlyAccessInvitee.whitelistFeature;
await addUser(userData);
} else {
await addUser(userData);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions migrations/0012_add_early_access_invitees_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS "early_access_invitees" (
"id" serial PRIMARY KEY NOT NULL,
"email" text NOT NULL,
"name" text NOT NULL,
"whitelist_feature" text[] DEFAULT array[]::text[] NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "early_access_invitees_email_unique" UNIQUE("email")
);
Loading

0 comments on commit 319505d

Please sign in to comment.