From 12cd915895f504651693fffd119fe7ea7d62a43f Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sat, 25 Mar 2023 00:00:30 -0400 Subject: [PATCH 1/4] v1, testing --- .gitignore | 3 +++ pages/api/remaining.ts | 19 +++++++++++++++++++ .../20230325035657_location/migration.sql | 9 +++++++++ prisma/schema.prisma | 2 +- 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20230325035657_location/migration.sql 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..fd10d565 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,26 @@ 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=31ad79df45feb22c2e29e3f92c6455e4` + ).then((res) => res.json()); + + console.log({ ip, location }); + // await prisma.user.update({ + // where: { + // email: session.user.email!, + // }, + // data: { + // location: location.country_name, + // }, + // }); + } + 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[] From 9909381a25e626c3489ec9fa7f378bea0296458d Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sat, 25 Mar 2023 00:28:28 -0400 Subject: [PATCH 2/4] added prisma query to update location --- pages/api/remaining.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pages/api/remaining.ts b/pages/api/remaining.ts index fd10d565..3792abd5 100644 --- a/pages/api/remaining.ts +++ b/pages/api/remaining.ts @@ -29,18 +29,19 @@ export default async function handler( if (!user?.location) { const ip = requestIp.getClientIp(req); const location = await fetch( - `http://api.ipstack.com/${ip}?access_key=31ad79df45feb22c2e29e3f92c6455e4` + `http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK_API_KEY}` ).then((res) => res.json()); - console.log({ ip, location }); - // await prisma.user.update({ - // where: { - // email: session.user.email!, - // }, - // data: { - // location: location.country_name, - // }, - // }); + await prisma.user.update({ + where: { + email: session.user.email!, + }, + data: { + location: location.country_code, + }, + }); + + console.log("Updated user location"); } return res.status(200).json({ remainingGenerations: user?.credits }); From 7cb161e714f6535ee0c5f0a02ce662497dbc1f7e Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sat, 25 Mar 2023 00:29:16 -0400 Subject: [PATCH 3/4] added env var --- pages/api/remaining.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/api/remaining.ts b/pages/api/remaining.ts index 3792abd5..dba3762e 100644 --- a/pages/api/remaining.ts +++ b/pages/api/remaining.ts @@ -41,7 +41,7 @@ export default async function handler( }, }); - console.log("Updated user location"); + console.log(`Updated user location to ${location.country_code}`); } return res.status(200).json({ remainingGenerations: user?.credits }); From 015b718e62c065266d9c0c34def9e36584f4645f Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sat, 25 Mar 2023 00:41:18 -0400 Subject: [PATCH 4/4] cleaned up page --- pages/api/remaining.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/pages/api/remaining.ts b/pages/api/remaining.ts index dba3762e..3be29422 100644 --- a/pages/api/remaining.ts +++ b/pages/api/remaining.ts @@ -40,8 +40,6 @@ export default async function handler( location: location.country_code, }, }); - - console.log(`Updated user location to ${location.country_code}`); } return res.status(200).json({ remainingGenerations: user?.credits });