Skip to content

Commit

Permalink
feat(saas): Added Organization email
Browse files Browse the repository at this point in the history
  • Loading branch information
alifarooq9 committed Apr 11, 2024
1 parent 78b611c commit a47c218
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function CreateFirstOrgForm() {

const user = await getUser();

if (userOrgs.length === 0) {
if (userOrgs.length === 0 && !user!.isNewUser) {
return (
<div className="fixed inset-0 flex h-screen w-screen flex-col items-center justify-center bg-black/80">
<div className="w-full max-w-xl">
Expand Down
32 changes: 30 additions & 2 deletions starterkits/saas/src/app/(app)/_components/create-org-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const createOrgFormSchema = z.object({
.trim()
.min(3, "Name must be at least 3 characters long")
.max(50, "Name must be at most 50 characters long"),
email: z.string().email("Invalid email address"),
});

export type CreateOrgFormSchema = z.infer<typeof createOrgFormSchema>;
Expand All @@ -51,11 +52,13 @@ export function CreateOrgForm({ open, setOpen }: CreateOrgFormProps) {
resolver: zodResolver(createOrgFormSchema),
defaultValues: {
name: "",
email: "",
},
});

const { mutateAsync, isPending: isMutatePending } = useMutation({
mutationFn: ({ name }: { name: string }) => createOrgMutation({ name }),
mutationFn: ({ name, email }: { name: string; email: string }) =>
createOrgMutation({ name, email }),
});

const [isPending, startAwaitableTransition] = useAwaitableTransition();
Expand Down Expand Up @@ -91,7 +94,32 @@ export function CreateOrgForm({ open, setOpen }: CreateOrgFormProps) {
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-3"
>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Org Email</FormLabel>
<FormControl>
<Input
placeholder="[email protected]"
{...field}
/>
</FormControl>
<FormDescription>
Enter the email of your organization.
This could be your personal email or a
shared email.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="name"
Expand Down
28 changes: 26 additions & 2 deletions starterkits/saas/src/app/(app)/_components/new-user-org-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const createOrgFormSchema = z.object({
.trim()
.min(3, "Name must be at least 3 characters long")
.max(50, "Name must be at most 50 characters long"),
email: z.string().email("Invalid email address"),
});

type CreateOrgFormSchema = z.infer<typeof createOrgFormSchema>;
Expand Down Expand Up @@ -76,11 +77,13 @@ export function NewUserOrgForm({
resolver: zodResolver(createOrgFormSchema),
defaultValues: {
name: "",
email: "",
},
});

const { mutateAsync, isPending: isMutatePending } = useMutation({
mutationFn: ({ name }: { name: string }) => createOrgMutation({ name }),
mutationFn: ({ name, email }: { name: string; email: string }) =>
createOrgMutation({ name, email }),
});

const [isPending, startAwaitableTransition] = useAwaitableTransition();
Expand Down Expand Up @@ -118,7 +121,28 @@ export function NewUserOrgForm({
Create an organization to get started
</CardDescription>
</CardHeader>
<CardContent className="grid gap-2">
<CardContent className="grid gap-3">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Org Email</FormLabel>
<FormControl>
<Input
placeholder="[email protected]"
{...field}
/>
</FormControl>
<FormDescription>
Enter the email of your organization.
This could be your personal email or a
shared email.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="name"
Expand Down
8 changes: 2 additions & 6 deletions starterkits/saas/src/server/actions/plans/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { db } from "@/server/db";
import { subscriptions } from "@/server/db/schema";
import { configureLemonSqueezy } from "@/server/lemonsqueezy";
import { protectedProcedure } from "@/server/procedures";
import {
createCheckout,
getSubscription,
SubscriptionItem,
} from "@lemonsqueezy/lemonsqueezy.js";
import { createCheckout, getSubscription } from "@lemonsqueezy/lemonsqueezy.js";
import { eq } from "drizzle-orm";
import { redirect } from "next/navigation";

Expand Down Expand Up @@ -45,7 +41,7 @@ export async function getCheckoutURL(variantId?: number, embed = false) {
dark: true,
},
checkoutData: {
email: user.email ?? undefined,
email: currentOrg.email ?? undefined,
custom: {
user_id: user.id,
org_id: currentOrg.id,
Expand Down
14 changes: 13 additions & 1 deletion starterkits/saas/src/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const organizations = createTable("organization", {
.primaryKey()
.default(sql`gen_random_uuid()`),
name: varchar("name", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull(),
image: varchar("image", { length: 255 }),
createdAt: timestamp("createdAt", { mode: "date" }).notNull().defaultNow(),
ownerId: varchar("ownerId", { length: 255 })
Expand All @@ -151,6 +152,10 @@ export const organizationsRelations = relations(
references: [users.id],
}),
membersToOrganizations: many(membersToOrganizations),
subscriptions: one(subscriptions, {
fields: [organizations.id],
references: [subscriptions.orgId],
}),
}),
);

Expand Down Expand Up @@ -329,6 +334,13 @@ export const subscriptions = createTable("subscription", {
subscriptionItemId: serial("subscriptionItemId"),
orgId: text("orgId")
.notNull()
.references(() => organizations.id),
.references(() => organizations.id, { onDelete: "cascade" }),
variantId: integer("variantId").notNull(),
});

export const subscriptionsRelations = relations(subscriptions, ({ one }) => ({
organization: one(organizations, {
fields: [subscriptions.orgId],
references: [organizations.id],
}),
}));

0 comments on commit a47c218

Please sign in to comment.