diff --git a/package-lock.json b/package-lock.json index 5bddb73..21c9e00 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@prisma/adapter-libsql": "^5.19.1", "@prisma/client": "^5.19.1", "@radix-ui/react-avatar": "^1.1.0", + "@radix-ui/react-checkbox": "^1.1.1", "@radix-ui/react-dialog": "^1.1.1", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-label": "^2.1.0", @@ -2304,6 +2305,35 @@ } } }, + "node_modules/@radix-ui/react-checkbox": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/react-checkbox/-/react-checkbox-1.1.1.tgz", + "integrity": "sha512-0i/EKJ222Afa1FE0C6pNJxDq1itzcl3HChE9DwskA4th4KRse8ojx8a1nVcOjwJdbpDLcz7uol77yYnQNMHdKw==", + "dependencies": { + "@radix-ui/primitive": "1.1.0", + "@radix-ui/react-compose-refs": "1.1.0", + "@radix-ui/react-context": "1.1.0", + "@radix-ui/react-presence": "1.1.0", + "@radix-ui/react-primitive": "2.0.0", + "@radix-ui/react-use-controllable-state": "1.1.0", + "@radix-ui/react-use-previous": "1.1.0", + "@radix-ui/react-use-size": "1.1.0" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-collection": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@radix-ui/react-collection/-/react-collection-1.1.0.tgz", @@ -2791,6 +2821,20 @@ } } }, + "node_modules/@radix-ui/react-use-previous": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@radix-ui/react-use-previous/-/react-use-previous-1.1.0.tgz", + "integrity": "sha512-Z/e78qg2YFnnXcW88A4JmTtm4ADckLno6F7OXotmkQfeuCVaKuYzqAATPhVzl3delXE7CxIV8shofPn3jPc5Og==", + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-use-rect": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@radix-ui/react-use-rect/-/react-use-rect-1.1.0.tgz", diff --git a/package.json b/package.json index b6ab943..fb5f991 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@prisma/adapter-libsql": "^5.19.1", "@prisma/client": "^5.19.1", "@radix-ui/react-avatar": "^1.1.0", + "@radix-ui/react-checkbox": "^1.1.1", "@radix-ui/react-dialog": "^1.1.1", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-label": "^2.1.0", diff --git a/prisma/dev.db b/prisma/dev.db index 4e681af..aa2a551 100644 Binary files a/prisma/dev.db and b/prisma/dev.db differ diff --git a/prisma/dev.db-journal b/prisma/dev.db-journal index 13e174e..8f0b674 100644 Binary files a/prisma/dev.db-journal and b/prisma/dev.db-journal differ diff --git a/prisma/migrations/20240917053947_discount/migration.sql b/prisma/migrations/20240917053947_discount/migration.sql new file mode 100644 index 0000000..befafe4 --- /dev/null +++ b/prisma/migrations/20240917053947_discount/migration.sql @@ -0,0 +1,26 @@ +/* + Warnings: + + - Added the required column `discountPercentage` to the `Referral` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Referral" ( + "id" TEXT NOT NULL PRIMARY KEY, + "code" TEXT NOT NULL, + "discountPercentage" TEXT NOT NULL, + "createdby_id" TEXT NOT NULL, + "usedby_id" TEXT, + "isUsed" BOOLEAN NOT NULL DEFAULT false, + "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "Referral_createdby_id_fkey" FOREIGN KEY ("createdby_id") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "Referral_usedby_id_fkey" FOREIGN KEY ("usedby_id") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); +INSERT INTO "new_Referral" ("code", "created_at", "createdby_id", "id", "isUsed", "usedby_id") SELECT "code", "created_at", "createdby_id", "id", "isUsed", "usedby_id" FROM "Referral"; +DROP TABLE "Referral"; +ALTER TABLE "new_Referral" RENAME TO "Referral"; +CREATE UNIQUE INDEX "Referral_code_key" ON "Referral"("code"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/migrations/20240917061033_discount_percentage/migration.sql b/prisma/migrations/20240917061033_discount_percentage/migration.sql new file mode 100644 index 0000000..5998f51 --- /dev/null +++ b/prisma/migrations/20240917061033_discount_percentage/migration.sql @@ -0,0 +1,26 @@ +/* + Warnings: + + - You are about to drop the column `discountPercentage` on the `Referral` table. All the data in the column will be lost. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Referral" ( + "id" TEXT NOT NULL PRIMARY KEY, + "code" TEXT NOT NULL, + "discount_percentage" TEXT NOT NULL DEFAULT '20', + "createdby_id" TEXT NOT NULL, + "usedby_id" TEXT, + "isUsed" BOOLEAN NOT NULL DEFAULT false, + "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "Referral_createdby_id_fkey" FOREIGN KEY ("createdby_id") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "Referral_usedby_id_fkey" FOREIGN KEY ("usedby_id") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); +INSERT INTO "new_Referral" ("code", "created_at", "createdby_id", "id", "isUsed", "usedby_id") SELECT "code", "created_at", "createdby_id", "id", "isUsed", "usedby_id" FROM "Referral"; +DROP TABLE "Referral"; +ALTER TABLE "new_Referral" RENAME TO "Referral"; +CREATE UNIQUE INDEX "Referral_code_key" ON "Referral"("code"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7e6171a..5be0632 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -63,12 +63,13 @@ model Session { } model Referral { - id String @id @default(cuid()) - code String @unique - createdById String @map("createdby_id") - usedById String? @map("usedby_id") - isUsed Boolean @default(false) // Whether the referral has been used or not, itll use only once - created_at DateTime @default(now()) + id String @id @default(cuid()) + code String @unique + discountPercentage String @default("20") @map("discount_percentage") + createdById String @map("createdby_id") + usedById String? @map("usedby_id") + isUsed Boolean @default(false) // Whether the referral has been used or not, itll use only once + created_at DateTime @default(now()) // Relations createdBy User @relation("createdByUser", fields: [createdById], references: [id], onDelete: Cascade) // Admin who created the referral @@ -86,7 +87,7 @@ model Form { designation String? photo String @map("photo_url") // URL of the photo uploaded by the participan collegeIdCard String? @map("college_id_card") - entityName String @map("entity_name") + entityName String @map("entity_name") referralId String? @map("referral_id") createdById String @map("created_by_id") created_at DateTime @default(now()) @@ -98,11 +99,11 @@ model Form { model VerificationRequest { id String @id @default(cuid()) - identifier String // Like email or phone number - otp String + identifier String // Like email or phone number + otp String expires DateTime created_at DateTime @default(now()) updated_at DateTime @updatedAt @@unique([identifier, otp]) // Ensure OTP is unique for a given identifier -} \ No newline at end of file +} diff --git a/src/app/actions/create-coupon-code.ts b/src/app/actions/create-coupon-code.ts index b4a1f25..83c4c89 100644 --- a/src/app/actions/create-coupon-code.ts +++ b/src/app/actions/create-coupon-code.ts @@ -1,12 +1,13 @@ "use server"; import prisma from "@/server/db"; -export const saveCoupon = async (coupon: string, id: string) => { +export const saveCoupon = async (coupon: string, id: string, discount: string = "20") => { const resp = await prisma.referral.create({ data: { code: coupon, isUsed: false, createdById: id, + discountPercentage: discount, }, }); }; diff --git a/src/components/Admin/code-generation-card.tsx b/src/components/Admin/code-generation-card.tsx index 51a2734..59a4271 100644 --- a/src/components/Admin/code-generation-card.tsx +++ b/src/components/Admin/code-generation-card.tsx @@ -10,18 +10,19 @@ import { createCouponCode } from "@/lib/helper"; import { useQuery } from "@tanstack/react-query"; import { type Session as NextAuthSession } from "next-auth"; import CouponGeneratorDialog from "../coupon-generator-dialog"; +import { Checkbox } from "../ui/checkbox"; +import { useState } from "react"; export function Coupon({ session }: { session: NextAuthSession }) { + const [discount, setDiscount] = useState("20"); + const [checked, setChecked] = useState(false); const { data, isPending, isError, error, refetch } = useQuery({ queryKey: ["coupon"], queryFn: createCouponCode, enabled: false, }); - const handleCreateCoupon = async () => { - - await refetch(); - } + return ( @@ -42,6 +43,30 @@ export function Coupon({ session }: { session: NextAuthSession }) { +
+ + { + setDiscount(e.target.value ); + }} + /> + { + setChecked(!checked); + }} + /> + +
@@ -64,7 +89,7 @@ export function Coupon({ session }: { session: NextAuthSession }) {