diff --git a/.gitignore b/.gitignore index 3a05d510..9759bd18 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts .env + +# Scripts +getUsersEmails.ts diff --git a/pages/api/remaining.ts b/pages/api/remaining.ts index b8e6780d..3be29422 100644 --- a/pages/api/remaining.ts +++ b/pages/api/remaining.ts @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { getServerSession } from "next-auth/next"; import { authOptions } from "./auth/[...nextauth]"; import prisma from "../../lib/prismadb"; +import requestIp from "request-ip"; export default async function handler( req: NextApiRequest, @@ -21,8 +22,25 @@ export default async function handler( }, select: { credits: true, + location: true, }, }); + if (!user?.location) { + const ip = requestIp.getClientIp(req); + const location = await fetch( + `http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK_API_KEY}` + ).then((res) => res.json()); + + await prisma.user.update({ + where: { + email: session.user.email!, + }, + data: { + location: location.country_code, + }, + }); + } + return res.status(200).json({ remainingGenerations: user?.credits }); } diff --git a/prisma/migrations/20230325035657_location/migration.sql b/prisma/migrations/20230325035657_location/migration.sql new file mode 100644 index 00000000..3be0b0cd --- /dev/null +++ b/prisma/migrations/20230325035657_location/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - You are about to drop the column `boughtCredits` on the `User` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "User" DROP COLUMN "boughtCredits", +ADD COLUMN "location" TEXT; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e8d31481..e477a1b5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,7 +42,7 @@ model User { emailVerified DateTime? image String? credits Int @default(3) - boughtCredits Int @default(0) // remove boughtCredits + location String? accounts Account[] sessions Session[] rooms Room[]