Refactor user actions and authentication components

- Updated user verification logic to include atoll and island relationships.
- Introduced CreateClient function for integrating with external client API.
- Replaced 'house_name' with 'address' in user signup data handling.
- Added new Checkbox component for improved UI interactions.
- Migrated database provider from SQLite to PostgreSQL and redefined user schema.
- Removed obsolete migration files and ensured database integrity with new structure.
This commit is contained in:
i701 2024-11-30 23:38:20 +05:00
parent 4e78ff2de9
commit 490150f9b7
16 changed files with 270 additions and 195 deletions

View File

@ -3,10 +3,10 @@
import { authClient } from "@/lib/auth-client";
import prisma from "@/lib/db";
import { signUpFormSchema } from "@/lib/schemas";
import { headers } from "next/headers";
// import type { User } from "@prisma/client";
import { redirect } from "next/navigation";
import { z } from "zod";
import { headers } from "next/headers";
const formSchema = z.object({
phoneNumber: z
.string()
@ -112,7 +112,7 @@ export async function signup(_actionState: ActionState, formData: FormData) {
name: parsedData.data.name,
islandId: parsedData.data.island_id,
atollId: parsedData.data.atoll_id,
house_name: parsedData.data.house_name,
address: parsedData.data.address,
id_card: parsedData.data.id_card,
dob: new Date(parsedData.data.dob),
role: "USER",

89
actions/ninja/client.ts Normal file
View File

@ -0,0 +1,89 @@
"use server";
// const raw = {
// group_settings_id: "",
// address1: "",
// city: "F",
// state: "Dharanboodhoo",
// postal_code: "12040",
// country_id: "462",
// address2: "Skyvilla",
// contacts: [
// {
// first_name: "Abdulla",
// last_name: "Aidhaan",
// email: "",
// phone: "778-0588",
// send_email: false,
// custom_value1: "1971-02-24",
// custom_value2: "A265117",
// custom_value3: "",
// },
// ],
// };
type CreateClientProps = {
group_settings_id: string;
address1: string;
city: string;
state: string;
postal_code: string;
country_id: string;
address2: string;
contacts: Contact;
};
type Contact = {
first_name: string;
last_name: string;
email: string;
phone: string;
send_email: boolean;
custom_value1: string;
custom_value2: string;
custom_value3: string;
};
export async function CreateClient({
group_settings_id = "",
address1 = "",
city,
state,
postal_code = "",
country_id = "462",
address2,
contacts,
}: CreateClientProps) {
const response = await fetch(
"https://finance-staging.baraveli.dev/api/v1/clients",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-token": `${process.env.NINJA_API_KEY}`,
},
body: JSON.stringify({
group_settings_id,
address1,
city,
state,
postal_code,
country_id,
address2,
contacts: [
{
first_name: contacts.first_name,
last_name: contacts.last_name,
email: contacts.email || "",
phone: contacts.phone,
send_email: contacts.send_email,
custom_value1: contacts.custom_value1,
custom_value2: contacts.custom_value2,
custom_value3: contacts.custom_value3 || "",
},
],
}),
},
);
const data = await response.json();
console.log(data.data.contacts);
return data;
}

View File

@ -2,17 +2,21 @@
import prisma from "@/lib/db";
import { revalidatePath } from "next/cache";
import { CreateClient } from "./ninja/client";
export async function VerifyUser(userId: string) {
const user = await prisma.user.findUnique({
where: {
id: userId,
},
include: {
atoll: true,
island: true,
},
});
if (!user) {
throw new Error("User not found");
}
user.verified = true;
await prisma.user.update({
where: {
id: userId,
@ -21,5 +25,24 @@ export async function VerifyUser(userId: string) {
verified: true,
},
});
await CreateClient({
group_settings_id: "",
address1: "",
city: user.atoll?.name || "",
state: user.island?.name || "",
postal_code: "",
country_id: "462",
address2: user.address || "",
contacts: {
first_name: user.name?.split(" ")[0] || "",
last_name: user.name?.split(" ")[1] || "",
email: user.email || "",
phone: user.phoneNumber || "",
send_email: false,
custom_value1: user.dob?.toISOString().split("T")[0] || "",
custom_value2: user.id_card || "",
custom_value3: "",
},
});
revalidatePath("/users");
}

View File

@ -0,0 +1,30 @@
"use client"
import * as React from "react"
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
import { Check } from "lucide-react"
import { cn } from "@/lib/utils"
const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
>(({ className, ...props }, ref) => (
<CheckboxPrimitive.Root
ref={ref}
className={cn(
"peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
className
)}
{...props}
>
<CheckboxPrimitive.Indicator
className={cn("flex items-center justify-center text-current")}
>
<Check className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
))
Checkbox.displayName = CheckboxPrimitive.Root.displayName
export { Checkbox }

View File

@ -1,63 +0,0 @@
-- CreateTable
CREATE TABLE "user" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT NOT NULL,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"firstPaymentDone" BOOLEAN NOT NULL DEFAULT false,
"image" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"phoneNumber" TEXT NOT NULL,
"phoneNumberVerified" BOOLEAN NOT NULL DEFAULT false,
"role" TEXT,
"lang" TEXT
);
-- CreateTable
CREATE TABLE "session" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"ipAddress" TEXT,
"userAgent" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateTable
CREATE TABLE "account" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"providerId" TEXT NOT NULL,
"accessToken" TEXT,
"refreshToken" TEXT,
"accessTokenExpiresAt" DATETIME,
"refreshTokenExpiresAt" DATETIME,
"scope" TEXT,
"password" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"idToken" TEXT
);
-- CreateTable
CREATE TABLE "verification" (
"id" TEXT NOT NULL PRIMARY KEY,
"identifier" TEXT NOT NULL,
"value" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
-- CreateIndex
CREATE UNIQUE INDEX "user_phoneNumber_key" ON "user"("phoneNumber");
-- CreateIndex
CREATE UNIQUE INDEX "session_token_key" ON "session"("token");

View File

@ -1,25 +0,0 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_user" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT NOT NULL,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"firstPaymentDone" BOOLEAN NOT NULL DEFAULT false,
"verified" BOOLEAN NOT NULL DEFAULT false,
"image" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"phoneNumber" TEXT NOT NULL,
"phoneNumberVerified" BOOLEAN NOT NULL DEFAULT false,
"role" TEXT,
"lang" TEXT
);
INSERT INTO "new_user" ("createdAt", "email", "emailVerified", "firstPaymentDone", "id", "image", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt") SELECT "createdAt", "email", "emailVerified", "firstPaymentDone", "id", "image", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt" FROM "user";
DROP TABLE "user";
ALTER TABLE "new_user" RENAME TO "user";
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
CREATE UNIQUE INDEX "user_phoneNumber_key" ON "user"("phoneNumber");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -1,11 +0,0 @@
/*
Warnings:
- A unique constraint covering the columns `[id_card]` on the table `user` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "user" ADD COLUMN "id_card" TEXT;
-- CreateIndex
CREATE UNIQUE INDEX "user_id_card_key" ON "user"("id_card");

View File

@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "user" ADD COLUMN "island" TEXT;

View File

@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "user" ADD COLUMN "house_name" TEXT;

View File

@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "user" ADD COLUMN "dob" DATETIME;

View File

@ -1,30 +0,0 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_user" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"firstPaymentDone" BOOLEAN NOT NULL DEFAULT false,
"verified" BOOLEAN NOT NULL DEFAULT false,
"island" TEXT,
"house_name" TEXT,
"id_card" TEXT,
"dob" DATETIME,
"image" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"phoneNumber" TEXT NOT NULL,
"phoneNumberVerified" BOOLEAN NOT NULL DEFAULT false,
"role" TEXT,
"lang" TEXT
);
INSERT INTO "new_user" ("createdAt", "dob", "email", "emailVerified", "firstPaymentDone", "house_name", "id", "id_card", "image", "island", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt", "verified") SELECT "createdAt", "dob", "email", "emailVerified", "firstPaymentDone", "house_name", "id", "id_card", "image", "island", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt", "verified" FROM "user";
DROP TABLE "user";
ALTER TABLE "new_user" RENAME TO "user";
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
CREATE UNIQUE INDEX "user_id_card_key" ON "user"("id_card");
CREATE UNIQUE INDEX "user_phoneNumber_key" ON "user"("phoneNumber");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -1,17 +0,0 @@
-- CreateTable
CREATE TABLE "Atoll" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateTable
CREATE TABLE "Island" (
"id" TEXT NOT NULL PRIMARY KEY,
"atollId" TEXT NOT NULL,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Island_atollId_fkey" FOREIGN KEY ("atollId") REFERENCES "Atoll" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

View File

@ -1,39 +0,0 @@
/*
Warnings:
- You are about to drop the column `island` on the `user` table. All the data in the column will be lost.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_user" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"firstPaymentDone" BOOLEAN NOT NULL DEFAULT false,
"verified" BOOLEAN NOT NULL DEFAULT false,
"house_name" TEXT,
"id_card" TEXT,
"dob" DATETIME,
"image" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"phoneNumber" TEXT NOT NULL,
"phoneNumberVerified" BOOLEAN NOT NULL DEFAULT false,
"role" TEXT,
"lang" TEXT,
"atollId" TEXT,
"islandId" TEXT,
CONSTRAINT "user_atollId_fkey" FOREIGN KEY ("atollId") REFERENCES "Atoll" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "user_islandId_fkey" FOREIGN KEY ("islandId") REFERENCES "Island" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_user" ("createdAt", "dob", "email", "emailVerified", "firstPaymentDone", "house_name", "id", "id_card", "image", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt", "verified") SELECT "createdAt", "dob", "email", "emailVerified", "firstPaymentDone", "house_name", "id", "id_card", "image", "lang", "name", "phoneNumber", "phoneNumberVerified", "role", "updatedAt", "verified" FROM "user";
DROP TABLE "user";
ALTER TABLE "new_user" RENAME TO "user";
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
CREATE UNIQUE INDEX "user_id_card_key" ON "user"("id_card");
CREATE UNIQUE INDEX "user_phoneNumber_key" ON "user"("phoneNumber");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -0,0 +1,110 @@
-- CreateTable
CREATE TABLE "user" (
"id" TEXT NOT NULL,
"name" TEXT,
"email" TEXT,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"firstPaymentDone" BOOLEAN NOT NULL DEFAULT false,
"verified" BOOLEAN NOT NULL DEFAULT false,
"address" TEXT,
"id_card" TEXT,
"dob" TIMESTAMP(3),
"image" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"phoneNumber" TEXT NOT NULL,
"phoneNumberVerified" BOOLEAN NOT NULL DEFAULT false,
"role" TEXT,
"lang" TEXT,
"atollId" TEXT,
"islandId" TEXT,
CONSTRAINT "user_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "session" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,
"ipAddress" TEXT,
"userAgent" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "session_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"providerId" TEXT NOT NULL,
"accessToken" TEXT,
"refreshToken" TEXT,
"accessTokenExpiresAt" TIMESTAMP(3),
"refreshTokenExpiresAt" TIMESTAMP(3),
"scope" TEXT,
"password" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"idToken" TEXT,
CONSTRAINT "account_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "verification" (
"id" TEXT NOT NULL,
"identifier" TEXT NOT NULL,
"value" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "verification_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Atoll" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Atoll_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Island" (
"id" TEXT NOT NULL,
"atollId" TEXT NOT NULL,
"name" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Island_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
-- CreateIndex
CREATE UNIQUE INDEX "user_id_card_key" ON "user"("id_card");
-- CreateIndex
CREATE UNIQUE INDEX "user_phoneNumber_key" ON "user"("phoneNumber");
-- CreateIndex
CREATE UNIQUE INDEX "session_token_key" ON "session"("token");
-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_atollId_fkey" FOREIGN KEY ("atollId") REFERENCES "Atoll"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_islandId_fkey" FOREIGN KEY ("islandId") REFERENCES "Island"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Island" ADD CONSTRAINT "Island_atollId_fkey" FOREIGN KEY ("atollId") REFERENCES "Atoll"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -0,0 +1,14 @@
-- CreateTable
CREATE TABLE "Device" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"mac" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"userId" TEXT,
CONSTRAINT "Device_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Device" ADD CONSTRAINT "Device_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
provider = "postgresql"