Skip to content

Commit

Permalink
Merge pull request #11 from TEDx-SJEC/discount
Browse files Browse the repository at this point in the history
Discount
  • Loading branch information
Vyshnav001 authored Sep 17, 2024
2 parents c12e287 + 3940c9f commit e4c9b2f
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 16 deletions.
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
Binary file modified prisma/dev.db-journal
Binary file not shown.
26 changes: 26 additions & 0 deletions prisma/migrations/20240917053947_discount/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 26 additions & 0 deletions prisma/migrations/20240917061033_discount_percentage/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 11 additions & 10 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion src/app/actions/create-coupon-code.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
};
35 changes: 30 additions & 5 deletions src/components/Admin/code-generation-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Tabs defaultValue="account" className="w-[400px]">
Expand All @@ -42,6 +43,30 @@ export function Coupon({ session }: { session: NextAuthSession }) {
<Label htmlFor="username">Code</Label>
<Input id="username" disabled value={isPending ? "" : data} />
</div>
<div className="space-y-1">
<Label htmlFor="username">Discount(%)</Label>
<Input
id="discount"
value={discount}
disabled={!checked}
onChange={(e) => {
setDiscount(e.target.value );
}}
/>
<Checkbox
id="discount_terms"
checked={checked}
onClick={() => {
setChecked(!checked);
}}
/>
<label
htmlFor="terms"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Edit default discount
</label>
</div>
</CardContent>
<CardFooter>
<CouponGeneratorDialog onGenerateCoupon={refetch} />
Expand All @@ -64,7 +89,7 @@ export function Coupon({ session }: { session: NextAuthSession }) {
<Button
disabled={data === undefined ? true : false}
onClick={async () => {
await saveCoupon(data as string, session.user.id);
await saveCoupon(data as string, session.user.id , discount);
}}
>
Save coupon
Expand Down
30 changes: 30 additions & 0 deletions src/components/ui/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client"

import * as React from "react"
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
import { CheckIcon } from "@radix-ui/react-icons"

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")}
>
<CheckIcon className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
))
Checkbox.displayName = CheckboxPrimitive.Root.displayName

export { Checkbox }

0 comments on commit e4c9b2f

Please sign in to comment.